Search in sources :

Example 6 with TemplateHashModelEx

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

the class TemplateModelUtilTest method testGetKeyValuePairIterator.

@Test
public void testGetKeyValuePairIterator() throws Exception {
    Map<Object, Object> map = new LinkedHashMap<Object, Object>();
    TemplateHashModelEx thme = new TemplateHashModelExOnly(map);
    assertetGetKeyValuePairIteratorContent("", thme);
    map.put("k1", 11);
    assertetGetKeyValuePairIteratorContent("str(k1): num(11)", thme);
    map.put("k2", "v2");
    assertetGetKeyValuePairIteratorContent("str(k1): num(11), str(k2): str(v2)", thme);
    map.put("k2", null);
    assertetGetKeyValuePairIteratorContent("str(k1): num(11), str(k2): null", thme);
    map.put(3, 33);
    try {
        assertetGetKeyValuePairIteratorContent("fails anyway...", thme);
        fail();
    } catch (TemplateModelException e) {
        assertThat(e.getMessage(), allOf(containsString("keys must be"), containsString("string"), containsString("number")));
    }
    map.remove(3);
    map.put(null, 44);
    try {
        assertetGetKeyValuePairIteratorContent("fails anyway...", thme);
        fail();
    } catch (TemplateModelException e) {
        assertThat(e.getMessage(), allOf(containsString("keys must be"), containsString("string"), containsString("Null")));
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateHashModelEx(freemarker.template.TemplateHashModelEx) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 7 with TemplateHashModelEx

use of freemarker.template.TemplateHashModelEx 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 8 with TemplateHashModelEx

use of freemarker.template.TemplateHashModelEx 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 9 with TemplateHashModelEx

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

the class AddConcatExpression method _eval_concatenateHashes.

private static TemplateModel _eval_concatenateHashes(TemplateModel leftModel, TemplateModel rightModel) throws TemplateModelException {
    if (leftModel instanceof TemplateHashModelEx && rightModel instanceof TemplateHashModelEx) {
        TemplateHashModelEx leftModelEx = (TemplateHashModelEx) leftModel;
        TemplateHashModelEx rightModelEx = (TemplateHashModelEx) rightModel;
        if (leftModelEx.size() == 0) {
            return rightModelEx;
        } else if (rightModelEx.size() == 0) {
            return leftModelEx;
        } else {
            return new ConcatenatedHashEx(leftModelEx, rightModelEx);
        }
    } else {
        return new ConcatenatedHash((TemplateHashModel) leftModel, (TemplateHashModel) rightModel);
    }
}
Also used : TemplateHashModelEx(freemarker.template.TemplateHashModelEx)

Example 10 with TemplateHashModelEx

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

TemplateHashModelEx (freemarker.template.TemplateHashModelEx)10 TemplateModelIterator (freemarker.template.TemplateModelIterator)6 TemplateModelException (freemarker.template.TemplateModelException)5 TemplateModel (freemarker.template.TemplateModel)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Set (java.util.Set)2 Test (org.junit.Test)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DefaultObjectWrapperBuilder (freemarker.template.DefaultObjectWrapperBuilder)1 MalformedTemplateNameException (freemarker.template.MalformedTemplateNameException)1 SimpleHash (freemarker.template.SimpleHash)1 Template (freemarker.template.Template)1 TemplateDirectiveBody (freemarker.template.TemplateDirectiveBody)1 TemplateDirectiveModel (freemarker.template.TemplateDirectiveModel)1 TemplateException (freemarker.template.TemplateException)1 KeyValuePair (freemarker.template.TemplateHashModelEx2.KeyValuePair)1 KeyValuePairIterator (freemarker.template.TemplateHashModelEx2.KeyValuePairIterator)1