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");
}
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);
}
}
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;
}
}
}
}
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);
}
}
}
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;
}
Aggregations