use of freemarker.template.Template in project sonarqube by SonarSource.
the class HtmlReport method writeToFile.
public void writeToFile(IssuesReport report, File toFile, boolean complete) {
try {
freemarker.log.Logger.selectLoggerLibrary(freemarker.log.Logger.LIBRARY_NONE);
freemarker.template.Configuration cfg = new freemarker.template.Configuration();
cfg.setClassForTemplateLoading(HtmlReport.class, "");
Map<String, Object> root = Maps.newHashMap();
root.put("report", report);
root.put("ruleNameProvider", ruleNameProvider);
root.put("sourceProvider", sourceProvider);
root.put("complete", complete);
Template template = cfg.getTemplate("issuesreport.ftl");
try (FileOutputStream fos = new FileOutputStream(toFile);
Writer writer = new OutputStreamWriter(fos, fs.encoding())) {
template.process(root, writer);
writer.flush();
}
} catch (Exception e) {
throw new IllegalStateException("Fail to generate HTML Issues Report to: " + toFile, e);
}
}
use of freemarker.template.Template in project pcgen by PCGen.
the class AbstractOutputTestCase method processThroughFreeMarker.
public void processThroughFreeMarker(String testString, String expectedResult) {
try {
Configuration c = new Configuration();
Template t = new Template("test", testString, c);
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
Map<String, Object> input = OutputDB.buildDataModel(id);
t.process(input, bw);
String s = sw.getBuffer().toString();
assertEquals(expectedResult, s);
} catch (IOException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
} catch (TemplateException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
}
}
use of freemarker.template.Template in project perun by CESNET.
the class PerunNotifTemplateManagerImpl method testPerunNotifMessageText.
@Override
public String testPerunNotifMessageText(String template, Map<Integer, List<PerunBean>> regexIdsBeans) throws IOException, TemplateException {
//Initialization of freemarker
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
Configuration myConfiguration = createFreemarkerConfiguration(stringTemplateLoader);
stringTemplateLoader.putTemplate("test", template);
Template freeMarkerTemplate = myConfiguration.getTemplate(template, new Locale("en"));
StringWriter stringWriter = new StringWriter(4096);
Map<String, Object> container = new HashMap<String, Object>();
Map<String, Map<String, PerunBean>> resultMapOfBeans = new HashMap<String, Map<String, PerunBean>>();
for (Integer regexId : regexIdsBeans.keySet()) {
Map<String, PerunBean> normalizedBeans = new HashMap<String, PerunBean>();
List<PerunBean> perunBeans = regexIdsBeans.get(regexId);
for (PerunBean retrievedObject : perunBeans) {
normalizedBeans.put(normalizeName(retrievedObject.getClass().toString()), retrievedObject);
}
resultMapOfBeans.put(regexId.toString(), normalizedBeans);
}
container.put("retrievedObjects", resultMapOfBeans);
container.put("perunSession", session);
container.put("perun", perun);
freeMarkerTemplate.process(container, stringWriter);
return stringWriter.toString();
}
use of freemarker.template.Template in project perun by CESNET.
the class PerunNotifTemplateManagerImpl method validateTemplateMessage.
private void validateTemplateMessage(PerunNotifTemplateMessage message) throws InternalErrorException, TemplateMessageSyntaxErrorException {
String templateName = Integer.toString(message.getTemplateId());
Locale locale = message.getLocale();
try {
Template freeMarkerTemplate = this.configuration.getTemplate(templateName + "_" + locale.getLanguage(), locale);
} catch (ParseException ex) {
throw new TemplateMessageSyntaxErrorException(message, ex);
} catch (IOException ex) {
// template not found
throw new InternalErrorException("FreeMarker Template internal error.", ex);
}
}
use of freemarker.template.Template in project perun by CESNET.
the class PerunNotifTemplateManagerImpl method compileTemplate.
private String compileTemplate(final String templateName, Locale locale, Map<String, Object> container) throws IOException, TemplateException {
class NotificationTemplateExceptionHandler implements TemplateExceptionHandler {
@Override
public void handleTemplateException(TemplateException te, Environment env, java.io.Writer out) throws TemplateException {
if (te instanceof InvalidReferenceException) {
// skip undefined values
logger.info("Undefined value found in the TemplateMessage " + templateName + ".", te);
} else {
throw te;
}
}
}
this.configuration.setTemplateExceptionHandler(new NotificationTemplateExceptionHandler());
StringWriter stringWriter = new StringWriter(4096);
Template freeMarkerTemplate = null;
try {
freeMarkerTemplate = this.configuration.getTemplate(templateName + "_" + locale.getLanguage(), locale);
} catch (FileNotFoundException ex) {
if (!(locale.equals(DEFAULT_LOCALE))) {
// if we do not know the language, try to send it at least in default locale
freeMarkerTemplate = this.configuration.getTemplate(templateName + "_" + DEFAULT_LOCALE.getLanguage(), DEFAULT_LOCALE);
logger.info("There is no message with template " + templateName + " in locale " + locale.getLanguage() + ", therefore the message will be sent in " + DEFAULT_LOCALE.getLanguage() + " locale.");
} else {
throw ex;
}
}
freeMarkerTemplate.process(container, stringWriter);
return stringWriter.toString();
}
Aggregations