/** * Build a FreeMarker template model for the given model Map. * <p>The default implementation builds a {@link SimpleHash}. * @param model the model to use for rendering * @param exchange current exchange * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof */ protected SimpleHash getTemplateModel(Map<String, Object> model, ServerWebExchange exchange) { SimpleHash fmModel = new SimpleHash(getObjectWrapper()); fmModel.putAll(model); return fmModel; }
private static TemplateModel wrapMap(Map table) { SimpleHash model = new SimpleHash(); for (Iterator it = table.entrySet().iterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); model.put(String.valueOf(entry.getKey()), new SimpleScalar(String.valueOf(entry.getValue()))); } return model; }
config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
/** * Loads a template from a file passed as the first argument, loads an XML * document from the standard input, passes it to the template as variable * <tt>document</tt> and writes the result of template processing to * standard output. * * @deprecated Will be removed (main method in a library, often classified as CWE-489 "Leftover Debug Code"). */ @Deprecated public static void main(String[] args) throws Exception { org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(); Document document = builder.build(System.in); SimpleHash model = new SimpleHash(); model.put("document", new NodeListModel(document)); FileReader fr = new FileReader(args[0]); Template template = new Template(args[0], fr); Writer w = new java.io.OutputStreamWriter(System.out); template.process(model, w); w.flush(); w.close(); }
dataModelHash = new SimpleHash(wrapper); } else { TemplateModel wrappedDataModel = wrapper.wrap(dataModel);
NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { SimpleHash hash = new SimpleHash(); for (int i = 0; i < attributes.getLength(); i++) { Attr att = (Attr) attributes.item(i);
config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
@Override public void render(final View view, final Renderer.Context ctx) throws Exception { String name = view.name() + suffix; Template template = template(name, ctx.charset()); Map<String, Object> hash = new HashMap<>(); hash.put("_vname", view.name()); hash.put("_vpath", template.getName()); hash.put("xss", xss); // Locale: Locale locale = (Locale) hash.getOrDefault("locale", ctx.locale()); hash.putIfAbsent("locale", locale); // locals Map<String, Object> locals = ctx.locals(); hash.putAll(locals); // model hash.putAll(view.model()); TemplateModel model = new SimpleHash(hash, new FtlWrapper(freemarker.getObjectWrapper())); // TODO: remove string writer StringWriter writer = new StringWriter(); // Locale: template.setLocale(locale); // output template.process(model, writer); ctx.type(MediaType.html) .send(writer.toString()); }
private SimpleHash buildType(ComplexType ft) { // create a variable "attributes" which his a list of all the // attributes, but at the same time, is a map keyed by name Map<String, Object> attributeMap = new LinkedHashMap<String, Object>(); Collection<PropertyDescriptor> descriptors = ft.getDescriptors(); for (Iterator<PropertyDescriptor> it = descriptors.iterator(); it.hasNext(); ) { PropertyDescriptor descr = it.next(); Map<String, Object> attribute = new HashMap<String, Object>(); attribute.put("name", descr.getName().getLocalPart()); attribute.put("namespace", getNamespace(descr.getName())); attribute.put("prefix", getPrefix(descr.getName())); attribute.put("type", descr.getType().getBinding().getName()); attribute.put( "isGeometry", Boolean.valueOf(Geometry.class.isAssignableFrom(descr.getType().getBinding()))); attributeMap.put(descr.getName().toString(), attribute); } // build up the result, feature type is represented by its name an // attributes SimpleHash map = new SimpleHash(); map.put("attributes", new SequenceMapModel(attributeMap, this)); map.put("name", ft.getName().getLocalPart()); map.put("namespace", getNamespace(ft.getName())); map.put("prefix", getPrefix(ft.getName())); return map; }
root = new SimpleHash(); root.put("page", new JspContextModel(pageContext, JspContextModel.PAGE_SCOPE)); root.put("request", new JspContextModel(pageContext, JspContextModel.REQUEST_SCOPE));
public TemplateModel wrap(Object object) throws TemplateModelException { // check for feature collection if (object instanceof FeatureCollection) { // create a model with just one variable called 'features' SimpleHash map = new SimpleHash(); map.put( "features", templateFeatureCollectionFactory.createTemplateFeatureCollection( (FeatureCollection) object, this)); map.put("type", wrap(((FeatureCollection) object).getSchema())); return map; } else if (object instanceof ComplexType) { return buildType((ComplexType) object); } else if (object instanceof Feature) { return buildComplex((Feature) object); } return super.wrap(object); }
public LazyLoadingModel(ObjectWrapper wrapper, Map<String, Object> eagerModel, final ContentStore db) { this.eagerModel = new SimpleHash(eagerModel, wrapper); this.db = db; this.wrapper = wrapper; }
final SimpleHash catchAllParamValue; if (catchAllParamName != null) { catchAllParamValue = new SimpleHash((ObjectWrapper) null); macroCtx.setLocalVar(catchAllParamName, catchAllParamValue); } else {
return useAdaptersForContainers ? (TemplateModel) DefaultMapAdapter.adapt((Map<?, ?>) obj, this) : (TemplateModel) new SimpleHash((Map<?, ?>) obj, this);
SimpleHash result = new SimpleHash(env.getObjectWrapper()); result.put(RESULT_EXISTS, template != null);
SimpleHash globalModel = new SimpleHash(); TemplateHashModel enumModels = BeansWrapper.getDefaultInstance().getEnumModels(); TemplateHashModel myEnumModel = (TemplateHashModel) enumModels.get("your.fully.qualified.enum.MyEnum"); globalModel.put("MyEnum", myEnumModel); freemarkerConfiguration.setAllSharedVariables(globalModel);
private SimpleHash buildComplex(ComplexAttribute att) { SimpleHash map = new SimpleHash();
@Autowired private void init(FreeMarkerConfigurer freeMarkerConfigurer, DirectiveComponent directiveComponent) throws IOException, TemplateModelException { Map<String, Object> freemarkerVariables = new HashMap<>(); adminConfiguration = freeMarkerConfigurer.getConfiguration(); for (Entry<String, AbstractTemplateDirective> entry : directiveComponent.getTemplateDirectiveMap().entrySet()) { freemarkerVariables.put(directivePrefix + entry.getKey(), entry.getValue()); } freemarkerVariables.putAll(directiveComponent.getMethodMap()); adminConfiguration.setAllSharedVariables(new SimpleHash(freemarkerVariables, adminConfiguration.getObjectWrapper())); webConfiguration = new Configuration(Configuration.getVersion()); File webFile = new File(siteComponent.getWebTemplateFilePath()); webFile.mkdirs(); webConfiguration.setDirectoryForTemplateLoading(webFile); copyConfig(adminConfiguration, webConfiguration); Map<String, Object> webFreemarkerVariables = new HashMap<>(freemarkerVariables); webFreemarkerVariables.put(TemplateCacheComponent.CONTENT_CACHE, new NoCacheDirective()); webConfiguration.setAllSharedVariables(new SimpleHash(webFreemarkerVariables, webConfiguration.getObjectWrapper())); taskConfiguration = new Configuration(Configuration.getVersion()); File taskFile = new File(siteComponent.getTaskTemplateFilePath()); taskFile.mkdirs(); taskConfiguration.setDirectoryForTemplateLoading(taskFile); copyConfig(adminConfiguration, taskConfiguration); for (Entry<String, AbstractTaskDirective> entry : directiveComponent.getTaskDirectiveMap().entrySet()) { freemarkerVariables.put(directivePrefix + entry.getKey(), entry.getValue()); } taskConfiguration.setAllSharedVariables(new SimpleHash(freemarkerVariables, taskConfiguration.getObjectWrapper())); }
@Autowired private void init(FreeMarkerConfigurer freeMarkerConfigurer, DirectiveComponent directiveComponent) throws IOException, TemplateModelException { Map<String, Object> freemarkerVariables = new HashMap<>(); adminConfiguration = freeMarkerConfigurer.getConfiguration(); for (Entry<String, AbstractTemplateDirective> entry : directiveComponent.getTemplateDirectiveMap().entrySet()) { freemarkerVariables.put(directivePrefix + entry.getKey(), entry.getValue()); } freemarkerVariables.putAll(directiveComponent.getMethodMap()); adminConfiguration.setAllSharedVariables(new SimpleHash(freemarkerVariables, adminConfiguration.getObjectWrapper())); webConfiguration = new Configuration(Configuration.getVersion()); File webFile = new File(siteComponent.getWebTemplateFilePath()); webFile.mkdirs(); webConfiguration.setDirectoryForTemplateLoading(webFile); copyConfig(adminConfiguration, webConfiguration); Map<String, Object> webFreemarkerVariables = new HashMap<>(freemarkerVariables); webFreemarkerVariables.put(TemplateCacheComponent.CONTENT_CACHE, new NoCacheDirective()); webConfiguration.setAllSharedVariables(new SimpleHash(webFreemarkerVariables, webConfiguration.getObjectWrapper())); taskConfiguration = new Configuration(Configuration.getVersion()); File taskFile = new File(siteComponent.getTaskTemplateFilePath()); taskFile.mkdirs(); taskConfiguration.setDirectoryForTemplateLoading(taskFile); copyConfig(adminConfiguration, taskConfiguration); for (Entry<String, AbstractTaskDirective> entry : directiveComponent.getTaskDirectiveMap().entrySet()) { freemarkerVariables.put(directivePrefix + entry.getKey(), entry.getValue()); } taskConfiguration.setAllSharedVariables(new SimpleHash(freemarkerVariables, taskConfiguration.getObjectWrapper())); }
private static TemplateModel wrapMap(Map table) { SimpleHash model = new SimpleHash(); for (Iterator it = table.entrySet().iterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); model.put(String.valueOf(entry.getKey()), new SimpleScalar(String.valueOf(entry.getValue()))); } return model; }