You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by il...@apache.org on 2001/06/05 17:35:20 UTC

cvs commit: jakarta-turbine/src/java/org/apache/turbine/services/velocity TurbineVelocityService.java

ilkka       01/06/05 08:35:20

  Modified:    src/java/org/apache/turbine/services/velocity
                        TurbineVelocityService.java
  Log:
  Added support for features included in Velocity 1.1. These would have been nice to have in Turbine 2.1 also, but somehow I missed the point where Velocity 1.2-dev  was taken into use (BTW why Velocity 1.1 was skipped by moving from 1.0 to 1.2-dev???).
  
  1) Support for defining  the template encoding during run-time based on the RunData.getTemplateEncoding() method
  2) Translations of other than the default file resource loader template paths to webapp space also (or should we allow resource loaders using absolute paths, which is quite unsecure?)
  3) Translation of local jar resource loader template paths to webapp space making jars a nice alternative to distribute templates instead of the template directory
  
  Revision  Changes    Path
  1.47      +76 -26    jakarta-turbine/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java
  
  Index: TurbineVelocityService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java,v
  retrieving revision 1.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- TurbineVelocityService.java	2001/05/26 00:57:37	1.46
  +++ TurbineVelocityService.java	2001/06/05 15:35:19	1.47
  @@ -56,6 +56,7 @@
   
   import java.util.Vector;
   
  +import java.util.Iterator;
   import java.io.IOException;
   import java.io.OutputStream;
   import java.io.OutputStreamWriter;
  @@ -101,12 +102,18 @@
    * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
    * @author <a href="mailto:sean@informage.ent">Sean Legassick</a>
    * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  - * @version $Id: TurbineVelocityService.java,v 1.46 2001/05/26 00:57:37 dlr Exp $
  + * @version $Id: TurbineVelocityService.java,v 1.47 2001/06/05 15:35:19 ilkka Exp $
    */
   public class TurbineVelocityService extends BaseTemplateEngineService
       implements VelocityService
   {
       /**
  +     * The generic resource loader path property in velocity.
  +     */
  +    private static final String RESOURCE_LOADER_PATH =
  +      ".resource.loader.path";
  +  
  +    /**
        * Default character encoding to use if not specified in the RunData object.
        */
       private String defaultCharSet = "ISO-8859-1";
  @@ -343,8 +350,7 @@
                   /*
                    * Request based encoding is supported in Velocity 1.1.
                    */
  -                // Velocity.mergeTemplate(filename, encoding, context, writer);
  -                Velocity.mergeTemplate(filename, context, writer);
  +                Velocity.mergeTemplate(filename, encoding, context, writer);
               }
               else
               {
  @@ -409,16 +415,16 @@
            * Now we have to perform a couple of path translations
            * for our log file and template paths.
            */
  -        String logFile = TurbineServlet.getRealPath
  +        String path = TurbineServlet.getRealPath
               (configuration.getString(Velocity.RUNTIME_LOG, null));
  -        if (StringUtils.isValid(logFile))
  +        if (StringUtils.isValid(path))
           {
  -            configuration.setProperty(Velocity.RUNTIME_LOG, logFile);
  +            configuration.setProperty(Velocity.RUNTIME_LOG, path);
           }
           else
           {
               String msg = VelocityService.SERVICE_NAME + " runtime log file " +
  -                "is misconfigured: '" + logFile + "' is not a valid log file";
  +                "is misconfigured: '" + path + "' is not a valid log file";
               if (TurbineServlet.getServletConfig() instanceof TurbineConfig)
               {
                   msg += ": TurbineConfig users must use a path relative to " +
  @@ -429,29 +435,73 @@
   
           /*
            * Get all the template paths where the velocity
  -         * runtime should search for templates.
  +         * runtime should search for templates and
  +         * collect them into a separate vector
  +         * to avoid concurrent modification exceptions.
            */
  -        Vector templatePaths = configuration.getVector(
  -            Velocity.FILE_RESOURCE_LOADER_PATH);
  +        String key;
  +        Vector keys = new Vector();
  +        for (Iterator i = configuration.getKeys(); i.hasNext();)
  +        {
  +            key = (String) i.next();
  +            if (key.endsWith(RESOURCE_LOADER_PATH))
  +            {
  +                keys.add(key);
  +            }
  +        }
   
           /*
  -         * Clear the configuration for the template paths, we
  -         * want to translate them all to the webapp space.
  +         * Loop through all template paths, clear the corresponding
  +         * velocity properties and translate them all to the webapp space.
            */
  -        Velocity.clearProperty(Velocity.FILE_RESOURCE_LOADER_PATH);
  -        configuration.clearProperty(Velocity.FILE_RESOURCE_LOADER_PATH);
  -        
  -        int pathsToProcess = templatePaths.size();
  -        
  -        // If I use this line I get an endless loop?
  -        // What the hell is that? Is that a bug with
  -        // Vector used in conjunction with a for loop.
  -        //for (int i = 0; i < templatePaths.size(); i++)
  -        
  -        for (int i = 0; i < pathsToProcess; i++)
  -        {
  -            configuration.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
  -                TurbineServlet.getRealPath((String) templatePaths.get(i)));
  +        int ind;
  +        Vector paths;
  +        String entry;
  +        for (Iterator i = keys.iterator(); i.hasNext();)
  +        {
  +            key = (String) i.next();
  +            paths = configuration.getVector(key,null);
  +            if (paths != null)
  +            {
  +                Velocity.clearProperty(key);
  +                configuration.clearProperty(key);
  +
  +                for (Iterator j = paths.iterator(); j.hasNext();)
  +                {
  +                    path = (String) j.next();
  +                    if (path.startsWith("jar:file"))
  +                    {
  +                        /*
  +                         * A local jar resource URL path is a bit more
  +                         * complicated, but we can translate it as well.
  +                         */
  +                        ind = path.indexOf("!/");
  +                        if (ind >= 0)
  +                        {
  +                            entry = path.substring(ind);
  +                            path = path.substring(9,ind);
  +                        }
  +                        else
  +                        {
  +                            entry = "!/";
  +                            path = path.substring(9);
  +                        }
  +                        path = "jar:file:" +
  +                            TurbineServlet.getRealPath(path) + entry;
  +                    }
  +                    else if (!path.startsWith("jar:"))
  +                    {
  +                        /*
  +                         * But we don't translate remote jar URLs.
  +                         */
  +                        path = TurbineServlet.getRealPath(path);
  +                    }
  +                    /*
  +                     *  Put the translated paths back to the configuration.
  +                     */
  +                    configuration.addProperty(key,path);
  +                }
  +            }
           }
           
           try
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org