use of freemarker.template.TemplateScalarModel in project alfresco-repository by Alfresco.
the class HasAspectMethod method exec.
/**
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
*/
public Object exec(List args) throws TemplateModelException {
int result = 0;
if (args.size() == 2) {
// arg 0 must be a wrapped TemplateNode object
BeanModel arg0 = (BeanModel) args.get(0);
// arg 1 can be either wrapped QName object or a String
String arg1String = null;
Object arg1 = args.get(1);
if (arg1 instanceof BeanModel) {
arg1String = ((BeanModel) arg1).getWrappedObject().toString();
} else if (arg1 instanceof TemplateScalarModel) {
arg1String = ((TemplateScalarModel) arg1).getAsString();
}
if (arg0.getWrappedObject() instanceof TemplateNode) {
// test to see if this node has the aspect
if (((TemplateNode) arg0.getWrappedObject()).hasAspect(arg1String)) {
result = 1;
}
}
}
return Integer.valueOf(result);
}
use of freemarker.template.TemplateScalarModel in project asterixdb by apache.
the class LoadFileDirective method execute.
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
String fileParam = null;
String defaultParam = null;
boolean trimParam = false;
for (Object paramObj : params.entrySet()) {
Map.Entry param = (Map.Entry) paramObj;
String paramName = (String) param.getKey();
TemplateModel paramValue = (TemplateModel) param.getValue();
switch(paramName) {
case PARAM_FILE:
if (paramValue instanceof TemplateScalarModel) {
fileParam = ((TemplateScalarModel) paramValue).getAsString();
} else {
throw new TemplateModelException(PARAM_FILE + " must be a string");
}
break;
case PARAM_DEFAULT_TEXT:
if (paramValue instanceof TemplateScalarModel) {
defaultParam = ((TemplateScalarModel) paramValue).getAsString();
} else {
throw new TemplateModelException(PARAM_DEFAULT_TEXT + " must be a string");
}
break;
case PARAM_TRIM:
if (paramValue instanceof TemplateBooleanModel) {
trimParam = ((TemplateBooleanModel) paramValue).getAsBoolean();
} else {
throw new TemplateModelException(PARAM_TRIM + " must be a boolean");
}
break;
default:
throw new TemplateModelException("Unknown param: " + paramName);
}
}
if (fileParam == null) {
throw new TemplateModelException("The required \"" + PARAM_FILE + "\" parameter" + "is missing.");
}
if (body != null) {
throw new TemplateModelException("Body is not supported by this directive");
}
Writer out = env.getOut();
File baseDir = ((FileTemplateLoader) ((Configuration) env.getTemplate().getParent()).getTemplateLoader()).baseDir;
File file = new File(baseDir, fileParam);
if (file.exists()) {
if (trimParam) {
LicenseUtil.readAndTrim(out, file);
out.write('\n');
} else {
IOUtils.copy(new FileInputStream(file), out, StandardCharsets.UTF_8);
}
} else if (defaultParam != null) {
out.append(defaultParam).append("\n");
} else {
throw new IOException("File not found: " + file);
}
}
use of freemarker.template.TemplateScalarModel 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.TemplateScalarModel in project wombat by PLOS.
the class AppLinkDirective method getValue.
@Override
protected String getValue(Environment env, Map params) throws TemplateModelException {
Object pathObj = params.get("path");
if (!(pathObj instanceof TemplateScalarModel)) {
throw new RuntimeException("path parameter required");
}
String path = ((TemplateScalarModel) pathObj).getAsString();
HttpServletRequest request = ((HttpRequestHashModel) env.getDataModel().get("Request")).getRequest();
return request.getContextPath() + "/" + path;
}
use of freemarker.template.TemplateScalarModel in project freemarker by apache.
the class Environment method invokeNodeHandlerFor.
/**
* Used for {@code #visit} and {@code #recurse}.
*/
void invokeNodeHandlerFor(TemplateNodeModel node, TemplateSequenceModel namespaces) throws TemplateException, IOException {
if (nodeNamespaces == null) {
SimpleSequence ss = new SimpleSequence(1);
ss.add(currentNamespace);
nodeNamespaces = ss;
}
int prevNodeNamespaceIndex = this.nodeNamespaceIndex;
String prevNodeName = this.currentNodeName;
String prevNodeNS = this.currentNodeNS;
TemplateSequenceModel prevNodeNamespaces = nodeNamespaces;
TemplateNodeModel prevVisitorNode = currentVisitorNode;
currentVisitorNode = node;
if (namespaces != null) {
this.nodeNamespaces = namespaces;
}
try {
TemplateModel macroOrTransform = getNodeProcessor(node);
if (macroOrTransform instanceof Macro) {
invoke((Macro) macroOrTransform, null, null, null, null);
} else if (macroOrTransform instanceof TemplateTransformModel) {
visitAndTransform(null, (TemplateTransformModel) macroOrTransform, null);
} else {
String nodeType = node.getNodeType();
if (nodeType != null) {
// If the node's type is 'text', we just output it.
if ((nodeType.equals("text") && node instanceof TemplateScalarModel)) {
out.write(((TemplateScalarModel) node).getAsString());
} else if (nodeType.equals("document")) {
recurse(node, namespaces);
} else // we just ignore it.
if (!nodeType.equals("pi") && !nodeType.equals("comment") && !nodeType.equals("document_type")) {
throw new _MiscTemplateException(this, noNodeHandlerDefinedDescription(node, node.getNodeNamespace(), nodeType));
}
} else {
throw new _MiscTemplateException(this, noNodeHandlerDefinedDescription(node, node.getNodeNamespace(), "default"));
}
}
} finally {
this.currentVisitorNode = prevVisitorNode;
this.nodeNamespaceIndex = prevNodeNamespaceIndex;
this.currentNodeName = prevNodeName;
this.currentNodeNS = prevNodeNS;
this.nodeNamespaces = prevNodeNamespaces;
}
}
Aggregations