Search in sources :

Example 11 with Template

use of freemarker.template.Template in project jforum2 by rafaelsteil.

the class ExceptionWriter method handleExceptionData.

public void handleExceptionData(Throwable t, Writer w, RequestContext request) {
    StringWriter strWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(strWriter);
    t.printStackTrace(writer);
    String currentUrl = this.extractCurrentUrl(request);
    writer.write(currentUrl);
    writer.close();
    try {
        logger.error(strWriter);
        String message = "";
        Throwable cause = t.getCause();
        while (cause != null) {
            message = cause.toString();
            cause = cause.getCause();
        }
        if (message == null || message.equals("")) {
            message = t.getMessage();
        }
        if (message == null || message.equals("")) {
            message = t.toString();
        }
        boolean canViewStackTrace = !SystemGlobals.getBoolValue(ConfigKeys.STACKTRACE_MODERATORS_ONLY) || (SessionFacade.isLogged() && SessionFacade.getUserSession().isModerator());
        String filter = "[<>]";
        String stackTrace = canViewStackTrace ? strWriter.toString() : "Only moderators can view stack trace.";
        stackTrace = stackTrace.replaceAll(filter, "");
        message = message.replaceAll(filter, "");
        SimpleHash templateContext = JForumExecutionContext.getTemplateContext();
        templateContext.put("stackTrace", stackTrace);
        templateContext.put("message", message);
        Template template = JForumExecutionContext.templateConfig().getTemplate("exception.html");
        template.process(templateContext, w);
    } catch (Exception e) {
        strWriter = new StringWriter();
        writer = new PrintWriter(strWriter);
        e.printStackTrace(writer);
        writer.close();
        logger.error(strWriter);
    }
}
Also used : StringWriter(java.io.StringWriter) SimpleHash(freemarker.template.SimpleHash) PrintWriter(java.io.PrintWriter) Template(freemarker.template.Template)

Example 12 with Template

use of freemarker.template.Template in project spring-framework by spring-projects.

the class FreeMarkerMacroTests method testExposeSpringMacroHelpers.

@Test
public void testExposeSpringMacroHelpers() throws Exception {
    FreeMarkerView fv = new FreeMarkerView() {

        @Override
        @SuppressWarnings("rawtypes")
        protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response) throws TemplateException {
            Map model = fmModel.toMap();
            assertTrue(model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext);
            RequestContext rc = (RequestContext) model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
            BindStatus status = rc.getBindStatus("tb.name");
            assertEquals("name", status.getExpression());
            assertEquals("juergen", status.getValue());
        }
    };
    fv.setUrl(TEMPLATE_FILE);
    fv.setApplicationContext(wac);
    fv.setExposeSpringMacroHelpers(true);
    Map<String, Object> model = new HashMap<>();
    model.put("tb", new TestBean("juergen", 99));
    fv.render(model, request, response);
}
Also used : HashMap(java.util.HashMap) TestBean(org.springframework.tests.sample.beans.TestBean) SimpleHash(freemarker.template.SimpleHash) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) DummyMacroRequestContext(org.springframework.web.servlet.view.DummyMacroRequestContext) RequestContext(org.springframework.web.servlet.support.RequestContext) BindStatus(org.springframework.web.servlet.support.BindStatus) HashMap(java.util.HashMap) Map(java.util.Map) Template(freemarker.template.Template) Test(org.junit.Test)

Example 13 with Template

use of freemarker.template.Template in project camel by apache.

the class SpringBootStarterMojo method getTemplate.

private Template getTemplate(String name) throws IOException {
    Configuration cfg = new Configuration(Configuration.getVersion());
    cfg.setTemplateLoader(new URLTemplateLoader() {

        @Override
        protected URL getURL(String name) {
            return SpringBootStarterMojo.class.getResource("/" + name);
        }
    });
    cfg.setDefaultEncoding("UTF-8");
    Template template = cfg.getTemplate(name);
    return template;
}
Also used : URLTemplateLoader(freemarker.cache.URLTemplateLoader) Configuration(freemarker.template.Configuration) URL(java.net.URL) Template(freemarker.template.Template)

Example 14 with Template

use of freemarker.template.Template in project camel by apache.

the class SpringBootStarterMojo method createBasePomFromScratch.

private Document createBasePomFromScratch() throws Exception {
    getLog().info("Creating a new pom.xml for the starter from scratch");
    Template pomTemplate = getTemplate("spring-boot-starter-template-pom.template");
    Map<String, String> props = new HashMap<>();
    props.put("version", project.getVersion());
    props.put("componentId", getComponentId());
    props.put("componentName", project.getName());
    props.put("componentDescription", project.getDescription());
    StringWriter sw = new StringWriter();
    pomTemplate.process(props, sw);
    String xml = sw.toString();
    ByteArrayInputStream bin = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document pom = builder.parse(bin);
    return pom;
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) Template(freemarker.template.Template)

Example 15 with Template

use of freemarker.template.Template in project camel by apache.

the class SpringBootStarterMojo method writeSpringProvides.

private void writeSpringProvides() throws IOException, TemplateException {
    Template fileTemplate = getTemplate("spring-boot-starter-template-spring.provides");
    Map<String, String> props = new HashMap<>();
    props.put("artifactId", project.getArtifactId());
    File outDir = new File(starterDir(), "src/main/resources/META-INF");
    outDir.mkdirs();
    File outFile = new File(outDir, "spring.provides");
    StringWriter sw = new StringWriter();
    fileTemplate.process(props, sw);
    sw.close();
    writeIfChanged(sw.toString(), outFile);
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) File(java.io.File) Template(freemarker.template.Template)

Aggregations

Template (freemarker.template.Template)72 IOException (java.io.IOException)33 StringWriter (java.io.StringWriter)32 Configuration (freemarker.template.Configuration)30 Writer (java.io.Writer)26 HashMap (java.util.HashMap)26 TemplateException (freemarker.template.TemplateException)23 OutputStreamWriter (java.io.OutputStreamWriter)13 File (java.io.File)9 Map (java.util.Map)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 JSONObject (org.json.JSONObject)6 SimpleHash (freemarker.template.SimpleHash)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 StringTemplateLoader (freemarker.cache.StringTemplateLoader)4 Environment (freemarker.core.Environment)4 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)4 BufferedWriter (java.io.BufferedWriter)4 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4