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