Search in sources :

Example 6 with Template

use of freemarker.template.Template in project head by mifos.

the class TallyXMLGenerator method getTallyXML.

public static String getTallyXML(List<AccountingDto> accountingData, String fileName) {
    Template temp = getTemplate("master.template");
    String masterData = "";
    try {
        List<TallyMessage> tallyMessages = TALLY_MESSAGE_GENERATOR.generateTallyMessages(accountingData);
        masterData = getMasterData(tallyMessages, fileName);
    } catch (Exception e) {
        masterData = "Contact admin: There was an error encountered :";
        masterData += e.getMessage();
        return masterData;
    }
    /* Create a data-model */
    Map<String, Object> root = new HashMap<String, Object>();
    Map<String, Object> tallyMessage = new HashMap<String, Object>();
    root.put("tallyMessage", tallyMessage);
    tallyMessage.put("headOfficeName", "HEAD OFFICE");
    tallyMessage.put("data", masterData);
    StringWriter bow = new StringWriter();
    process(temp, root, bow);
    return bow.toString();
}
Also used : StringWriter(java.io.StringWriter) TallyMessage(org.mifos.platform.accounting.tally.message.TallyMessage) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) AccountingRuntimeException(org.mifos.platform.accounting.AccountingRuntimeException) Template(freemarker.template.Template)

Example 7 with Template

use of freemarker.template.Template in project head by mifos.

the class TallyXMLGenerator method getTallyMessageData.

private static String getTallyMessageData(TallyMessage tallyMessage, String fileName) {
    Template temp = getTemplate("tally_mesage.template");
    /* Create a data-model */
    Map<String, Object> root = new HashMap<String, Object>();
    Map<String, Object> voucher = new HashMap<String, Object>();
    root.put("voucher", voucher);
    voucher.put("type", tallyMessage.getVoucherType().getValue());
    voucher.put("date", TallyMessageGenerator.DATE_FORMAT.format(tallyMessage.getVoucherDate()));
    voucher.put("fileName", fileName);
    voucher.put("data", getVoucherData(tallyMessage));
    StringWriter bow = new StringWriter();
    process(temp, root, bow);
    return bow.toString() + "\n";
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Template(freemarker.template.Template)

Example 8 with Template

use of freemarker.template.Template in project spark by perwendel.

the class FreeMarkerTemplateEngine method render.

@Override
public String render(ModelAndView modelAndView) {
    try {
        StringWriter stringWriter = new StringWriter();
        Template template = configuration.getTemplate(modelAndView.getViewName());
        template.process(modelAndView.getModel(), stringWriter);
        return stringWriter.toString();
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } catch (TemplateException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 9 with Template

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

the class InstallServlet method service.

/** 
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
	 */
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {
        String encoding = SystemGlobals.getValue(ConfigKeys.ENCODING);
        req.setCharacterEncoding(encoding);
        // Request
        RequestContext request = new WebRequestContext(req);
        ResponseContext response = new WebResponseContext(res);
        request.setCharacterEncoding(encoding);
        JForumExecutionContext ex = JForumExecutionContext.get();
        ForumContext forumContext = new JForumContext(request.getContextPath(), SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION), request, response, false);
        ex.setForumContext(forumContext);
        // Assigns the information to user's thread 
        JForumExecutionContext.set(ex);
        // Context
        SimpleHash context = JForumExecutionContext.getTemplateContext();
        context.put("contextPath", req.getContextPath());
        context.put("serverName", req.getServerName());
        context.put("templateName", "default");
        context.put("serverPort", Integer.toString(req.getServerPort()));
        context.put("I18n", I18n.getInstance());
        context.put("encoding", encoding);
        context.put("extension", SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
        context.put("JForumContext", forumContext);
        context.put("version", SystemGlobals.getValue(ConfigKeys.VERSION));
        if (SystemGlobals.getBoolValue(ConfigKeys.INSTALLED)) {
            JForumExecutionContext.setRedirect(request.getContextPath() + "/forums/list" + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
        } else {
            // Module and Action
            String moduleClass = ModulesRepository.getModuleClass(request.getModule());
            context.put("moduleName", request.getModule());
            context.put("action", request.getAction());
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), encoding));
            try {
                if (moduleClass != null) {
                    // Here we go, baby
                    Command c = (Command) Class.forName(moduleClass).newInstance();
                    Template template = c.process(request, response, context);
                    if (JForumExecutionContext.getRedirectTo() == null) {
                        response.setContentType("text/html; charset=" + encoding);
                        template.process(context, out);
                        out.flush();
                    }
                }
            } catch (Exception e) {
                response.setContentType("text/html; charset=" + encoding);
                if (out != null) {
                    new ExceptionWriter().handleExceptionData(e, out, request);
                } else {
                    new ExceptionWriter().handleExceptionData(e, new BufferedWriter(new OutputStreamWriter(response.getOutputStream())), request);
                }
            }
        }
        String redirectTo = JForumExecutionContext.getRedirectTo();
        if (redirectTo != null) {
            response.sendRedirect(response.encodeRedirectURL(redirectTo));
        }
    } finally {
        JForumExecutionContext.finish();
    }
}
Also used : ExceptionWriter(net.jforum.exceptions.ExceptionWriter) JForumContext(net.jforum.context.JForumContext) ForumContext(net.jforum.context.ForumContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Template(freemarker.template.Template) WebResponseContext(net.jforum.context.web.WebResponseContext) WebRequestContext(net.jforum.context.web.WebRequestContext) ResponseContext(net.jforum.context.ResponseContext) WebResponseContext(net.jforum.context.web.WebResponseContext) SimpleHash(freemarker.template.SimpleHash) OutputStreamWriter(java.io.OutputStreamWriter) RequestContext(net.jforum.context.RequestContext) WebRequestContext(net.jforum.context.web.WebRequestContext) JForumContext(net.jforum.context.JForumContext)

Example 10 with Template

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

the class JForum method processCommand.

private Writer processCommand(Writer out, RequestContext request, ResponseContext response, String encoding, SimpleHash context, String moduleClass) throws Exception {
    // Here we go, baby
    Command c = this.retrieveCommand(moduleClass);
    Template template = c.process(request, response, context);
    if (JForumExecutionContext.getRedirectTo() == null) {
        String contentType = JForumExecutionContext.getContentType();
        if (contentType == null) {
            contentType = "text/html; charset=" + encoding;
        }
        response.setContentType(contentType);
        // manipulation
        if (!JForumExecutionContext.isCustomContent()) {
            out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), encoding));
            template.process(JForumExecutionContext.getTemplateContext(), out);
            out.flush();
        }
    }
    return out;
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) Template(freemarker.template.Template) BufferedWriter(java.io.BufferedWriter)

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