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