Search in sources :

Example 1 with TemplateModelIterator

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

the class TemplateModelUtil method getAsMap.

static ImmutableMap<String, TemplateModel> getAsMap(TemplateModel value) throws TemplateModelException {
    if (value == null)
        return ImmutableMap.of();
    if (value instanceof TemplateHashModelEx) {
        TemplateHashModelEx ftlHash = (TemplateHashModelEx) value;
        ImmutableMap.Builder<String, TemplateModel> builder = ImmutableMap.builder();
        for (TemplateModelIterator iterator = ftlHash.keys().iterator(); iterator.hasNext(); ) {
            String key = iterator.next().toString();
            builder.put(key, ftlHash.get(key));
        }
        return builder.build();
    }
    throw new TemplateModelException("Hash type expected");
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) TemplateModel(freemarker.template.TemplateModel) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with TemplateModelIterator

use of freemarker.template.TemplateModelIterator 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 3 with TemplateModelIterator

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

the class Environment method getKnownVariableNames.

/**
 * Returns a set of variable names that are known at the time of call. This includes names of all shared variables
 * in the {@link Configuration}, names of all global variables that were assigned during the template processing,
 * names of all variables in the current name-space, names of all local variables and loop variables. If the passed
 * root data model implements the {@link TemplateHashModelEx} interface, then all names it retrieves through a call
 * to {@link TemplateHashModelEx#keys()} method are returned as well. The method returns a new Set object on each
 * call that is completely disconnected from the Environment. That is, modifying the set will have no effect on the
 * Environment object.
 */
public Set getKnownVariableNames() throws TemplateModelException {
    // shared vars.
    Set set = configuration.getSharedVariableNames();
    // root hash
    if (rootDataModel instanceof TemplateHashModelEx) {
        TemplateModelIterator rootNames = ((TemplateHashModelEx) rootDataModel).keys().iterator();
        while (rootNames.hasNext()) {
            set.add(((TemplateScalarModel) rootNames.next()).getAsString());
        }
    }
    // globals
    for (TemplateModelIterator tmi = globalNamespace.keys().iterator(); tmi.hasNext(); ) {
        set.add(((TemplateScalarModel) tmi.next()).getAsString());
    }
    // current name-space
    for (TemplateModelIterator tmi = currentNamespace.keys().iterator(); tmi.hasNext(); ) {
        set.add(((TemplateScalarModel) tmi.next()).getAsString());
    }
    // locals and loop vars
    if (currentMacroContext != null) {
        set.addAll(currentMacroContext.getLocalVariableNames());
    }
    if (localContextStack != null) {
        for (int i = localContextStack.size() - 1; i >= 0; i--) {
            LocalContext lc = localContextStack.get(i);
            set.addAll(lc.getLocalVariableNames());
        }
    }
    return set;
}
Also used : Set(java.util.Set) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateHashModelEx(freemarker.template.TemplateHashModelEx)

Example 4 with TemplateModelIterator

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

the class CollectionAndSequence method initSequence.

private void initSequence() throws TemplateModelException {
    if (data == null) {
        data = new ArrayList<TemplateModel>();
        TemplateModelIterator it = collection.iterator();
        while (it.hasNext()) {
            data.add(it.next());
        }
    }
}
Also used : TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateModel(freemarker.template.TemplateModel)

Example 5 with TemplateModelIterator

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

the class Environment method getKnownVariableNames.

/**
 * Returns a set of variable names that are known at the time of call. This
 * includes names of all shared variables in the {@link Configuration},
 * names of all global variables that were assigned during the template processing,
 * names of all variables in the current name-space, names of all local variables
 * and loop variables. If the passed root data model implements the
 * {@link TemplateHashModelEx} interface, then all names it retrieves through a call to
 * {@link TemplateHashModelEx#keys()} method are returned as well.
 * The method returns a new Set object on each call that is completely
 * disconnected from the Environment. That is, modifying the set will have
 * no effect on the Environment object.
 */
public Set getKnownVariableNames() throws TemplateModelException {
    // shared vars.
    Set set = getConfiguration().getSharedVariableNames();
    // root hash
    if (rootDataModel instanceof TemplateHashModelEx) {
        TemplateModelIterator rootNames = ((TemplateHashModelEx) rootDataModel).keys().iterator();
        while (rootNames.hasNext()) {
            set.add(((TemplateScalarModel) rootNames.next()).getAsString());
        }
    }
    // globals
    for (TemplateModelIterator tmi = globalNamespace.keys().iterator(); tmi.hasNext(); ) {
        set.add(((TemplateScalarModel) tmi.next()).getAsString());
    }
    // current name-space
    for (TemplateModelIterator tmi = currentNamespace.keys().iterator(); tmi.hasNext(); ) {
        set.add(((TemplateScalarModel) tmi.next()).getAsString());
    }
    // locals and loop vars
    if (currentMacroContext != null) {
        set.addAll(currentMacroContext.getLocalVariableNames());
    }
    if (localContextStack != null) {
        for (int i = localContextStack.size() - 1; i >= 0; i--) {
            LocalContext lc = (LocalContext) localContextStack.get(i);
            set.addAll(lc.getLocalVariableNames());
        }
    }
    return set;
}
Also used : Set(java.util.Set) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateHashModelEx(freemarker.template.TemplateHashModelEx)

Aggregations

TemplateModelIterator (freemarker.template.TemplateModelIterator)11 TemplateHashModelEx (freemarker.template.TemplateHashModelEx)6 TemplateModel (freemarker.template.TemplateModel)4 TemplateModelException (freemarker.template.TemplateModelException)4 ArrayList (java.util.ArrayList)4 TemplateCollectionModel (freemarker.template.TemplateCollectionModel)2 TemplateScalarModel (freemarker.template.TemplateScalarModel)2 List (java.util.List)2 Set (java.util.Set)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 CollectionAndSequence (freemarker.core.CollectionAndSequence)1 DebugModel (freemarker.debug.DebugModel)1 SimpleNumber (freemarker.template.SimpleNumber)1 SimpleSequence (freemarker.template.SimpleSequence)1 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)1 BigInteger (java.math.BigInteger)1 LinkedHashMap (java.util.LinkedHashMap)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1