jvanzyl     01/08/05 09:03:57

  Added:       src/services/java/org/apache/fulcrum/template
                        BaseTemplateEngineService.java
                        DefaultTemplateContext.java TemplateContext.java
                        TemplateEngineService.java TemplateService.java
                        TurbineTemplate.java TurbineTemplateService.java
  Log:
  - decoupled from turbine
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/template/BaseTemplateEngineService.java
  
  Index: BaseTemplateEngineService.java
  ===================================================================
  package org.apache.fulcrum.template;
  
  /* ====================================================================
   * 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 java.io.OutputStream;
  import java.util.Iterator;
  import java.util.Hashtable;
  import org.apache.fulcrum.BaseService;
  import org.apache.fulcrum.ServiceException;
  
  /**
   * The base implementation of Turbine {@link
   * org.apache.fulcrum.template.TemplateEngineService}.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: BaseTemplateEngineService.java,v 1.1 2001/08/05 16:03:57 jvanzyl 
Exp $
   */
  public abstract class BaseTemplateEngineService 
      extends BaseService
      implements TemplateEngineService
  {
      /**
       * A Map containing the configuration for the template
       * engine service. The configuration contains:
       *
       * 1) template extensions
       * 2) default page
       * 3) default screen
       * 4) default layout
       * 5) default navigation
       * 6) default error screen
       */
      private Hashtable configuration = new Hashtable();
  
      /**
       * @see org.apache.fulcrum.template.TemplateEngineService#registerConfiguration
       */
      public void registerConfiguration(String defaultExt)
      {
          initConfiguration(defaultExt);
          TurbineTemplate.registerTemplateEngineService(this);
      }
  
      /**
       * @see 
org.apache.fulcrum.template.TemplateEngineService#getTemplateEngineServiceConfiguration
       */
      public Hashtable getTemplateEngineServiceConfiguration()
      {
          return configuration;
      }                
  
      /**
       * @see 
org.apache.fulcrum.template.TemplateEngineService#getAssociatedFileExtensions
       */
      public String[] getAssociatedFileExtensions()
      {
          return (String []) configuration.get(TEMPLATE_EXTENSIONS);
      }
  
      /**
       * Note engine file extension associations.  First attempts to
       * pull a list of custom extensions from the property file value
       * keyed by <code>template.extension</code>.  If none are defined,
       * uses the value keyed by
       * <code>template.default.extension</code>, defaulting to the
       * emergency value supplied by <code>defaultExt</code>.
       *
       * @param defaultExt The default used when the default defined in the
       *                   properties file is missing or misconfigured.
       */
      protected void initConfiguration(String defaultExt)
      {
          /*
           * Should modify the configuration class to take defaults
           * here, should have to do this.
           */
          String[] fileExtensionAssociations =
              getConfiguration().getStringArray(TEMPLATE_EXTENSIONS);
  
          if (fileExtensionAssociations == null ||
              fileExtensionAssociations.length == 0)
          {
              fileExtensionAssociations = new String[1];
              fileExtensionAssociations[0] = getConfiguration().getString(
                  DEFAULT_TEMPLATE_EXTENSION, defaultExt);
          }
  
          configuration.put(TEMPLATE_EXTENSIONS, fileExtensionAssociations); 
  
          configuration.put(DEFAULT_PAGE_TEMPLATE, 
              getConfiguration().getString(DEFAULT_PAGE_TEMPLATE));
  
          configuration.put(DEFAULT_LAYOUT_TEMPLATE, 
              getConfiguration().getString(DEFAULT_LAYOUT_TEMPLATE));
  
      }
  
      /**
       * @see org.apache.fulcrum.template.TemplateEngineService#templateExists
       */
      public abstract boolean templateExists(String template);
  
      public abstract String handleRequest(TemplateContext context,
                                           String template)
          throws ServiceException;
  
      public abstract void handleRequest(TemplateContext context,
                                         String template, OutputStream os)
          throws ServiceException;
  }
  
  
  
  1.1                  
jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/template/DefaultTemplateContext.java
  
  Index: DefaultTemplateContext.java
  ===================================================================
  package org.apache.fulcrum.template;
  
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" 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"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 java.util.HashMap;
  import java.util.List;
  import java.util.Iterator;
  
  /**
   *  General purpose implemention of the application Context 
   *  interface for general application use.  This class should 
   *  be used in place of the original Context class.
   *
   *  This implementation uses a HashMap  (@see java.util.HashMap ) 
   *  for data storage.
   *
   *  This context implementation cannot be shared between threads
   *  without those threads synchronizing access between them, as 
   *  the HashMap is not synchronized, nor are some of the fundamentals
   *  of AbstractContext.  If you need to share a Context between 
   *  threads with simultaneous access for some reason, please create 
   *  your own and extend the interface Context 
   *  
   *  @see org.apache.velocity.context.Context
   *
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   *  @author <a href="mailto:[EMAIL PROTECTED]";>Fedor Karpelevitch</a>
   *  @version $Id: DefaultTemplateContext.java,v 1.1 2001/08/05 16:03:57 jvanzyl Exp $
   */
  public class DefaultTemplateContext
      implements TemplateContext
  {
      TemplateContext innerContext;
  
      /**
       *  storage for key/value pairs 
       */
      private HashMap context = new HashMap();
  
      /** 
       * default contructor, does nothing 
       * interesting
       */
      public DefaultTemplateContext()
      {
      }
      
      /**
       * Allow chained contexts.
       */
      public DefaultTemplateContext(TemplateContext context)
      {
          super();
          
          //!! I realize this is not the most efficient
          // way to do this, but I'm not sure if chained
          // contexts can work with templating solutions
          // other than velocity. I don't see why not right
          // of the bat, but this will work for now.
          
          Object[] keys = context.getKeys();
          
          for (int i = 0; i < keys.length; i++)
          {
              put((String) keys[i], context.get((String)keys[i]));
          }            
      }
  
      public void put(String key, Object value)
      {
          context.put(key, value);
      }
  
      public Object get(String key)
      {   
          return context.get(key);
      }        
  
      public Object remove(Object key)
      {
          return context.remove(key);
      }
  
      public boolean containsKey(Object key)
      {
          return context.containsKey(key);
      }
  
      public Object[] getKeys()
      {
          return context.keySet().toArray();
      }
  }
  
  
  
  
  1.1                  
jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/template/TemplateContext.java
  
  Index: TemplateContext.java
  ===================================================================
  package org.apache.fulcrum.template;
  
  /*
   * 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" 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"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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/>.
   */
  
  /**
   * A generic template context interface which can be employed to wrap
   * and/or extend context classes from template engines plugged into
   * Fulcrum.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   */
  public interface TemplateContext
  {
      /**
       * Adds a name/value pair to the context.
       *
       * @param key   The name to key the provided value with.
       * @param value The corresponding value.
       */
      public void put(String key, Object value);
  
      /**
       * Gets the value corresponding to the provided key from the context.
       *
       * @param key The name of the desired value.
       * @return    The value corresponding to the provided key.
       */
      public Object get(String key);
   
      /**
       * Indicates whether the specified key is in the context.
       *
       * @param key The key to look for.
       * @return    Whether the key is in the context.
       */
      public boolean containsKey(Object key);
  
      /**
       * Get all the keys for the values in the context
       */
      public Object[] getKeys();
  
      /**
       * Removes the value associated with the specified key from the context.
       *
       * @param key The name of the value to remove.
       * @return    The value that the key was mapped to, or <code>null</code> 
       *            if unmapped.
       */
      public Object remove(Object key);
  }
  
  
  
  1.1                  
jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/template/TemplateEngineService.java
  
  Index: TemplateEngineService.java
  ===================================================================
  package org.apache.fulcrum.template;
  
  /* ====================================================================
   * 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 java.io.OutputStream;
  import java.util.Hashtable;
  import org.apache.fulcrum.ServiceException;
  
  /**
   * This is the interface that all template engine services must adhere
   * to. This includes the Velocity, WebMacro, FreeMarker, and JSP
   * services.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
   * @version $Id: TemplateEngineService.java,v 1.1 2001/08/05 16:03:57 jvanzyl Exp $ 
*/
  public interface TemplateEngineService
  {
      public static final String TEMPLATE_EXTENSIONS = "template.extension";
      public static final String DEFAULT_TEMPLATE_EXTENSION = 
"template.default.extension";
      public static final String DEFAULT_LAYOUT_TEMPLATE = "default.page.template";
      public static final String DEFAULT_PAGE_TEMPLATE = "default.layout.template";
  
      /**
       * Return the configuration of the template engine in
       * the form of a Hashtable.
       */
      public Hashtable getTemplateEngineServiceConfiguration();
  
      /**
       * Initializes file extension associations and registers with the
       * template service.
       *
       * @param defaultExt The default file extension association to use
       *                   in case of properties file misconfiguration.
       */
      public void registerConfiguration(String defaultExt);
  
      /**
       * Supplies the file extension to key this engine in {@link
       * org.apache.fulcrum.template.TemplateService}'s
       * registry with.
       */
      public String[] getAssociatedFileExtensions();
  
      /**
       * Use the specific template engine to determine whether
       * a given template exists. This allows Turbine the TemplateService
       * to delegate the search for a template to the template
       * engine being used for the view. This gives us the
       * advantage of fully utilizing the capabilities of
       * template engine with respect to retrieving templates
       * from arbitrary sources.
       *
       * @param template The name of the template to check the existance of.
       * @return         Whether the specified template exists.
       */
      public boolean templateExists(String template);
  
      /**
       * @see org.apache.fulcrum.template.TemplateEngineService
       */
      public abstract String handleRequest(TemplateContext context,
                                           String template)
          throws ServiceException;
  
      /**
       * @see org.apache.fulcrum.template.TemplateEngineService
       */
      public abstract void handleRequest(TemplateContext context,
                                         String template,
                                         OutputStream outputStream)
          throws ServiceException;
  }
  
  
  
  1.1                  
jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/template/TemplateService.java
  
  Index: TemplateService.java
  ===================================================================
  package org.apache.fulcrum.template;
  
  /* ====================================================================
   * 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 java.io.OutputStream;
  import org.apache.fulcrum.Service;
  import org.apache.fulcrum.ServiceException;
  
  /**
   * This service provides a method for mapping templates to their
   * appropriate Screens or Navigations.  It also allows templates to
   * define a layout/navigations/screen modularization within the
   * template structure.  It also performs caching if turned on in the
   * properties file.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>John D. McNally</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Ilkka Priha</a>
   * @version $Id: TemplateService.java,v 1.1 2001/08/05 16:03:57 jvanzyl Exp $
   */
  public interface TemplateService 
      extends Service
  {
  
      /**
       * The key under which this service is stored in TurbineServices.
       */
      public static final String SERVICE_NAME = "TemplateService";
  
      public static final String CONTEXT = "TEMPLATE_CONTEXT";
  
      /**
       * Translates the supplied template paths into their Turbine-canonical
       * equivalent (probably absolute paths).
       *
       * @param templatePaths An array of template paths. 
       * @return An array of translated template paths.
       */
      public String[] translateTemplatePaths(String[] templatePaths);
  
      /**
       * Delegates to the appropriate {@link
       * org.apache.fulcrum.template.TemplateEngineService} to
       * check the existance of the specified template.
       *
       * @param template      The template to check for the existance of.
       * @param templatePaths The paths to check for the template.
       */
      public boolean templateExists(String template, 
                                    String[] templatePaths);
  
      /**
       * Registers the provided template engine for use by the
       * <code>TemplateService</code>.
       *
       * @param service The <code>TemplateEngineService</code> to register.
       */
      public void registerTemplateEngineService(TemplateEngineService service);
  
      public String handleRequest(TemplateContext context, String template)
          throws ServiceException;
  
      public void handleRequest(TemplateContext context, String template,
                                OutputStream outputStream)
          throws ServiceException;
  
      public TemplateContext getTemplateContext();
  
      public boolean templateExists(String template);
  }
  
  
  
  1.1                  
jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/template/TurbineTemplate.java
  
  Index: TurbineTemplate.java
  ===================================================================
  package org.apache.fulcrum.template;
  
  /* ====================================================================
   * 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 java.io.OutputStream;
  import org.apache.fulcrum.ServiceException;
  import org.apache.fulcrum.TurbineServices;
  
  /**
   * This is a simple static accessor to common TemplateService tasks such as
   * getting a Screen that is associated with a screen template.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>John D. McNally</a>
   * @version $Id: TurbineTemplate.java,v 1.1 2001/08/05 16:03:57 jvanzyl Exp $
   */
  public abstract class TurbineTemplate
  {
      /**
       * Utility method for accessing the service 
       * implementation
       *
       * @return a TemplateService implementation instance
       */
      protected static TemplateService getService()
      {
          return (TemplateService)TurbineServices
              .getInstance().getService(TemplateService.SERVICE_NAME);
      }
  
      public static final void registerTemplateEngineService(TemplateEngineService 
service)
      {
          getService().registerTemplateEngineService(service);
      }                
  
      public static final String[] translateTemplatePaths(String[] templatePaths)
      {
          return getService().translateTemplatePaths(templatePaths);
      }
  
      public static final boolean templateExists(String template, String[] 
templatePaths)
      {
          return getService().templateExists(template, templatePaths);
      }
  
      public static final String handleRequest(TemplateContext context, String 
template)
          throws ServiceException
      {
          return getService().handleRequest(context, template);
      }
  
      public static final void handleRequest(TemplateContext context, 
                                               String template,
                                               OutputStream outputStream)
          throws ServiceException                                             
      {
          getService().handleRequest(context, template, outputStream);
      }
  
      public static final TemplateContext getTemplateContext()
      {
          return getService().getTemplateContext();
      }        
      
      public static final boolean templateExists(String template)
      {
          return getService().templateExists(template);
      }
  }
  
  
  
  1.1                  
jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/template/TurbineTemplateService.java
  
  Index: TurbineTemplateService.java
  ===================================================================
  package org.apache.fulcrum.template;
  
  /* ====================================================================
   * 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 java.io.File;
  import java.io.OutputStream;
  import java.util.HashMap;
  import java.util.Hashtable;
  import org.apache.fulcrum.ServiceException;
  import org.apache.fulcrum.BaseService;
  import org.apache.fulcrum.InitializationException;
  
  /**
   * This service provides a method for mapping templates to their
   * appropriate Screens or Navigations.  It also allows templates to
   * define a layout/navigations/screen modularization within the
   * template structure.  It also performs caching if turned on in the
   * properties file.
   *
   * Since everything is keyed off the template variable, 
   * if data.getParameters().getString("template") returns
   * /about_us/directions/driving.vm, the search for the 
   * Screen class is as follows (in order):
   * 
   * 1. about_us.directions.Driving 
   * 2. about_us.directions.Default 
   * 3. about_us.Default 
   * 4. Default 
   * 5. VelocityScreen
   *
   * If the template variable does not exist, then VelocityScreen will be 
   * executed and templates/screens/index.vm will be executed. 
   * If index.vm is not found or if the template is invalid or Velocity 
   * execution throws an exception of any reason, then
   * templates/screens/error.vm will be executed. 
   * 
   * For the Layouts and Navigations, the following paths will be 
   * searched in the layouts and navigations template
   * subdirectories (in order): 
   * 
   * 1./about_us/directions/driving.vm 
   * 2./about_us/directions/default.vm 
   * 3./about_us/default.vm 
   * 4./default.vm 
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>John D. McNally</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Dave Bryson</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Ilkka Priha</a>
   * @version $Id: TurbineTemplateService.java,v 1.1 2001/08/05 16:03:57 jvanzyl Exp $
   */
  public class TurbineTemplateService 
      extends BaseService
      implements TemplateService
  {
      /**
       * The default file extension used as a registry key when a
       * template's file extension cannot be determined.
       */
      protected static final String NO_FILE_EXT = "";
  
      /** 
       * Default extension for templates. 
       */
      private String defaultExtension;
  
      /**
       * The mappings of template file extensions to {@link
       * org.apache.fulcrum.template.TemplateEngineService}
       * implementations.  Implementing template engines can locate
       * templates within the capability of any resource loaders they
       * may posses, and other template engines are stuck with file
       * based template hierarchy only.
       */
      private HashMap templateEngineRegistry;
      
      public TurbineTemplateService()
      {
      }
  
      /**
       * Called the first time the Service is used.
       *
       * @exception InitializationException.
       */
      public void init() 
          throws InitializationException
      {
          initTemplate();
          setInit(true);
      }
  
      /**
       * Initialize the template service.
       */
      private void initTemplate()
      {
      }
  
      /**
       * Translates the supplied template paths into their Turbine-canonical
       * equivalent (probably absolute paths).
       *
       * @param templatePaths An array of template paths. 
       * @return An array of translated template paths.
       */
      public String[] translateTemplatePaths(String[] templatePaths)
      {
          for (int i = 0; i < templatePaths.length; i++)
          {
              templatePaths[i] = getRealPath(templatePaths[i]);
          }
          return templatePaths;
      }
  
      /**
       * Delegates to the appropriate {@link
       * org.apache.fulcrum.template.TemplateEngineService} to
       * check the existance of the specified template.
       *
       * @param template The template to check for the existance of.
       * @param templatePaths The paths to check for the template.
       */
      public boolean templateExists(String template, 
                                    String[] templatePaths)
      {
          for (int i = 0; i < templatePaths.length; i++)
          {
              if (new File(templatePaths[i],template).exists())
              {
                  return true;
              }
          }
          return false;
      }
  
      public boolean templateExists(String template)
      {
          TemplateEngineService tes = getTemplateEngineService(template);
          return tes.templateExists(template);
      }
  
      /**
       * Registers the provided template engine for use by the
       * <code>TemplateService</code>.
       *
       * @param service The <code>TemplateEngineService</code> to register.
       */
      public synchronized void registerTemplateEngineService(TemplateEngineService 
service)
      {
          // Clone the registry to write to non-sync'd 
          // Map implementations.
          HashMap registry = templateEngineRegistry != null ?
              (HashMap) templateEngineRegistry.clone() : new HashMap();
          
          String[] exts = service.getAssociatedFileExtensions();
  
          for (int i = 0; i < exts.length; i++)
          {
              registry.put(exts[i], service);
          }
          templateEngineRegistry = registry;
      }
  
      /**
       * The {@link org.apache.fulcrum.template.TemplateEngineService}
       * associated with the specified template's file extension.
       * 
       * @param template The template name.
       * @return The template engine service.
       */
      protected TemplateEngineService getTemplateEngineService(String template)
      {
          HashMap registry = templateEngineRegistry;
          if (registry != null && template != null)
          {
              int dotIndex = template.lastIndexOf('.');
              String ext = dotIndex == -1 ? 
                  defaultExtension : template.substring(dotIndex + 1);
              return (TemplateEngineService) registry.get(ext);
          }
          else
          {
              return null;
          }
      }
      
      public String handleRequest(TemplateContext context, String template)
          throws ServiceException
      {
          TemplateEngineService tes = getTemplateEngineService(template);
          return tes.handleRequest(context, template);
      }
  
      public void handleRequest(TemplateContext context, 
                                  String template, 
                                  OutputStream outputStream)
          throws ServiceException
      {
          TemplateEngineService tes = getTemplateEngineService(template);
          tes.handleRequest(context, template, outputStream);
      }
  
      public TemplateContext getTemplateContext()
      {
          return new DefaultTemplateContext();
      }
  }
  
  
  

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

Reply via email to