KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > template > FreeMarkerWorker


1 /*
2  * $Id: FreeMarkerWorker.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.util.template;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.Reader JavaDoc;
31 import java.io.StringReader JavaDoc;
32 import java.io.Writer JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Locale JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
42
43 import javax.servlet.ServletContext JavaDoc;
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45
46 import javolution.util.FastMap;
47
48 import org.ofbiz.base.location.FlexibleLocation;
49 import org.ofbiz.base.util.Debug;
50 import org.ofbiz.base.util.UtilValidate;
51 import org.ofbiz.base.util.cache.UtilCache;
52
53 import freemarker.core.Environment;
54 import freemarker.ext.beans.BeanModel;
55 import freemarker.ext.beans.BeansWrapper;
56 import freemarker.template.Configuration;
57 import freemarker.template.SimpleHash;
58 import freemarker.template.SimpleScalar;
59 import freemarker.template.Template;
60 import freemarker.template.TemplateException;
61 import freemarker.template.TemplateHashModel;
62 import freemarker.template.TemplateModel;
63 import freemarker.template.TemplateModelException;
64 //import com.clarkware.profiler.Profiler;
65

66
67 /**
68  * FreemarkerViewHandler - Freemarker Template Engine Util
69  *
70  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
71  * @version $Rev: 5462 $
72  * @since 3.0
73  */

74 public class FreeMarkerWorker {
75     
76     public static final String JavaDoc module = FreeMarkerWorker.class.getName();
77     
78     // use soft references for this so that things from Content records don't kill all of our memory, or maybe not for performance reasons... hmmm, leave to config file...
79
public static UtilCache cachedTemplates = new UtilCache("template.ftl.general", 0, 0, false);
80     // these are mode "code" oriented so don't use soft references
81
public static UtilCache cachedLocationTemplates = new UtilCache("template.ftl.location", 0, 0, false);
82
83     public static Map JavaDoc ftlTransforms = new HashMap JavaDoc();
84     
85     static {
86         try {
87             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
88
89             // note: loadClass is necessary for these since this class doesn't know anything about them at compile time
90
// double note: may want to make this more dynamic and configurable in the future
91
ftlTransforms.put("ofbizUrl", loader.loadClass("org.ofbiz.webapp.ftl.OfbizUrlTransform").newInstance());
92             ftlTransforms.put("ofbizContentUrl", loader.loadClass("org.ofbiz.webapp.ftl.OfbizContentTransform").newInstance());
93             ftlTransforms.put("ofbizCurrency", loader.loadClass("org.ofbiz.webapp.ftl.OfbizCurrencyTransform").newInstance());
94             ftlTransforms.put("ofbizAmount", loader.loadClass("org.ofbiz.webapp.ftl.OfbizAmountTransform").newInstance());
95             ftlTransforms.put("setRequestAttribute", loader.loadClass("org.ofbiz.webapp.ftl.SetRequestAttributeMethod").newInstance());
96             ftlTransforms.put("renderWrappedText", loader.loadClass("org.ofbiz.webapp.ftl.RenderWrappedTextTransform").newInstance());
97
98             ftlTransforms.put("barcodeTransform", loader.loadClass("org.ofbiz.webapp.barcode.BarcodeTransform").newInstance());
99
100             ftlTransforms.put("menuWrap", loader.loadClass("org.ofbiz.widget.menu.MenuWrapTransform").newInstance());
101             
102             ftlTransforms.put("editRenderSubContent", loader.loadClass("org.ofbiz.content.webapp.ftl.EditRenderSubContentTransform").newInstance());
103             ftlTransforms.put("renderSubContent", loader.loadClass("org.ofbiz.content.webapp.ftl.RenderSubContentTransform").newInstance());
104             ftlTransforms.put("loopSubContent", loader.loadClass("org.ofbiz.content.webapp.ftl.LoopSubContentTransform").newInstance());
105             ftlTransforms.put("traverseSubContent", loader.loadClass("org.ofbiz.content.webapp.ftl.TraverseSubContentTransform").newInstance());
106
107             ftlTransforms.put("checkPermission", loader.loadClass("org.ofbiz.content.webapp.ftl.CheckPermissionTransform").newInstance());
108             ftlTransforms.put("injectNodeTrailCsv", loader.loadClass("org.ofbiz.content.webapp.ftl.InjectNodeTrailCsvTransform").newInstance());
109             
110             ftlTransforms.put("editRenderSubContentCache", loader.loadClass("org.ofbiz.content.webapp.ftl.EditRenderSubContentCacheTransform").newInstance());
111             ftlTransforms.put("renderSubContentCache", loader.loadClass("org.ofbiz.content.webapp.ftl.RenderSubContentCacheTransform").newInstance());
112             ftlTransforms.put("loopSubContentCache", loader.loadClass("org.ofbiz.content.webapp.ftl.LoopSubContentCacheTransform").newInstance());
113             ftlTransforms.put("traverseSubContentCache", loader.loadClass("org.ofbiz.content.webapp.ftl.TraverseSubContentCacheTransform").newInstance());
114             ftlTransforms.put("wrapSubContentCache", loader.loadClass("org.ofbiz.content.webapp.ftl.WrapSubContentCacheTransform").newInstance());
115             ftlTransforms.put("limitedSubContent", loader.loadClass("org.ofbiz.content.webapp.ftl.LimitedSubContentCacheTransform").newInstance());
116             ftlTransforms.put("renderSubContentAsText", loader.loadClass("org.ofbiz.content.webapp.ftl.RenderSubContentAsText").newInstance());
117             ftlTransforms.put("renderContentAsText", loader.loadClass("org.ofbiz.content.webapp.ftl.RenderContentAsText").newInstance());
118         } catch (ClassNotFoundException JavaDoc e) {
119             Debug.logError(e, "Could not pre-initialize dynamically loaded class: ", module);
120         } catch (IllegalAccessException JavaDoc e) {
121             Debug.logError(e, "Could not pre-initialize dynamically loaded class: ", module);
122         } catch (InstantiationException JavaDoc e) {
123             Debug.logError(e, "Could not pre-initialize dynamically loaded class: ", module);
124         }
125     }
126
127     public static void renderTemplateAtLocation(String JavaDoc location, Map JavaDoc context, Writer JavaDoc outWriter) throws MalformedURLException JavaDoc, TemplateException, IOException JavaDoc {
128         Template template = (Template) cachedTemplates.get(location);
129         if (template == null) {
130             synchronized (FreeMarkerWorker.class) {
131                 template = (Template) cachedTemplates.get(location);
132                 if (template == null) {
133                     URL JavaDoc locationUrl = FlexibleLocation.resolveLocation(location);
134                     if (locationUrl == null) {
135                         throw new IllegalArgumentException JavaDoc("FreeMarker file not found at location: " + location);
136                     }
137                     Reader JavaDoc locationReader = new InputStreamReader JavaDoc(locationUrl.openStream());
138                     
139                     String JavaDoc locationProtocol = locationUrl.getProtocol();
140                     String JavaDoc filename = null;
141                     Configuration config = null;
142                     if ("file".equals(locationProtocol)) {
143                         String JavaDoc locationFile = locationUrl.getFile();
144                         int lastSlash = locationFile.lastIndexOf("/");
145                         String JavaDoc locationDir = locationFile.substring(0, lastSlash);
146                         filename = locationFile.substring(lastSlash + 1);
147                         if (Debug.verboseOn()) Debug.logVerbose("FreeMarker render: filename=" + filename + ", locationDir=" + locationDir, module);
148                         //DEJ20050104 Don't know what to do here, FreeMarker does some funky stuff when loading includes and can't find a way to make it happy...
149
config = makeSingleUseOfbizFtlConfig(locationDir);
150                     } else {
151                         filename = locationUrl.toExternalForm();
152                         config = makeDefaultOfbizConfig();
153                     }
154                     template = new Template(filename, locationReader, config);
155                     
156                     cachedTemplates.put(location, template);
157                     
158                     // ensure that freemarker uses locale to display locale sensitive data
159
Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
160                     if (locale == null)
161                         locale = Locale.getDefault();
162                     template.setSetting("locale", locale.toString());
163                 }
164             }
165         }
166         
167         if (context == null) {
168             context = FastMap.newInstance();
169         }
170         
171         // add the OFBiz transforms/methods
172
addAllOfbizTransforms(context);
173
174         // process the template with the given data and write
175
// the email body to the String buffer
176
template.process(context, outWriter);
177     }
178     
179     public static void renderTemplate(String JavaDoc templateIdString, String JavaDoc template, Map JavaDoc context, Writer JavaDoc outWriter) throws TemplateException, IOException JavaDoc {
180         //if (Debug.infoOn()) Debug.logInfo("template:" + template.toString(), "");
181
Reader JavaDoc templateReader = new StringReader JavaDoc(template);
182         renderTemplate(templateIdString, templateReader, context, outWriter);
183     }
184     
185     public static void renderTemplate(String JavaDoc templateIdString, Reader JavaDoc templateReader, Map JavaDoc context, Writer JavaDoc outWriter) throws TemplateException, IOException JavaDoc {
186         if (context == null) {
187             context = new HashMap JavaDoc();
188         }
189         
190         Configuration config = makeDefaultOfbizConfig();
191         Template template = new Template(templateIdString, templateReader, config);
192         
193         // ensure that freemarker uses locale to display locale sensitive data
194
Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
195         if (locale == null)
196             locale = Locale.getDefault();
197         template.setSetting("locale", locale.toString());
198         
199         // add the OFBiz transforms/methods
200
addAllOfbizTransforms(context);
201         
202         cachedTemplates.put(templateIdString, template);
203         // process the template with the given data and write
204
// the email body to the String buffer
205
template.process(context, outWriter);
206     }
207  
208     public static Template getTemplateCached(String JavaDoc dataResourceId) {
209         Template t = (Template)cachedTemplates.get("DataResource:" + dataResourceId);
210         return t;
211     }
212
213     public static void renderTemplateCached(Template template, Map JavaDoc context, Writer JavaDoc outWriter) throws TemplateException, IOException JavaDoc {
214         template.process(context, outWriter);
215     }
216     
217     public static void addAllOfbizTransforms(Map JavaDoc context) {
218         BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
219         TemplateHashModel staticModels = wrapper.getStaticModels();
220         context.put("Static", staticModels);
221
222         context.putAll(ftlTransforms);
223     }
224
225     private static Configuration defaultOfbizConfig = null;
226     public static Configuration makeDefaultOfbizConfig() throws TemplateException, IOException JavaDoc {
227         if (defaultOfbizConfig == null) {
228             synchronized (FreeMarkerWorker.class) {
229                 if (defaultOfbizConfig == null) {
230                     Configuration config = new Configuration();
231                     config.setObjectWrapper(BeansWrapper.getDefaultInstance());
232                     config.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
233                     config.setSetting("number_format", "0.##########");
234                     defaultOfbizConfig = config;
235                 }
236             }
237         }
238         return defaultOfbizConfig;
239     }
240     
241     public static Configuration makeSingleUseOfbizFtlConfig(String JavaDoc locationDir) throws TemplateException, IOException JavaDoc {
242         Configuration config = new Configuration();
243         config.setObjectWrapper(BeansWrapper.getDefaultInstance());
244         config.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
245         config.setSetting("number_format", "0.##########");
246         if (locationDir != null) {
247             File JavaDoc locationDirFile = new File JavaDoc(locationDir);
248             if (locationDirFile != null) {
249                 if (locationDirFile.isFile()) {
250                     /* maybe best not to do this, maybe best to throw an exception
251                     String realDir = locationDir.substring(0, locationDir.lastIndexOf("/"));
252                     locationDirFile = new File(realDir);
253                     */

254                     throw new IllegalArgumentException JavaDoc("Could not create FTL Configuration object because locationDir is a file: " + locationDir);
255                 }
256                 if (locationDirFile != null && locationDirFile.isDirectory()) {
257                     config.setDirectoryForTemplateLoading(locationDirFile);
258                 }
259             }
260         }
261         return config;
262     }
263     
264     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
265         Map JavaDoc templateContext = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
266         return getArg(args, key, templateContext);
267     }
268
269     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc templateContext) {
270         //SimpleScalar s = null;
271
Object JavaDoc o = null;
272         String JavaDoc returnVal = null;
273         o = args.get(key);
274         returnVal = (String JavaDoc) unwrap(o);
275         if (returnVal == null) {
276             try {
277                 if (templateContext != null) {
278                     returnVal = (String JavaDoc) templateContext.get(key);
279                 }
280             } catch (ClassCastException JavaDoc e2) {
281                 //return null;
282
}
283         }
284         return returnVal;
285     }
286
287     public static Object JavaDoc getArgObject(Map JavaDoc args, String JavaDoc key, Map JavaDoc templateContext) {
288         //SimpleScalar s = null;
289
Object JavaDoc o = null;
290         Object JavaDoc returnVal = null;
291         o = args.get(key);
292         returnVal = unwrap(o);
293         if (returnVal == null) {
294             try {
295                 if (templateContext != null) {
296                     returnVal = templateContext.get(key);
297                 }
298             } catch (ClassCastException JavaDoc e2) {
299                 //return null;
300
}
301         }
302         return returnVal;
303     }
304
305
306    /**
307     * Gets BeanModel from FreeMarker context and returns the object that it wraps.
308     * @param varName the name of the variable in the FreeMarker context.
309     * @param env the FreeMarker Environment
310     */

311     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
312         Object JavaDoc obj = null;
313         try {
314             obj = env.getVariable(varName);
315             if (obj != null) {
316                 if (obj == TemplateModel.NOTHING) {
317                     obj = null;
318                 } else if (obj instanceof BeanModel) {
319                     BeanModel bean = (BeanModel) obj;
320                     obj = bean.getWrappedObject();
321                 } else if (obj instanceof SimpleScalar) {
322                     obj = obj.toString();
323                 }
324             }
325         } catch (TemplateModelException e) {
326             Debug.logInfo(e.getMessage(), module);
327         }
328         return obj;
329     }
330
331    /**
332     * Gets BeanModel from FreeMarker context and returns the object that it wraps.
333     * @param varName the name of the variable in the FreeMarker context.
334     * @param env the FreeMarker Environment
335     */

336     public static BeanModel getBeanModel(String JavaDoc varName, Environment env) {
337         BeanModel bean = null;
338         try {
339             bean = (BeanModel) env.getVariable(varName);
340         } catch (TemplateModelException e) {
341             Debug.logInfo(e.getMessage(), module);
342         }
343         return bean;
344     }
345
346     public static Object JavaDoc get(SimpleHash args, String JavaDoc key) {
347         Object JavaDoc returnObj = null;
348         Object JavaDoc o = null;
349         try {
350             o = args.get(key);
351         } catch(TemplateModelException e) {
352             Debug.logVerbose(e.getMessage(), module);
353             return returnObj;
354         }
355
356         returnObj = unwrap(o);
357
358         if (returnObj == null) {
359             Object JavaDoc ctxObj = null;
360             try {
361                 ctxObj = args.get("context");
362             } catch(TemplateModelException e) {
363                 Debug.logInfo(e.getMessage(), module);
364                 return returnObj;
365             }
366             Map JavaDoc ctx = null;
367             if (ctxObj instanceof BeanModel) {
368                 ctx = (Map JavaDoc)((BeanModel)ctxObj).getWrappedObject();
369             returnObj = ctx.get(key);
370             }
371             /*
372             try {
373                 Map templateContext = (Map)FreeMarkerWorker.getWrappedObject("context", env);
374                 if (templateContext != null) {
375                     returnObj = (String)templateContext.get(key);
376                 }
377             } catch(ClassCastException e2) {
378                 //return null;
379             }
380             */

381         }
382         return returnObj;
383     }
384
385     public static Object JavaDoc unwrap(Object JavaDoc o) {
386         Object JavaDoc returnObj = null;
387
388         if (o == TemplateModel.NOTHING) {
389             returnObj = null;
390         } else if (o instanceof SimpleScalar) {
391             returnObj = o.toString();
392         } else if (o instanceof BeanModel) {
393             returnObj = ((BeanModel)o).getWrappedObject();
394         }
395     
396         return returnObj;
397     }
398
399     public static void checkForLoop(String JavaDoc path, Map JavaDoc ctx) throws IOException JavaDoc {
400         List JavaDoc templateList = (List JavaDoc)ctx.get("templateList");
401         if (templateList == null) {
402             templateList = new ArrayList JavaDoc();
403         } else {
404             if (templateList.contains(path)) {
405                 throw new IOException JavaDoc(path + " has already been visited.");
406             }
407         }
408         templateList.add(path);
409         ctx.put("templateList", templateList);
410         return;
411     }
412
413     public static Map JavaDoc createEnvironmentMap(Environment env) {
414         Map JavaDoc templateRoot = new HashMap JavaDoc();
415         Set JavaDoc varNames = null;
416         try {
417             varNames = env.getKnownVariableNames();
418         } catch (TemplateModelException e1) {
419             Debug.logError(e1, "Error getting FreeMarker variable names, will not put pass current context on to sub-content", module);
420         }
421         if (varNames != null) {
422             Iterator JavaDoc varNameIter = varNames.iterator();
423             while (varNameIter.hasNext()) {
424                 String JavaDoc varName = (String JavaDoc) varNameIter.next();
425                 //freemarker.ext.beans.StringModel varObj = (freemarker.ext.beans.StringModel ) varNameIter.next();
426
//Object varObj = varNameIter.next();
427
//String varName = varObj.toString();
428
templateRoot.put(varName, FreeMarkerWorker.getWrappedObject(varName, env));
429             }
430         }
431         return templateRoot;
432     }
433     
434     public static void saveContextValues(Map JavaDoc context, String JavaDoc [] saveKeyNames, Map JavaDoc saveMap ) {
435         //Map saveMap = new HashMap();
436
for (int i=0; i<saveKeyNames.length; i++) {
437             String JavaDoc key = (String JavaDoc)saveKeyNames[i];
438             Object JavaDoc o = context.get(key);
439             if (o instanceof Map JavaDoc)
440                 o = new HashMap JavaDoc((Map JavaDoc)o);
441             else if (o instanceof List JavaDoc)
442                 o = new ArrayList JavaDoc((List JavaDoc)o);
443             saveMap.put(key, o);
444         }
445         return ;
446     }
447
448     public static Map JavaDoc saveValues(Map JavaDoc context, String JavaDoc [] saveKeyNames) {
449         Map JavaDoc saveMap = new HashMap JavaDoc();
450         for (int i=0; i<saveKeyNames.length; i++) {
451             String JavaDoc key = (String JavaDoc)saveKeyNames[i];
452             Object JavaDoc o = context.get(key);
453             if (o instanceof Map JavaDoc)
454                 o = new HashMap JavaDoc((Map JavaDoc)o);
455             else if (o instanceof List JavaDoc)
456                 o = new ArrayList JavaDoc((List JavaDoc)o);
457             saveMap.put(key, o);
458         }
459         return saveMap;
460     }
461
462
463     public static void reloadValues(Map JavaDoc context, Map JavaDoc saveValues, Environment env ) {
464         Set JavaDoc keySet = saveValues.keySet();
465         Iterator JavaDoc it = keySet.iterator();
466         while (it.hasNext()) {
467             String JavaDoc key = (String JavaDoc)it.next();
468             Object JavaDoc o = saveValues.get(key);
469             if (o instanceof Map JavaDoc) {
470                 Map JavaDoc map = new HashMap JavaDoc();
471                 map.putAll((Map JavaDoc)o);
472                 context.put(key, map);
473             } else if (o instanceof List JavaDoc) {
474                 List JavaDoc list = new ArrayList JavaDoc();
475                 list.addAll((List JavaDoc)o);
476                 context.put(key, list);
477             } else {
478                 context.put(key, o);
479             }
480             env.setVariable(key, autoWrap(o, env));
481         }
482         return;
483     }
484
485     public static void removeValues(Map JavaDoc context, String JavaDoc [] removeKeyNames ) {
486         for (int i=0; i<removeKeyNames.length; i++) {
487             String JavaDoc key = (String JavaDoc)removeKeyNames[i];
488             context.remove(key);
489         }
490         return;
491     }
492
493     public static void overrideWithArgs(Map JavaDoc ctx, Map JavaDoc args) {
494         Set JavaDoc keySet = args.keySet();
495         Iterator JavaDoc it = keySet.iterator();
496         while (it.hasNext()) {
497             String JavaDoc key = (String JavaDoc)it.next();
498             Object JavaDoc obj = args.get(key);
499             //if (Debug.infoOn()) Debug.logInfo("in overrideWithArgs, key(3):" + key + " obj:" + obj + " class:" + obj.getClass().getName() , module);
500
if (obj != null) {
501                 if (obj == TemplateModel.NOTHING) {
502                     ctx.put(key, null);
503                 } else {
504                     Object JavaDoc unwrappedObj = unwrap(obj);
505                     if (unwrappedObj == null)
506                         unwrappedObj = obj;
507                     ctx.put(key, unwrappedObj.toString());
508                 }
509             } else {
510                 ctx.put(key, null);
511             }
512         }
513         return;
514     }
515
516     public static void convertContext(Map JavaDoc ctx) {
517         Set JavaDoc keySet = ctx.keySet();
518         Iterator JavaDoc it = keySet.iterator();
519         while (it.hasNext()) {
520             String JavaDoc key = (String JavaDoc)it.next();
521             Object JavaDoc obj = ctx.get(key);
522             if (obj != null) {
523                 Object JavaDoc unwrappedObj = unwrap(obj);
524                 if (unwrappedObj != null) {
525                     ctx.put(key, unwrappedObj);
526                 }
527             }
528         }
529         return;
530     }
531
532     public static void getSiteParameters(HttpServletRequest JavaDoc request, Map JavaDoc ctx) {
533         if (request == null) {
534             return;
535         }
536         if (ctx == null) {
537             throw new IllegalArgumentException JavaDoc("Error in getSiteParameters, context/ctx cannot be null");
538         }
539         ServletContext JavaDoc servletContext = request.getSession().getServletContext();
540         String JavaDoc rootDir = (String JavaDoc)ctx.get("rootDir");
541         String JavaDoc webSiteId = (String JavaDoc)ctx.get("webSiteId");
542         String JavaDoc https = (String JavaDoc)ctx.get("https");
543         if (UtilValidate.isEmpty(rootDir)) {
544             rootDir = servletContext.getRealPath("/");
545             ctx.put("rootDir", rootDir);
546         }
547         if (UtilValidate.isEmpty(webSiteId)) {
548             webSiteId = (String JavaDoc) servletContext.getAttribute("webSiteId");
549             ctx.put("webSiteId", webSiteId);
550         }
551         if (UtilValidate.isEmpty(https)) {
552             https = (String JavaDoc) servletContext.getAttribute("https");
553             ctx.put("https", https);
554         }
555         return;
556     }
557
558     public static TemplateModel autoWrap(Object JavaDoc obj, Environment env) {
559        BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
560        TemplateModel templateModelObj = null;
561        try {
562            templateModelObj = wrapper.wrap(obj);
563        } catch(TemplateModelException e) {
564            throw new RuntimeException JavaDoc(e.getMessage());
565        }
566        return templateModelObj;
567     }
568 }
569
Popular Tags