Search in sources :

Example 6 with InputReaderException

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

Example 7 with InputReaderException

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.");
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) HashMap(java.util.HashMap) InputReaderException(com.devonfw.cobigen.api.exception.InputReaderException)

Example 8 with InputReaderException

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);
}
Also used : IOException(java.io.IOException) InputFileTo(com.devonfw.cobigen.api.externalprocess.to.InputFileTo) InputReaderException(com.devonfw.cobigen.api.exception.InputReaderException)

Aggregations

InputReaderException (com.devonfw.cobigen.api.exception.InputReaderException)8 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 GeneratorCreationException (com.devonfw.cobigen.eclipse.common.exceptions.GeneratorCreationException)2 CoreException (org.eclipse.core.runtime.CoreException)2 MergeException (com.devonfw.cobigen.api.exception.MergeException)1 PluginNotAvailableException (com.devonfw.cobigen.api.exception.PluginNotAvailableException)1 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)1 InputFileTo (com.devonfw.cobigen.api.externalprocess.to.InputFileTo)1 IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)1 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)1 Tuple (com.devonfw.cobigen.api.util.Tuple)1 PackageFolder (com.devonfw.cobigen.javaplugin.inputreader.to.PackageFolder)1 JavaClass (com.thoughtworks.qdox.model.JavaClass)1 ParseException (com.thoughtworks.qdox.parser.ParseException)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 Charset (java.nio.charset.Charset)1