View Javadoc

1   package org.apache.commons.jelly.tags.velocity;
2   
3   /*
4    * Copyright 2001,2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.commons.jelly.JellyTagException;
20  import org.apache.commons.jelly.TagSupport;
21  import org.apache.velocity.app.VelocityEngine;
22  
23  /***
24   * Support methods for the Velocity tag library.  Currently this is only
25   * used to get an instance of the VelocityEngine.  For each unique base
26   * directory specified, a new VelocityEngine instance is stored in the
27   * context (as the author hasn't figured out how to change the resource
28   * loader of an already init'd VelocityEngine).
29   *
30   * @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
31   * @version $Id: VelocityTagSupport.java 155420 2005-02-26 13:06:03Z dirkv $
32   */
33  public abstract class VelocityTagSupport extends TagSupport
34  {
35      /*** The VelocityEngine variable name in the JellyContext.  */
36      public static final String VELOCITY_ENGINE_VAR_NAME =
37              "org.apache.maven.jelly.tags.velocity.VelocityEngine";
38  
39      /***
40       * Gets or creates a VelocityEngine if one doesn't already exist for
41       * the specified base directory.
42       *
43       * @return A VelocityEngine with a file resource loader configured
44       * for the specified base directory.
45       */
46      public VelocityEngine getVelocityEngine( String basedir ) throws JellyTagException
47      {
48          VelocityEngine ve = ( VelocityEngine ) getContext().getVariable(
49                  keyName( basedir ) );
50  
51          if ( ve == null )
52          {
53              ve = new VelocityEngine();
54              ve.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this );
55              ve.setProperty( VelocityEngine.FILE_RESOURCE_LOADER_PATH, basedir );
56  
57              try {
58                  ve.init();
59              }
60              catch (Exception e) {
61                  throw new JellyTagException(e);
62              }
63  
64              getContext().setVariable( keyName( basedir ), ve );
65          }
66  
67          return ve;
68      }
69  
70      /***
71       * Constructs the name of the key used to reference the
72       * VelocityEngine for the specified base directory.
73       *
74       * @param basedir The base directory used by the VelocityEngine
75       * @return The key used to reference the VelocityEngine that has
76       * been initialized with the specified base directory.
77       */
78      private String keyName( String basedir )
79      {
80          return new StringBuffer()
81              .append( VELOCITY_ENGINE_VAR_NAME )
82              .append( '.' )
83              .append( basedir )
84              .toString();
85      }
86  }