Search in sources :

Example 1 with JavaEnumSource

use of org.jboss.forge.roaster.model.source.JavaEnumSource in project kie-wb-common by kiegroup.

the class JavaRoasterModelDriver method loadModel.

@Override
public ModelDriverResult loadModel() throws ModelDriverException {
    ModelDriverResult result = new ModelDriverResult();
    DataModel dataModel;
    String fileContent;
    dataModel = createModel();
    result.setDataModel(dataModel);
    List<Path> rootPaths = new ArrayList<Path>();
    rootPaths.add(javaRootPath);
    Collection<FileUtils.ScanResult> scanResults = FileUtils.getInstance().scan(ioService, rootPaths, ".java", true);
    if (scanResults != null) {
        for (FileUtils.ScanResult scanResult : scanResults) {
            logger.debug("Starting file loading into model, file: " + scanResult.getFile());
            fileContent = ioService.readAllString(scanResult.getFile());
            if (fileContent == null || "".equals(fileContent)) {
                logger.debug("file: " + scanResult.getFile() + " is empty.");
                result.addError(new DriverError("File has no content", Paths.convert(scanResult.getFile())));
                continue;
            }
            try {
                JavaType<?> javaType = Roaster.parse(fileContent);
                final boolean isManaged = isManagedJavaType(javaType);
                final boolean vetoed = (isManaged ? isVetoed(javaType) : false);
                if (isManaged && !vetoed) {
                    if (javaType.getSyntaxErrors() != null && !javaType.getSyntaxErrors().isEmpty()) {
                        // if a file has parsing errors it will be skipped.
                        addSyntaxErrors(result, scanResult.getFile(), javaType.getSyntaxErrors());
                    } else if (javaType.isEnum()) {
                        loadFromJavaEnum((JavaEnumSource) javaType, scanResult.getFile(), dataModel, result);
                    } else {
                        loadFromJavaClass((JavaClassSource) javaType, scanResult.getFile(), dataModel, result);
                    }
                } else if (vetoed) {
                    logger.debug("The class, {}, in the file, {}, was vetoed and will be skipped.", javaType.getQualifiedName(), scanResult.getFile());
                } else {
                    logger.debug("File: " + scanResult.getFile() + " do not contain a managed java type, it will be skipped.");
                }
            } catch (ParserException e) {
                result.addError(new DriverError(e.getMessage(), Paths.convert(scanResult.getFile())));
            } catch (Exception e) {
                // Unexpected error.
                logger.error(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
                throw new ModelDriverException(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
            }
        }
    }
    return result;
}
Also used : Path(org.uberfire.java.nio.file.Path) ParserException(org.jboss.forge.roaster.ParserException) FileUtils(org.kie.workbench.common.services.datamodeller.util.FileUtils) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) IOException(java.io.IOException) ParserException(org.jboss.forge.roaster.ParserException) DriverError(org.kie.workbench.common.services.datamodeller.driver.model.DriverError) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) JavaEnumSource(org.jboss.forge.roaster.model.source.JavaEnumSource) ModelDriverResult(org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ParserException (org.jboss.forge.roaster.ParserException)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1 JavaEnumSource (org.jboss.forge.roaster.model.source.JavaEnumSource)1 DataModel (org.kie.workbench.common.services.datamodeller.core.DataModel)1 ModelDriverException (org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)1 DriverError (org.kie.workbench.common.services.datamodeller.driver.model.DriverError)1 ModelDriverResult (org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult)1 FileUtils (org.kie.workbench.common.services.datamodeller.util.FileUtils)1 Path (org.uberfire.java.nio.file.Path)1