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;
}
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;
}
}
}
}
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;
}
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;
}
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;
}
Aggregations