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