Search in sources :

Example 71 with TemplateModelException

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("");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) TemplateNumberModel(freemarker.template.TemplateNumberModel) Environment(freemarker.core.Environment) TemplateScalarModel(freemarker.template.TemplateScalarModel) SimpleScalar(freemarker.template.SimpleScalar)

Example 72 with TemplateModelException

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("");
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) TemplateNumberModel(freemarker.template.TemplateNumberModel) Environment(freemarker.core.Environment) TemplateScalarModel(freemarker.template.TemplateScalarModel) Map(java.util.Map) SimpleScalar(freemarker.template.SimpleScalar)

Example 73 with TemplateModelException

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;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Example 74 with TemplateModelException

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("<", "&lt;");
    inputString = inputString.replaceAll(">", "&gt;");
    // Replacing new line characters
    inputString = replaceNewLines(inputString);
    inputString = inputString.replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
    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>";
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleScalar(freemarker.template.SimpleScalar)

Example 75 with TemplateModelException

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;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Configuration(freemarker.template.Configuration) SoftCacheStorage(freemarker.cache.SoftCacheStorage) FreemarkerCatalog(com.developmentontheedge.be5.metadata.model.FreemarkerCatalog) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) Module(com.developmentontheedge.be5.metadata.model.Module)

Aggregations

TemplateModelException (freemarker.template.TemplateModelException)87 TemplateModel (freemarker.template.TemplateModel)24 IOException (java.io.IOException)21 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 SimpleScalar (freemarker.template.SimpleScalar)17 BeanModel (freemarker.ext.beans.BeanModel)14 Environment (freemarker.core.Environment)13 Writer (java.io.Writer)13 ArrayList (java.util.ArrayList)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Map (java.util.Map)10 List (java.util.List)9 SimpleNumber (freemarker.template.SimpleNumber)7 TemplateHashModel (freemarker.template.TemplateHashModel)7 Iterator (java.util.Iterator)6 freemarker.core._TemplateModelException (freemarker.core._TemplateModelException)5 TemplateHashModelEx (freemarker.template.TemplateHashModelEx)5 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)5 TemplateModelIterator (freemarker.template.TemplateModelIterator)5 UMAException (com.ibm.uma.UMAException)4