jvanzyl     01/07/09 18:13:26

  Added:       src/java/org/apache/turbine/modules/actions
                        TemplateAction.java TemplateSecureAction.java
               src/java/org/apache/turbine/modules/layouts
                        TemplateLayout.java
               src/java/org/apache/turbine/modules/screens
                        TemplateSecureScreen.java
               src/java/org/apache/turbine/pipeline ClassicPipeline.java
                        Resolver.java
  Log:
  - adding the start of the pipeline code
  - some of the unified templating modules
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/modules/actions/TemplateAction.java
  
  Index: TemplateAction.java
  ===================================================================
  package org.apache.turbine.modules.actions;
  
  /* ====================================================================
   * 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 [EMAIL PROTECTED]
   *
   * 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.RunData;
  import org.apache.turbine.TemplateContext;
  
  import java.lang.reflect.Method;
  import java.util.Enumeration;
  import org.apache.turbine.modules.ActionEvent;
  import org.apache.turbine.modules.screens.TemplateScreen;
  import org.apache.turbine.services.template.TurbineTemplate;
  import org.apache.turbine.util.ParameterParser;
  import org.apache.turbine.util.ValueParser;
  import org.apache.turbine.util.template.TemplateInfo;
  
  /**
   * If you are using VelocitySite stuff, then your Action's should
   * extend this class instead of extending the ActionEvent class.  The
   * difference between this class and the ActionEvent class is that
   * this class will first attempt to execute one of your doMethod's
   * with a constructor like this:
   *
   * <p>doEvent(RunData data, Context context)
   *
   * <p>It gets the context from the TemplateInfo.getTemplateContext()
   * method. If it can't find a method like that, then it will try to
   * execute the method without the Context in it.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jon S. Stevens</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: TemplateAction.java,v 1.1 2001/07/10 01:13:22 jvanzyl Exp $
   */
  public abstract class TemplateAction 
      extends ActionEvent
  {
      /**
       * You need to implement this in your classes that extend this
       * class.
       *
       * @param data A Turbine RunData object.
       * @exception Exception, a generic exception.
       */
      public void doPerform(RunData data)
          throws Exception
      {
      }
  
      public void doPerform(RunData data, TemplateContext context)
          throws Exception
      {
      }
  
      /**
       * This overrides the default Action.perform() to execute the
       * doEvent() method.  If that fails, then it will execute the
       * doPerform() method instead.
       *
       * @param data A Turbine RunData object.
       * @exception Exception, a generic exception.
       */
      protected void perform( RunData data )
          throws Exception
      {
          try
          {
              executeEvents(data, getTemplateContext(data));
          }
          catch (NoSuchMethodException e)
          {
              doPerform( data );
          }
      }
  
      /**
       * This method should be called to execute the event based system.
       *
       * @param data A Turbine RunData object.
       * @param context Velocity context information.
       * @exception Exception, a generic exception.
       */
      public void executeEvents(RunData data, TemplateContext context)
          throws Exception
      {
          // Name of the button.
          String theButton = null;
  
          // ParameterParser.
          ParameterParser pp = data.getParameters();
  
          String button = pp.convert(BUTTON);
  
          // Loop through and find the button.
          for (Enumeration e = pp.keys() ; e.hasMoreElements() ;)
          {
              String key = (String) e.nextElement();
              if (key.startsWith(button))
              {
                  theButton = formatString(key);
                  break;
              }
          }
  
          if (theButton == null)
          {
              throw new NoSuchMethodException(
                  "ActionEvent: The button was null");
          }                
  
          try
          {
              // The arguments to the method to find.
              Class[] classes = new Class[2];
              classes[0] = RunData.class;
              classes[1] = TemplateContext.class;
  
              // The arguments to pass to the method to execute.
              Object[] args = new Object[2];
  
              Method method = getClass().getMethod(theButton, classes);
              args[0] = data;
              args[1] = context;
              method.invoke(this, args );
          }
          catch (NoSuchMethodException nsme)
          {
              // Attempt to execut things the old way..
              super.executeEvents(data);
          }
      }
  
      /**
       * This method is used when you want to short circuit an Action
       * and change the template that will be executed next.
       *
       * @param data Turbine information.
       * @param template The template that will be executed next.
       */
      public void setTemplate(RunData data,
                              String template)
      {
          TemplateScreen.setTemplate(data, template);
      }
  
      /**
       * Return the Context needed by Velocity.
       *
       * @param RunData data
       * @return Context, a context for web pages.
       */
      protected org.apache.turbine.TemplateContext getTemplateContext(RunData data)
      {
          return (org.apache.turbine.TemplateContext)
              TurbineTemplate.getTemplateContext(data);
      }
  }
  
  
  
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/modules/actions/TemplateSecureAction.java
  
  Index: TemplateSecureAction.java
  ===================================================================
  package org.apache.turbine.modules.actions;
  
  /* ====================================================================
   * 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 [EMAIL PROTECTED]
   *
   * 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.RunData;
  import org.apache.turbine.TemplateContext;
  
  /**
   * VelocitySecure action.
   *
   * Always performs a Security Check that you've defined before
   * executing the doBuildtemplate().  You should extend this class and
   * add the specific security check needed.  If you have a number of
   * screens that need to perform the same check, you could make a base
   * screen by extending this class and implementing the isAuthorized().
   * Then each action that needs to perform the same check could extend
   * your base action.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Dave Bryson</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jon S. Stevens</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: TemplateSecureAction.java,v 1.1 2001/07/10 01:13:23 jvanzyl Exp $
   */
  public abstract class TemplateSecureAction 
      extends TemplateAction
  {
      /**
       * This method overrides the method in WebMacroSiteAction to
       * perform a security check first.
       *
       * @param data Turbine information.
       * @exception Exception, a generic exception.
       */
      protected void perform( RunData data ) throws Exception
      {
          if ( isAuthorized( data ) )
          {
              super.perform(data);
          }
      }
  
      /**
       * Implement this method to perform the security check needed.
       * You should set the template in this method that you want the
       * user to be sent to if they're unauthorized.  See the
       * WebMacroSecurityCheck utility.
       *
       * @param data Turbine information.
       * @return True if the user is authorized to access the screen.
       * @exception Exception, a generic exception.
       */
      protected abstract boolean isAuthorized( RunData data )
          throws Exception;
  }
  
  
  
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/modules/layouts/TemplateLayout.java
  
  Index: TemplateLayout.java
  ===================================================================
  package org.apache.turbine.modules.layouts;
  
  /* ====================================================================
   * 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 [EMAIL PROTECTED]
   *
   * 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.modules.Layout;
  import org.apache.turbine.Turbine;
  import org.apache.turbine.services.template.TurbineTemplate;
  import org.apache.turbine.TemplateContext;
  import org.apache.turbine.RunData;
  import org.apache.turbine.util.template.TemplateNavigation;
  
  /**
   */
  public class TemplateLayout 
      extends Layout
  {
      /**
       * Method called by LayoutLoader.
       *
       * @param RunData
       * @return processed template in a String
       */
      public String doBuild( RunData data ) throws Exception
      {
          // This doesn't make it easy to add new module types.
          //TurbineTemplate.doBuildLayout(data);
          
          // Get the context needed for template rendering.
          TemplateContext context = TurbineTemplate.getTemplateContext( data );
  
          String returnValue = "";
  
          // First, generate the screen and put it in the context so
          // we can grab it the layout template.
          returnValue = Turbine.getModuleLoader().getModule(
              Turbine.SCREENS, data.getScreen()).evaluate(data);
  
          // variable for the screen in the layout template
          context.put("screen_placeholder", returnValue);
  
           // Variable to reference the navigation screen in the layout
           // template.
          context.put("navigation", new TemplateNavigation( data ));
  
          // Grab the layout template set in the TemplatePage.
          // If null, then use the default layout template 
          // (done by the TemplateInfo object )
          String templateName = data.getTemplateInfo().getLayoutTemplate();
  
          return TurbineTemplate
              .handleRequest(context, "layouts" + templateName);
      }
  }
  
  
  
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/modules/screens/TemplateSecureScreen.java
  
  Index: TemplateSecureScreen.java
  ===================================================================
  package org.apache.turbine.modules.screens;
  
  /* ====================================================================
   * 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 [EMAIL PROTECTED]
   *
   * 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.RunData;
  import org.apache.turbine.services.template.TurbineTemplate;
  import org.apache.turbine.TemplateContext;
  
  /**
   * VelocitySecureScreen
   *
   * Always performs a Security Check that you've defined before
   * executing the doBuildtemplate().  You should extend this class and
   * add the specific security check needed.  If you have a number of
   * screens that need to perform the same check, you could make a base
   * screen by extending this class and implementing the isAuthorized().
   * Then each screen that needs to perform the same check could extend
   * your base screen.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Dave Bryson</a>
   * @version $Id: TemplateSecureScreen.java,v 1.1 2001/07/10 01:13:25 jvanzyl Exp $
   */
  public abstract class TemplateSecureScreen 
      extends TemplateScreen
  {
      /**
       * Implement this to add information to the context.
       *
       * @param data Turbine information.
       * @param context Context for web pages.
       * @exception Exception, a generic exception.
       */
      protected abstract void doBuildTemplate( RunData data,
                                               TemplateContext context )
          throws Exception;
  
      /**
       * This method overrides the method in WebMacroSiteScreen to
       * perform a security check first.
       *
       * @param data Turbine information.
       * @exception Exception, a generic exception.
       */
      protected void doBuildTemplate( RunData data ) throws Exception
      {
          if ( isAuthorized( data ) )
          {
              doBuildTemplate( data, TurbineTemplate.getTemplateContext( data ) );
          }
      }
  
      /**
       * Implement this method to perform the security check needed.
       * You should set the template in this method that you want the
       * user to be sent to if they're unauthorized. See the
       * WebMacroSecuritCheck utility.
       *
       * @param data Turbine information.
       * @return True if the user is authorized to access the screen.
       * @exception Exception, a generic exception.
       */
      protected abstract boolean isAuthorized( RunData data )
          throws Exception;
  }
  
  
  
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/pipeline/ClassicPipeline.java
  
  Index: ClassicPipeline.java
  ===================================================================
  package org.apache.turbine.pipeline;
  
  import org.apache.turbine.Turbine;
  import org.apache.turbine.Pipeline;
  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;
  
  /**
   * This is a pipeline that emulates the standard Turbine
   * 2.1 page/layout/navigation/screen pipeline.
   *
   * The ClassicPipeline uses the 'template' as a
   * starting point for output.
   */
  public class ClassicPipeline
      implements Pipeline
  {
      public void preExecuteAction(RunData data)
          throws Exception
      {
          TurbineTemplate.doBuildBeforeAction(data);
      }
  
      public void executeAction(RunData data)
          throws Exception
      {
          // If an action has been defined, execute it here.  Actions
          // can re-define the template definition.
          if (data.hasAction())
          {
              Turbine.getModuleLoader().getModule(
                  Turbine.ACTIONS, data.getAction()).execute(data);
          }
      }
  
      public void postExecuteAction(RunData data)
          throws Exception
      {
          TurbineTemplate.doBuildAfterAction(data);
      }
  
      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());
          
          String layout = screen.getLayout(data);
  
          // 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 l = Turbine.getModuleLoader().getModule(
                  Turbine.LAYOUTS, layout).evaluate(data);
                  
              context.put("layoutPlaceHolder", l);
  
              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);            
          }
      }
      
      public void finished(RunData data)
          throws Exception
      {
          TurbineTemplate.doPostBuild(data);
      }
  }
  
  
  
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/pipeline/Resolver.java
  
  Index: Resolver.java
  ===================================================================
  package org.apache.turbine.pipeline;
  
  // Renderers and context builders
  // We need to match up the target template with sibling
  // templates and context builders for each of these templates.
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Iterator;
  
  // Given a target template
  
  // 1. find all possible classes that could be used
  //    as context builders
  // 2. find all possible sibling templates that could
  //    be used with the target template
  
  // We are using the classic turbine method of using
  // a screen template.
  
  // Say we have a base template of:
  //
  // /base/science/chemistry/Titanium.vm
  //
  // 1. We want to find the context builder to this
  // target template if there is one, there won't
  // be if we're using pull.
  //
  // 2. Find the layout/nav templates to use and find
  //    their context builders.
  
  public class Resolver
  {
      /**
       * 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
       * URL parameters are changed into slashes. Empty names or
       * names without a file part are not accepted.
       * 
       * @param template The template name.
       * @param buffer A buffer for the result.
       * @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)
          throws Exception
      {
          char c;
          int j = 0;
          int ind = -1;
          buffer.setLength(0);
          buffer.append(template);
          int len = buffer.length();
          while (j < len)
          {
              c = buffer.charAt(j);
              if (c == ',')
              {
                  c = '/';
                  buffer.setCharAt(j,c);
              }
              if (c == '/')
              {
                  ind = j;
                  if (j < (len - 1))
                  {
                      c = buffer.charAt(j + 1);
                      if ((c == '/') ||
                          (c == ','))
                      {
                          buffer.deleteCharAt(j);
                          len--;
                          continue;
                      }
                  }
              }
              j++;
          }
          if ((len == 0) ||
              (ind >= (len - 1)))
          {
              throw new Exception(
                  "Syntax error in template name '" + template + '\'');
          }
          return ind;
      }
  
      /**
       * 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 getParsedModule(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--;
          }
          if (i >= 0)
          {
              for (int j = 0; j <= i; j++)
              {
                  if (pckage.charAt(j) == '/')
                  {
                      pckage.setCharAt(j,'.');
                  }
              }
          }
    
          // Remove a possible file extension.
          for (int j = i + 1; j < pckage.length(); j++)
          {
              if (pckage.charAt(j) == '.')
              {
                  pckage.delete(j,pckage.length());
                  break;
              }
          }
          
          
          // 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");
          }
  
          // 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.
       *
       * @param template The template name.
       * @param key The module type key.
       * @return The default page module name.
       */
      protected static String getDefaultModule(String template, int key)
      {
          return "default";
      }
  
      public static void main(String[] args)
          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());
          while (j.hasNext())
          {
              System.out.println(j.next());
          }                
      }
  }
  
  
  

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

Reply via email to