Search in sources :

Example 11 with SimpleSequence

use of freemarker.template.SimpleSequence in project be5 by DevelopmentOnTheEdge.

the class Environment method visit.

/**
 * "visit" a macro.
 */
void visit(Macro macro, Map namedArgs, List positionalArgs, List bodyParameterNames, TemplateElement nestedBlock) throws TemplateException, IOException {
    if (macro == Macro.DO_NOTHING_MACRO) {
        return;
    }
    pushElement(macro);
    try {
        Macro.Context previousMacroContext = currentMacroContext;
        Macro.Context mc = macro.new Context(this, nestedBlock, bodyParameterNames);
        String catchAll = macro.getCatchAll();
        TemplateModel unknownVars = null;
        if (namedArgs != null) {
            if (catchAll != null)
                unknownVars = new SimpleHash();
            for (Iterator it = namedArgs.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry entry = (Map.Entry) it.next();
                String varName = (String) entry.getKey();
                boolean hasVar = macro.hasArgNamed(varName);
                if (hasVar || catchAll != null) {
                    Expression arg = (Expression) entry.getValue();
                    TemplateModel value = arg.eval(this);
                    if (hasVar) {
                        mc.setLocalVar(varName, value);
                    } else {
                        ((SimpleHash) unknownVars).put(varName, value);
                    }
                } else {
                    throw new _MiscTemplateException(this, new Object[] { "Macro ", new _DelayedJQuote(macro.getName()), " has no such argument: ", varName });
                }
            }
        } else if (positionalArgs != null) {
            if (catchAll != null)
                unknownVars = new SimpleSequence();
            String[] argumentNames = macro.getArgumentNamesInternal();
            int size = positionalArgs.size();
            if (argumentNames.length < size && catchAll == null) {
                throw new _MiscTemplateException(this, new Object[] { "Macro " + StringUtil.jQuote(macro.getName()) + " only accepts " + argumentNames.length + " parameters." });
            }
            for (int i = 0; i < size; i++) {
                Expression argExp = (Expression) positionalArgs.get(i);
                TemplateModel argModel = argExp.eval(this);
                try {
                    if (i < argumentNames.length) {
                        String argName = argumentNames[i];
                        mc.setLocalVar(argName, argModel);
                    } else {
                        ((SimpleSequence) unknownVars).add(argModel);
                    }
                } catch (RuntimeException re) {
                    throw new _MiscTemplateException(re, this);
                }
            }
        }
        if (catchAll != null) {
            mc.setLocalVar(catchAll, unknownVars);
        }
        ArrayList prevLocalContextStack = localContextStack;
        localContextStack = null;
        Namespace prevNamespace = currentNamespace;
        Configurable prevParent = getParent();
        currentNamespace = (Namespace) macroToNamespaceLookup.get(macro);
        currentMacroContext = mc;
        try {
            mc.runMacro(this);
        } catch (ReturnInstruction.Return re) {
        } catch (TemplateException te) {
            handleTemplateException(te);
        } finally {
            currentMacroContext = previousMacroContext;
            localContextStack = prevLocalContextStack;
            currentNamespace = prevNamespace;
            setParent(prevParent);
        }
    } finally {
        popElement();
    }
}
Also used : TemplateException(freemarker.template.TemplateException) ArrayList(java.util.ArrayList) TemplateModel(freemarker.template.TemplateModel) SimpleSequence(freemarker.template.SimpleSequence) SimpleHash(freemarker.template.SimpleHash) Iterator(java.util.Iterator) TemplateModelIterator(freemarker.template.TemplateModelIterator) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with SimpleSequence

use of freemarker.template.SimpleSequence in project be5 by DevelopmentOnTheEdge.

the class Environment method visit.

/**
 * "Visit" A TemplateNodeModel
 */
void visit(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) {
            visit((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

SimpleSequence (freemarker.template.SimpleSequence)12 TemplateModel (freemarker.template.TemplateModel)7 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)6 TemplateScalarModel (freemarker.template.TemplateScalarModel)5 TemplateNodeModel (freemarker.template.TemplateNodeModel)4 TemplateModelIterator (freemarker.template.TemplateModelIterator)3 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 SimpleHash (freemarker.template.SimpleHash)2 SimpleScalar (freemarker.template.SimpleScalar)2 TemplateTransformModel (freemarker.template.TemplateTransformModel)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 IPagerVO (com.agiletec.aps.tags.util.IPagerVO)1 PagerTagHelper (com.agiletec.aps.tags.util.PagerTagHelper)1 CollectionAndSequence (freemarker.core.CollectionAndSequence)1 ObjectWrapper (freemarker.template.ObjectWrapper)1 TemplateException (freemarker.template.TemplateException)1 TemplateHashModel (freemarker.template.TemplateHashModel)1 IOException (java.io.IOException)1