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));
}
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;
}
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;
}
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;
}
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;
}
Aggregations