Search in sources :

Example 76 with TemplateModel

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

the class ModelCacheTest method modelCacheOn.

@Test
public void modelCacheOn() throws Exception {
    BeansWrapper bw = new BeansWrapper(Configuration.VERSION_2_3_21);
    bw.setUseCache(true);
    assertTrue(bw.getUseCache());
    String s = "foo";
    assertSame(bw.wrap(s), bw.wrap(s));
    C c = new C();
    TemplateModel wrappedC = bw.wrap(c);
    assertSame(wrappedC, bw.wrap(c));
    bw.clearClassIntrospecitonCache();
    assertNotSame(wrappedC, bw.wrap(c));
    assertSame(bw.wrap(c), bw.wrap(c));
}
Also used : TemplateModel(freemarker.template.TemplateModel) Test(org.junit.Test)

Example 77 with TemplateModel

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

the class UnifiedCall method accept.

@Override
TemplateElement[] accept(Environment env) throws TemplateException, IOException {
    TemplateModel tm = nameExp.eval(env);
    // shortcut here.
    if (tm == Macro.DO_NOTHING_MACRO)
        return null;
    if (tm instanceof Macro) {
        Macro macro = (Macro) tm;
        if (macro.isFunction() && !legacySyntax) {
            throw new _MiscTemplateException(env, "Routine ", new _DelayedJQuote(macro.getName()), " is a function, not a directive. " + "Functions can only be called from expressions, like in ${f()}, ${x + f()} or ", "<@someDirective someParam=f() />", ".");
        }
        env.invoke(macro, namedArgs, positionalArgs, bodyParameterNames, this);
    } else {
        boolean isDirectiveModel = tm instanceof TemplateDirectiveModel;
        if (isDirectiveModel || tm instanceof TemplateTransformModel) {
            Map args;
            if (namedArgs != null && !namedArgs.isEmpty()) {
                args = new HashMap();
                for (Iterator it = namedArgs.entrySet().iterator(); it.hasNext(); ) {
                    Map.Entry entry = (Map.Entry) it.next();
                    String key = (String) entry.getKey();
                    Expression valueExp = (Expression) entry.getValue();
                    TemplateModel value = valueExp.eval(env);
                    args.put(key, value);
                }
            } else {
                args = EmptyMap.instance;
            }
            if (isDirectiveModel) {
                env.visit(getChildBuffer(), (TemplateDirectiveModel) tm, args, bodyParameterNames);
            } else {
                env.visitAndTransform(getChildBuffer(), (TemplateTransformModel) tm, args);
            }
        } else if (tm == null) {
            throw InvalidReferenceException.getInstance(nameExp, env);
        } else {
            throw new NonUserDefinedDirectiveLikeException(nameExp, tm, env);
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) TemplateModel(freemarker.template.TemplateModel) Iterator(java.util.Iterator) TemplateTransformModel(freemarker.template.TemplateTransformModel) HashMap(java.util.HashMap) EmptyMap(freemarker.template.EmptyMap) Map(java.util.Map) TemplateDirectiveModel(freemarker.template.TemplateDirectiveModel)

Example 78 with TemplateModel

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

the class VisitNode method accept.

@Override
TemplateElement[] accept(Environment env) throws IOException, TemplateException {
    TemplateModel node = targetNode.eval(env);
    if (!(node instanceof TemplateNodeModel)) {
        throw new NonNodeException(targetNode, node, env);
    }
    TemplateModel nss = namespaces == null ? null : namespaces.eval(env);
    if (namespaces instanceof StringLiteral) {
        nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null);
    } else if (namespaces instanceof ListLiteral) {
        nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env);
    }
    if (nss != null) {
        if (nss instanceof Environment.Namespace) {
            SimpleSequence ss = new SimpleSequence(1);
            ss.add(nss);
            nss = ss;
        } else if (!(nss instanceof TemplateSequenceModel)) {
            if (namespaces != null) {
                throw new NonSequenceException(namespaces, nss, env);
            } else {
                // Should not occur
                throw new _MiscTemplateException(env, "Expecting a sequence of namespaces after \"using\"");
            }
        }
    }
    env.invokeNodeHandlerFor((TemplateNodeModel) node, (TemplateSequenceModel) nss);
    return null;
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateNodeModel(freemarker.template.TemplateNodeModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel) SimpleSequence(freemarker.template.SimpleSequence)

Example 79 with TemplateModel

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

the class ListLiteral method _eval.

@Override
TemplateModel _eval(Environment env) throws TemplateException {
    SimpleSequence list = new SimpleSequence(items.size());
    for (Iterator it = items.iterator(); it.hasNext(); ) {
        Expression exp = (Expression) it.next();
        TemplateModel tm = exp.eval(env);
        if (env == null || !env.isClassicCompatible()) {
            exp.assertNonNull(tm, env);
        }
        list.add(tm);
    }
    return list;
}
Also used : Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) TemplateModel(freemarker.template.TemplateModel) SimpleSequence(freemarker.template.SimpleSequence)

Example 80 with TemplateModel

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

the class RecurseNode method accept.

@Override
TemplateElement[] accept(Environment env) throws IOException, TemplateException {
    TemplateModel node = targetNode == null ? null : targetNode.eval(env);
    if (node != null && !(node instanceof TemplateNodeModel)) {
        throw new NonNodeException(targetNode, node, "node", env);
    }
    TemplateModel nss = namespaces == null ? null : namespaces.eval(env);
    if (namespaces instanceof StringLiteral) {
        nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null);
    } else if (namespaces instanceof ListLiteral) {
        nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env);
    }
    if (nss != null) {
        if (nss instanceof TemplateHashModel) {
            SimpleSequence ss = new SimpleSequence(1);
            ss.add(nss);
            nss = ss;
        } else if (!(nss instanceof TemplateSequenceModel)) {
            if (namespaces != null) {
                throw new NonSequenceException(namespaces, nss, env);
            } else {
                // Should not occur
                throw new _MiscTemplateException(env, "Expecting a sequence of namespaces after \"using\"");
            }
        }
    }
    env.recurse((TemplateNodeModel) node, (TemplateSequenceModel) nss);
    return null;
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateNodeModel(freemarker.template.TemplateNodeModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel) SimpleSequence(freemarker.template.SimpleSequence)

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