Search in sources :

Example 1 with TemplateTransformModel

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;
}
Also used : TemplateTransformModel(freemarker.template.TemplateTransformModel) TemplateModel(freemarker.template.TemplateModel) Template(freemarker.template.Template)

Example 2 with TemplateTransformModel

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;
}
Also used : TemplateTransformModel(freemarker.template.TemplateTransformModel) TemplateModel(freemarker.template.TemplateModel)

Example 3 with TemplateTransformModel

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;
}
Also used : HashMap(java.util.HashMap) Iterator(java.util.Iterator) TemplateTransformModel(freemarker.template.TemplateTransformModel) TemplateModel(freemarker.template.TemplateModel) Map(java.util.Map) HashMap(java.util.HashMap) EmptyMap(freemarker.template.EmptyMap)

Example 4 with TemplateTransformModel

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;
}
Also used : TemplateTransformModel(freemarker.template.TemplateTransformModel) TemplateModel(freemarker.template.TemplateModel)

Example 5 with TemplateTransformModel

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;
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateNodeModel(freemarker.template.TemplateNodeModel) TemplateTransformModel(freemarker.template.TemplateTransformModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel) SimpleSequence(freemarker.template.SimpleSequence)

Aggregations

TemplateModel (freemarker.template.TemplateModel)8 TemplateTransformModel (freemarker.template.TemplateTransformModel)8 EmptyMap (freemarker.template.EmptyMap)2 SimpleSequence (freemarker.template.SimpleSequence)2 Template (freemarker.template.Template)2 TemplateNodeModel (freemarker.template.TemplateNodeModel)2 TemplateScalarModel (freemarker.template.TemplateScalarModel)2 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 TemplateDirectiveModel (freemarker.template.TemplateDirectiveModel)1