Search in sources :

Example 1 with GenericJSONMerger

use of com.devonfw.cobigen.jsonplugin.merger.generic.GenericJSONMerger in project cobigen by devonfw.

the class JSONMerger method merge.

@Override
public String merge(File base, String patch, String targetCharset) throws MergeException {
    String file = base.getAbsolutePath();
    JsonObject objBase = null;
    JsonObject objPatch = null;
    try (InputStream in = Files.newInputStream(base.toPath());
        InputStreamReader inSR = new InputStreamReader(in, Charset.forName(targetCharset));
        JsonReader reader = new JsonReader(inSR)) {
        JsonParser parser = new JsonParser();
        JsonElement jsonBase = parser.parse(reader);
        objBase = jsonBase.getAsJsonObject();
    } catch (JsonIOException e) {
        throw new MergeException(base, "Not JSON file", e);
    } catch (JsonSyntaxException e) {
        throw new MergeException(base, "JSON syntax error. ", e);
    } catch (FileNotFoundException e) {
        throw new MergeException(base, "File not found", e);
    } catch (IOException e) {
        throw new MergeException(base, "Could not read " + file, e);
    }
    try {
        JsonParser parser = new JsonParser();
        JsonElement jsonPatch = parser.parse(patch);
        objPatch = jsonPatch.getAsJsonObject();
    } catch (JsonIOException e) {
        throw new MergeException(base, "Not JSON patch code", e);
    } catch (JsonSyntaxException e) {
        throw new MergeException(base, "JSON Patch syntax error. ", e);
    }
    JsonObject result = null;
    // Override would be defined by patchOverrides at PluginActivator
    if (this.type.contains(Constants.GENERIC_MERGE)) {
        GenericJSONMerger ng2merge = new GenericJSONMerger(objBase, objPatch);
        result = ng2merge.merge(this.patchOverrides);
    } else {
        throw new MergeException(base, "Merge strategy not yet supported!");
    }
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    return gson.toJson(result);
}
Also used : InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonIOException(com.google.gson.JsonIOException) MergeException(com.devonfw.cobigen.api.exception.MergeException) JsonElement(com.google.gson.JsonElement) GenericJSONMerger(com.devonfw.cobigen.jsonplugin.merger.generic.GenericJSONMerger) JsonReader(com.google.gson.stream.JsonReader) JsonParser(com.google.gson.JsonParser)

Aggregations

MergeException (com.devonfw.cobigen.api.exception.MergeException)1 GenericJSONMerger (com.devonfw.cobigen.jsonplugin.merger.generic.GenericJSONMerger)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 JsonIOException (com.google.gson.JsonIOException)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 JsonReader (com.google.gson.stream.JsonReader)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1