Search in sources :

Example 16 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project alfresco-repository by Alfresco.

the class HasAspectMethod method exec.

/**
 * @see freemarker.template.TemplateMethodModel#exec(java.util.List)
 */
public Object exec(List args) throws TemplateModelException {
    int result = 0;
    if (args.size() == 2) {
        // arg 0 must be a wrapped TemplateNode object
        BeanModel arg0 = (BeanModel) args.get(0);
        // arg 1 can be either wrapped QName object or a String
        String arg1String = null;
        Object arg1 = args.get(1);
        if (arg1 instanceof BeanModel) {
            arg1String = ((BeanModel) arg1).getWrappedObject().toString();
        } else if (arg1 instanceof TemplateScalarModel) {
            arg1String = ((TemplateScalarModel) arg1).getAsString();
        }
        if (arg0.getWrappedObject() instanceof TemplateNode) {
            // test to see if this node has the aspect
            if (((TemplateNode) arg0.getWrappedObject()).hasAspect(arg1String)) {
                result = 1;
            }
        }
    }
    return Integer.valueOf(result);
}
Also used : BeanModel(freemarker.ext.beans.BeanModel) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Example 17 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project asterixdb by apache.

the class LoadFileDirective method execute.

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    String fileParam = null;
    String defaultParam = null;
    boolean trimParam = false;
    for (Object paramObj : params.entrySet()) {
        Map.Entry param = (Map.Entry) paramObj;
        String paramName = (String) param.getKey();
        TemplateModel paramValue = (TemplateModel) param.getValue();
        switch(paramName) {
            case PARAM_FILE:
                if (paramValue instanceof TemplateScalarModel) {
                    fileParam = ((TemplateScalarModel) paramValue).getAsString();
                } else {
                    throw new TemplateModelException(PARAM_FILE + " must be a string");
                }
                break;
            case PARAM_DEFAULT_TEXT:
                if (paramValue instanceof TemplateScalarModel) {
                    defaultParam = ((TemplateScalarModel) paramValue).getAsString();
                } else {
                    throw new TemplateModelException(PARAM_DEFAULT_TEXT + " must be a string");
                }
                break;
            case PARAM_TRIM:
                if (paramValue instanceof TemplateBooleanModel) {
                    trimParam = ((TemplateBooleanModel) paramValue).getAsBoolean();
                } else {
                    throw new TemplateModelException(PARAM_TRIM + " must be a boolean");
                }
                break;
            default:
                throw new TemplateModelException("Unknown param: " + paramName);
        }
    }
    if (fileParam == null) {
        throw new TemplateModelException("The required \"" + PARAM_FILE + "\" parameter" + "is missing.");
    }
    if (body != null) {
        throw new TemplateModelException("Body is not supported by this directive");
    }
    Writer out = env.getOut();
    File baseDir = ((FileTemplateLoader) ((Configuration) env.getTemplate().getParent()).getTemplateLoader()).baseDir;
    File file = new File(baseDir, fileParam);
    if (file.exists()) {
        if (trimParam) {
            LicenseUtil.readAndTrim(out, file);
            out.write('\n');
        } else {
            IOUtils.copy(new FileInputStream(file), out, StandardCharsets.UTF_8);
        }
    } else if (defaultParam != null) {
        out.append(defaultParam).append("\n");
    } else {
        throw new IOException("File not found: " + file);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TemplateScalarModel(freemarker.template.TemplateScalarModel) FileTemplateLoader(freemarker.cache.FileTemplateLoader) Map(java.util.Map) File(java.io.File) TemplateBooleanModel(freemarker.template.TemplateBooleanModel) Writer(java.io.Writer)

Example 18 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel 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 19 with TemplateScalarModel

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

the class AppLinkDirective method getValue.

@Override
protected String getValue(Environment env, Map params) throws TemplateModelException {
    Object pathObj = params.get("path");
    if (!(pathObj instanceof TemplateScalarModel)) {
        throw new RuntimeException("path parameter required");
    }
    String path = ((TemplateScalarModel) pathObj).getAsString();
    HttpServletRequest request = ((HttpRequestHashModel) env.getDataModel().get("Request")).getRequest();
    return request.getContextPath() + "/" + path;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequestHashModel(freemarker.ext.servlet.HttpRequestHashModel) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Example 20 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project freemarker by apache.

the class Environment method invokeNodeHandlerFor.

/**
 * Used for {@code #visit} and {@code #recurse}.
 */
void invokeNodeHandlerFor(TemplateNodeModel node, TemplateSequenceModel namespaces) throws TemplateException, IOException {
    if (nodeNamespaces == null) {
        SimpleSequence ss = new SimpleSequence(1);
        ss.add(currentNamespace);
        nodeNamespaces = ss;
    }
    int prevNodeNamespaceIndex = this.nodeNamespaceIndex;
    String prevNodeName = this.currentNodeName;
    String prevNodeNS = this.currentNodeNS;
    TemplateSequenceModel prevNodeNamespaces = nodeNamespaces;
    TemplateNodeModel prevVisitorNode = currentVisitorNode;
    currentVisitorNode = node;
    if (namespaces != null) {
        this.nodeNamespaces = namespaces;
    }
    try {
        TemplateModel macroOrTransform = getNodeProcessor(node);
        if (macroOrTransform instanceof Macro) {
            invoke((Macro) macroOrTransform, null, null, null, null);
        } else if (macroOrTransform instanceof TemplateTransformModel) {
            visitAndTransform(null, (TemplateTransformModel) macroOrTransform, null);
        } else {
            String nodeType = node.getNodeType();
            if (nodeType != null) {
                // If the node's type is 'text', we just output it.
                if ((nodeType.equals("text") && node instanceof TemplateScalarModel)) {
                    out.write(((TemplateScalarModel) node).getAsString());
                } else if (nodeType.equals("document")) {
                    recurse(node, namespaces);
                } else // we just ignore it.
                if (!nodeType.equals("pi") && !nodeType.equals("comment") && !nodeType.equals("document_type")) {
                    throw new _MiscTemplateException(this, noNodeHandlerDefinedDescription(node, node.getNodeNamespace(), nodeType));
                }
            } else {
                throw new _MiscTemplateException(this, noNodeHandlerDefinedDescription(node, node.getNodeNamespace(), "default"));
            }
        }
    } finally {
        this.currentVisitorNode = prevVisitorNode;
        this.nodeNamespaceIndex = prevNodeNamespaceIndex;
        this.currentNodeName = prevNodeName;
        this.currentNodeNS = prevNodeNS;
        this.nodeNamespaces = prevNodeNamespaces;
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateNodeModel(freemarker.template.TemplateNodeModel) TemplateTransformModel(freemarker.template.TemplateTransformModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel) SimpleSequence(freemarker.template.SimpleSequence)

Aggregations

TemplateScalarModel (freemarker.template.TemplateScalarModel)41 TemplateModel (freemarker.template.TemplateModel)18 TemplateModelException (freemarker.template.TemplateModelException)18 TemplateHashModel (freemarker.template.TemplateHashModel)9 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)9 IOException (java.io.IOException)9 TemplateNumberModel (freemarker.template.TemplateNumberModel)8 Environment (freemarker.core.Environment)7 BeanModel (freemarker.ext.beans.BeanModel)7 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)7 SimpleScalar (freemarker.template.SimpleScalar)6 Test (org.junit.Test)6 SimpleSequence (freemarker.template.SimpleSequence)5 Template (freemarker.template.Template)5 TemplateNodeModel (freemarker.template.TemplateNodeModel)5 Writer (java.io.Writer)5 Map (java.util.Map)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 TemplateBooleanModel (freemarker.template.TemplateBooleanModel)4 TemplateException (freemarker.template.TemplateException)3