| FreeMarkerUtil.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * The contents of this file are subject to the terms of the Liferay Enterprise
5 * Subscription License ("License"). You may not use this file except in
6 * compliance with the License. You can obtain a copy of the License by
7 * contacting Liferay, Inc. See the License for the specific language governing
8 * permissions and limitations under the License, including but not limited to
9 * distribution rights of the Software.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 package com.liferay.portal.freemarker;
21
22 import freemarker.cache.ClassTemplateLoader;
23
24 import freemarker.template.Configuration;
25 import freemarker.template.DefaultObjectWrapper;
26 import freemarker.template.Template;
27
28 import java.io.StringWriter;
29 import java.io.Writer;
30
31 /**
32 * <a href="FreeMarkerUtil.java.html"><b><i>View Source</i></b></a>
33 *
34 * @author Tariq Dweik
35 * @author Brian Wing Shun Chan
36 *
37 */
38 public class FreeMarkerUtil {
39
40 public static String process(String name, Object context)
41 throws Exception {
42
43 StringWriter writer = new StringWriter();
44
45 process(name, context, writer);
46
47 return writer.toString();
48 }
49
50 public static void process(String name, Object context, Writer writer)
51 throws Exception {
52
53 Template template = _getConfiguration().getTemplate(name);
54
55 template.process(context, writer);
56 }
57
58 private static Configuration _getConfiguration() {
59 if (_configuration == null) {
60 _configuration = new Configuration();
61
62 _configuration.setObjectWrapper(new DefaultObjectWrapper());
63 _configuration.setTemplateLoader(
64 new ClassTemplateLoader(FreeMarkerUtil.class, "/"));
65 }
66
67 return _configuration;
68 }
69
70 private static Configuration _configuration;
71
72 }