Search in sources :

Example 1 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project pcgen by PCGen.

the class ItemFacetModel method getInternalHashModel.

private TemplateHashModel getInternalHashModel() throws TemplateModelException {
    if (!cacheSet) {
        T obj = facet.get(id);
        TemplateModel wrapped = WRAPPER_FACET.wrap(id, obj);
        if (wrapped instanceof TemplateHashModel) {
            cache = (TemplateHashModel) wrapped;
            cacheSet = true;
        }
    }
    return cache;
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) TemplateModel(freemarker.template.TemplateModel)

Example 2 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project freemarker by apache.

the class AddConcatExpression method _eval.

/**
 * @param leftExp
 *            Used for error messages only; can be {@code null}
 * @param rightExp
 *            Used for error messages only; can be {@code null}
 */
static TemplateModel _eval(Environment env, TemplateObject parent, Expression leftExp, TemplateModel leftModel, Expression rightExp, TemplateModel rightModel) throws TemplateModelException, TemplateException, NonStringException {
    if (leftModel instanceof TemplateNumberModel && rightModel instanceof TemplateNumberModel) {
        Number first = EvalUtil.modelToNumber((TemplateNumberModel) leftModel, leftExp);
        Number second = EvalUtil.modelToNumber((TemplateNumberModel) rightModel, rightExp);
        return _evalOnNumbers(env, parent, first, second);
    } else if (leftModel instanceof TemplateSequenceModel && rightModel instanceof TemplateSequenceModel) {
        return new ConcatenatedSequence((TemplateSequenceModel) leftModel, (TemplateSequenceModel) rightModel);
    } else {
        boolean hashConcatPossible = leftModel instanceof TemplateHashModel && rightModel instanceof TemplateHashModel;
        try {
            // We try string addition first. If hash addition is possible, then instead of throwing exception
            // we return null and do hash addition instead. (We can't simply give hash addition a priority, like
            // with sequence addition above, as FTL strings are often also FTL hashes.)
            Object leftOMOrStr = EvalUtil.coerceModelToStringOrMarkup(leftModel, leftExp, /* returnNullOnNonCoercableType = */
            hashConcatPossible, (String) null, env);
            if (leftOMOrStr == null) {
                return _eval_concatenateHashes(leftModel, rightModel);
            }
            // Same trick with null return as above.
            Object rightOMOrStr = EvalUtil.coerceModelToStringOrMarkup(rightModel, rightExp, /* returnNullOnNonCoercableType = */
            hashConcatPossible, (String) null, env);
            if (rightOMOrStr == null) {
                return _eval_concatenateHashes(leftModel, rightModel);
            }
            if (leftOMOrStr instanceof String) {
                if (rightOMOrStr instanceof String) {
                    return new SimpleScalar(((String) leftOMOrStr).concat((String) rightOMOrStr));
                } else {
                    // rightOMOrStr instanceof TemplateMarkupOutputModel
                    TemplateMarkupOutputModel<?> rightMO = (TemplateMarkupOutputModel<?>) rightOMOrStr;
                    return EvalUtil.concatMarkupOutputs(parent, rightMO.getOutputFormat().fromPlainTextByEscaping((String) leftOMOrStr), rightMO);
                }
            } else {
                // leftOMOrStr instanceof TemplateMarkupOutputModel
                TemplateMarkupOutputModel<?> leftMO = (TemplateMarkupOutputModel<?>) leftOMOrStr;
                if (rightOMOrStr instanceof String) {
                    // markup output
                    return EvalUtil.concatMarkupOutputs(parent, leftMO, leftMO.getOutputFormat().fromPlainTextByEscaping((String) rightOMOrStr));
                } else {
                    // rightOMOrStr instanceof TemplateMarkupOutputModel
                    return EvalUtil.concatMarkupOutputs(parent, leftMO, (TemplateMarkupOutputModel<?>) rightOMOrStr);
                }
            }
        } catch (NonStringOrTemplateOutputException e) {
            // user code might throws this (very unlikely), and then in 2.3.x we did catch that too, incorrectly.
            if (hashConcatPossible) {
                return _eval_concatenateHashes(leftModel, rightModel);
            } else {
                throw e;
            }
        }
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) SimpleNumber(freemarker.template.SimpleNumber) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateNumberModel(freemarker.template.TemplateNumberModel) SimpleScalar(freemarker.template.SimpleScalar)

Example 3 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project xxl-job by xuxueli.

the class FtlUtil method generateStaticModel.

public static TemplateHashModel generateStaticModel(String packageName) {
    try {
        BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
        TemplateHashModel staticModels = wrapper.getStaticModels();
        TemplateHashModel fileStatics = (TemplateHashModel) staticModels.get(packageName);
        return fileStatics;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) BeansWrapper(freemarker.ext.beans.BeansWrapper)

Example 4 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project ofbiz-framework by apache.

the class FreeMarkerWorker method makeConfiguration.

public static Configuration makeConfiguration(BeansWrapper wrapper) {
    Configuration newConfig = newConfiguration();
    newConfig.setObjectWrapper(wrapper);
    TemplateHashModel staticModels = wrapper.getStaticModels();
    newConfig.setSharedVariable("Static", staticModels);
    try {
        newConfig.setSharedVariable("EntityQuery", staticModels.get("org.apache.ofbiz.entity.util.EntityQuery"));
    } catch (TemplateModelException e) {
        Debug.logError(e, module);
    }
    newConfig.setLocalizedLookup(false);
    newConfig.setSharedVariable("StringUtil", new BeanModel(StringUtil.INSTANCE, wrapper));
    TemplateLoader[] templateLoaders = { new FlexibleTemplateLoader(), new StringTemplateLoader() };
    MultiTemplateLoader multiTemplateLoader = new MultiTemplateLoader(templateLoaders);
    newConfig.setTemplateLoader(multiTemplateLoader);
    Map<?, ?> freemarkerImports = UtilProperties.getProperties("freemarkerImports");
    if (freemarkerImports != null) {
        newConfig.setAutoImports(freemarkerImports);
    }
    newConfig.setLogTemplateExceptions(false);
    newConfig.setTemplateExceptionHandler(new FreeMarkerWorker.OFBizTemplateExceptionHandler());
    try {
        newConfig.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
        newConfig.setSetting("number_format", "0.##########");
    } catch (TemplateException e) {
        Debug.logError("Unable to set date/time and number formats in FreeMarker: " + e, module);
    }
    // Transforms properties file set up as key=transform name, property=transform class name
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Enumeration<URL> resources;
    try {
        resources = loader.getResources("freemarkerTransforms.properties");
    } catch (IOException e) {
        Debug.logError(e, "Could not load list of freemarkerTransforms.properties", module);
        throw UtilMisc.initCause(new InternalError(e.getMessage()), e);
    }
    while (resources.hasMoreElements()) {
        URL propertyURL = resources.nextElement();
        Debug.logInfo("loading properties: " + propertyURL, module);
        Properties props = UtilProperties.getProperties(propertyURL);
        if (UtilValidate.isEmpty(props)) {
            Debug.logError("Unable to locate properties file " + propertyURL, module);
        } else {
            loadTransforms(loader, props, newConfig);
        }
    }
    return newConfig;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Configuration(freemarker.template.Configuration) BeanModel(freemarker.ext.beans.BeanModel) TemplateException(freemarker.template.TemplateException) StringTemplateLoader(freemarker.cache.StringTemplateLoader) URLTemplateLoader(freemarker.cache.URLTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) IOException(java.io.IOException) UtilProperties(org.apache.ofbiz.base.util.UtilProperties) Properties(java.util.Properties) URL(java.net.URL) StringTemplateLoader(freemarker.cache.StringTemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) TemplateHashModel(freemarker.template.TemplateHashModel)

Example 5 with TemplateHashModel

use of freemarker.template.TemplateHashModel in project GMM by Katharsas.

the class ApplicationConfiguration method freemarkerConfig.

/**
 * FreeMarker (ftl) configuration
 */
@Bean
public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
    final FreeMarkerConfigurer result = new FreeMarkerConfigurer();
    // template path
    result.setTemplateLoaderPath("/WEB-INF/ftl/");
    result.setDefaultEncoding("UTF-8");
    // static access
    final Version version = freemarker.template.Configuration.getVersion();
    final BeansWrapper wrapper = new BeansWrapper(version);
    final TemplateHashModel statics = wrapper.getStaticModels();
    final Map<String, Object> shared = new HashMap<>();
    for (final Class<?> clazz : ElFunctions.staticClasses) {
        shared.put(clazz.getSimpleName(), statics.get(clazz.getName()));
    }
    result.setFreemarkerVariables(shared);
    return result;
}
Also used : TemplateHashModel(freemarker.template.TemplateHashModel) Version(freemarker.template.Version) HashMap(java.util.HashMap) BeansWrapper(freemarker.ext.beans.BeansWrapper) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

TemplateHashModel (freemarker.template.TemplateHashModel)26 TemplateScalarModel (freemarker.template.TemplateScalarModel)9 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)8 TemplateModelException (freemarker.template.TemplateModelException)8 Test (org.junit.Test)8 TemplateSequenceModel (freemarker.template.TemplateSequenceModel)7 TemplateModel (freemarker.template.TemplateModel)6 TemplateNumberModel (freemarker.template.TemplateNumberModel)4 TemplateException (freemarker.template.TemplateException)3 Version (freemarker.template.Version)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 BeansWrapper (freemarker.ext.beans.BeansWrapper)2 Configuration (freemarker.template.Configuration)2 SimpleScalar (freemarker.template.SimpleScalar)2 IOException (java.io.IOException)2 URL (java.net.URL)2 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 StringTemplateLoader (freemarker.cache.StringTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1