Search in sources :

Example 61 with Template

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);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Template(freemarker.template.Template) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 62 with Template

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());
    }
}
Also used : Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template) BufferedWriter(java.io.BufferedWriter)

Example 63 with Template

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();
}
Also used : Configuration(freemarker.template.Configuration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Template(freemarker.template.Template) StringTemplateLoader(cz.metacentrum.perun.notif.StringTemplateLoader) PerunBean(cz.metacentrum.perun.core.api.PerunBean) StringWriter(java.io.StringWriter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 64 with Template

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);
    }
}
Also used : TemplateMessageSyntaxErrorException(cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException) ParseException(freemarker.core.ParseException) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Template(freemarker.template.Template)

Example 65 with Template

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();
}
Also used : StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) FileNotFoundException(java.io.FileNotFoundException) Environment(freemarker.core.Environment) TemplateExceptionHandler(freemarker.template.TemplateExceptionHandler) StringWriter(java.io.StringWriter) InvalidReferenceException(freemarker.core.InvalidReferenceException) Template(freemarker.template.Template)

Aggregations

Template (freemarker.template.Template)81 StringWriter (java.io.StringWriter)35 IOException (java.io.IOException)34 Configuration (freemarker.template.Configuration)33 HashMap (java.util.HashMap)28 Writer (java.io.Writer)27 TemplateException (freemarker.template.TemplateException)24 OutputStreamWriter (java.io.OutputStreamWriter)13 File (java.io.File)9 Map (java.util.Map)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 SimpleHash (freemarker.template.SimpleHash)6 JSONObject (org.json.JSONObject)6 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 StringTemplateLoader (freemarker.cache.StringTemplateLoader)4 Environment (freemarker.core.Environment)4 BufferedWriter (java.io.BufferedWriter)4 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4