Search in sources :

Example 1 with GeneratorCreationException

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;
}
Also used : Path(java.nio.file.Path) CoreException(org.eclipse.core.runtime.CoreException) GeneratorCreationException(com.devonfw.cobigen.eclipse.common.exceptions.GeneratorCreationException) Charset(java.nio.charset.Charset) PluginNotAvailableException(com.devonfw.cobigen.api.exception.PluginNotAvailableException) InputReaderException(com.devonfw.cobigen.api.exception.InputReaderException)

Example 2 with GeneratorCreationException

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);
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) GeneratorCreationException(com.devonfw.cobigen.eclipse.common.exceptions.GeneratorCreationException) CoreException(org.eclipse.core.runtime.CoreException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IProject(org.eclipse.core.resources.IProject)

Example 3 with GeneratorCreationException

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

GeneratorCreationException (com.devonfw.cobigen.eclipse.common.exceptions.GeneratorCreationException)3 CoreException (org.eclipse.core.runtime.CoreException)3 InputReaderException (com.devonfw.cobigen.api.exception.InputReaderException)2 MergeException (com.devonfw.cobigen.api.exception.MergeException)1 PluginNotAvailableException (com.devonfw.cobigen.api.exception.PluginNotAvailableException)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 Charset (java.nio.charset.Charset)1 Path (java.nio.file.Path)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IType (org.eclipse.jdt.core.IType)1