Search in sources :

Example 1 with MergeException

use of com.devonfw.cobigen.api.exception.MergeException in project cobigen by devonfw.

the class JavaMerger method merge.

@Override
public String merge(File base, String patch, String targetCharset) throws MergeException {
    ModifyableJavaClass baseClass;
    String lineDelimiter;
    Path path = Paths.get(base.getAbsolutePath());
    try (FileInputStream stream = new FileInputStream(path.toString());
        BufferedInputStream bis = new BufferedInputStream(stream);
        InputStreamReader reader = new InputStreamReader(bis, targetCharset)) {
        baseClass = (ModifyableJavaClass) JavaParserUtil.getFirstJavaClass(reader);
        lineDelimiter = SystemUtil.determineLineDelimiter(path, targetCharset);
    } catch (IOException e) {
        throw new MergeException(base, "Cannot read base file.", e);
    } catch (ParseException e) {
        throw new MergeException(base, "The syntax of the base file is invalid. Error in line: " + e.getLine() + " / column: " + e.getColumn() + ": " + e.getMessage(), e);
    }
    ModifyableJavaClass patchClass;
    try (StringReader reader = new StringReader(patch)) {
        patchClass = (ModifyableJavaClass) JavaParserUtil.getFirstJavaClass(reader);
    } catch (ParseException e) {
        throw new MergeException(base, "The syntax of the generated patch is invalid. Error in line: " + e.getLine() + " / column: " + e.getColumn() + ": " + e.getMessage(), e);
    }
    if (baseClass == null) {
        throw new MergeException(base, "The base file does not declare a valid JavaClass.");
    } else if (patchClass == null) {
        throw new MergeException(base, "The patch does not declare a valid JavaClass.");
    }
    ModifyableJavaClass mergedClass = merge(baseClass, patchClass);
    return StringUtil.consolidateLineEndings(mergedClass.getSource().getCodeBlock(), lineDelimiter);
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) MergeException(com.devonfw.cobigen.api.exception.MergeException) StringReader(java.io.StringReader) IOException(java.io.IOException) ParseException(com.thoughtworks.qdox.parser.ParseException) FileInputStream(java.io.FileInputStream) ModifyableJavaClass(com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass)

Example 2 with MergeException

use of com.devonfw.cobigen.api.exception.MergeException in project cobigen by devonfw.

the class PropertyMerger method merge.

@Override
public String merge(File base, String patch, String targetCharset) throws MergeException {
    Properties baseProperties = new Properties();
    try (FileInputStream fis = new FileInputStream(base);
        InputStreamReader isr = new InputStreamReader(fis, targetCharset)) {
        baseProperties.load(isr);
    } catch (IOException e) {
        throw new MergeException(base, "Could not read base file.", e);
    }
    Properties patchProperties = new Properties();
    try (ByteArrayInputStream inStream = new ByteArrayInputStream(patch.getBytes())) {
        patchProperties.load(inStream);
    } catch (IOException e) {
        throw new MergeException(base, "Could not read generated patch.", e);
    }
    Set<Object> conflicts = getConflictingProperties(baseProperties, patchProperties);
    try (FileInputStream in = new FileInputStream(base);
        InputStreamReader reader = new InputStreamReader(in, targetCharset);
        BufferedReader br = new BufferedReader(reader)) {
        String lineDelimiter = SystemUtil.determineLineDelimiter(base.toPath(), targetCharset);
        return concatContents(conflicts, br, patch, lineDelimiter);
    } catch (IOException e) {
        throw new MergeException(base, "Could not read base file.", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) MergeException(com.devonfw.cobigen.api.exception.MergeException) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 3 with MergeException

use of com.devonfw.cobigen.api.exception.MergeException 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)

Example 4 with MergeException

use of com.devonfw.cobigen.api.exception.MergeException in project cobigen by devonfw.

the class ExternalServerMergerProxy method merge.

@Override
public String merge(File base, String patch, String targetCharset) throws MergeException {
    String baseFileContents;
    try {
        baseFileContents = new String(Files.readAllBytes(base.toPath()), Charset.forName(targetCharset));
    } catch (IOException e) {
        throw new MergeException(base, "Could not read base file!", e);
    }
    MergeTo mergeTo = new MergeTo(baseFileContents, patch, this.patchOverrides);
    return this.externalProcess.postJsonRequest("merge", mergeTo);
}
Also used : MergeTo(com.devonfw.cobigen.api.externalprocess.to.MergeTo) MergeException(com.devonfw.cobigen.api.exception.MergeException) IOException(java.io.IOException)

Example 5 with MergeException

use of com.devonfw.cobigen.api.exception.MergeException in project cobigen by devonfw.

the class JavaInputConverter method convertInput.

/**
 * Converts a list of IDE objects to the supported CobiGen input types
 *
 * @param javaElements java IDE objects (mainly of type {@link IJavaElement}), which should be converted
 * @param inputInterpreter to interpret inputs
 * @return the corresponding {@link List} of inputs for the {@link CobiGen generator}
 * @throws GeneratorCreationException if any exception occurred during converting the inputs or creating the generator
 */
public static List<Object> convertInput(List<Object> javaElements, InputInterpreter inputInterpreter) throws GeneratorCreationException {
    List<Object> convertedInputs = Lists.newLinkedList();
    /*
     * Precondition / Assumption: all elements of the list are of the same type
     */
    for (Object elem : javaElements) {
        if (elem instanceof IPackageFragment) {
            try {
                IPackageFragment frag = (IPackageFragment) elem;
                Object packageFolder = inputInterpreter.read(Paths.get(frag.getCorrespondingResource().getLocationURI()), StandardCharsets.UTF_8, frag.getElementName(), ClassLoaderUtil.getProjectClassLoader(frag.getJavaProject()));
                convertedInputs.add(packageFolder);
            } catch (MalformedURLException e) {
                throw new GeneratorCreationException("An internal exception occurred while building the project class loader.", e);
            } catch (CoreException e) {
                throw new GeneratorCreationException("An eclipse internal exception occurred.", e);
            } catch (InputReaderException e) {
                throw new GeneratorCreationException("Could not read from resource " + elem.toString(), e);
            }
        } else if (elem instanceof ICompilationUnit) {
            // same project
            try {
                IType[] types = ((ICompilationUnit) elem).getTypes();
                if (types.length < 1) {
                    throw new GeneratorCreationException("The input does not declare a class");
                }
                IType rootType = types[0];
                try {
                    ClassLoader projectClassLoader = ClassLoaderUtil.getProjectClassLoader(rootType.getJavaProject());
                    Object input = inputInterpreter.read(Paths.get(((ICompilationUnit) elem).getCorrespondingResource().getRawLocationURI()), StandardCharsets.UTF_8, projectClassLoader);
                    convertedInputs.add(input);
                } catch (MalformedURLException e) {
                    throw new GeneratorCreationException("An internal exception occurred while loading Java class " + rootType.getFullyQualifiedName(), e);
                } catch (InputReaderException e) {
                    throw new GeneratorCreationException("Could not read from resource " + elem.toString(), e);
                }
            } catch (MergeException e) {
                throw new GeneratorCreationException("Could not parse Java base file: " + ((ICompilationUnit) elem).getElementName() + ":\n" + e.getMessage(), e);
            } catch (CoreException e) {
                throw new GeneratorCreationException("An eclipse internal exception occurred.", e);
            }
        }
    }
    return convertedInputs;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) MalformedURLException(java.net.MalformedURLException) GeneratorCreationException(com.devonfw.cobigen.eclipse.common.exceptions.GeneratorCreationException) CoreException(org.eclipse.core.runtime.CoreException) MergeException(com.devonfw.cobigen.api.exception.MergeException) InputReaderException(com.devonfw.cobigen.api.exception.InputReaderException) IType(org.eclipse.jdt.core.IType)

Aggregations

MergeException (com.devonfw.cobigen.api.exception.MergeException)13 IOException (java.io.IOException)9 File (java.io.File)6 FileNotFoundException (java.io.FileNotFoundException)3 FileReader (java.io.FileReader)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 Test (org.junit.Test)3 FileInputStream (java.io.FileInputStream)2 Path (java.nio.file.Path)2 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)1 InputReaderException (com.devonfw.cobigen.api.exception.InputReaderException)1 PluginNotAvailableException (com.devonfw.cobigen.api.exception.PluginNotAvailableException)1 InputReader (com.devonfw.cobigen.api.extension.InputReader)1 Merger (com.devonfw.cobigen.api.extension.Merger)1 TextTemplateEngine (com.devonfw.cobigen.api.extension.TextTemplateEngine)1 MergeTo (com.devonfw.cobigen.api.externalprocess.to.MergeTo)1 GeneratorCreationException (com.devonfw.cobigen.eclipse.common.exceptions.GeneratorCreationException)1 TemplatesConfiguration (com.devonfw.cobigen.impl.config.TemplatesConfiguration)1 Template (com.devonfw.cobigen.impl.config.entity.Template)1