package org.kurento.modulecreator.json;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.kurento.modulecreator.definition.ModuleDefinition;
import org.kurento.modulecreator.definition.Param;
import org.kurento.modulecreator.definition.Property;
import org.kurento.modulecreator.definition.RemoteClass;
import org.kurento.modulecreator.definition.TypeRef;

public class JsonModuleSaverLoader {

	private static JsonModuleSaverLoader INSTANCE = new JsonModuleSaverLoader();

	public static JsonModuleSaverLoader getInstance() {
		return INSTANCE;
	}

	private Gson gson;

	private JsonModuleSaverLoader() {
		GsonBuilder gsonBuilder = new GsonBuilder().setPrettyPrinting();
		gsonBuilder.registerTypeAdapter(TypeRef.class, new TypeRefAdapter());
		gsonBuilder.registerTypeAdapter(Param.class, new DataItemAdapter());
		gsonBuilder.registerTypeAdapter(Property.class, new DataItemAdapter());
		gsonBuilder.registerTypeAdapter(RemoteClass.class,
				new RemoteClassAdapter());
		gsonBuilder.registerTypeAdapter(Method.class, new MethodAdapter());
		gson = gsonBuilder.create();
	}

	public ModuleDefinition loadFromFile(Path file) throws FileNotFoundException,
			IOException {
		return loadFromInputStream(Files.newInputStream(file));
	}

	public ModuleDefinition loadFromClasspath(String resourceName) throws IOException {
		return loadFromInputStream(this.getClass().getResourceAsStream(
				resourceName));
	}

	private ModuleDefinition loadFromInputStream(InputStream is) throws IOException {
		String moduleString = loadTextFromInputStream(is);
		ModuleDefinition module = gson.fromJson(moduleString, ModuleDefinition.class);
		return module;
	}

	private String loadTextFromInputStream(InputStream is) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = br.readLine()) != null) {
			sb.append(line).append("\n");
		}

		return sb.toString();
	}

	public void writeToFile(ModuleDefinition module, File file)
			throws FileNotFoundException {

		String jsonModuleString = gson.toJson(module);

		PrintWriter writer = new PrintWriter(file);
		writer.println(jsonModuleString);
		writer.close();
	}

}

@@ -335,73 +335,8 @@
 h;%0A%0A
-import com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;%0A
 impo
@@ -609,16 +609,82 @@
 peRef;%0A%0A
+import com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;%0A%0A
 public c
@@ -1445,17 +1445,20 @@
 th file)
- 
+%0A%09%09%09
 throws F
@@ -1478,20 +1478,17 @@
 ception,
-%0A%09%09%09
+ 
 IOExcept
@@ -1618,17 +1618,20 @@
 rceName)
- 
+%0A%09%09%09
 throws I
@@ -1786,33 +1786,36 @@
 (InputStream is)
- 
+%0A%09%09%09
 throws IOExcepti
@@ -1927,17 +1927,21 @@
 eString,
- 
+%0A%09%09%09%09
 ModuleDe
@@ -2443,16 +2443,95 @@
 dule);%0A%0A
+%09%09if (!file.getParentFile().exists()) %7B%0A%09%09%09file.getParentFile().mkdirs();%0A%09%09%7D%0A%0A
 %09%09PrintW

Generate output folder if not exist when creating fusioned kmd file