Search in sources :

Example 1 with SimpleSequence

use of freemarker.template.SimpleSequence in project openj9 by eclipse.

the class BuildInfo method get.

public TemplateModel get(String arg0) throws TemplateModelException {
    if (arg0.equals("stream")) {
        return new SimpleScalar(config.getBuildInfo().getStreamName());
    } else if (arg0.equals("version")) {
        return new Version(config);
    } else if (arg0.equals("fsroots")) {
        return new FsRoots(config);
    } else if (arg0.equals("buildid")) {
        String buildidstr = config.replaceMacro("buildid");
        int buildidint = Integer.valueOf(buildidstr);
        return new SimpleScalar(Integer.toString(buildidint));
    } else if (arg0.equals("unique_build_id")) {
        long uniqueBuildId = config.getBuildSpec().getId().hashCode();
        uniqueBuildId <<= 32;
        String buildidstr = config.replaceMacro("buildid");
        int buildidint = Integer.valueOf(buildidstr);
        uniqueBuildId |= buildidint;
        return new SimpleScalar("0x" + Long.toHexString(uniqueBuildId).toUpperCase());
    } else if (arg0.equalsIgnoreCase("sourceControl")) {
        return new SourceControl(config);
    } else if (arg0.equalsIgnoreCase("runtime")) {
        return new SimpleScalar(config.getBuildInfo().getRuntime());
    } else if (arg0.equalsIgnoreCase("defaultSizes")) {
        return new DefaultSizes(config);
    } else if (arg0.equalsIgnoreCase("jcls")) {
        return new SimpleSequence(config.getBuildInfo().getJCLs());
    } else if (arg0.equalsIgnoreCase("projects")) {
        return new SimpleSequence(config.getBuildInfo().getSources());
    } else if (arg0.equalsIgnoreCase("asmBuilders")) {
        return new SimpleSequence(config.getBuildInfo().getASMBuilders());
    } else if (arg0.equalsIgnoreCase("build_date")) {
        return new SimpleScalar(config.replaceMacro("build_date"));
    } else if (arg0.equalsIgnoreCase("vm_buildtag")) {
        return new SimpleScalar(config.replaceMacro("vm_build_tag"));
    } else if (arg0.equalsIgnoreCase("gc_buildtag")) {
        return new SimpleScalar(config.replaceMacro("gc_build_tag"));
    } else if (arg0.equalsIgnoreCase("jit_buildtag")) {
        return new SimpleScalar(config.replaceMacro("jit_build_tag"));
    }
    return null;
}
Also used : SimpleScalar(freemarker.template.SimpleScalar) SimpleSequence(freemarker.template.SimpleSequence)

Example 2 with SimpleSequence

use of freemarker.template.SimpleSequence in project freemarker by apache.

the class ListLiteral method evaluateStringsToNamespaces.

// A hacky routine used by VisitNode and RecurseNode
TemplateSequenceModel evaluateStringsToNamespaces(Environment env) throws TemplateException {
    TemplateSequenceModel val = (TemplateSequenceModel) eval(env);
    SimpleSequence result = new SimpleSequence(val.size());
    for (int i = 0; i < items.size(); i++) {
        Object itemExpr = items.get(i);
        if (itemExpr instanceof StringLiteral) {
            String s = ((StringLiteral) itemExpr).getAsString();
            try {
                Environment.Namespace ns = env.importLib(s, null);
                result.add(ns);
            } catch (IOException ioe) {
                throw new _MiscTemplateException(((StringLiteral) itemExpr), "Couldn't import library ", new _DelayedJQuote(s), ": ", new _DelayedGetMessage(ioe));
            }
        } else {
            result.add(val.get(i));
        }
    }
    return result;
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) IOException(java.io.IOException) SimpleSequence(freemarker.template.SimpleSequence)

Example 3 with SimpleSequence

use of freemarker.template.SimpleSequence in project freemarker by apache.

the class DynamicKeyName method dealWithRangeKey.

private TemplateModel dealWithRangeKey(TemplateModel targetModel, RangeModel range, Environment env) throws UnexpectedTypeException, InvalidReferenceException, TemplateException {
    final TemplateSequenceModel targetSeq;
    final String targetStr;
    if (targetModel instanceof TemplateSequenceModel) {
        targetSeq = (TemplateSequenceModel) targetModel;
        targetStr = null;
    } else {
        targetSeq = null;
        try {
            targetStr = target.evalAndCoerceToPlainText(env);
        } catch (NonStringException e) {
            throw new UnexpectedTypeException(target, target.eval(env), "sequence or " + NonStringException.STRING_COERCABLE_TYPES_DESC, NUMERICAL_KEY_LHO_EXPECTED_TYPES, env);
        }
    }
    final int size = range.size();
    final boolean rightUnbounded = range.isRightUnbounded();
    final boolean rightAdaptive = range.isRightAdaptive();
    // produces an empty sequence, which thus doesn't contain any illegal indexes.
    if (!rightUnbounded && size == 0) {
        return emptyResult(targetSeq != null);
    }
    final int firstIdx = range.getBegining();
    if (firstIdx < 0) {
        throw new _MiscTemplateException(keyExpression, "Negative range start index (", Integer.valueOf(firstIdx), ") isn't allowed for a range used for slicing.");
    }
    final int targetSize = targetStr != null ? targetStr.length() : targetSeq.size();
    final int step = range.getStep();
    // Right-bounded ranges at this point aren't empty, so the right index surely can't reach targetSize.
    if (rightAdaptive && step == 1 ? firstIdx > targetSize : firstIdx >= targetSize) {
        throw new _MiscTemplateException(keyExpression, "Range start index ", Integer.valueOf(firstIdx), " is out of bounds, because the sliced ", (targetStr != null ? "string" : "sequence"), " has only ", Integer.valueOf(targetSize), " ", (targetStr != null ? "character(s)" : "element(s)"), ". ", "(Note that indices are 0-based).");
    }
    final int resultSize;
    if (!rightUnbounded) {
        final int lastIdx = firstIdx + (size - 1) * step;
        if (lastIdx < 0) {
            if (!rightAdaptive) {
                throw new _MiscTemplateException(keyExpression, "Negative range end index (", Integer.valueOf(lastIdx), ") isn't allowed for a range used for slicing.");
            } else {
                resultSize = firstIdx + 1;
            }
        } else if (lastIdx >= targetSize) {
            if (!rightAdaptive) {
                throw new _MiscTemplateException(keyExpression, "Range end index ", Integer.valueOf(lastIdx), " is out of bounds, because the sliced ", (targetStr != null ? "string" : "sequence"), " has only ", Integer.valueOf(targetSize), " ", (targetStr != null ? "character(s)" : "element(s)"), ". (Note that indices are 0-based).");
            } else {
                resultSize = Math.abs(targetSize - firstIdx);
            }
        } else {
            resultSize = size;
        }
    } else {
        resultSize = targetSize - firstIdx;
    }
    if (resultSize == 0) {
        return emptyResult(targetSeq != null);
    }
    if (targetSeq != null) {
        ArrayList /*<TemplateModel>*/
        list = new ArrayList(resultSize);
        int srcIdx = firstIdx;
        for (int i = 0; i < resultSize; i++) {
            list.add(targetSeq.get(srcIdx));
            srcIdx += step;
        }
        // List items are already wrapped, so the wrapper will be null:
        return new SimpleSequence(list, null);
    } else {
        final int exclEndIdx;
        if (step < 0 && resultSize > 1) {
            if (!(range.isAffactedByStringSlicingBug() && resultSize == 2)) {
                throw new _MiscTemplateException(keyExpression, "Decreasing ranges aren't allowed for slicing strings (as it would give reversed text). " + "The index range was: first = ", Integer.valueOf(firstIdx), ", last = ", Integer.valueOf(firstIdx + (resultSize - 1) * step));
            } else {
                // Emulate the legacy bug, where "foo"[n .. n-1] gives "" instead of an error (if n >= 1).
                // Fix this in FTL [2.4]
                exclEndIdx = firstIdx;
            }
        } else {
            exclEndIdx = firstIdx + resultSize;
        }
        return new SimpleScalar(targetStr.substring(firstIdx, exclEndIdx));
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) ArrayList(java.util.ArrayList) SimpleSequence(freemarker.template.SimpleSequence) SimpleScalar(freemarker.template.SimpleScalar)

Example 4 with SimpleSequence

use of freemarker.template.SimpleSequence in project freemarker by apache.

the class Environment method setMacroContextLocalsFromArguments.

/**
 * Sets the local variables corresponding to the macro call arguments in the macro context.
 */
private void setMacroContextLocalsFromArguments(final Macro.Context macroCtx, final Macro macro, final Map namedArgs, final List positionalArgs) throws TemplateException, _MiscTemplateException {
    String catchAllParamName = macro.getCatchAll();
    if (namedArgs != null) {
        final SimpleHash catchAllParamValue;
        if (catchAllParamName != null) {
            catchAllParamValue = new SimpleHash((ObjectWrapper) null);
            macroCtx.setLocalVar(catchAllParamName, catchAllParamValue);
        } else {
            catchAllParamValue = null;
        }
        for (Iterator it = namedArgs.entrySet().iterator(); it.hasNext(); ) {
            final Map.Entry argNameAndValExp = (Map.Entry) it.next();
            final String argName = (String) argNameAndValExp.getKey();
            final boolean isArgNameDeclared = macro.hasArgNamed(argName);
            if (isArgNameDeclared || catchAllParamName != null) {
                Expression argValueExp = (Expression) argNameAndValExp.getValue();
                TemplateModel argValue = argValueExp.eval(this);
                if (isArgNameDeclared) {
                    macroCtx.setLocalVar(argName, argValue);
                } else {
                    catchAllParamValue.put(argName, argValue);
                }
            } else {
                throw new _MiscTemplateException(this, (macro.isFunction() ? "Function " : "Macro "), new _DelayedJQuote(macro.getName()), " has no parameter with name ", new _DelayedJQuote(argName), ".");
            }
        }
    } else if (positionalArgs != null) {
        final SimpleSequence catchAllParamValue;
        if (catchAllParamName != null) {
            catchAllParamValue = new SimpleSequence((ObjectWrapper) null);
            macroCtx.setLocalVar(catchAllParamName, catchAllParamValue);
        } else {
            catchAllParamValue = null;
        }
        String[] argNames = macro.getArgumentNamesInternal();
        final int argsCnt = positionalArgs.size();
        if (argNames.length < argsCnt && catchAllParamName == null) {
            throw new _MiscTemplateException(this, (macro.isFunction() ? "Function " : "Macro "), new _DelayedJQuote(macro.getName()), " only accepts ", new _DelayedToString(argNames.length), " parameters, but got ", new _DelayedToString(argsCnt), ".");
        }
        for (int i = 0; i < argsCnt; i++) {
            Expression argValueExp = (Expression) positionalArgs.get(i);
            TemplateModel argValue = argValueExp.eval(this);
            try {
                if (i < argNames.length) {
                    String argName = argNames[i];
                    macroCtx.setLocalVar(argName, argValue);
                } else {
                    catchAllParamValue.add(argValue);
                }
            } catch (RuntimeException re) {
                throw new _MiscTemplateException(re, this);
            }
        }
    }
}
Also used : TemplateModel(freemarker.template.TemplateModel) SimpleSequence(freemarker.template.SimpleSequence) SimpleHash(freemarker.template.SimpleHash) Iterator(java.util.Iterator) TemplateModelIterator(freemarker.template.TemplateModelIterator) ObjectWrapper(freemarker.template.ObjectWrapper) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap)

Example 5 with SimpleSequence

use of freemarker.template.SimpleSequence 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

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