1. /*
  2. * Copyright 2002-2004 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. package org.springframework.ui.velocity;
  17. import java.io.StringWriter;
  18. import java.io.Writer;
  19. import java.util.Map;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.apache.velocity.VelocityContext;
  23. import org.apache.velocity.app.VelocityEngine;
  24. import org.apache.velocity.exception.VelocityException;
  25. /**
  26. * Utility class for working with a VelocityEngine.
  27. * Provides convenience methods to merge a Velocity template with a model.
  28. * @author Juergen Hoeller
  29. * @since 22.01.2004
  30. */
  31. public abstract class VelocityEngineUtils {
  32. private static final Log logger = LogFactory.getLog(VelocityEngineUtils.class);
  33. /**
  34. * Merge the specified Velocity template with the given model and write
  35. * the result to the given Writer.
  36. * @param velocityEngine VelocityEngine to work with
  37. * @param templateLocation the location of template, relative to Velocity's
  38. * resource loader path
  39. * @param model the Map that contains model names as keys and model objects
  40. * as values
  41. * @param writer the Writer to write the result to
  42. * @throws VelocityException if the template wasn't found or rendering failed
  43. */
  44. public static void mergeTemplate(VelocityEngine velocityEngine, String templateLocation, Map model,
  45. Writer writer) throws VelocityException {
  46. try {
  47. VelocityContext velocityContext = new VelocityContext(model);
  48. velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
  49. }
  50. catch (VelocityException ex) {
  51. throw ex;
  52. }
  53. catch (RuntimeException ex) {
  54. throw ex;
  55. }
  56. catch (Exception ex) {
  57. logger.error("Why does VelocityEngine throw a generic checked exception, after all?", ex);
  58. throw new VelocityException(ex.getMessage());
  59. }
  60. }
  61. /**
  62. * Merge the specified Velocity template with the given model and write
  63. * the result to the given Writer.
  64. * @param velocityEngine VelocityEngine to work with
  65. * @param templateLocation the location of template, relative to Velocity's
  66. * resource loader path
  67. * @param encoding the encoding of the template file
  68. * @param model the Map that contains model names as keys and model objects
  69. * as values
  70. * @param writer the Writer to write the result to
  71. * @throws VelocityException if the template wasn't found or rendering failed
  72. */
  73. public static void mergeTemplate(VelocityEngine velocityEngine, String templateLocation, String encoding,
  74. Map model, Writer writer) throws VelocityException {
  75. try {
  76. VelocityContext velocityContext = new VelocityContext(model);
  77. velocityEngine.mergeTemplate(templateLocation, encoding, velocityContext, writer);
  78. }
  79. catch (VelocityException ex) {
  80. throw ex;
  81. }
  82. catch (RuntimeException ex) {
  83. throw ex;
  84. }
  85. catch (Exception ex) {
  86. logger.error("Why does VelocityEngine throw a generic checked exception, after all?", ex);
  87. throw new VelocityException(ex.getMessage());
  88. }
  89. }
  90. /**
  91. * Merge the specified Velocity template with the given model into a String.
  92. * @param velocityEngine VelocityEngine to work with
  93. * @param templateLocation the location of template, relative to Velocity's
  94. * resource loader path
  95. * @param model the Map that contains model names as keys and model objects
  96. * as values
  97. * @return the result as String
  98. * @throws VelocityException if the template wasn't found or rendering failed
  99. */
  100. public static String mergeTemplateIntoString(VelocityEngine velocityEngine, String templateLocation,
  101. Map model) throws VelocityException {
  102. StringWriter result = new StringWriter();
  103. mergeTemplate(velocityEngine, templateLocation, model, result);
  104. return result.toString();
  105. }
  106. /**
  107. * Merge the specified Velocity template with the given model into a String.
  108. * @param velocityEngine VelocityEngine to work with
  109. * @param templateLocation the location of template, relative to Velocity's
  110. * resource loader path
  111. * @param encoding the encoding of the template file
  112. * @param model the Map that contains model names as keys and model objects
  113. * as values
  114. * @return the result as String
  115. * @throws VelocityException if the template wasn't found or rendering failed
  116. */
  117. public static String mergeTemplateIntoString(VelocityEngine velocityEngine, String templateLocation,
  118. String encoding, Map model) throws VelocityException {
  119. StringWriter result = new StringWriter();
  120. mergeTemplate(velocityEngine, templateLocation, encoding, model, result);
  121. return result.toString();
  122. }
  123. }