Search in sources :

Example 61 with TemplateModel

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

the class Environment method getNodeProcessor.

private TemplateModel getNodeProcessor(final String nodeName, final String nsURI, int startIndex) throws TemplateException {
    TemplateModel result = null;
    int i;
    int size = nodeNamespaces.size();
    for (i = startIndex; i < size; i++) {
        Namespace ns = null;
        try {
            ns = (Namespace) nodeNamespaces.get(i);
        } catch (ClassCastException cce) {
            throw new _MiscTemplateException(this, "A \"using\" clause should contain a sequence of namespaces or strings that indicate the " + "location of importable macro libraries.");
        }
        result = getNodeProcessor(ns, nodeName, nsURI);
        if (result != null)
            break;
    }
    if (result != null) {
        this.nodeNamespaceIndex = i + 1;
        this.currentNodeName = nodeName;
        this.currentNodeNS = nsURI;
    }
    return result;
}
Also used : TemplateModel(freemarker.template.TemplateModel)

Example 62 with TemplateModel

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

the class Environment method getLocalVariable.

/**
 * Returns the loop or macro local variable corresponding to this variable name. Possibly null. (Note that the
 * misnomer is kept for backward compatibility: loop variables are not local variables according to our
 * terminology.)
 */
public TemplateModel getLocalVariable(String name) throws TemplateModelException {
    if (localContextStack != null) {
        for (int i = localContextStack.size() - 1; i >= 0; i--) {
            LocalContext lc = localContextStack.get(i);
            TemplateModel tm = lc.getLocalVariable(name);
            if (tm != null) {
                return tm;
            }
        }
    }
    return currentMacroContext == null ? null : currentMacroContext.getLocalVariable(name);
}
Also used : TemplateModel(freemarker.template.TemplateModel)

Example 63 with TemplateModel

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

the class Environment method visit.

void visit(final TemplateElement[] childBuffer, TemplateDirectiveModel directiveModel, Map args, final List bodyParameterNames) throws TemplateException, IOException {
    TemplateDirectiveBody nested;
    if (childBuffer == null) {
        nested = null;
    } else {
        nested = new NestedElementTemplateDirectiveBody(childBuffer);
    }
    final TemplateModel[] outArgs;
    if (bodyParameterNames == null || bodyParameterNames.isEmpty()) {
        outArgs = NO_OUT_ARGS;
    } else {
        outArgs = new TemplateModel[bodyParameterNames.size()];
    }
    if (outArgs.length > 0) {
        pushLocalContext(new LocalContext() {

            public TemplateModel getLocalVariable(String name) {
                int index = bodyParameterNames.indexOf(name);
                return index != -1 ? outArgs[index] : null;
            }

            public Collection getLocalVariableNames() {
                return bodyParameterNames;
            }
        });
    }
    try {
        directiveModel.execute(this, args, outArgs, nested);
    } catch (FlowControlException e) {
        throw e;
    } catch (TemplateException e) {
        throw e;
    } catch (IOException e) {
        // For backward compatibility, we assume that this is because the output Writer has thrown it.
        throw e;
    } catch (Exception e) {
        if (EvalUtil.shouldWrapUncheckedException(e, this)) {
            throw new _MiscTemplateException(e, this, "Directive has thrown an unchecked exception; see the cause exception.");
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new UndeclaredThrowableException(e);
        }
    } finally {
        if (outArgs.length > 0) {
            localContextStack.pop();
        }
    }
}
Also used : TemplateException(freemarker.template.TemplateException) TemplateModel(freemarker.template.TemplateModel) IOException(java.io.IOException) UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) MalformedTemplateNameException(freemarker.template.MalformedTemplateNameException) TemplateModelException(freemarker.template.TemplateModelException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) TemplateDirectiveBody(freemarker.template.TemplateDirectiveBody) UndeclaredThrowableException(freemarker.template.utility.UndeclaredThrowableException) Collection(java.util.Collection)

Example 64 with TemplateModel

use of freemarker.template.TemplateModel 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)

Example 65 with TemplateModel

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

the class EvalUtil method compare.

/**
 * Compares two expressions according the rules of the FTL comparator operators.
 *
 * @param leftExp not {@code null}
 * @param operator one of the {@code COMP_OP_...} constants, like {@link #CMP_OP_EQUALS}.
 * @param operatorString can be null {@code null}; the actual operator used, used for more accurate error message.
 * @param rightExp not {@code null}
 * @param env {@code null} is tolerated, but should be avoided
 */
static boolean compare(Expression leftExp, int operator, String operatorString, Expression rightExp, Expression defaultBlamed, Environment env) throws TemplateException {
    TemplateModel ltm = leftExp.eval(env);
    TemplateModel rtm = rightExp.eval(env);
    return compare(ltm, leftExp, operator, operatorString, rtm, rightExp, defaultBlamed, false, false, false, false, env);
}
Also used : TemplateModel(freemarker.template.TemplateModel)

Aggregations

TemplateModel (freemarker.template.TemplateModel)108 TemplateModelException (freemarker.template.TemplateModelException)26 Map (java.util.Map)23 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)15 TemplateException (freemarker.template.TemplateException)13 HashMap (java.util.HashMap)13 SimpleScalar (freemarker.template.SimpleScalar)11 IOException (java.io.IOException)11 List (java.util.List)11 Template (freemarker.template.Template)9 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)8 TemplateTransformModel (freemarker.template.TemplateTransformModel)8 SimpleHash (freemarker.template.SimpleHash)7 SimpleSequence (freemarker.template.SimpleSequence)7 Writer (java.io.Writer)7 Iterator (java.util.Iterator)7 Environment (freemarker.core.Environment)6 TemplateHashModel (freemarker.template.TemplateHashModel)6