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

package org.springframework.ui.velocity;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;

/**
 * Utility class for working with a VelocityEngine.
 * Provides convenience methods to merge a Velocity template with a model.
 *
 * @author Juergen Hoeller
 * @since 22.01.2004
 */
public abstract class VelocityEngineUtils {

	private static final Log logger = LogFactory.getLog(VelocityEngineUtils.class);

	/**
	 * Merge the specified Velocity template with the given model and write
	 * the result to the given Writer.
	 * @param velocityEngine VelocityEngine to work with
	 * @param templateLocation the location of template, relative to Velocity's
	 * resource loader path
	 * @param model the Map that contains model names as keys and model objects
	 * as values
	 * @param writer the Writer to write the result to
	 * @throws VelocityException if the template wasn't found or rendering failed
	 */
	public static void mergeTemplate(
	    VelocityEngine velocityEngine, String templateLocation, Map model, Writer writer)
	    throws VelocityException {
		try {
			VelocityContext velocityContext = new VelocityContext(model);
			velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
		}
		catch (VelocityException ex) {
			throw ex;
		}
		catch (RuntimeException ex) {
			throw ex;
		}
		catch (Exception ex) {
			logger.error("Why does VelocityEngine throw a generic checked exception, after all?", ex);
			throw new VelocityException(ex.getMessage());
		}
	}

	/**
	 * Merge the specified Velocity template with the given model and write
	 * the result to the given Writer.
	 * @param velocityEngine VelocityEngine to work with
	 * @param templateLocation the location of template, relative to Velocity's
	 * resource loader path
	 * @param encoding the encoding of the template file
	 * @param model the Map that contains model names as keys and model objects
	 * as values
	 * @param writer the Writer to write the result to
	 * @throws VelocityException if the template wasn't found or rendering failed
	 */
	public static void mergeTemplate(
	    VelocityEngine velocityEngine, String templateLocation, String encoding, Map model, Writer writer)
	    throws VelocityException {
		try {
			VelocityContext velocityContext = new VelocityContext(model);
			velocityEngine.mergeTemplate(templateLocation, encoding, velocityContext, writer);
		}
		catch (VelocityException ex) {
			throw ex;
		}
		catch (RuntimeException ex) {
			throw ex;
		}
		catch (Exception ex) {
			logger.error("Why does VelocityEngine throw a generic checked exception, after all?", ex);
			throw new VelocityException(ex.getMessage());
		}
	}

	/**
	 * Merge the specified Velocity template with the given model into a String.
	 * <p>When using this method to prepare a text for a mail to be sent with Spring's
	 * mail support, consider wrapping VelocityException in MailPreparationException.
	 * @param velocityEngine VelocityEngine to work with
	 * @param templateLocation the location of template, relative to Velocity's
	 * resource loader path
	 * @param model the Map that contains model names as keys and model objects
	 * as values
	 * @return the result as String
	 * @throws VelocityException if the template wasn't found or rendering failed
	 * @see org.springframework.mail.MailPreparationException
	 */
	public static String mergeTemplateIntoString(
	    VelocityEngine velocityEngine, String templateLocation, Map model)
	    throws VelocityException {
		StringWriter result = new StringWriter();
		mergeTemplate(velocityEngine, templateLocation, model, result);
		return result.toString();
	}

	/**
	 * Merge the specified Velocity template with the given model into a String.
	 * <p>When using this method to prepare a text for a mail to be sent with Spring's
	 * mail support, consider wrapping VelocityException in MailPreparationException.
	 * @param velocityEngine VelocityEngine to work with
	 * @param templateLocation the location of template, relative to Velocity's
	 * resource loader path
	 * @param encoding the encoding of the template file
	 * @param model the Map that contains model names as keys and model objects
	 * as values
	 * @return the result as String
	 * @throws VelocityException if the template wasn't found or rendering failed
	 * @see org.springframework.mail.MailPreparationException
	 */
	public static String mergeTemplateIntoString(
	    VelocityEngine velocityEngine, String templateLocation, String encoding, Map model)
	    throws VelocityException {
		StringWriter result = new StringWriter();
		mergeTemplate(velocityEngine, templateLocation, encoding, model, result);
		return result.toString();
	}

}

@@ -21,9 +21,9 @@
 -200
-5
+6
  the
@@ -53,17 +53,16 @@
 hors.%0A *
- 
 %0A * Lice
@@ -234,17 +234,16 @@
 se at%0A *
- 
 %0A *     
@@ -288,17 +288,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -1278,16 +1278,17 @@
 lass);%0A%0A
+%0A
 %09/**%0A%09 *
@@ -1922,32 +1922,33 @@
 cityException %7B%0A
+%0A
 %09%09try %7B%0A%09%09%09Veloc
@@ -2332,34 +2332,32 @@
 xception(ex.
-getMessage
+toString
 ());%0A%09%09%7D%0A%09%7D%0A
@@ -3079,16 +3079,17 @@
 ption %7B%0A
+%0A
 %09%09try %7B%0A
@@ -3499,18 +3499,16 @@
 (ex.
-getMessage
+toString
 ());
@@ -4229,36 +4229,34 @@
 ateIntoString(%0A%09
-    
+%09%09
 VelocityEngine v
@@ -4299,36 +4299,34 @@
 on, Map model)%0A%09
-    
+%09%09
 throws VelocityE
@@ -4328,32 +4328,33 @@
 cityException %7B%0A
+%0A
 %09%09StringWriter r
@@ -5254,20 +5254,18 @@
 tring(%0A%09
-    
+%09%09
 Velocity
@@ -5341,20 +5341,18 @@
 model)%0A%09
-    
+%09%09
 throws V
@@ -5370,16 +5370,17 @@
 ption %7B%0A
+%0A
 %09%09String

throw VelocityException with full Exception toString instead of just getMessage