Search in sources :

Example 81 with TemplateModelException

use of freemarker.template.TemplateModelException in project graylog2-server by Graylog2.

the class IndentTemplateDirective method execute.

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    int countParam = 0;
    // Check if no parameters were given:
    if (params.size() != 1) {
        throw new TemplateModelException("Provide 'count' parameter to use the @indent directive.");
    }
    if (loopVars.length != 0) {
        throw new TemplateModelException("This directive doesn't allow loop variables.");
    }
    for (Object entry : params.entrySet()) {
        Map.Entry parameter = (Map.Entry) entry;
        String paramName = (String) parameter.getKey();
        TemplateModel paramValue = (TemplateModel) parameter.getValue();
        if (paramName.equals(PARAM_NAME_COUNT)) {
            if (!(paramValue instanceof TemplateNumberModel)) {
                throw new TemplateModelException("Parameter '" + PARAM_NAME_COUNT + "' must be a number.");
            }
            countParam = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
            if (countParam < 0) {
                throw new TemplateModelException("Parameter '" + PARAM_NAME_COUNT + "' can't be negative.");
            }
        }
    }
    // If there is non-empty nested content:
    if (body != null) {
        // Executes the nested body. Same as <#nested> in FTL, except
        // that we use our own writer instead of the current output writer.
        body.render(new IndentFilterWriter(countParam, env.getOut()));
    } else {
        throw new RuntimeException("Body is missing");
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateNumberModel(freemarker.template.TemplateNumberModel) TemplateModel(freemarker.template.TemplateModel) Map(java.util.Map)

Example 82 with TemplateModelException

use of freemarker.template.TemplateModelException in project wombat by PLOS.

the class ArticleExcerptTransformDirective method getValue.

@Override
protected String getValue(Environment env, Map params) throws TemplateModelException, IOException {
    Object xmlParam = params.get("xml");
    if (!(xmlParam instanceof TemplateScalarModel)) {
        throw new TemplateModelException("xml param must be a non-null string");
    }
    String xml = ((TemplateScalarModel) xmlParam).getAsString();
    boolean isTextOnly = TemplateModelUtil.getBooleanValue((TemplateModel) params.get("textOnly"));
    if (isTextOnly) {
        return StringEscapeUtils.escapeHtml(XmlUtil.extractText(xml));
    }
    Site site = new SitePageContext(siteResolver, env).getSite();
    Transformer transformer = SITE_TRANSFORMER_FACTORY.build(site);
    StringWriter html = new StringWriter();
    try {
        transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(html));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    return html.toString();
}
Also used : Site(org.ambraproject.wombat.config.site.Site) TemplateModelException(freemarker.template.TemplateModelException) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) TemplateScalarModel(freemarker.template.TemplateScalarModel) TransformerException(javax.xml.transform.TransformerException)

Example 83 with TemplateModelException

use of freemarker.template.TemplateModelException in project wombat by PLOS.

the class FetchHtmlDirective method execute.

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    Object typeObj = params.get("type");
    if (typeObj == null) {
        throw new TemplateModelException("type parameter required");
    }
    Object pathObj = params.get("path");
    if (pathObj == null) {
        throw new TemplateModelException("path parameter required");
    }
    String pageType = typeObj.toString();
    Set<HtmlElementTransformation> transformations = ELEMENT_TRANSFORMS.get(pageType);
    if (transformations == null) {
        throw new TemplateModelException(String.format("type parameter '%s' is invalid.", pageType));
    }
    ImmutableList<HtmlElementSubstitution> substitutions = HtmlElementSubstitution.buildList(body, SUBST_ATTR_NAME);
    SitePageContext sitePageContext = new SitePageContext(siteResolver, env);
    try (Reader html = editorialContentApi.readHtml(sitePageContext, pageType, pathObj.toString(), transformations, substitutions)) {
        IOUtils.copy(html, env.getOut());
    } catch (EntityNotFoundException e) {
        // TODO: Allow themes to provide custom, user-visible error blocks
        log.error("Could not retrieve HTML of type \"{}\" at path \"{}\"", typeObj, pathObj);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Reader(java.io.Reader) EntityNotFoundException(org.ambraproject.wombat.service.EntityNotFoundException)

Example 84 with TemplateModelException

use of freemarker.template.TemplateModelException in project wombat by PLOS.

the class BuildInfoDirective method getValue.

@Override
protected Object getValue(Environment env, Map params) throws TemplateException, IOException {
    String component = params.get("component").toString();
    BuildInfo info;
    switch(component) {
        case "webapp":
            info = buildInfoService.getWebappBuildInfo();
            break;
        case "service":
            info = buildInfoService.getServiceBuildInfo();
            break;
        default:
            throw new TemplateModelException("component required");
    }
    final Object value;
    if (info == null) {
        value = null;
    } else {
        String field = params.get("field").toString();
        switch(field) {
            case "version":
                value = info.getVersion();
                break;
            case "date":
                value = info.getDate();
                break;
            case "user":
                value = info.getUser();
                break;
            case "commitIdAbbrev":
                value = info.getGitCommitIdAbbrev();
                break;
            case "enabledDevFeatures":
                value = info.getEnabledDevFeatures();
                break;
            default:
                throw new TemplateModelException("field required");
        }
    }
    return (value != null) ? value : "?";
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) BuildInfo(org.ambraproject.wombat.util.BuildInfo)

Example 85 with TemplateModelException

use of freemarker.template.TemplateModelException in project wombat by PLOS.

the class Iso8601DateDirective method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Environment environment, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    if (params.get("date") == null) {
        throw new TemplateModelException("date parameter is required");
    }
    String jsonDate = params.get("date").toString();
    if (params.get("format") == null) {
        throw new TemplateModelException("format parameter is required");
    }
    DateTimeFormatter format = DateTimeFormatter.ofPattern(params.get("format").toString());
    String formattedDate = (jsonDate.length() <= 10) ? LocalDate.parse(jsonDate).format(format) : Instant.parse(jsonDate).atZone(GMT).format(format);
    environment.getOut().write(formattedDate);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) DateTimeFormatter(java.time.format.DateTimeFormatter)

Aggregations

TemplateModelException (freemarker.template.TemplateModelException)87 TemplateModel (freemarker.template.TemplateModel)24 IOException (java.io.IOException)21 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 SimpleScalar (freemarker.template.SimpleScalar)17 BeanModel (freemarker.ext.beans.BeanModel)14 Environment (freemarker.core.Environment)13 Writer (java.io.Writer)13 ArrayList (java.util.ArrayList)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Map (java.util.Map)10 List (java.util.List)9 SimpleNumber (freemarker.template.SimpleNumber)7 TemplateHashModel (freemarker.template.TemplateHashModel)7 Iterator (java.util.Iterator)6 freemarker.core._TemplateModelException (freemarker.core._TemplateModelException)5 TemplateHashModelEx (freemarker.template.TemplateHashModelEx)5 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)5 TemplateModelIterator (freemarker.template.TemplateModelIterator)5 UMAException (com.ibm.uma.UMAException)4