Velocity 1.6 oriented updates to Spring's Velocity support

This commit is contained in:
Juergen Hoeller 2012-11-29 23:20:22 +01:00
parent 48f405e8fd
commit 89fc6acba8
3 changed files with 37 additions and 35 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2005 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,10 +26,16 @@ import org.apache.velocity.runtime.log.LogSystem;
* Velocity LogSystem implementation for Jakarta Commons Logging. * Velocity LogSystem implementation for Jakarta Commons Logging.
* Used by VelocityConfigurer to redirect log output. * Used by VelocityConfigurer to redirect log output.
* *
* <p><b>NOTE:</b> To be replaced by Velocity 1.5's <code>LogChute</code> mechanism
* and Velocity 1.6's <code>CommonsLogLogChute</code> implementation once we
* upgrade to Velocity 1.6+ (likely Velocity 1.7+) in a future version of Spring.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 07.08.2003 * @since 07.08.2003
* @see VelocityEngineFactoryBean * @see VelocityEngineFactoryBean
* @deprecated as of Spring 3.2, in favor of Velocity 1.6's <code>CommonsLogLogChute</code>
*/ */
@Deprecated
public class CommonsLoggingLogSystem implements LogSystem { public class CommonsLoggingLogSystem implements LogSystem {
private static final Log logger = LogFactory.getLog(VelocityEngine.class); private static final Log logger = LogFactory.getLog(VelocityEngine.class);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -99,9 +99,9 @@ public class SpringResourceLoader extends ResourceLoader {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Looking for Velocity resource with name [" + source + "]"); logger.debug("Looking for Velocity resource with name [" + source + "]");
} }
for (int i = 0; i < this.resourceLoaderPaths.length; i++) { for (String resourceLoaderPath : this.resourceLoaderPaths) {
org.springframework.core.io.Resource resource = org.springframework.core.io.Resource resource =
this.resourceLoader.getResource(this.resourceLoaderPaths[i] + source); this.resourceLoader.getResource(resourceLoaderPath + source);
try { try {
return resource.getInputStream(); return resource.getInputStream();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,15 +42,16 @@ public abstract class VelocityEngineUtils {
* Merge the specified Velocity template with the given model and write * Merge the specified Velocity template with the given model and write
* the result to the given Writer. * the result to the given Writer.
* @param velocityEngine VelocityEngine to work with * @param velocityEngine VelocityEngine to work with
* @param templateLocation the location of template, relative to Velocity's * @param templateLocation the location of template, relative to Velocity's resource loader path
* resource loader path * @param model the Map that contains model names as keys and model objects as values
* @param model the Map that contains model names as keys and model objects
* as values
* @param writer the Writer to write the result to * @param writer the Writer to write the result to
* @throws VelocityException if the template wasn't found or rendering failed * @throws VelocityException if the template wasn't found or rendering failed
* @deprecated Use {@link #mergeTemplate(VelocityEngine, String, String, Map, Writer)}
* instead, following Velocity 1.6's corresponding deprecation in its own API.
*/ */
@Deprecated
public static void mergeTemplate( public static void mergeTemplate(
VelocityEngine velocityEngine, String templateLocation, Map model, Writer writer) VelocityEngine velocityEngine, String templateLocation, Map<String, Object> model, Writer writer)
throws VelocityException { throws VelocityException {
try { try {
@ -70,20 +71,18 @@ public abstract class VelocityEngineUtils {
} }
/** /**
* Merge the specified Velocity template with the given model and write * Merge the specified Velocity template with the given model and write the result
* the result to the given Writer. * to the given Writer.
* @param velocityEngine VelocityEngine to work with * @param velocityEngine VelocityEngine to work with
* @param templateLocation the location of template, relative to Velocity's * @param templateLocation the location of template, relative to Velocity's resource loader path
* resource loader path
* @param encoding the encoding of the template file * @param encoding the encoding of the template file
* @param model the Map that contains model names as keys and model objects * @param model the Map that contains model names as keys and model objects as values
* as values
* @param writer the Writer to write the result to * @param writer the Writer to write the result to
* @throws VelocityException if the template wasn't found or rendering failed * @throws VelocityException if the template wasn't found or rendering failed
*/ */
public static void mergeTemplate( public static void mergeTemplate(
VelocityEngine velocityEngine, String templateLocation, String encoding, Map model, Writer writer) VelocityEngine velocityEngine, String templateLocation, String encoding,
throws VelocityException { Map<String, Object> model, Writer writer) throws VelocityException {
try { try {
VelocityContext velocityContext = new VelocityContext(model); VelocityContext velocityContext = new VelocityContext(model);
@ -106,17 +105,17 @@ public abstract class VelocityEngineUtils {
* <p>When using this method to prepare a text for a mail to be sent with Spring's * <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. * mail support, consider wrapping VelocityException in MailPreparationException.
* @param velocityEngine VelocityEngine to work with * @param velocityEngine VelocityEngine to work with
* @param templateLocation the location of template, relative to Velocity's * @param templateLocation the location of template, relative to Velocity's resource loader path
* resource loader path * @param model the Map that contains model names as keys and model objects as values
* @param model the Map that contains model names as keys and model objects
* as values
* @return the result as String * @return the result as String
* @throws VelocityException if the template wasn't found or rendering failed * @throws VelocityException if the template wasn't found or rendering failed
* @see org.springframework.mail.MailPreparationException * @see org.springframework.mail.MailPreparationException
* @deprecated Use {@link #mergeTemplateIntoString(VelocityEngine, String, String, Map)}
* instead, following Velocity 1.6's corresponding deprecation in its own API.
*/ */
public static String mergeTemplateIntoString( @Deprecated
VelocityEngine velocityEngine, String templateLocation, Map model) public static String mergeTemplateIntoString(VelocityEngine velocityEngine, String templateLocation,
throws VelocityException { Map<String, Object> model) throws VelocityException {
StringWriter result = new StringWriter(); StringWriter result = new StringWriter();
mergeTemplate(velocityEngine, templateLocation, model, result); mergeTemplate(velocityEngine, templateLocation, model, result);
@ -128,18 +127,15 @@ public abstract class VelocityEngineUtils {
* <p>When using this method to prepare a text for a mail to be sent with Spring's * <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. * mail support, consider wrapping VelocityException in MailPreparationException.
* @param velocityEngine VelocityEngine to work with * @param velocityEngine VelocityEngine to work with
* @param templateLocation the location of template, relative to Velocity's * @param templateLocation the location of template, relative to Velocity's resource loader path
* resource loader path
* @param encoding the encoding of the template file * @param encoding the encoding of the template file
* @param model the Map that contains model names as keys and model objects * @param model the Map that contains model names as keys and model objects as values
* as values
* @return the result as String * @return the result as String
* @throws VelocityException if the template wasn't found or rendering failed * @throws VelocityException if the template wasn't found or rendering failed
* @see org.springframework.mail.MailPreparationException * @see org.springframework.mail.MailPreparationException
*/ */
public static String mergeTemplateIntoString( public static String mergeTemplateIntoString(VelocityEngine velocityEngine, String templateLocation,
VelocityEngine velocityEngine, String templateLocation, String encoding, Map model) String encoding, Map<String, Object> model) throws VelocityException {
throws VelocityException {
StringWriter result = new StringWriter(); StringWriter result = new StringWriter();
mergeTemplate(velocityEngine, templateLocation, encoding, model, result); mergeTemplate(velocityEngine, templateLocation, encoding, model, result);