void accept(Environment env) throws IOException, TemplateException {
TemplateModel tm = nameExp.getAsTemplateModel(env);
if (tm == Macro.DO_NOTHING_MACRO) return; // shortcut here.
if (tm instanceof Macro) {
Macro macro = (Macro) tm;
if (macro.isFunction && !legacySyntax) {
throw new TemplateException("Routine " + macro.getName() +
" is a function. A function can only be called " +
"within the evaluation of an expression.", env);
}
env.visit(macro, namedArgs, positionalArgs, bodyParameterNames,
nestedBlock);
}
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.getAsTemplateModel(env);
args.put(key, value);
}
} else {
args = EmptyMap.instance;
}
if(isDirectiveModel) {
env.visit(nestedBlock, (TemplateDirectiveModel) tm, args,
bodyParameterNames);
}
else {
env.visit(nestedBlock, (TemplateTransformModel) tm, args);
}
}
else if (tm == null) {
throw new InvalidReferenceException(this.getStartLocation() + " " +
nameExp + " not found.", env);
} else {
throw new TemplateException(getStartLocation() + ": " + nameExp +
" is not a user-defined directive. It is a " +
tm.getClass().getName(), env);
}
}
}
|