Search in sources :

Example 1 with TemplateSequenceModel

use of freemarker.template.TemplateSequenceModel in project wombat by PLOS.

the class TemplateModelUtil method getAsMultimap.

static ImmutableListMultimap<String, TemplateModel> getAsMultimap(TemplateModel value) throws TemplateModelException {
    if (value == null)
        return ImmutableListMultimap.of();
    if (value instanceof TemplateHashModelEx) {
        TemplateHashModelEx ftlHash = (TemplateHashModelEx) value;
        ImmutableListMultimap.Builder<String, TemplateModel> builder = ImmutableListMultimap.builder();
        for (TemplateModelIterator iterator = ftlHash.keys().iterator(); iterator.hasNext(); ) {
            String key = iterator.next().toString();
            TemplateModel model = ftlHash.get(key);
            if (model instanceof TemplateSequenceModel) {
                TemplateSequenceModel sequenceModel = (TemplateSequenceModel) model;
                int size = sequenceModel.size();
                for (int i = 0; i < size; i++) {
                    builder.put(key, sequenceModel.get(i));
                }
            } else {
                builder.put(key, model);
            }
        }
        return builder.build();
    }
    throw new TemplateModelException("Hash type expected");
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) TemplateModel(freemarker.template.TemplateModel)

Example 2 with TemplateSequenceModel

use of freemarker.template.TemplateSequenceModel in project wombat by PLOS.

the class TemplateModelUtil method getAsList.

static ImmutableList<TemplateModel> getAsList(TemplateModel value) throws TemplateModelException {
    if (value == null)
        return ImmutableList.of();
    if (value instanceof TemplateSequenceModel) {
        ImmutableList.Builder<TemplateModel> builder = ImmutableList.builder();
        TemplateSequenceModel sequenceModel = (TemplateSequenceModel) value;
        int size = sequenceModel.size();
        for (int i = 0; i < size; i++) {
            builder.add(sequenceModel.get(i));
        }
        return builder.build();
    } else {
        return ImmutableList.of(value);
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) ImmutableList(com.google.common.collect.ImmutableList) TemplateModel(freemarker.template.TemplateModel)

Example 3 with TemplateSequenceModel

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

the class AddConcatExpression method _eval.

/**
 * @param leftExp
 *            Used for error messages only; can be {@code null}
 * @param rightExp
 *            Used for error messages only; can be {@code null}
 */
static TemplateModel _eval(Environment env, TemplateObject parent, Expression leftExp, TemplateModel leftModel, Expression rightExp, TemplateModel rightModel) throws TemplateModelException, TemplateException, NonStringException {
    if (leftModel instanceof TemplateNumberModel && rightModel instanceof TemplateNumberModel) {
        Number first = EvalUtil.modelToNumber((TemplateNumberModel) leftModel, leftExp);
        Number second = EvalUtil.modelToNumber((TemplateNumberModel) rightModel, rightExp);
        return _evalOnNumbers(env, parent, first, second);
    } else if (leftModel instanceof TemplateSequenceModel && rightModel instanceof TemplateSequenceModel) {
        return new ConcatenatedSequence((TemplateSequenceModel) leftModel, (TemplateSequenceModel) rightModel);
    } else {
        boolean hashConcatPossible = leftModel instanceof TemplateHashModel && rightModel instanceof TemplateHashModel;
        try {
            // We try string addition first. If hash addition is possible, then instead of throwing exception
            // we return null and do hash addition instead. (We can't simply give hash addition a priority, like
            // with sequence addition above, as FTL strings are often also FTL hashes.)
            Object leftOMOrStr = EvalUtil.coerceModelToStringOrMarkup(leftModel, leftExp, /* returnNullOnNonCoercableType = */
            hashConcatPossible, (String) null, env);
            if (leftOMOrStr == null) {
                return _eval_concatenateHashes(leftModel, rightModel);
            }
            // Same trick with null return as above.
            Object rightOMOrStr = EvalUtil.coerceModelToStringOrMarkup(rightModel, rightExp, /* returnNullOnNonCoercableType = */
            hashConcatPossible, (String) null, env);
            if (rightOMOrStr == null) {
                return _eval_concatenateHashes(leftModel, rightModel);
            }
            if (leftOMOrStr instanceof String) {
                if (rightOMOrStr instanceof String) {
                    return new SimpleScalar(((String) leftOMOrStr).concat((String) rightOMOrStr));
                } else {
                    // rightOMOrStr instanceof TemplateMarkupOutputModel
                    TemplateMarkupOutputModel<?> rightMO = (TemplateMarkupOutputModel<?>) rightOMOrStr;
                    return EvalUtil.concatMarkupOutputs(parent, rightMO.getOutputFormat().fromPlainTextByEscaping((String) leftOMOrStr), rightMO);
                }
            } else {
                // leftOMOrStr instanceof TemplateMarkupOutputModel
                TemplateMarkupOutputModel<?> leftMO = (TemplateMarkupOutputModel<?>) leftOMOrStr;
                if (rightOMOrStr instanceof String) {
                    // markup output
                    return EvalUtil.concatMarkupOutputs(parent, leftMO, leftMO.getOutputFormat().fromPlainTextByEscaping((String) rightOMOrStr));
                } else {
                    // rightOMOrStr instanceof TemplateMarkupOutputModel
                    return EvalUtil.concatMarkupOutputs(parent, leftMO, (TemplateMarkupOutputModel<?>) rightOMOrStr);
                }
            }
        } catch (NonStringOrTemplateOutputException e) {
            // user code might throws this (very unlikely), and then in 2.3.x we did catch that too, incorrectly.
            if (hashConcatPossible) {
                return _eval_concatenateHashes(leftModel, rightModel);
            } else {
                throw e;
            }
        }
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) SimpleNumber(freemarker.template.SimpleNumber) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateNumberModel(freemarker.template.TemplateNumberModel) SimpleScalar(freemarker.template.SimpleScalar)

Example 4 with TemplateSequenceModel

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

the class Environment method recurse.

void recurse(TemplateNodeModel node, TemplateSequenceModel namespaces) throws TemplateException, IOException {
    if (node == null) {
        node = this.getCurrentVisitorNode();
        if (node == null) {
            throw new _TemplateModelException("The target node of recursion is missing or null.");
        }
    }
    TemplateSequenceModel children = node.getChildNodes();
    if (children == null) {
        return;
    }
    int size = children.size();
    for (int i = 0; i < size; i++) {
        TemplateNodeModel child = (TemplateNodeModel) children.get(i);
        if (child != null) {
            invokeNodeHandlerFor(child, namespaces);
        }
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateNodeModel(freemarker.template.TemplateNodeModel)

Example 5 with TemplateSequenceModel

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

Aggregations

TemplateSequenceModel (freemarker.template.TemplateSequenceModel)21 TemplateScalarModel (freemarker.template.TemplateScalarModel)9 TemplateModel (freemarker.template.TemplateModel)8 TemplateHashModel (freemarker.template.TemplateHashModel)7 SimpleSequence (freemarker.template.SimpleSequence)6 TemplateNodeModel (freemarker.template.TemplateNodeModel)6 SimpleScalar (freemarker.template.SimpleScalar)5 TemplateNumberModel (freemarker.template.TemplateNumberModel)4 TemplateModelException (freemarker.template.TemplateModelException)3 Test (org.junit.Test)3 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)2 TemplateTransformModel (freemarker.template.TemplateTransformModel)2 IOException (java.io.IOException)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 freemarker.core._TemplateModelException (freemarker.core._TemplateModelException)1 DebugModel (freemarker.debug.DebugModel)1 WrapperTemplateModel (freemarker.ext.util.WrapperTemplateModel)1 AdapterTemplateModel (freemarker.template.AdapterTemplateModel)1