Search in sources :

Example 36 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project ofbiz-framework by apache.

the class OfbizContentTransform method getArg.

private static String getArg(Map args, String key) {
    String result = "";
    Object obj = args.get(key);
    if (obj != null) {
        if (Debug.verboseOn())
            Debug.logVerbose("Arg Object : " + obj.getClass().getName(), module);
        if (obj instanceof TemplateScalarModel) {
            TemplateScalarModel s = (TemplateScalarModel) obj;
            try {
                result = s.getAsString();
            } catch (TemplateModelException e) {
                Debug.logError(e, "Template Exception", module);
            }
        } else {
            result = obj.toString();
        }
    }
    return result;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Example 37 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project be5 by DevelopmentOnTheEdge.

the class Environment method visit.

/**
 * "Visit" A TemplateNodeModel
 */
void visit(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) {
            visit((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)

Example 38 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project be5 by DevelopmentOnTheEdge.

the class Project method getVariables.

public Map<String, String> getVariables() {
    Map<String, String> result = new TreeMap<>();
    try {
        Template template = new Template("", "", getConfiguration());
        Environment environment = template.createProcessingEnvironment(getContext(null), new StringWriter());
        environment.process();
        for (Object name : environment.getKnownVariableNames()) {
            if ("null".equals(name))
                continue;
            Object value = environment.__getitem__(name.toString());
            if (value instanceof TemplateScalarModel) {
                result.put(name.toString(), ((TemplateScalarModel) value).getAsString());
            }
            if (value instanceof String) {
                result.put(name.toString(), (String) value);
            }
            if (value instanceof Number) {
                result.put(name.toString(), ((Number) value).toString());
            }
        }
        return result;
    } catch (IOException | TemplateException e) {
        throw new RuntimeException("Unexpected exception in getVariableNames: " + e);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Template(freemarker.template.Template) StringWriter(java.io.StringWriter) Environment(freemarker.core.Environment) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Example 39 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project ma-core-public by infiniteautomation.

the class MessageFormatDirective method execute.

@Override
public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    TemplateModel key = (TemplateModel) params.get("key");
    String out;
    if (key == null) {
        // No key. Look for a message.
        BeanModel model = (BeanModel) params.get("message");
        if (model == null) {
            if (params.containsKey("message"))
                // The parameter is there, but the value is null.
                out = "";
            else
                // The parameter wasn't given
                throw new TemplateModelException("One of key or message must be provided");
        } else {
            TranslatableMessage message = (TranslatableMessage) model.getWrappedObject();
            if (message == null)
                out = "";
            else
                out = message.translate(translations);
        }
    } else {
        if (key instanceof TemplateScalarModel)
            out = translations.translate(((TemplateScalarModel) key).getAsString());
        else
            throw new TemplateModelException("key must be a string");
    }
    env.getOut().write(out);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 40 with TemplateScalarModel

use of freemarker.template.TemplateScalarModel in project ma-core-public by infiniteautomation.

the class UsedImagesDirective method execute.

@Override
public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    boolean writeLogo = false;
    TemplateModel logo = (TemplateModel) params.get("logo");
    if (logo instanceof TemplateScalarModel) {
        String s = ((TemplateScalarModel) logo).getAsString();
        if (Boolean.parseBoolean(s)) {
            writeLogo = true;
            if (!imageList.contains(Common.applicationLogo))
                imageList.add(Common.applicationLogo);
            env.getOut().write(Common.applicationLogo);
        }
    }
    if (!writeLogo) {
        TemplateModel src = (TemplateModel) params.get("src");
        if (src instanceof TemplateScalarModel) {
            String s = "images/" + ((TemplateScalarModel) src).getAsString();
            if (!imageList.contains(s))
                imageList.add(s);
            env.getOut().write(s);
        } else
            throw new TemplateModelException("key must be a string");
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel)

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