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);
}
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);
}
}
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);
}
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);
}
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;
}
Aggregations