use of freemarker.template.TemplateTransformModel in project freemarker by apache.
the class Environment method getNodeProcessor.
private TemplateModel getNodeProcessor(Namespace ns, String localName, String nsURI) throws TemplateException {
TemplateModel result = null;
if (nsURI == null) {
result = ns.get(localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
} else {
Template template = ns.getTemplate();
String prefix = template.getPrefixForNamespace(nsURI);
if (prefix == null) {
// since it has no prefix registered for the namespace
return null;
}
if (prefix.length() > 0) {
result = ns.get(prefix + ":" + localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
} else {
if (nsURI.length() == 0) {
result = ns.get(Template.NO_NS_PREFIX + ":" + localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
}
if (nsURI.equals(template.getDefaultNS())) {
result = ns.get(Template.DEFAULT_NAMESPACE_PREFIX + ":" + localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
}
if (result == null) {
result = ns.get(localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
}
}
}
return result;
}
use of freemarker.template.TemplateTransformModel in project freemarker by apache.
the class Environment method getTransform.
TemplateTransformModel getTransform(Expression exp) throws TemplateException {
TemplateTransformModel ttm = null;
TemplateModel tm = exp.eval(this);
if (tm instanceof TemplateTransformModel) {
ttm = (TemplateTransformModel) tm;
} else if (exp instanceof Identifier) {
tm = configuration.getSharedVariable(exp.toString());
if (tm instanceof TemplateTransformModel) {
ttm = (TemplateTransformModel) tm;
}
}
return ttm;
}
use of freemarker.template.TemplateTransformModel in project freemarker by apache.
the class TransformBlock method accept.
@Override
TemplateElement[] accept(Environment env) throws TemplateException, IOException {
TemplateTransformModel ttm = env.getTransform(transformExpression);
if (ttm != null) {
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;
}
env.visitAndTransform(getChildBuffer(), ttm, args);
} else {
TemplateModel tm = transformExpression.eval(env);
throw new UnexpectedTypeException(transformExpression, tm, "transform", new Class[] { TemplateTransformModel.class }, env);
}
return null;
}
use of freemarker.template.TemplateTransformModel in project be5 by DevelopmentOnTheEdge.
the class Environment method getTransform.
TemplateTransformModel getTransform(Expression exp) throws TemplateException {
TemplateTransformModel ttm = null;
TemplateModel tm = exp.eval(this);
if (tm instanceof TemplateTransformModel) {
ttm = (TemplateTransformModel) tm;
} else if (exp instanceof Identifier) {
tm = getConfiguration().getSharedVariable(exp.toString());
if (tm instanceof TemplateTransformModel) {
ttm = (TemplateTransformModel) tm;
}
}
return ttm;
}
use of freemarker.template.TemplateTransformModel 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;
}
}
Aggregations