Search in sources :

Example 6 with TemplateException

use of freemarker.template.TemplateException in project solo by b3log.

the class TopBars method getTopBarHTML.

/**
     * Generates top bar HTML.
     * 
     * @param request the specified request
     * @param response the specified response
     * @return top bar HTML
     * @throws ServiceException service exception 
     */
public String getTopBarHTML(final HttpServletRequest request, final HttpServletResponse response) throws ServiceException {
    Stopwatchs.start("Gens Top Bar HTML");
    try {
        final Template topBarTemplate = ConsoleRenderer.TEMPLATE_CFG.getTemplate("top-bar.ftl");
        final StringWriter stringWriter = new StringWriter();
        final Map<String, Object> topBarModel = new HashMap<String, Object>();
        userMgmtService.tryLogInWithCookie(request, response);
        final JSONObject currentUser = userQueryService.getCurrentUser(request);
        Keys.fillServer(topBarModel);
        topBarModel.put(Common.IS_LOGGED_IN, false);
        topBarModel.put(Common.IS_MOBILE_REQUEST, Requests.mobileRequest(request));
        topBarModel.put("mobileLabel", langPropsService.get("mobileLabel"));
        topBarModel.put("onlineVisitor1Label", langPropsService.get("onlineVisitor1Label"));
        topBarModel.put(Common.ONLINE_VISITOR_CNT, statisticQueryService.getOnlineVisitorCount());
        if (null == currentUser) {
            topBarModel.put(Common.LOGIN_URL, userService.createLoginURL(Common.ADMIN_INDEX_URI));
            topBarModel.put("loginLabel", langPropsService.get("loginLabel"));
            topBarModel.put("registerLabel", langPropsService.get("registerLabel"));
            topBarTemplate.process(topBarModel, stringWriter);
            return stringWriter.toString();
        }
        topBarModel.put(Common.IS_LOGGED_IN, true);
        topBarModel.put(Common.LOGOUT_URL, userService.createLogoutURL("/"));
        topBarModel.put(Common.IS_ADMIN, Role.ADMIN_ROLE.equals(currentUser.getString(User.USER_ROLE)));
        topBarModel.put(Common.IS_VISITOR, Role.VISITOR_ROLE.equals(currentUser.getString(User.USER_ROLE)));
        topBarModel.put("adminLabel", langPropsService.get("adminLabel"));
        topBarModel.put("logoutLabel", langPropsService.get("logoutLabel"));
        final String userName = currentUser.getString(User.USER_NAME);
        topBarModel.put(User.USER_NAME, userName);
        topBarTemplate.process(topBarModel, stringWriter);
        return stringWriter.toString();
    } catch (final JSONException e) {
        LOGGER.log(Level.ERROR, "Gens top bar HTML failed", e);
        throw new ServiceException(e);
    } catch (final IOException e) {
        LOGGER.log(Level.ERROR, "Gens top bar HTML failed", e);
        throw new ServiceException(e);
    } catch (final TemplateException e) {
        LOGGER.log(Level.ERROR, "Gens top bar HTML failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}
Also used : StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 7 with TemplateException

use of freemarker.template.TemplateException in project gitblit by gitblit.

the class FreemarkerPanel method evaluateFreemarkerTemplate.

/**
	 * Evaluates the template and returns the result.
	 *
	 * @param templateReader
	 *            used to read the template
	 * @return the result of evaluating the velocity template
	 */
private String evaluateFreemarkerTemplate(Template template) {
    if (evaluatedTemplate == null) {
        // Get model as a map
        final Map<String, Object> map = (Map<String, Object>) getDefaultModelObject();
        // create a writer for capturing the Velocity output
        StringWriter writer = new StringWriter();
        // of error
        try {
            // execute the Freemarker script and capture the output in writer
            Freemarker.evaluate(template, map, writer);
            // replace the tag's body the Freemarker output
            evaluatedTemplate = writer.toString();
            if (escapeHtml) {
                // encode the result in order to get valid html output that
                // does not break the rest of the page
                evaluatedTemplate = Strings.escapeMarkup(evaluatedTemplate).toString();
            }
            return evaluatedTemplate;
        } catch (IOException e) {
            onException(e);
        } catch (TemplateException e) {
            onException(e);
        }
        return null;
    }
    return evaluatedTemplate;
}
Also used : StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Map(java.util.Map)

Example 8 with TemplateException

use of freemarker.template.TemplateException in project ORCID-Source by ORCID.

the class TemplateManagerImpl method processTemplate.

@Override
public String processTemplate(String templateName, Map<String, Object> params, Locale locale) {
    try {
        Template template = freeMarkerConfiguration.getTemplate(templateName, locale);
        StringWriter result = new StringWriter();
        template.process(params, result);
        return result.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 TemplateException

use of freemarker.template.TemplateException in project ORCID-Source by ORCID.

the class SwaggerUIBuilder method buildSwaggerHTML.

/**
     * Build the swagger UI HTML page
     *
     * @param baseUri
     *            the URL of the main website. e.g. http://orcid.org
     * @param apiUri
     *            the URL of the API e.g. http://pub.orcid.org
     * @param showOAuth
     *            if true, input boxes allowing user to enter client id and
     *            secret will be shown
     * @return a 200 response containing the HTML as text.
     */
public Response buildSwaggerHTML(String baseUri, String apiUri, boolean showOAuth) {
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("swaggerJsonUrl", apiUri + OrcidApiConstants.SWAGGER_PATH + OrcidApiConstants.SWAGGER_FILE);
    map.put("swaggerBaseUrl", baseUri + SWAGGER_STATIC_HTML_PATH);
    map.put("showOAuth", showOAuth);
    map.put("baseUri", baseUri);
    map.put("apiUri", apiUri);
    try {
        Template template = freeMarkerConfiguration.getTemplate(SWAGGER_UI_FTL);
        StringWriter result = new StringWriter();
        template.process(map, result);
        return Response.ok(result.toString()).build();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (TemplateException e) {
        throw new RuntimeException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 10 with TemplateException

use of freemarker.template.TemplateException in project pcgen by PCGen.

the class VariableReport method outputReport.

/**
	 * Output the variable report for the supplied data in a particular format.
	 * 
	 * @param gameModeVarMap The map of variable definitions for each game mode. 
	 * @param gameModeVarCountMap The map of the number of variables for each game mode.
	 * @param reportFormat The format in which to output the report.
	 * @param outputWriter The writer to output the report to.
	 * @throws IOException If the template cannot be accessed or the writer cannot be written to.
	 * @throws TemplateException If there is an error in processing the template.
	 */
public void outputReport(Map<String, List<VarDefine>> gameModeVarMap, Map<String, Integer> gameModeVarCountMap, ReportFormat reportFormat, Writer outputWriter) throws IOException, TemplateException {
    // Configuration
    Writer file = null;
    Configuration cfg = new Configuration();
    int dataPathLen = ConfigurationSettings.getPccFilesDir().length();
    try {
        // Set Directory for templates
        File codeDir = new File("code");
        File templateDir = new File(codeDir, "templates");
        cfg.setDirectoryForTemplateLoading(templateDir);
        // load template
        Template template = cfg.getTemplate(reportFormat.getTemplate());
        // data-model
        Map<String, Object> input = new HashMap<>();
        input.put("gameModeVarMap", gameModeVarMap);
        input.put("gameModeVarCountMap", gameModeVarCountMap);
        input.put("pathIgnoreLen", dataPathLen + 1);
        // Process the template
        template.process(input, outputWriter);
        outputWriter.flush();
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (Exception e2) {
            }
        }
    }
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) PCGFile(pcgen.io.PCGFile) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Template(freemarker.template.Template)

Aggregations

TemplateException (freemarker.template.TemplateException)39 IOException (java.io.IOException)34 Template (freemarker.template.Template)20 HashMap (java.util.HashMap)16 Writer (java.io.Writer)15 StringWriter (java.io.StringWriter)12 Map (java.util.Map)8 Configuration (freemarker.template.Configuration)7 WriterRepresentation (org.restlet.representation.WriterRepresentation)6 File (java.io.File)5 OutputStreamWriter (java.io.OutputStreamWriter)4 MediaType (org.restlet.data.MediaType)4 Representation (org.restlet.representation.Representation)4 BufferedWriter (java.io.BufferedWriter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileWriter (java.io.FileWriter)3 JSONException (org.json.JSONException)3 StringRepresentation (org.restlet.representation.StringRepresentation)3 ParseException (freemarker.core.ParseException)2 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)2