use of freemarker.template.TemplateModelException in project graylog2-server by Graylog2.
the class IndentTemplateDirective method execute.
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
int countParam = 0;
// Check if no parameters were given:
if (params.size() != 1) {
throw new TemplateModelException("Provide 'count' parameter to use the @indent directive.");
}
if (loopVars.length != 0) {
throw new TemplateModelException("This directive doesn't allow loop variables.");
}
for (Object entry : params.entrySet()) {
Map.Entry parameter = (Map.Entry) entry;
String paramName = (String) parameter.getKey();
TemplateModel paramValue = (TemplateModel) parameter.getValue();
if (paramName.equals(PARAM_NAME_COUNT)) {
if (!(paramValue instanceof TemplateNumberModel)) {
throw new TemplateModelException("Parameter '" + PARAM_NAME_COUNT + "' must be a number.");
}
countParam = ((TemplateNumberModel) paramValue).getAsNumber().intValue();
if (countParam < 0) {
throw new TemplateModelException("Parameter '" + PARAM_NAME_COUNT + "' can't be negative.");
}
}
}
// If there is non-empty nested content:
if (body != null) {
// Executes the nested body. Same as <#nested> in FTL, except
// that we use our own writer instead of the current output writer.
body.render(new IndentFilterWriter(countParam, env.getOut()));
} else {
throw new RuntimeException("Body is missing");
}
}
use of freemarker.template.TemplateModelException in project wombat by PLOS.
the class ArticleExcerptTransformDirective method getValue.
@Override
protected String getValue(Environment env, Map params) throws TemplateModelException, IOException {
Object xmlParam = params.get("xml");
if (!(xmlParam instanceof TemplateScalarModel)) {
throw new TemplateModelException("xml param must be a non-null string");
}
String xml = ((TemplateScalarModel) xmlParam).getAsString();
boolean isTextOnly = TemplateModelUtil.getBooleanValue((TemplateModel) params.get("textOnly"));
if (isTextOnly) {
return StringEscapeUtils.escapeHtml(XmlUtil.extractText(xml));
}
Site site = new SitePageContext(siteResolver, env).getSite();
Transformer transformer = SITE_TRANSFORMER_FACTORY.build(site);
StringWriter html = new StringWriter();
try {
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(html));
} catch (TransformerException e) {
throw new RuntimeException(e);
}
return html.toString();
}
use of freemarker.template.TemplateModelException in project wombat by PLOS.
the class FetchHtmlDirective method execute.
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
Object typeObj = params.get("type");
if (typeObj == null) {
throw new TemplateModelException("type parameter required");
}
Object pathObj = params.get("path");
if (pathObj == null) {
throw new TemplateModelException("path parameter required");
}
String pageType = typeObj.toString();
Set<HtmlElementTransformation> transformations = ELEMENT_TRANSFORMS.get(pageType);
if (transformations == null) {
throw new TemplateModelException(String.format("type parameter '%s' is invalid.", pageType));
}
ImmutableList<HtmlElementSubstitution> substitutions = HtmlElementSubstitution.buildList(body, SUBST_ATTR_NAME);
SitePageContext sitePageContext = new SitePageContext(siteResolver, env);
try (Reader html = editorialContentApi.readHtml(sitePageContext, pageType, pathObj.toString(), transformations, substitutions)) {
IOUtils.copy(html, env.getOut());
} catch (EntityNotFoundException e) {
// TODO: Allow themes to provide custom, user-visible error blocks
log.error("Could not retrieve HTML of type \"{}\" at path \"{}\"", typeObj, pathObj);
}
}
use of freemarker.template.TemplateModelException in project wombat by PLOS.
the class BuildInfoDirective method getValue.
@Override
protected Object getValue(Environment env, Map params) throws TemplateException, IOException {
String component = params.get("component").toString();
BuildInfo info;
switch(component) {
case "webapp":
info = buildInfoService.getWebappBuildInfo();
break;
case "service":
info = buildInfoService.getServiceBuildInfo();
break;
default:
throw new TemplateModelException("component required");
}
final Object value;
if (info == null) {
value = null;
} else {
String field = params.get("field").toString();
switch(field) {
case "version":
value = info.getVersion();
break;
case "date":
value = info.getDate();
break;
case "user":
value = info.getUser();
break;
case "commitIdAbbrev":
value = info.getGitCommitIdAbbrev();
break;
case "enabledDevFeatures":
value = info.getEnabledDevFeatures();
break;
default:
throw new TemplateModelException("field required");
}
}
return (value != null) ? value : "?";
}
use of freemarker.template.TemplateModelException in project wombat by PLOS.
the class Iso8601DateDirective method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute(Environment environment, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
if (params.get("date") == null) {
throw new TemplateModelException("date parameter is required");
}
String jsonDate = params.get("date").toString();
if (params.get("format") == null) {
throw new TemplateModelException("format parameter is required");
}
DateTimeFormatter format = DateTimeFormatter.ofPattern(params.get("format").toString());
String formattedDate = (jsonDate.length() <= 10) ? LocalDate.parse(jsonDate).format(format) : Instant.parse(jsonDate).atZone(GMT).format(format);
environment.getOut().write(formattedDate);
}
Aggregations