use of freemarker.template.TemplateModelException in project ofbiz-framework by apache.
the class SetRequestAttributeMethod method exec.
/*
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
*/
public Object exec(List args) throws TemplateModelException {
if (args == null || args.size() != 2)
throw new TemplateModelException("Invalid number of arguements");
if (!(args.get(0) instanceof TemplateScalarModel))
throw new TemplateModelException("First argument not an instance of TemplateScalarModel");
if (!(args.get(1) instanceof BeanModel) && !(args.get(1) instanceof TemplateNumberModel) && !(args.get(1) instanceof TemplateScalarModel))
throw new TemplateModelException("Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel");
Environment env = Environment.getCurrentEnvironment();
BeanModel req = (BeanModel) env.getVariable("request");
HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
String name = ((TemplateScalarModel) args.get(0)).getAsString();
Object value = null;
if (args.get(1) instanceof TemplateScalarModel)
value = ((TemplateScalarModel) args.get(1)).getAsString();
if (args.get(1) instanceof TemplateNumberModel)
value = ((TemplateNumberModel) args.get(1)).getAsNumber();
if (args.get(1) instanceof BeanModel)
value = ((BeanModel) args.get(1)).getWrappedObject();
request.setAttribute(name, value);
return new SimpleScalar("");
}
use of freemarker.template.TemplateModelException in project ofbiz-framework by apache.
the class SetContextFieldTransform method exec.
/*
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
*/
@SuppressWarnings("unchecked")
public Object exec(List args) throws TemplateModelException {
if (args == null || args.size() != 2)
throw new TemplateModelException("Invalid number of arguements");
if (!(args.get(0) instanceof TemplateScalarModel))
throw new TemplateModelException("First argument not an instance of TemplateScalarModel");
if (!(args.get(1) instanceof BeanModel) && !(args.get(1) instanceof TemplateNumberModel) && !(args.get(1) instanceof TemplateScalarModel))
throw new TemplateModelException("Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel");
Environment env = Environment.getCurrentEnvironment();
BeanModel req = (BeanModel) env.getVariable("context");
Map context = (Map) req.getWrappedObject();
String name = ((TemplateScalarModel) args.get(0)).getAsString();
Object value = null;
if (args.get(1) instanceof TemplateScalarModel)
value = ((TemplateScalarModel) args.get(1)).getAsString();
if (args.get(1) instanceof TemplateNumberModel)
value = ((TemplateNumberModel) args.get(1)).getAsNumber();
if (args.get(1) instanceof BeanModel)
value = ((BeanModel) args.get(1)).getWrappedObject();
context.put(name, value);
return new SimpleScalar("");
}
use of freemarker.template.TemplateModelException in project ofbiz-framework by apache.
the class OfbizContentTransform method getArg.
private static String getArg(Map args, String key) {
String result = "";
Object obj = args.get(key);
if (obj != null) {
if (Debug.verboseOn())
Debug.logVerbose("Arg Object : " + obj.getClass().getName(), module);
if (obj instanceof TemplateScalarModel) {
TemplateScalarModel s = (TemplateScalarModel) obj;
try {
result = s.getAsString();
} catch (TemplateModelException e) {
Debug.logError(e, "Template Exception", module);
}
} else {
result = obj.toString();
}
}
return result;
}
use of freemarker.template.TemplateModelException in project siddhi by wso2.
the class FormatDescriptionMethod method exec.
@Override
public Object exec(List args) throws TemplateModelException {
if (args.size() != 1) {
throw new TemplateModelException("There should be a single argument of type string " + "passed to format description method");
}
SimpleScalar arg1 = (SimpleScalar) args.get(0);
String inputString = arg1.getAsString();
// Replacing spaces that should not be considered in text wrapping with non breaking spaces
inputString = replaceLeadingSpaces(inputString);
inputString = inputString.replaceAll("<", "<");
inputString = inputString.replaceAll(">", ">");
// Replacing new line characters
inputString = replaceNewLines(inputString);
inputString = inputString.replaceAll("\t", " ");
inputString = inputString.replaceAll("```([^```]*)```", "</p><pre>$1</pre><p style=\"word-wrap: break-word;margin: 0;\">");
inputString = inputString.replaceAll("`([^`]*)`", "<code>$1</code>");
return "<p style=\"word-wrap: break-word;margin: 0;\">" + inputString + "</p>";
}
use of freemarker.template.TemplateModelException in project be5 by DevelopmentOnTheEdge.
the class FreemarkerUtils method getConfiguration.
public static Configuration getConfiguration(Project project) {
Configuration config = (Configuration) configTemplate.clone();
config.setCacheStorage(new SoftCacheStorage());
try {
config.setSharedVariable("project", project);
} catch (TemplateModelException e) {
throw new RuntimeException("Unexpected error: " + e, e);
}
FreemarkerScript macroCollection = project.getMacroCollection().optScript(FreemarkerCatalog.MAIN_MACRO_LIBRARY);
if (macroCollection != null) {
config.addAutoInclude(macroCollection.getCompletePath().toString());
}
for (Module module : project.getModules()) {
FreemarkerCatalog collection = module.getMacroCollection();
if (collection != null) {
FreemarkerScript script = collection.optScript(FreemarkerCatalog.MAIN_MACRO_LIBRARY);
if (script != null) {
config.addAutoInclude(script.getCompletePath().toString());
}
}
}
config.setTemplateLoader(new ProjectTemplateLoader(project));
return config;
}
Aggregations