nbubna      2003/03/06 19:44:39

  Modified:    src/java/org/apache/velocity/tools/view/servlet
                        WebappLoader.java
  Log:
  Add support for optional <name>.resource.loader.path property
  (let people specify a base path for templates relative to webapp root)
  
  Revision  Changes    Path
  1.2       +34 -8     
jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/servlet/WebappLoader.java
  
  Index: WebappLoader.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity-tools/src/java/org/apache/velocity/tools/view/servlet/WebappLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WebappLoader.java 5 Mar 2003 06:13:03 -0000       1.1
  +++ WebappLoader.java 7 Mar 2003 03:44:38 -0000       1.2
  @@ -58,23 +58,27 @@
   
   import java.io.InputStream;
   
  +import org.apache.commons.collections.ExtendedProperties;
  +
  +import org.apache.velocity.exception.ResourceNotFoundException;
   import org.apache.velocity.runtime.RuntimeServices;
   import org.apache.velocity.runtime.resource.Resource;
   import org.apache.velocity.runtime.resource.loader.ResourceLoader;
   
  -import org.apache.velocity.exception.ResourceNotFoundException;
  -
   import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
   
  -import org.apache.commons.collections.ExtendedProperties;
  -
   /**
    *  
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">Nathan Bubna</a>
    * @version $Id$
    */
   public class WebappLoader extends ResourceLoader
   {
  +
  +    /** The root path for templates (relative to webapp's root). */
  +    protected String path = null;
  +
       protected ServletContext servletContext = null;
   
       /**
  @@ -83,15 +87,33 @@
       public void init( ExtendedProperties configuration)
       {
           rsvc.info("WebappLoader : initialization starting.");
  +        
  +        // get custom default path
  +        path = configuration.getString("path");
  +        if (path == null || path.length() == 0)
  +        {
  +            path = "/";
  +        }
  +        else
  +        {
  +            // make sure the path ends with a '/'
  +            if (!path.endsWith("/"))
  +            {
  +                path += '/';
  +            }
  +            rsvc.info("WebappLoader : template path (relative to webapp root) is '" 
+ path + "'");
  +        }
   
  +        // get the ServletContext
           Object obj = 
rsvc.getApplicationAttribute(VelocityViewServlet.SERVLET_CONTEXT_KEY);
  -
           if (obj instanceof ServletContext)
           {
               servletContext = (ServletContext)obj;
           }
           else
  +        {
               rsvc.error("WebappLoader : unable to retrieve ServletContext");
  +        }
   
           rsvc.info("WebappLoader : initialization complete.");
       }
  @@ -117,10 +139,14 @@
           
           try 
           {
  -            if (!name.startsWith("/"))
  -                name = "/" + name;
  +            // since the path always ends in '/',
  +            // make sure the name never ends in one
  +            if (name.startsWith("/"))
  +            {
  +                name = name.substring(1);
  +            }
   
  -            result = servletContext.getResourceAsStream( name );
  +            result = servletContext.getResourceAsStream(path + name);
           }
           catch( Exception fnfe )
           {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to