KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > view > freemarker > RiotFreeMarkerConfigurer


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.view.freemarker;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
34
35 import freemarker.template.Configuration;
36 import freemarker.template.ObjectWrapper;
37 import freemarker.template.TemplateException;
38 import freemarker.template.TemplateExceptionHandler;
39
40 /**
41  * @author Felix Gnass [fgnass at neteye dot de]
42  * @since 6.4
43  */

44 public class RiotFreeMarkerConfigurer extends FreeMarkerConfigurer {
45
46     private static final Log log = LogFactory
47             .getLog(RiotFreeMarkerConfigurer.class);
48     
49     private TemplateExceptionHandler exceptionHandler =
50             new ErrorPrintingExceptionHandler();
51     
52     private Properties JavaDoc macroLibraries;
53     
54     private ObjectWrapper objectWrapper;
55     
56     private boolean whitespaceStripping = false;
57     
58     /**
59      * Sets the macro libraries to be auto-imported, keyed by their namespace.
60      */

61     public void setMacroLibraries(Properties JavaDoc macroLibraries) {
62         this.macroLibraries = macroLibraries;
63     }
64
65     /**
66      * Sets the {@link TemplateExceptionHandler} to be used. By default an
67      * {@link ErrorPrintingExceptionHandler} will be used.
68      */

69     public void setExceptionHandler(TemplateExceptionHandler exceptionHandler) {
70         this.exceptionHandler = exceptionHandler;
71     }
72     
73     /**
74      * Sets the {@link ObjectWrapper} to be used. If <code>null</code>
75      * (which is the default), FreeMarker's DefaultObjectWrapper will be used.
76      */

77     public void setObjectWrapper(ObjectWrapper objectWrapper) {
78         this.objectWrapper = objectWrapper;
79     }
80
81     /**
82      * Sets whether the FTL parser will try to remove superfluous
83      * white-space around certain FTL tags.
84      */

85     public void setWhitespaceStripping(boolean whitespaceStripping) {
86         this.whitespaceStripping = whitespaceStripping;
87     }
88     
89     protected void postProcessTemplateLoaders(List JavaDoc templateLoaders) {
90         super.postProcessTemplateLoaders(templateLoaders);
91         templateLoaders.add(new ResourceTemplateLoader(getResourceLoader()));
92     }
93     
94     protected void postProcessConfiguration(Configuration config)
95             throws IOException JavaDoc, TemplateException {
96         
97         config.setWhitespaceStripping(whitespaceStripping);
98         importMacroLibraries(config);
99         config.setTemplateExceptionHandler(exceptionHandler);
100         if (objectWrapper != null) {
101             config.setObjectWrapper(objectWrapper);
102         }
103     }
104     
105     protected void importMacroLibraries(Configuration config) {
106         if (macroLibraries != null) {
107             Enumeration JavaDoc names = macroLibraries.propertyNames();
108             while (names.hasMoreElements()) {
109                 String JavaDoc namespace = (String JavaDoc) names.nextElement();
110                 String JavaDoc lib = macroLibraries.getProperty(namespace);
111                 log.info(lib + " imported under namespace " + namespace);
112                 config.addAutoImport(namespace, lib);
113             }
114         }
115     }
116     
117 }
118
Popular Tags