Search in sources :

Example 1 with TextTemplate

use of com.devonfw.cobigen.api.extension.TextTemplate in project cobigen by devonfw.

the class FreeMarkerTemplateEngineTest method testBasicGeneration.

/**
 * Tests a basic FreeMarker generation
 */
@Test
public void testBasicGeneration() {
    // arrange
    final File templateFolder = new File(testFileRootPath + "basicGeneration/").getAbsoluteFile();
    TextTemplate template = new TextTemplate() {

        @Override
        public String getRelativeTemplatePath() {
            return "template.ftl";
        }

        @Override
        public Path getAbsoluteTemplatePath() {
            return templateFolder.toPath().resolve("template.ftl");
        }
    };
    HashMap<String, Object> model = new HashMap<>();
    List<Object> fields = new ArrayList<>();
    HashMap<Object, Object> fieldAttr = new HashMap<>();
    fieldAttr.put("type", "A");
    fields.add(fieldAttr);
    fieldAttr = new HashMap<>();
    fieldAttr.put("type", "B");
    fields.add(fieldAttr);
    fieldAttr = new HashMap<>();
    fieldAttr.put("type", "C");
    fields.add(fieldAttr);
    HashMap<String, Object> fieldsAccessor = new HashMap<>();
    fieldsAccessor.put("fields", fields);
    model.put("pojo", fieldsAccessor);
    // act
    StringWriter out = new StringWriter();
    FreeMarkerTemplateEngine templateEngine = new FreeMarkerTemplateEngine();
    templateEngine.setTemplateFolder(templateFolder.toPath());
    templateEngine.process(template, model, out, "UTF-8");
    // assert
    assertThat(out).hasToString("A,B,C,");
}
Also used : FreeMarkerTemplateEngine(com.devonfw.cobigen.tempeng.freemarker.FreeMarkerTemplateEngine) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) File(java.io.File) TextTemplate(com.devonfw.cobigen.api.extension.TextTemplate) Test(org.junit.Test)

Example 2 with TextTemplate

use of com.devonfw.cobigen.api.extension.TextTemplate in project cobigen by devonfw.

the class VelocityTemplateEngineTest method testProcess.

/**
 * Tests a basic velocity generation. Test design used from freemarker plugin
 */
@Test
public void testProcess() {
    // arrange
    final File templateFolder = new File("src/test/resources/unit/").getAbsoluteFile();
    TextTemplate template = new TextTemplate() {

        @Override
        public String getRelativeTemplatePath() {
            return "temp1.vm";
        }

        @Override
        public Path getAbsoluteTemplatePath() {
            return templateFolder.toPath().resolve("temp1.vm");
        }
    };
    HashMap<String, Object> model = new HashMap<>();
    List<Object> fields = new ArrayList<>();
    HashMap<Object, Object> fieldAttr = new HashMap<>();
    fieldAttr.put("type", "A");
    fields.add(fieldAttr);
    fieldAttr = new HashMap<>();
    fieldAttr.put("type", "B");
    fields.add(fieldAttr);
    fieldAttr = new HashMap<>();
    fieldAttr.put("type", "C");
    fields.add(fieldAttr);
    HashMap<String, Object> fieldsAccessor = new HashMap<>();
    fieldsAccessor.put("fields", fields);
    model.put("pojo", fieldsAccessor);
    // act
    StringWriter out = new StringWriter();
    this.engine.setTemplateFolder(templateFolder.toPath());
    this.engine.process(template, model, out, "UTF-8");
    // assert
    assertThat(out).hasToString("A,B,C,");
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) File(java.io.File) TextTemplate(com.devonfw.cobigen.api.extension.TextTemplate) Test(org.junit.Test)

Example 3 with TextTemplate

use of com.devonfw.cobigen.api.extension.TextTemplate in project cobigen by devonfw.

the class FreeMarkerTemplateEngine method process.

@Override
public void process(TextTemplate template, Map<String, Object> model, Writer out, String outputEncoding) {
    Template fmTemplate = null;
    try {
        fmTemplate = this.freeMarkerConfig.getTemplate(template.getRelativeTemplatePath());
    } catch (ParseException e) {
        throw new CobiGenRuntimeException("Could not parse FreeMarker template: " + template.getAbsoluteTemplatePath() + ". (FreeMarker v" + FreemarkerMetadata.VERSION + " )", e);
    } catch (Throwable e) {
        throw new CobiGenRuntimeException("An error occured while retrieving the FreeMarker template: " + template.getAbsoluteTemplatePath() + " from the FreeMarker configuration. (FreeMarker v" + FreemarkerMetadata.VERSION + " )", e);
    }
    if (fmTemplate != null) {
        try {
            Environment env = fmTemplate.createProcessingEnvironment(model, out);
            env.setOutputEncoding(outputEncoding);
            // no duplicate logging
            env.setLogTemplateExceptions(false);
            env.process();
        } catch (TemplateException e) {
            throw new CobiGenRuntimeException("An error occurred while generating the template: " + template.getAbsoluteTemplatePath() + " (FreeMarker v" + FreemarkerMetadata.VERSION + ")", e);
        } catch (Throwable e) {
            throw new CobiGenRuntimeException("An unkonwn error occurred while generating the template: " + template.getAbsoluteTemplatePath() + " (FreeMarker v" + FreemarkerMetadata.VERSION + ")", e);
        }
    }
}
Also used : CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) TemplateException(freemarker.template.TemplateException) Environment(freemarker.core.Environment) ParseException(freemarker.core.ParseException) TextTemplate(com.devonfw.cobigen.api.extension.TextTemplate) Template(freemarker.template.Template)

Example 4 with TextTemplate

use of com.devonfw.cobigen.api.extension.TextTemplate in project cobigen by devonfw.

the class VelocityTemplateEngine method process.

@Override
public void process(TextTemplate template, Map<String, Object> model, Writer out, String outputEncoding) {
    this.engine.setProperty(RuntimeConstants.OUTPUT_ENCODING, outputEncoding);
    executeInThisClassloader(null, (p) -> {
        this.engine.init();
        return null;
    });
    Context context = new VelocityContext(model);
    Template vmTemplate = null;
    try {
        vmTemplate = executeInThisClassloader(template.getRelativeTemplatePath(), (path) -> this.engine.getTemplate(path));
    } catch (Throwable e) {
        throw new CobiGenRuntimeException("An error occured while retrieving the Velocity template " + template.getAbsoluteTemplatePath() + " from the Velocity configuration. (Velocity v" + VelocityMetadata.VERSION + ")", e);
    }
    if (vmTemplate != null) {
        try {
            vmTemplate.merge(context, out);
        } catch (VelocityException e) {
            throw new CobiGenRuntimeException("An error occurred while generating the template." + template.getAbsoluteTemplatePath() + "(Velocity v" + VelocityMetadata.VERSION + ")", e);
        } catch (Throwable e) {
            throw new CobiGenRuntimeException("An unkonwn error occurred while generating the template." + template.getAbsoluteTemplatePath() + "(Velocity v" + VelocityMetadata.VERSION + ")", e);
        }
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) TextTemplateEngine(com.devonfw.cobigen.api.extension.TextTemplateEngine) NullResourceCache(com.devonfw.cobigen.tempeng.velocity.runtime.resources.NullResourceCache) LoggerFactory(org.slf4j.LoggerFactory) RuntimeConstants(org.apache.velocity.runtime.RuntimeConstants) Function(java.util.function.Function) VelocityContext(org.apache.velocity.VelocityContext) LogChuteDelegate(com.devonfw.cobigen.tempeng.velocity.log.LogChuteDelegate) Name(com.devonfw.cobigen.api.annotation.Name) Template(org.apache.velocity.Template) ResourceManagerDelegate(com.devonfw.cobigen.tempeng.velocity.runtime.resources.ResourceManagerDelegate) Context(org.apache.velocity.context.Context) VelocityEngine(org.apache.velocity.app.VelocityEngine) TextTemplate(com.devonfw.cobigen.api.extension.TextTemplate) Map(java.util.Map) Writer(java.io.Writer) VelocityMetadata(com.devonfw.cobigen.tempeng.velocity.constant.VelocityMetadata) VelocityException(org.apache.velocity.exception.VelocityException) Path(java.nio.file.Path) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) VelocityContext(org.apache.velocity.VelocityContext) VelocityException(org.apache.velocity.exception.VelocityException) Template(org.apache.velocity.Template) TextTemplate(com.devonfw.cobigen.api.extension.TextTemplate)

Aggregations

TextTemplate (com.devonfw.cobigen.api.extension.TextTemplate)4 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)2 File (java.io.File)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Name (com.devonfw.cobigen.api.annotation.Name)1 TextTemplateEngine (com.devonfw.cobigen.api.extension.TextTemplateEngine)1 FreeMarkerTemplateEngine (com.devonfw.cobigen.tempeng.freemarker.FreeMarkerTemplateEngine)1 VelocityMetadata (com.devonfw.cobigen.tempeng.velocity.constant.VelocityMetadata)1 LogChuteDelegate (com.devonfw.cobigen.tempeng.velocity.log.LogChuteDelegate)1 NullResourceCache (com.devonfw.cobigen.tempeng.velocity.runtime.resources.NullResourceCache)1 ResourceManagerDelegate (com.devonfw.cobigen.tempeng.velocity.runtime.resources.ResourceManagerDelegate)1 Environment (freemarker.core.Environment)1 ParseException (freemarker.core.ParseException)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 Writer (java.io.Writer)1 Path (java.nio.file.Path)1