KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ui > velocity > VelocityEngineUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.ui.velocity;
18
19 import java.io.StringWriter JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.velocity.VelocityContext;
26 import org.apache.velocity.app.VelocityEngine;
27 import org.apache.velocity.exception.VelocityException;
28
29 /**
30  * Utility class for working with a VelocityEngine.
31  * Provides convenience methods to merge a Velocity template with a model.
32  *
33  * @author Juergen Hoeller
34  * @since 22.01.2004
35  */

36 public abstract class VelocityEngineUtils {
37
38     private static final Log logger = LogFactory.getLog(VelocityEngineUtils.class);
39
40
41     /**
42      * Merge the specified Velocity template with the given model and write
43      * the result to the given Writer.
44      * @param velocityEngine VelocityEngine to work with
45      * @param templateLocation the location of template, relative to Velocity's
46      * resource loader path
47      * @param model the Map that contains model names as keys and model objects
48      * as values
49      * @param writer the Writer to write the result to
50      * @throws VelocityException if the template wasn't found or rendering failed
51      */

52     public static void mergeTemplate(
53             VelocityEngine velocityEngine, String JavaDoc templateLocation, Map JavaDoc model, Writer JavaDoc writer)
54             throws VelocityException {
55
56         try {
57             VelocityContext velocityContext = new VelocityContext(model);
58             velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
59         }
60         catch (VelocityException ex) {
61             throw ex;
62         }
63         catch (RuntimeException JavaDoc ex) {
64             throw ex;
65         }
66         catch (Exception JavaDoc ex) {
67             logger.error("Why does VelocityEngine throw a generic checked exception, after all?", ex);
68             throw new VelocityException(ex.toString());
69         }
70     }
71
72     /**
73      * Merge the specified Velocity template with the given model and write
74      * the result to the given Writer.
75      * @param velocityEngine VelocityEngine to work with
76      * @param templateLocation the location of template, relative to Velocity's
77      * resource loader path
78      * @param encoding the encoding of the template file
79      * @param model the Map that contains model names as keys and model objects
80      * as values
81      * @param writer the Writer to write the result to
82      * @throws VelocityException if the template wasn't found or rendering failed
83      */

84     public static void mergeTemplate(
85             VelocityEngine velocityEngine, String JavaDoc templateLocation, String JavaDoc encoding, Map JavaDoc model, Writer JavaDoc writer)
86             throws VelocityException {
87
88         try {
89             VelocityContext velocityContext = new VelocityContext(model);
90             velocityEngine.mergeTemplate(templateLocation, encoding, velocityContext, writer);
91         }
92         catch (VelocityException ex) {
93             throw ex;
94         }
95         catch (RuntimeException JavaDoc ex) {
96             throw ex;
97         }
98         catch (Exception JavaDoc ex) {
99             logger.error("Why does VelocityEngine throw a generic checked exception, after all?", ex);
100             throw new VelocityException(ex.toString());
101         }
102     }
103
104     /**
105      * Merge the specified Velocity template with the given model into a String.
106      * <p>When using this method to prepare a text for a mail to be sent with Spring's
107      * mail support, consider wrapping VelocityException in MailPreparationException.
108      * @param velocityEngine VelocityEngine to work with
109      * @param templateLocation the location of template, relative to Velocity's
110      * resource loader path
111      * @param model the Map that contains model names as keys and model objects
112      * as values
113      * @return the result as String
114      * @throws VelocityException if the template wasn't found or rendering failed
115      * @see org.springframework.mail.MailPreparationException
116      */

117     public static String JavaDoc mergeTemplateIntoString(
118             VelocityEngine velocityEngine, String JavaDoc templateLocation, Map JavaDoc model)
119             throws VelocityException {
120
121         StringWriter JavaDoc result = new StringWriter JavaDoc();
122         mergeTemplate(velocityEngine, templateLocation, model, result);
123         return result.toString();
124     }
125
126     /**
127      * Merge the specified Velocity template with the given model into a String.
128      * <p>When using this method to prepare a text for a mail to be sent with Spring's
129      * mail support, consider wrapping VelocityException in MailPreparationException.
130      * @param velocityEngine VelocityEngine to work with
131      * @param templateLocation the location of template, relative to Velocity's
132      * resource loader path
133      * @param encoding the encoding of the template file
134      * @param model the Map that contains model names as keys and model objects
135      * as values
136      * @return the result as String
137      * @throws VelocityException if the template wasn't found or rendering failed
138      * @see org.springframework.mail.MailPreparationException
139      */

140     public static String JavaDoc mergeTemplateIntoString(
141             VelocityEngine velocityEngine, String JavaDoc templateLocation, String JavaDoc encoding, Map JavaDoc model)
142             throws VelocityException {
143
144         StringWriter JavaDoc result = new StringWriter JavaDoc();
145         mergeTemplate(velocityEngine, templateLocation, encoding, model, result);
146         return result.toString();
147     }
148
149 }
150
Popular Tags