1 /*** 2 * The contents of this file are subject to the Mozilla Public 3 * License Version 1.1 (the "License"); you may not use this file 4 * except in compliance with the License. You may obtain a copy of 5 * the License at http://www.mozilla.org/MPL/ 6 * 7 * Software distributed under the License is distributed on an "AS 8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 9 * implied. See the License for the specific language governing 10 * rights and limitations under the License. 11 * 12 * The Original Code is pow2toolkit library. 13 * 14 * The Initial Owner of the Original Code is Power Of Two S.R.L. 15 * Portions created by Power Of Two S.R.L. are Copyright (C) Power Of Two S.R.L. 16 * All Rights Reserved. 17 * 18 * Contributor(s): 19 */ 20 21 package com.pow2.util; 22 23 import java.io.*; 24 25 import org.apache.log4j.*; 26 import org.apache.velocity.*; 27 import org.apache.velocity.app.*; 28 import org.apache.velocity.exception.*; 29 30 31 /*** 32 * VelocityUtil class. 33 * 34 * @author Luca Fossato 35 * @created 9 November 2001 36 */ 37 public class VelocityUtil 38 { 39 /*** Log4j category; */ 40 private final static Category cat = Category.getInstance(VelocityUtil.class.getName()); 41 42 43 /*** 44 * Get the string representation of the Velocity template 45 * populated using the context keys values. 46 * 47 * @param context the populated Velocity Context object. 48 * @param templateName the template name to use. 49 * @return the string representation of the Velocity template 50 * populated using the context keys values. 51 */ 52 public static String getTemplate(VelocityContext context, String templateName) 53 { 54 Template template = null; 55 56 try 57 { 58 template = Velocity.getTemplate(templateName); 59 } 60 catch (ResourceNotFoundException e) 61 { 62 cat.error("::getTemplate - couldn't find the template [" + templateName + "]", e); 63 } 64 catch (ParseErrorException e) 65 { 66 cat.error("::getTemplate - problem parsing the template [" + templateName + "]", e); 67 } 68 catch (MethodInvocationException e) 69 { 70 cat.error("::getTemplate - something invoked in the template [" + templateName + "] threw an exception...", e); 71 } 72 catch (Exception e) 73 { 74 cat.error("::getTemplate - exception:", e); 75 } 76 77 78 StringWriter sw = new StringWriter(); 79 80 try 81 { 82 template.merge(context, sw); 83 } 84 catch (Exception e) 85 { 86 cat.error("::getTemplate - cannot merge the template [" + templateName + "] with the context; exception:", e); 87 } 88 89 return sw.toString(); 90 } 91 }