Search in sources :

Example 1 with PrettyPrinter

use of org.drools.javaparser.printer.PrettyPrinter in project drools by kiegroup.

the class ModelWriter method writeModel.

public Result writeModel(MemoryFileSystem srcMfs, Collection<PackageModel> packageModels) {
    List<String> sourceFiles = new ArrayList<>();
    List<String> modelFiles = new ArrayList<>();
    PrettyPrinter prettyPrinter = getPrettyPrinter();
    for (PackageModel pkgModel : packageModels) {
        String pkgName = pkgModel.getName();
        String folderName = pkgName.replace('.', '/');
        for (ClassOrInterfaceDeclaration generatedPojo : pkgModel.getGeneratedPOJOsSource()) {
            final String source = JavaParserCompiler.toPojoSource(pkgModel.getName(), pkgModel.getImports(), generatedPojo);
            pkgModel.logRule(source);
            String pojoSourceName = "src/main/java/" + folderName + "/" + generatedPojo.getName() + ".java";
            srcMfs.write(pojoSourceName, source.getBytes());
            sourceFiles.add(pojoSourceName);
        }
        for (GeneratedClassWithPackage generatedPojo : pkgModel.getGeneratedAccumulateClasses()) {
            final String source = JavaParserCompiler.toPojoSource(pkgModel.getName(), generatedPojo.getImports(), generatedPojo.getGeneratedClass());
            pkgModel.logRule(source);
            String pojoSourceName = "src/main/java/" + folderName + "/" + generatedPojo.getGeneratedClass().getName() + ".java";
            srcMfs.write(pojoSourceName, source.getBytes());
            sourceFiles.add(pojoSourceName);
        }
        RuleSourceResult rulesSourceResult = pkgModel.getRulesSource();
        // main rules file:
        String rulesFileName = pkgModel.getRulesFileName();
        String rulesSourceName = "src/main/java/" + folderName + "/" + rulesFileName + ".java";
        String rulesSource = prettyPrinter.print(rulesSourceResult.getMainRuleClass());
        pkgModel.logRule(rulesSource);
        byte[] rulesBytes = rulesSource.getBytes();
        srcMfs.write(rulesSourceName, rulesBytes);
        modelFiles.add(pkgName + "." + rulesFileName);
        sourceFiles.add(rulesSourceName);
        // manage additional classes, please notice to not add to modelFiles.
        for (CompilationUnit cu : rulesSourceResult.getSplitted()) {
            String addFileName = cu.findFirst(ClassOrInterfaceDeclaration.class).get().getNameAsString();
            String addSourceName = "src/main/java/" + folderName + "/" + addFileName + ".java";
            String addSource = prettyPrinter.print(cu);
            pkgModel.logRule(addSource);
            byte[] addBytes = addSource.getBytes();
            srcMfs.write(addSourceName, addBytes);
            sourceFiles.add(addSourceName);
        }
    }
    return new Result(sourceFiles, modelFiles);
}
Also used : CompilationUnit(org.drools.javaparser.ast.CompilationUnit) PrettyPrinter(org.drools.javaparser.printer.PrettyPrinter) JavaParserCompiler.getPrettyPrinter(org.drools.modelcompiler.builder.JavaParserCompiler.getPrettyPrinter) ClassOrInterfaceDeclaration(org.drools.javaparser.ast.body.ClassOrInterfaceDeclaration) ArrayList(java.util.ArrayList) RuleSourceResult(org.drools.modelcompiler.builder.PackageModel.RuleSourceResult) RuleSourceResult(org.drools.modelcompiler.builder.PackageModel.RuleSourceResult)

Example 2 with PrettyPrinter

use of org.drools.javaparser.printer.PrettyPrinter in project drools by kiegroup.

the class JavaParserCompiler method createPrettyPrinter.

private static PrettyPrinter createPrettyPrinter() {
    PrettyPrinterConfiguration config = new PrettyPrinterConfiguration();
    config.setColumnAlignParameters(true);
    config.setColumnAlignFirstMethodChain(true);
    return new PrettyPrinter(config);
}
Also used : PrettyPrinter(org.drools.javaparser.printer.PrettyPrinter) PrettyPrinterConfiguration(org.drools.javaparser.printer.PrettyPrinterConfiguration)

Aggregations

PrettyPrinter (org.drools.javaparser.printer.PrettyPrinter)2 ArrayList (java.util.ArrayList)1 CompilationUnit (org.drools.javaparser.ast.CompilationUnit)1 ClassOrInterfaceDeclaration (org.drools.javaparser.ast.body.ClassOrInterfaceDeclaration)1 PrettyPrinterConfiguration (org.drools.javaparser.printer.PrettyPrinterConfiguration)1 JavaParserCompiler.getPrettyPrinter (org.drools.modelcompiler.builder.JavaParserCompiler.getPrettyPrinter)1 RuleSourceResult (org.drools.modelcompiler.builder.PackageModel.RuleSourceResult)1