jvanzyl     01/05/22 14:02:47

  Modified:    src/java/org/apache/turbine/services/jsp
                        TurbineJspService.java
  Log:
  - adding patch by david polito to allow multiple template paths
    to be used in the JSP service. thanks!
  
  Revision  Changes    Path
  1.15      +65 -15    
jakarta-turbine/src/java/org/apache/turbine/services/jsp/TurbineJspService.java
  
  Index: TurbineJspService.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/jsp/TurbineJspService.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- TurbineJspService.java    2001/04/10 21:31:44     1.14
  +++ TurbineJspService.java    2001/05/22 21:02:42     1.15
  @@ -89,8 +89,11 @@
   public class TurbineJspService extends BaseTemplateEngineService
       implements JspService
   {
  -    /** The base path prepended to filenames given in arguments */
  -    private String[] paths;
  +    /** The base path[s] prepended to filenames given in arguments */
  +    private String[] templatePaths;
  +    
  +    /** The relative path[s] prepended to filenames */
  +    private String[] relativeTemplatePaths;
   
       /** The buffer size for the output stream. */
       private int bufferSize;
  @@ -161,9 +164,18 @@
       public void handleRequest(RunData data, String filename, boolean isForward) 
           throws TurbineException
       {                                
  +        /** template name with relative path */
  +        String relativeTemplateName = getRelativeTemplateName(filename);
  +        
  +        if (relativeTemplateName == null)
  +        {
  +            throw new TurbineException(
  +            "Template " + filename + " not found in template paths");
  +        }
  +        
           // get the RequestDispatcher for the JSP
           RequestDispatcher dispatcher = data.getServletContext()
  -            .getRequestDispatcher(paths[0] + filename);
  +        .getRequestDispatcher(relativeTemplateName);
           
           try
           {
  @@ -211,19 +223,25 @@
            * Use the turbine template service to translate
            * the template paths.
            */
  -        paths = TurbineTemplate.translateTemplatePaths(
  -            config.getStringArray("templates"));
  +        templatePaths = TurbineTemplate.translateTemplatePaths(
  +        config.getStringArray("templates"));
           
  -        /* not working properly - removing until I can investigate (jdm)
  -        if (path.startsWith("/"))
  -        {
  -            path = path.substring(1);
  -        }
  -        if (path.length() > 0 && !path.endsWith("/"))
  +        /*
  +         * Set relative paths from config.
  +         * Needed for javax.servlet.RequestDispatcher
  +         */
  +        relativeTemplatePaths = config.getStringArray("templates");
  +        
  +        /*
  +         * Make sure that the relative paths begin with /
  +         */
  +        for (int i = 0; i < relativeTemplatePaths.length; i++)
           {
  -            path += "/";
  +            if (!relativeTemplatePaths[i].startsWith("/"))
  +            {
  +                relativeTemplatePaths[i] = "/" + relativeTemplatePaths[i];
  +            }
           }
  -        */
           
           bufferSize = config.getInt("buffer.size", 8192);
       
  @@ -244,6 +262,38 @@
        */
       public boolean templateExists(String template)
       {
  -        return TurbineTemplate.templateExists(template, paths);
  -    }              
  +        return TurbineTemplate.templateExists(template, templatePaths);
  +    } 
  +    /**
  +     * Searchs for a template in the default.template path[s] and
  +     * returns the template name with a relative path which is
  +     * required by <a 
href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletContext.html#getRequestDispatcher(java.lang.String)">
  +     * javax.servlet.RequestDispatcher</a>
  +     *
  +     * @param String template
  +     * @return String
  +     */
  +
  +    public String getRelativeTemplateName(String template)
  +    {
  +        /* 
  +         * A dummy String[] object used to pass a String to
  +         * TurbineTemplate.templateExists
  +         */
  +        String[] testTemplatePath = new String[1];
  +        
  +        /** 
  +         * Find which template path the template is in 
  +         */
  +        for (int i = 0; i < relativeTemplatePaths.length; i++)
  +        {
  +            testTemplatePath[0] = 
TurbineServlet.getRealPath(relativeTemplatePaths[i]);
  +            if (TurbineTemplate.templateExists(template, testTemplatePath))
  +            {
  +                return relativeTemplatePaths[i] + template;
  +            }
  +        }
  +        
  +        return null;
  +    }             
   }
  
  
  

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

Reply via email to