001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ui.velocity;
018:
019: import java.io.StringWriter;
020: import java.io.Writer;
021: import java.util.Map;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.velocity.VelocityContext;
026: import org.apache.velocity.app.VelocityEngine;
027: import org.apache.velocity.exception.VelocityException;
028:
029: /**
030: * Utility class for working with a VelocityEngine.
031: * Provides convenience methods to merge a Velocity template with a model.
032: *
033: * @author Juergen Hoeller
034: * @since 22.01.2004
035: */
036: public abstract class VelocityEngineUtils {
037:
038: private static final Log logger = LogFactory
039: .getLog(VelocityEngineUtils.class);
040:
041: /**
042: * Merge the specified Velocity template with the given model and write
043: * the result to the given Writer.
044: * @param velocityEngine VelocityEngine to work with
045: * @param templateLocation the location of template, relative to Velocity's
046: * resource loader path
047: * @param model the Map that contains model names as keys and model objects
048: * as values
049: * @param writer the Writer to write the result to
050: * @throws VelocityException if the template wasn't found or rendering failed
051: */
052: public static void mergeTemplate(VelocityEngine velocityEngine,
053: String templateLocation, Map model, Writer writer)
054: throws VelocityException {
055:
056: try {
057: VelocityContext velocityContext = new VelocityContext(model);
058: velocityEngine.mergeTemplate(templateLocation,
059: velocityContext, writer);
060: } catch (VelocityException ex) {
061: throw ex;
062: } catch (RuntimeException ex) {
063: throw ex;
064: } catch (Exception ex) {
065: logger
066: .error(
067: "Why does VelocityEngine throw a generic checked exception, after all?",
068: ex);
069: throw new VelocityException(ex.toString());
070: }
071: }
072:
073: /**
074: * Merge the specified Velocity template with the given model and write
075: * the result to the given Writer.
076: * @param velocityEngine VelocityEngine to work with
077: * @param templateLocation the location of template, relative to Velocity's
078: * resource loader path
079: * @param encoding the encoding of the template file
080: * @param model the Map that contains model names as keys and model objects
081: * as values
082: * @param writer the Writer to write the result to
083: * @throws VelocityException if the template wasn't found or rendering failed
084: */
085: public static void mergeTemplate(VelocityEngine velocityEngine,
086: String templateLocation, String encoding, Map model,
087: Writer writer) throws VelocityException {
088:
089: try {
090: VelocityContext velocityContext = new VelocityContext(model);
091: velocityEngine.mergeTemplate(templateLocation, encoding,
092: velocityContext, writer);
093: } catch (VelocityException ex) {
094: throw ex;
095: } catch (RuntimeException ex) {
096: throw ex;
097: } catch (Exception ex) {
098: logger
099: .error(
100: "Why does VelocityEngine throw a generic checked exception, after all?",
101: ex);
102: throw new VelocityException(ex.toString());
103: }
104: }
105:
106: /**
107: * Merge the specified Velocity template with the given model into a String.
108: * <p>When using this method to prepare a text for a mail to be sent with Spring's
109: * mail support, consider wrapping VelocityException in MailPreparationException.
110: * @param velocityEngine VelocityEngine to work with
111: * @param templateLocation the location of template, relative to Velocity's
112: * resource loader path
113: * @param model the Map that contains model names as keys and model objects
114: * as values
115: * @return the result as String
116: * @throws VelocityException if the template wasn't found or rendering failed
117: * @see org.springframework.mail.MailPreparationException
118: */
119: public static String mergeTemplateIntoString(
120: VelocityEngine velocityEngine, String templateLocation,
121: Map model) throws VelocityException {
122:
123: StringWriter result = new StringWriter();
124: mergeTemplate(velocityEngine, templateLocation, model, result);
125: return result.toString();
126: }
127:
128: /**
129: * Merge the specified Velocity template with the given model into a String.
130: * <p>When using this method to prepare a text for a mail to be sent with Spring's
131: * mail support, consider wrapping VelocityException in MailPreparationException.
132: * @param velocityEngine VelocityEngine to work with
133: * @param templateLocation the location of template, relative to Velocity's
134: * resource loader path
135: * @param encoding the encoding of the template file
136: * @param model the Map that contains model names as keys and model objects
137: * as values
138: * @return the result as String
139: * @throws VelocityException if the template wasn't found or rendering failed
140: * @see org.springframework.mail.MailPreparationException
141: */
142: public static String mergeTemplateIntoString(
143: VelocityEngine velocityEngine, String templateLocation,
144: String encoding, Map model) throws VelocityException {
145:
146: StringWriter result = new StringWriter();
147: mergeTemplate(velocityEngine, templateLocation, encoding,
148: model, result);
149: return result.toString();
150: }
151:
152: }
|