You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jv...@apache.org on 2001/07/12 22:22:59 UTC

cvs commit: jakarta-turbine/src/java/org/apache/turbine/pipeline Renderer.java ClassicPipeline.java Resolver.java

jvanzyl     01/07/12 13:22:59

  Modified:    src/java/org/apache/turbine/pipeline ClassicPipeline.java
                        Resolver.java
  Added:       src/java/org/apache/turbine/pipeline Renderer.java
  Log:
  - more additions to pipeline and rendering subsystem.
  
  Revision  Changes    Path
  1.3       +53 -33    jakarta-turbine/src/java/org/apache/turbine/pipeline/ClassicPipeline.java
  
  Index: ClassicPipeline.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/pipeline/ClassicPipeline.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ClassicPipeline.java	2001/07/10 13:39:11	1.2
  +++ ClassicPipeline.java	2001/07/12 20:22:54	1.3
  @@ -59,8 +59,6 @@
   import org.apache.turbine.RunData;
   import org.apache.turbine.TemplateContext;
   import org.apache.turbine.modules.ModuleLoader;
  -import org.apache.turbine.modules.Screen;
  -import org.apache.turbine.modules.Layout;
   import org.apache.turbine.services.template.TurbineTemplate;
   
   /**
  @@ -96,44 +94,66 @@
       {
           TurbineTemplate.doBuildAfterAction(data);
       }
  -
  +    
  +    //!! Get rid of hard coding.
       public void execute(RunData data)
           throws Exception
       {       
  -        // Ask the Screen for its Layout and then execute the Layout.
  -        // The Screen can override the getLayout() method to re-define
  -        // the Layout depending on data passed in via the
  -        // data.parameters object.
  -        Screen screen = (Screen) Turbine.getModuleLoader().getModule(
  -            Turbine.SCREENS, data.getScreen());
  +        // The whole pipeline starts with the target template
  +        // specified by the 'template' paramter.
  +        StringBuffer sb = new StringBuffer();
  +        
  +        Resolver.parseTemplatePath(
  +            data.getParameters().getString("template"),sb);
           
  -        String layout = screen.getLayout(data);
  +        String target = sb.toString();
  +        data.setTarget(target);
   
  -        // If the Layout has been set to be null, attempt to execute
  -        // the Screen that has been defined.
  -        if ( layout != null )
  -        {
  -            TemplateContext context = TurbineTemplate.getTemplateContext( data );
  +        String targetModuleType = Turbine.getConfiguration().getString(
  +            "pipeline.default.targetModuleType");
               
  -            String l = Turbine.getModuleLoader().getModule(
  -                Turbine.LAYOUTS, layout).evaluate(data);
  -                
  -            context.put("layoutPlaceHolder", l);
  -
  -            data.getResponse().setLocale(data.getLocale());
  -            data.getResponse().setContentType(data.getContentType());
  +        String module = Resolver.getModule(targetModuleType, target);
  +        Turbine.getModuleLoader().getModule(targetModuleType, module).execute(data);
  +        
  +        // The execution of the module that corresponds to the requested
  +        // template may result in the target template being changed.
  +        // This happens for example when a user isn't logged in. The
  +        // template requested is '/Index.vm', but executing an Index
  +        // module might result in the target template being changed
  +        // to '/Login.vm'. We want to reset the target template value
  +        // here in case it has changed.
  +        target = data.getTarget();
  +        
  +        // With this target template we start by rendering
  +        // the appropriate layout template and everything
  +        // goes from there.
  +        
  +        // Need to execute the module that matches the template
  +        // if it exists.
  +        String layoutTemplate = Resolver.getTemplate("layouts", target);        
  +        
  +        data.getResponse().setLocale(data.getLocale());
  +        data.getResponse().setContentType(data.getContentType());
   
  -            // Now I think we have to find a page template based
  -            // on the content type. What other criterion could
  -            // we use. Maybe you might want to change it if
  -            // you were branding ...
  -            data.getOut().print(TurbineTemplate
  -                .handleRequest(context, "pages" + "/Default.vm"));
  -        }
  -        else
  -        {
  -            screen.execute(data);            
  -        }
  +        // Now I think we have to find a page template based
  +        // on the content type. What other criterion could
  +        // we use. Maybe you might want to change it if
  +        // you were branding ...
  +        Renderer r = new Renderer();        
  +        TemplateContext context = TurbineTemplate.getTemplateContext(data);
  +        context.put("renderer", r);
  +        context.put("template", target);
  +        
  +        //!! We should be able to use the renderer here, but we have
  +        // the normalize the way it's used. right now the renderer
  +        // is being used in templates and you specify the module type
  +        // but when you start the pipeline you have the path to the template
  +        // and the module type is already part of the path which
  +        // doesn't work in the renderer. You end up with something
  +        // like /layouts/layouts/Default.vm which obviously doesn't
  +        // work.
  +        //data.getOut().print(r.render("layouts", data, layoutTemplate));
  +        data.getOut().print(TurbineTemplate.handleRequest(context, layoutTemplate));
       }
       
       public void finished(RunData data)
  
  
  
  1.3       +129 -8    jakarta-turbine/src/java/org/apache/turbine/pipeline/Resolver.java
  
  Index: Resolver.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/pipeline/Resolver.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Resolver.java	2001/07/10 13:39:13	1.2
  +++ Resolver.java	2001/07/12 20:22:55	1.3
  @@ -62,6 +62,9 @@
   import java.util.List;
   import java.util.Iterator;
   
  +import org.apache.turbine.Turbine;
  +import org.apache.turbine.services.template.TurbineTemplate;
  +
   // Given a target template
   
   // 1. find all possible classes that could be used
  @@ -109,6 +112,58 @@
   public class Resolver
   {
       /**
  +     * Used for resolving top level rendering process.
  +     */
  +    public static String getTemplate(String moduleType, String targetTemplate)
  +        throws Exception
  +    {
  +        StringBuffer sb = new StringBuffer();
  +        int i = parseTemplatePath(targetTemplate, sb);
  +        Iterator j = getPossibleTemplates(sb.toString());
  +        
  +        while (j.hasNext())
  +        {
  +            String template = moduleType + "/" + (String) j.next();
  +            if (TurbineTemplate.templateExists(template))
  +            {
  +                return template;
  +            }                
  +        }                
  +    
  +        // We should have a default template type for
  +        // each corresponding module and a default extension.
  +        return moduleType + 
  +            Turbine.getConfiguration().getString("template.default") + "." +
  +                Turbine.getConfiguration().getString("template.default.extension");
  +    }
  +
  +    public static String getModule(String moduleType, String template)
  +        throws Exception
  +    {
  +        StringBuffer sb = new StringBuffer();
  +        int i = parseTemplatePath(template, sb);
  +        Iterator j = getPossibleModules(sb.toString());
  +        
  +        while (j.hasNext())
  +        {
  +            String module = (String) j.next();
  +            
  +            try
  +            {
  +                Turbine.getModuleLoader().getModule(moduleType, module);
  +                return module;
  +            }
  +            catch (Exception e)
  +            {
  +                // do nothing
  +            }
  +        }                
  +        
  +        // Return the default module of 'moduleType'
  +        return Turbine.getDefaultModule(moduleType);
  +    }
  +
  +    /**
        * Parse the template name collected from URL parameters or
        * template context to a path name. Double slashes are changed
        * into single ones and commas used as path delemiters in
  @@ -120,8 +175,7 @@
        * @return The index of the separator between the path and the name.
        * @exception Exception Malformed template name.
        */
  -    private static int parseTemplatePath(String template,
  -                                  StringBuffer buffer)
  +    public static int parseTemplatePath(String template,StringBuffer buffer)
           throws Exception
       {
           char c;
  @@ -172,7 +226,7 @@
        * @return The parsed module name.
        * @exception Exception, a generaic exception.
        */
  -    protected static Iterator getParsedModule(String template)
  +    protected static Iterator getPossibleModules(String template)
           throws Exception
       {
           List packages = new ArrayList();
  @@ -180,11 +234,13 @@
           // Parse the template name and change it into a package.
           StringBuffer pckage = new StringBuffer();
           int i = parseTemplatePath(template,pckage);
  +        
           if (pckage.charAt(0) == '/')
           {
               pckage.deleteCharAt(0);
               i--;
           }
  +        
           if (i >= 0)
           {
               for (int j = 0; j <= i; j++)
  @@ -206,7 +262,6 @@
               }
           }
           
  -        
           // Try first an exact match for a module having the same
           // name as the input template, traverse then upper level
           // packages to find a default module named Default.
  @@ -243,6 +298,66 @@
       }
   
       /**
  +     * Get the parsed module name for the specified template.
  +     * 
  +     * @param template The template name.
  +     * @param key The module type key.
  +     * @return The parsed module name.
  +     * @exception Exception, a generaic exception.
  +     */
  +    protected static Iterator getPossibleTemplates(String template)
  +        throws Exception
  +    {
  +        List packages = new ArrayList();
  +        
  +        // Parse the template name and change it into a package.
  +        StringBuffer pckage = new StringBuffer();
  +        int i = parseTemplatePath(template,pckage);
  +        
  +        if (pckage.charAt(0) == '/')
  +        {
  +            pckage.deleteCharAt(0);
  +            i--;
  +        }
  +        
  +        String extension = ".vm";
  +        
  +        // Try first an exact match for a module having the same
  +        // name as the input template, traverse then upper level
  +        // packages to find a default module named Default.
  +        int j = 9999;
  +        String module;
  +        while (j-- > 0)
  +        {
  +            module = pckage.toString();
  +            
  +            packages.add(module);
  +            
  +            pckage.setLength(i + 1);
  +            if (i > 0)
  +            {
  +                // We have still packages to traverse.
  +                for (i = pckage.length() - 2; i >= 0; i--)
  +                {
  +                    if (pckage.charAt(i) == '/')
  +                    { 
  +                        break;
  +                    }
  +                }
  +            }
  +            else if (j > 0)
  +            {
  +                // Only the main level left.
  +                j = 1;
  +            }
  +            pckage.append("Default").append(extension);
  +        }
  +
  +        // Not found, return the default module name.
  +        return packages.iterator();
  +    }
  +
  +    /**
        * Get the default module name of the template engine
        * service corresponding to the template name extension of 
        * the named template.
  @@ -260,13 +375,19 @@
           throws Exception
       {
           StringBuffer sb = new StringBuffer();
  -        int i = parseTemplatePath("/base,science,chemistry,,,Titanium.vm", sb);
  -        System.out.println(i);
  -        System.out.println(sb);
  -        Iterator j = getParsedModule(sb.toString());
  +        int i = parseTemplatePath("science,chemistry,Titanium.vm", sb);
  +        
  +        Iterator j = getPossibleModules(sb.toString());
           while (j.hasNext())
           {
               System.out.println(j.next());
           }                
  +    
  +        Iterator k = getPossibleTemplates(sb.toString());
  +        while (k.hasNext())
  +        {
  +            System.out.println(k.next());
  +        }                
  +    
       }
   }
  
  
  
  1.1                  jakarta-turbine/src/java/org/apache/turbine/pipeline/Renderer.java
  
  Index: Renderer.java
  ===================================================================
  package org.apache.turbine.pipeline;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.turbine.Turbine;
  import org.apache.turbine.RunData;
  import org.apache.turbine.util.TurbineException;
  import org.apache.turbine.services.template.TurbineTemplate;
  
  public class Renderer
  {
      public String render(String moduleType, RunData data, String template)
          throws TurbineException,Exception
      {
          // Now before the renderer does it's work, we must
          // look for the existence of a module that matches
          // this template. When we find it we have to
          // execute it before the rendering occurs.
          String module = Resolver.getModule(moduleType, template);
          Turbine.getModuleLoader().getModule(moduleType, module).execute(data);
  
          //!! I shouldn't need that slash in there.
          return TurbineTemplate.handleRequest(
              TurbineTemplate.getTemplateContext(data), moduleType + "/" + template);
      }
  }
  
  
  

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