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,");
}
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,");
}
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);
}
}
}
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);
}
}
}
Aggregations