Coverage Report - org.melati.template.velocity.VelocityTemplateEngine
 
Classes in this File Line Coverage Branch Coverage Complexity
VelocityTemplateEngine
79%
38/48
60%
6/10
2.667
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/main/java/org/melati/template/velocity/VelocityTemplateEngine.java,v $
 3  
  * $Revision: 1.48 $
 4  
  *
 5  
  * Copyright (C) 2000 Tim Joyce
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     Tim Joyce <timj At paneris.org>
 42  
  */
 43  
 
 44  
 package org.melati.template.velocity;
 45  
 
 46  
 import java.io.IOException;
 47  
 
 48  
 import java.util.Properties;
 49  
 
 50  
 import org.melati.Melati;
 51  
 import org.melati.MelatiConfig;
 52  
 import org.melati.poem.AccessPoemException;
 53  
 import org.melati.template.AbstractTemplateEngine;
 54  
 import org.melati.template.TemplateContext;
 55  
 import org.melati.template.TemplateEngine;
 56  
 import org.melati.template.TemplateEngineException;
 57  
 import org.melati.template.NotFoundException;
 58  
 import org.melati.util.MelatiBugMelatiException;
 59  
 import org.melati.util.MelatiStringWriter;
 60  
 import org.melati.util.MelatiWriter;
 61  
 
 62  
 import org.apache.velocity.VelocityContext;
 63  
 import org.apache.velocity.app.Velocity;
 64  
 import org.apache.velocity.exception.MethodInvocationException;
 65  
 import org.apache.velocity.exception.ParseErrorException;
 66  
 import org.apache.velocity.exception.ResourceNotFoundException;
 67  
 
 68  
 /**
 69  
  * Wrapper for the Velocity Template Engine for use with Melati.
 70  
  */
 71  
 public class VelocityTemplateEngine extends AbstractTemplateEngine implements
 72  
     TemplateEngine {
 73  
 
 74  
   /** The name of the engine. */
 75  
   public static final String NAME = "velocity";
 76  
 
 77  
   /**
 78  
    * This is the string that is looked for when getInitParameter is called.
 79  
    */
 80  
   // private static final String INIT_PROPS_KEY = "velocity.properties";
 81  
   /**
 82  
    * Constructor.
 83  
    */
 84  
   public VelocityTemplateEngine() {
 85  128
     super();
 86  128
   }
 87  
 
 88  
   /**
 89  
    * Construct a new Engine.
 90  
    * 
 91  
    * @param melatiConfig
 92  
    *        a {@link MelatiConfig}
 93  
    * @throws TemplateEngineException
 94  
    *         if any problem occurs with the engine
 95  
    */
 96  
   public void init(MelatiConfig melatiConfig)
 97  
       throws TemplateEngineException {
 98  
     try {
 99  69
       Properties props = loadConfiguration();
 100  69
       Velocity.init(props);
 101  0
     } catch (Exception e) {
 102  0
       throw new TemplateEngineException(e);
 103  69
     }
 104  69
   }
 105  
 
 106  
 
 107  
   protected Properties loadConfiguration() {
 108  69
     Properties p = new Properties();
 109  69
     p.setProperty("resource.loader", "class");
 110  69
     p.setProperty("class.resource.loader.class",
 111  1
         WebMacroClasspathResourceLoader.class.getName());
 112  69
     return p;
 113  
   }
 114  
 
 115  
   /**
 116  
    * Get the template context for Velocity.
 117  
    * 
 118  
    * @param melati
 119  
    *        the {@link Melati}
 120  
    * @return a {@link TemplateContext}
 121  
    */
 122  
   public TemplateContext getTemplateContext(Melati melati) {
 123  113
     VelocityContext context = new VelocityContext();
 124  113
     return new VelocityServletTemplateContext(context);
 125  
   }
 126  
 
 127  
   /**
 128  
    * The name of the template engine (used to find the templets).
 129  
    * 
 130  
    * @return the name of the current configured template engine
 131  
    */
 132  
   public String getName() {
 133  171
     return NAME;
 134  
   }
 135  
 
 136  
   /**
 137  
    * @return the extension of the templates used by Velocity, including the dot.
 138  
    */
 139  
   public String templateExtension() {
 140  288
     return ".vm";
 141  
   }
 142  
 
 143  
   /**
 144  
    * Get a template by name.
 145  
    * 
 146  
    * @param templateName
 147  
    *        the name of the template to find
 148  
    * @return a template
 149  
    * @throws NotFoundException if template not found
 150  
    */
 151  
   public org.melati.template.Template template(String templateName)
 152  
       throws NotFoundException {
 153  
     try {
 154  104
       return new VelocityTemplate(templateName);
 155  77
     } catch (ResourceNotFoundException e) {
 156  77
       if (templateName.endsWith(templateExtension())) {
 157  
         // have a go at loading the webmacro template, and converting it!
 158  77
         String webmacroTemplateName = templateName.substring(0, templateName
 159  
             .lastIndexOf(templateExtension()))
 160  
             + ".wm";
 161  
         try {
 162  77
           return new VelocityTemplate(webmacroTemplateName);
 163  0
         } catch (ParseErrorException p) {
 164  0
           throw new MelatiBugMelatiException(
 165  
               "Problem converting a WebMacro template to a Velocity template: " + webmacroTemplateName,
 166  
               p);
 167  77
         } catch (ResourceNotFoundException e2) {
 168  77
             throw new NotFoundException("Could not find template " + templateName + " or " + webmacroTemplateName);
 169  
         } 
 170  0
       } else throw new NotFoundException("Could not find template " + templateName);
 171  4
     } catch (Exception e) {
 172  4
       throw new TemplateEngineException(e);
 173  
     }
 174  
   }
 175  
 
 176  
   /**
 177  
    * Get a Template by name and expand it against a context.
 178  
    * 
 179  
    * @param out
 180  
    *        a {@link MelatiWriter} to output on
 181  
    * @param templateName
 182  
    *        the name of the template to expand
 183  
    * @param templateContext
 184  
    *        the {@link TemplateContext} to expand the template against
 185  
    * @throws IOException if TemplateEngine does
 186  
    * @throws NotFoundException if template not found
 187  
    */
 188  
   public void expandTemplate(MelatiWriter out, String templateName,
 189  
       TemplateContext templateContext)
 190  
       throws IOException, NotFoundException {
 191  0
     expandTemplate(out, template(templateName), templateContext);
 192  0
   }
 193  
 
 194  
   /**
 195  
    * Expand a {@link org.melati.template.Template} against the context.
 196  
    * 
 197  
    * @param out
 198  
    *        a {@link MelatiWriter} to output on
 199  
    * @param template
 200  
    *        the {@link org.melati.template.Template} to expand
 201  
    * @param templateContext
 202  
    *        the {@link TemplateContext} to expand the template against
 203  
    */
 204  
   public void expandTemplate(MelatiWriter out,
 205  
       org.melati.template.Template template, TemplateContext templateContext)
 206  
       throws IOException {
 207  
     try {
 208  46
       template.write(out, templateContext, this);
 209  4
     } catch (TemplateEngineException problem) {
 210  4
       Exception underlying = problem.subException;
 211  4
       if (underlying instanceof AccessPoemException) {
 212  0
         throw (AccessPoemException)underlying;
 213  
       }
 214  4
       if (underlying instanceof MethodInvocationException) {
 215  4
         Throwable caught = ((MethodInvocationException)underlying)
 216  
             .getWrappedThrowable();
 217  4
         if (caught instanceof AccessPoemException) {
 218  0
           throw (AccessPoemException)caught;
 219  
         }
 220  
       }
 221  4
       throw problem;
 222  42
     }
 223  42
   }
 224  
 
 225  
   /**
 226  
    * Expand the Template against the context to a String.
 227  
    * 
 228  
    * @param template
 229  
    *        the {@link org.melati.template.Template} to expand
 230  
    * @param templateContext
 231  
    *        the {@link TemplateContext} to expand the template against
 232  
    * @throws IOException if TemplateEngine does
 233  
    * @return the interpolated template as a String
 234  
    * {@inheritDoc}
 235  
    * @see org.melati.template.TemplateEngine#expandedTemplate
 236  
    */
 237  
   public String expandedTemplate(org.melati.template.Template template,
 238  
       TemplateContext templateContext)
 239  
       throws IOException {
 240  2
     MelatiStringWriter s = new MelatiStringWriter();
 241  2
     expandTemplate(s, template, templateContext);
 242  2
     return s.toString();
 243  
   }
 244  
 
 245  
   /**
 246  
    * Return a {@link MelatiStringWriter}.
 247  
    * 
 248  
    * @return a MelatiStringWriter
 249  
    * {@inheritDoc}
 250  
    * @see Melati#getStringWriter()
 251  
    * @see org.melati.template.TemplateEngine#getStringWriter()
 252  
    */
 253  
   public MelatiStringWriter getStringWriter() {
 254  154
     return new MelatiStringWriter();
 255  
   }
 256  
 
 257  
   /**
 258  
    * Get the underlying engine.
 259  
    * 
 260  
    * @return null - for velocity there is none.
 261  
    */
 262  
   public Object getEngine() {
 263  0
     return null;
 264  
   }
 265  
   
 266  
 }