use of com.devonfw.cobigen.eclipse.common.exceptions.GeneratorCreationException in project cobigen by devonfw.
the class FileInputConverter method convertFile.
/**
* Reads the input file content and convert it to CobiGen valid input.
*
* @param cobigen initialized {@link CobiGen} instance
* @param inputFile the file with {@link Path} to the object and further information like charset.
* @return the output Object corresponding to the inputFile
* @throws GeneratorCreationException if the Reader couldn't read the input File or couldn't find the Plugin
*/
private static Object convertFile(CobiGen cobigen, IFile inputFile) throws GeneratorCreationException {
Object output = null;
Charset charset;
try {
charset = Charset.forName(inputFile.getCharset());
} catch (CoreException e) {
LOG.warn("Could not deterime charset for file " + inputFile.getLocationURI() + " reading with UTF-8.");
charset = Charset.forName("UTF-8");
}
Path inputFilePath = Paths.get(inputFile.getLocationURI());
try {
output = cobigen.read(inputFilePath, charset);
} catch (InputReaderException e) {
LOG.trace("Could not read file {}", inputFile.getLocationURI(), e);
throw new GeneratorCreationException("Could not read file " + inputFile.getLocationURI() + " with any input reader", e);
} catch (PluginNotAvailableException e) {
LOG.trace(e.getMessage(), e);
throw new GeneratorCreationException("Could not read file " + inputFile.getLocationURI() + " as no Plug-in for the given type could be found.", e);
}
return output;
}
use of com.devonfw.cobigen.eclipse.common.exceptions.GeneratorCreationException in project cobigen by devonfw.
the class GeneratorWrapperFactory method initializeGenerator.
/**
* Initializes the {@link CobiGen} with the correct configuration
*
* @return the configured{@link CobiGen}
* @throws GeneratorProjectNotExistentException if the generator configuration folder does not exist
* @throws InvalidConfigurationException if the context configuration is not valid
* @throws GeneratorCreationException if the generator configuration project does not exist
*/
private static CobiGen initializeGenerator() throws InvalidConfigurationException, GeneratorCreationException {
try {
ResourcesPluginUtil.refreshConfigurationProject();
IProject generatorProj = ResourcesPluginUtil.getGeneratorConfigurationProject();
if (generatorProj == null) {
throw new GeneratorCreationException("Configuration source could not be read. Have you downloaded the templates?");
}
// We need to check whether it is a valid Java Project
IJavaProject configJavaProject = JavaCore.create(generatorProj);
// If it is not valid, we should use the jar
if (null == generatorProj.getLocationURI() || !configJavaProject.exists()) {
File templatesDirectory = CobiGenPaths.getTemplatesFolderPath().toFile();
File jarPath = TemplatesJarUtil.getJarFile(false, templatesDirectory);
boolean fileExists = jarPath.exists();
if (!fileExists) {
MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning", "Not Downloaded the CobiGen Template Jar");
}
return CobiGenFactory.create(jarPath.toURI());
} else {
return CobiGenFactory.create(generatorProj.getLocationURI());
}
} catch (CoreException e) {
throw new GeneratorCreationException("An eclipse internal exception occurred", e);
} catch (Throwable e) {
throw new GeneratorCreationException("Configuration source could not be read.\nIf you were updating templates, it may mean" + " that you have no internet connection.", e);
}
}
use of com.devonfw.cobigen.eclipse.common.exceptions.GeneratorCreationException 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