package com.ibm.database.compatibility;

import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.Flushable;
import java.io.IOException;
import java.io.Writer;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonOperationWriter implements OperationWriter, Flushable, Closeable {
	
	private final Writer writer;
	private final BufferedWriter bufferedWriter;
	
	public JsonOperationWriter(String pathToFile) throws IOException {
		this(new FileWriter(pathToFile));
	}

	public JsonOperationWriter(File file) throws IOException {
		this(new FileWriter(file));
	}

	public JsonOperationWriter(Writer writer) {
		this.writer = writer;
		this.bufferedWriter = new BufferedWriter(writer);
	}
	
	@Override
	public void write(Operation op) throws IOException {
		bufferedWriter.write(GsonUtils.newGson().toJson(op));
	}

	@Override
	public void writeComment(String comment) throws IOException {
		String[] lines = comment.split("\n");
		for (String line : lines) {
			bufferedWriter.write("# ");
			bufferedWriter.write(line);			
		}
	}

	@Override
	public void close() throws IOException {
		bufferedWriter.close();
	}

	@Override
	public void flush() throws IOException {
		bufferedWriter.flush();		
	}

}

@@ -221,74 +221,8 @@
 r;%0A%0A
-import com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;%0A%0A
 publ
@@ -826,24 +826,52 @@
 oJson(op));%0A
+%09%09bufferedWriter.newLine();%0A
 %09%7D%0A%0A%09@Overri
@@ -1067,19 +1067,45 @@
 e(line);
+%0A
 %09%09%09
+bufferedWriter.newLine();
 %0A%09%09%7D%0A%09%7D%0A

Add newline output when using the JsonOperationWriter.