use of com.devonfw.cobigen.api.exception.InputReaderException 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;
}
use of com.devonfw.cobigen.api.exception.InputReaderException in project cobigen by devonfw.
the class InputInterpreterImpl method read.
@Override
public Object read(Path path, Charset inputCharset, Object... additionalArguments) throws InputReaderException {
List<TriggerInterpreter> triggerInterpreters = PluginRegistry.getTriggerInterpreters(path);
// We first try to find an input reader that is most likely readable
Map<TriggerInterpreter, Boolean> readableCache = new HashMap<>();
Object readable = null;
for (TriggerInterpreter triggerInterpreter : triggerInterpreters) {
readable = readInput(path, inputCharset, readableCache, triggerInterpreter, additionalArguments);
if (readable != null) {
return readable;
}
}
throw new InputReaderException("Could not read input at path " + path + " with any installed plugin.");
}
use of com.devonfw.cobigen.api.exception.InputReaderException in project cobigen by devonfw.
the class ExternalServerInputReaderProxy method read.
@Override
public Object read(Path path, Charset inputCharset, Object... additionalArguments) throws InputReaderException {
String fileContents;
String fileName = path.toString();
try {
fileContents = String.join("", Files.readAllLines(path, inputCharset));
} catch (IOException e) {
throw new InputReaderException("Could not read input file!" + fileName, e);
}
InputFileTo inputFile = new InputFileTo(fileName, fileContents, inputCharset.name());
return this.externalProcess.postJsonRequest("getInputModel", inputFile);
}
Aggregations