Search in sources :

Example 51 with TemplateModel

use of freemarker.template.TemplateModel 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);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TemplateScalarModel(freemarker.template.TemplateScalarModel) FileTemplateLoader(freemarker.cache.FileTemplateLoader) Map(java.util.Map) File(java.io.File) TemplateBooleanModel(freemarker.template.TemplateBooleanModel) Writer(java.io.Writer)

Example 52 with TemplateModel

use of freemarker.template.TemplateModel in project engine by craftercms.

the class AllHttpScopesAndAppContextHashModel method get.

@Override
public TemplateModel get(String key) throws TemplateModelException {
    // Lookup in page scope
    TemplateModel model = super.get(key);
    if (model != null) {
        return model;
    }
    // Lookup in request scope
    Object obj = request.getAttribute(key);
    if (obj != null) {
        return wrap(obj);
    }
    // Lookup in session scope
    HttpSession session = request.getSession(false);
    if (session != null) {
        obj = session.getAttribute(key);
        if (obj != null) {
            return wrap(obj);
        }
    }
    // Lookup in application scope
    obj = context.getAttribute(key);
    if (obj != null) {
        return wrap(obj);
    }
    // Lookup in application context
    try {
        return wrap(applicationContextAccessor.get(key));
    } catch (NoSuchBeanDefinitionException e) {
    }
    // return wrapper's null object (probably null).
    return wrap(null);
}
Also used : HttpSession(javax.servlet.http.HttpSession) TemplateModel(freemarker.template.TemplateModel) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 53 with TemplateModel

use of freemarker.template.TemplateModel in project engine by craftercms.

the class RenderComponentDirective method execute.

@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
    TemplateModel componentParam = (TemplateModel) params.get(COMPONENT_PARAM_NAME);
    TemplateModel componentPathParam = (TemplateModel) params.get(COMPONENT_PATH_PARAM_NAME);
    TemplateModel additionalModelParam = (TemplateModel) params.get(ADDITIONAL_MODEL_PARAM_NAME);
    Map<String, Object> additionalModel = null;
    SiteItem component;
    if (componentParam == null && componentPathParam == null) {
        throw new TemplateException("No '" + COMPONENT_PARAM_NAME + "' or '" + COMPONENT_PATH_PARAM_NAME + "' param specified", env);
    } else if (componentParam != null) {
        component = getComponentFromNode(componentParam, env);
    } else {
        component = getComponentFromPath(componentPathParam, env);
    }
    if (additionalModelParam != null) {
        additionalModel = unwrap(ADDITIONAL_MODEL_PARAM_NAME, additionalModelParam, Map.class, env);
    }
    Map<String, Object> templateModel = executeScripts(component, additionalModel, env);
    SimpleHash model = getFullModel(component, templateModel, additionalModel);
    Template template = getTemplate(component, env);
    Writer output = env.getOut();
    processComponentTemplate(template, model, output, env);
}
Also used : TemplateException(freemarker.template.TemplateException) SimpleHash(freemarker.template.SimpleHash) TemplateModel(freemarker.template.TemplateModel) HashMap(java.util.HashMap) Map(java.util.Map) SiteItem(org.craftercms.engine.model.SiteItem) Writer(java.io.Writer) Template(freemarker.template.Template)

Example 54 with TemplateModel

use of freemarker.template.TemplateModel in project sling by apache.

the class AdaptToMethod method exec.

@Override
public Object exec(final List arguments) throws TemplateModelException {
    if (arguments.size() != 2) {
        throw new TemplateModelException("wrong number of arguments, expecting 2 (adaptable and adapter type).");
    }
    try {
        final String classname = arguments.get(1).toString();
        final Class<?> clazz = dynamicClassLoaderManager.getDynamicClassLoader().loadClass(classname);
        final TemplateModel templateModel = (TemplateModel) arguments.get(0);
        final Object adaptable = DeepUnwrap.unwrap(templateModel);
        return adapterManager.getAdapter(adaptable, clazz);
    } catch (Exception e) {
        throw new TemplateModelException(e);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateModel(freemarker.template.TemplateModel) TemplateModelException(freemarker.template.TemplateModelException)

Example 55 with TemplateModel

use of freemarker.template.TemplateModel in project PublicCMS-preview by sanluan.

the class TemplateDirectiveHandler method reduce.

private Map<String, TemplateModel> reduce() throws TemplateModelException {
    Map<String, TemplateModel> reduceMap = new LinkedHashMap<>();
    ObjectWrapper objectWrapper = environment.getObjectWrapper();
    Namespace namespace = environment.getCurrentNamespace();
    Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
    for (int i = 0; iterator.hasNext(); i++) {
        Entry<String, Object> entry = iterator.next();
        if (i < loopVars.length) {
            loopVars[i] = objectWrapper.wrap(entry.getValue());
        } else {
            String key = entry.getKey();
            reduceMap.put(key, namespace.get(key));
            namespace.put(key, objectWrapper.wrap(entry.getValue()));
        }
    }
    return reduceMap;
}
Also used : Entry(java.util.Map.Entry) ObjectWrapper(freemarker.template.ObjectWrapper) TemplateModel(freemarker.template.TemplateModel) Namespace(freemarker.core.Environment.Namespace) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TemplateModel (freemarker.template.TemplateModel)108 TemplateModelException (freemarker.template.TemplateModelException)26 Map (java.util.Map)23 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)15 TemplateException (freemarker.template.TemplateException)13 HashMap (java.util.HashMap)13 SimpleScalar (freemarker.template.SimpleScalar)11 IOException (java.io.IOException)11 List (java.util.List)11 Template (freemarker.template.Template)9 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)8 TemplateTransformModel (freemarker.template.TemplateTransformModel)8 SimpleHash (freemarker.template.SimpleHash)7 SimpleSequence (freemarker.template.SimpleSequence)7 Writer (java.io.Writer)7 Iterator (java.util.Iterator)7 Environment (freemarker.core.Environment)6 TemplateHashModel (freemarker.template.TemplateHashModel)6