Search in sources :

Example 6 with TemplateModelIterator

use of freemarker.template.TemplateModelIterator in project PublicCMS-preview by sanluan.

the class GetHtmlMethod method exec.

@SuppressWarnings("unchecked")
@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException {
    String url = getString(0, arguments);
    TemplateHashModelEx paramters = getMap(1, arguments);
    String body = getString(1, arguments);
    String html = null;
    if (CommonUtils.notEmpty(url)) {
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            HttpUriRequest request;
            if (null != paramters || CommonUtils.notEmpty(body)) {
                HttpPost httppost = new HttpPost(url);
                if (null != paramters) {
                    List<NameValuePair> nvps = new ArrayList<>();
                    TemplateModelIterator it = paramters.keys().iterator();
                    while (it.hasNext()) {
                        String key = TemplateModelUtils.converString(it.next());
                        nvps.add(new BasicNameValuePair(key, TemplateModelUtils.converString(paramters.get(key))));
                    }
                    httppost.setEntity(new UrlEncodedFormEntity(nvps, DEFAULT_CHARSET));
                } else {
                    httppost.setEntity(new StringEntity(body, DEFAULT_CHARSET));
                }
                request = httppost;
            } else {
                request = new HttpGet(url);
            }
            try (CloseableHttpResponse response = httpclient.execute(request)) {
                HttpEntity entity = response.getEntity();
                if (null != entity) {
                    html = EntityUtils.toString(entity, DEFAULT_CHARSET);
                    EntityUtils.consume(entity);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
        return html;
    }
    return null;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) TemplateModelIterator(freemarker.template.TemplateModelIterator) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TemplateModelException(freemarker.template.TemplateModelException) StringEntity(org.apache.http.entity.StringEntity) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 7 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)

Example 8 with TemplateModelIterator

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

the class FtlToYaml method ftlToHash.

public static LinkedHashMap<String, Object> ftlToHash(TemplateHashModelEx model) throws TemplateModelException {
    LinkedHashMap<String, Object> result = new LinkedHashMap<>();
    TemplateCollectionModel keys = model.keys();
    TemplateModelIterator iterator = keys.iterator();
    while (iterator.hasNext()) {
        TemplateModel next = iterator.next();
        if (!(next instanceof TemplateScalarModel)) {
            throw new TemplateModelException("Invalid key: " + next);
        }
        String key = ((TemplateScalarModel) next).getAsString();
        TemplateModel value = model.get(key);
        try {
            result.put(key, ftlToObject(value));
        } catch (TemplateModelException e) {
            throw new TemplateModelException(key + ": " + e.getMessage(), e);
        }
    }
    return result;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateScalarModel(freemarker.template.TemplateScalarModel) TemplateModel(freemarker.template.TemplateModel) TemplateCollectionModel(freemarker.template.TemplateCollectionModel) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with TemplateModelIterator

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

the class BeanModel method values.

public TemplateCollectionModel values() throws TemplateModelException {
    List<Object> values = new ArrayList<Object>(size());
    TemplateModelIterator it = keys().iterator();
    while (it.hasNext()) {
        String key = ((TemplateScalarModel) it.next()).getAsString();
        values.add(get(key));
    }
    return new CollectionAndSequence(new SimpleSequence(values, wrapper));
}
Also used : TemplateModelIterator(freemarker.template.TemplateModelIterator) CollectionAndSequence(freemarker.core.CollectionAndSequence) ArrayList(java.util.ArrayList) TemplateScalarModel(freemarker.template.TemplateScalarModel) SimpleSequence(freemarker.template.SimpleSequence)

Example 10 with TemplateModelIterator

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

the class RmiDebugModelImpl method keys.

public String[] keys() throws TemplateModelException {
    TemplateHashModelEx h = (TemplateHashModelEx) model;
    List list = new ArrayList();
    TemplateModelIterator i = h.keys().iterator();
    while (i.hasNext()) {
        list.add(((TemplateScalarModel) i.next()).getAsString());
    }
    return (String[]) list.toArray(new String[list.size()]);
}
Also used : TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

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