You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by John McNally <jm...@collab.net> on 2001/04/05 17:03:16 UTC
Re: cvs commit:
jakarta-turbine/src/java/org/apache/turbine/services/template
TemplateService.java TurbineTemplateService.java
is this a tab problem?
jvanzyl@apache.org wrote:
>
> jvanzyl 01/04/04 23:25:52
>
> Modified: src/java/org/apache/turbine/services/template
> TemplateService.java TurbineTemplateService.java
> Log:
> - committing changes/cleanups to the template service sent to
> me by Ilkka. cleans up several efficiency problems, and gets
> rid of the layoutsnull problem.
>
> Revision Changes Path
> 1.11 +279 -193 jakarta-turbine/src/java/org/apache/turbine/services/template/TemplateService.java
>
> Index: TemplateService.java
> ===================================================================
> RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/template/TemplateService.java,v
> retrieving revision 1.10
> retrieving revision 1.11
> diff -u -r1.10 -r1.11
> --- TemplateService.java 2001/04/01 18:28:48 1.10
> +++ TemplateService.java 2001/04/05 06:25:48 1.11
> @@ -1,193 +1,279 @@
> -package org.apache.turbine.services.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 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.util.RunData;
> -
> -import org.apache.turbine.services.Service;
> -
> -/**
> - * 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:john.mcnally@clearink.com">John D. McNally</a>
> - * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
> - * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
> - * @version $Id: TemplateService.java,v 1.10 2001/04/01 18:28:48 jvanzyl Exp $
> - */
> -public interface TemplateService extends Service
> -{
> - /**
> - * The key under which this service is stored in TurbineServices.
> - */
> - public static final String SERVICE_NAME = "TurbineTemplateService";
> -
> - /**
> - * Locate and return the name of a screen template.
> - *
> - * @param key A String which is the key to the template.
> - * @return A String with the screen template path.
> - * @exception Exception, a generic exception.
> - */
> - public String getScreenTemplateName(String key)
> - throws Exception;
> -
> - /**
> - * Locate and return the name of a layout template.
> - *
> - * @param name A String with the name of the template.
> - * @return A String with the layout template path.
> - * @exception Exception, a generic exception.
> - */
> - public String getLayoutTemplateName(String name)
> - throws Exception;
> -
> - /**
> - * Locate and return the name of a Navigation module.
> - *
> - * @param name A String with the name of the template.
> - * @return A String with the name of the navigation.
> - * @exception Exception, a generic exception.
> - */
> - public String getNavigationName(String name)
> - throws Exception;
> -
> - /**
> - * Locate and return the name of a Screen module.
> - *
> - * @param name A String with the name of the template.
> - * @return A String with the name of the screen.
> - * @exception Exception, a generic exception.
> - */
> - public String getScreenName(String name)
> - throws Exception;
> -
> - /**
> - * Get the default extension given in the properties file.
> - *
> - * @return A String with the extension.
> - */
> - public String getDefaultExtension();
> -
> - /**
> - * Get the Screen template given in the properties file.
> - *
> - * @return A String which is the value of the TemplateService
> - * default.screen property.
> - */
> - public String getDefaultScreen();
> -
> - /**
> - * Get the default Navigation given in the properties file.
> - *
> - * @return A String which is the value of the TemplateService
> - * default.navigation property.
> - */
> - public String getDefaultNavigation();
> -
> - /**
> - * Get the default layout template given in the properties file.
> - *
> - * @return A String which is the value of the TemplateService
> - * default.layout.template property.
> - */
> - public String getDefaultLayoutTemplate();
> -
> - /**
> - * Translates the supplied template paths into their Turbine-canonical
> - * equivalent (probably absolute paths).
> - */
> - public String[] translateTemplatePaths(String[] templatePaths);
> -
> - /**
> - * Delegates to the appropriate {@link
> - * org.apache.turbine.services.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);
> -
> - /**
> - * Find the default page module for the given request.
> - *
> - * @param data The encapsulation of the request to retrieve the
> - * default page for.
> - * @return The default page.
> - */
> - public String getDefaultPageName(RunData data);
> -
> - /**
> - * Find the default layout module for the given request.
> - *
> - * @param data The encapsulation of the request to retrieve the
> - * default layout for.
> - * @return The default layout.
> - */
> - public String getDefaultLayoutName(RunData data);
> -
> -}
> +package org.apache.turbine.services.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 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.util.RunData;
> +
> +import org.apache.turbine.services.Service;
> +
> +/**
> + * 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:john.mcnally@clearink.com">John D. McNally</a>
> + * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
> + * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
> + * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
> + * @version $Id: TemplateService.java,v 1.11 2001/04/05 06:25:48 jvanzyl Exp $
> + */
> +public interface TemplateService extends Service
> +{
> + /**
> + * The key under which this service is stored in TurbineServices.
> + */
> + public static final String SERVICE_NAME = "TurbineTemplateService";
> +
> + /**
> + * Get the default template name extension specified
> + * in the template service properties.
> + *
> + * @return The default the extension.
> + */
> + public String getDefaultExtension();
> +
> + /**
> + * Get the default page module name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default page module name.
> + */
> + public String getDefaultPage();
> +
> + /**
> + * Get the default screen module name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default screen module name.
> + */
> + public String getDefaultScreen();
> +
> + /**
> + * Get the default layout module name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default layout module name.
> + */
> + public String getDefaultLayout();
> +
> + /**
> + * Get the default navigation module name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default navigation module name.
> + */
> + public String getDefaultNavigation();
> +
> + /**
> + * Get the default layout template name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default layout template name.
> + */
> + public String getDefaultLayoutTemplate();
> +
> + /**
> + * Get the default page module name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default page module name.
> + */
> + public String getDefaultPageName(String template);
> +
> + /**
> + * Get the default screen module name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default screen module name.
> + */
> + public String getDefaultScreenName(String template);
> +
> + /**
> + * Get the default layout module name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default layout module name.
> + */
> + public String getDefaultLayoutName(String template);
> +
> + /**
> + * Get the default navigation module name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default navigation module name.
> + */
> + public String getDefaultNavigationName(String template);
> +
> + /**
> + * Get the default layout template name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default layout template name.
> + */
> + public String getDefaultLayoutTemplateName(String template);
> +
> + /**
> + * Find the default page module name for the given request.
> + *
> + * @param data The encapsulation of the request to retrieve the
> + * default page for.
> + * @return The default page module name.
> + */
> + public String getDefaultPageName(RunData data);
> +
> + /**
> + * Find the default layout module name for the given request.
> + *
> + * @param data The encapsulation of the request to retrieve the
> + * default layout for.
> + * @return The default layout module name.
> + */
> + public String getDefaultLayoutName(RunData data);
> +
> + /**
> + * Locate and return the name of the screen module to be used
> + * with the named screen template.
> + *
> + * @param template The screen template name.
> + * @return The found screen module name.
> + * @exception Exception, a generic exception.
> + */
> + public String getScreenName(String template)
> + throws Exception;
> +
> + /**
> + * Locate and return the name of the layout module to be used
> + * with the named layout template.
> + *
> + * @param template The layout template name.
> + * @return The found layout module name.
> + * @exception Exception, a generic exception.
> + */
> + public String getLayoutName(String template)
> + throws Exception;
> +
> + /**
> + * Locate and return the name of the navigation module to be used
> + * with the named navigation template.
> + *
> + * @param template The navigation template name.
> + * @return The found navigation module name.
> + * @exception Exception, a generic exception.
> + */
> + public String getNavigationName(String name)
> + throws Exception;
> +
> + /**
> + * Locate and return the name of the screen template corresponding
> + * to the given template name parameter.
> + *
> + * @param template The template name parameter.
> + * @return The found screen template name.
> + * @exception Exception, a generic exception.
> + */
> + public String getScreenTemplateName(String template)
> + throws Exception;
> +
> + /**
> + * Locate and return the name of the layout template corresponding
> + * to the given screen template name parameter.
> + *
> + * @param template The template name parameter.
> + * @return The found screen template name.
> + * @exception Exception, a generic exception.
> + */
> + public String getLayoutTemplateName(String template)
> + throws Exception;
> +
> + /**
> + * 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.turbine.services.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);
> +}
>
>
>
> 1.32 +896 -832 jakarta-turbine/src/java/org/apache/turbine/services/template/TurbineTemplateService.java
>
> Index: TurbineTemplateService.java
> ===================================================================
> RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/services/template/TurbineTemplateService.java,v
> retrieving revision 1.31
> retrieving revision 1.32
> diff -u -r1.31 -r1.32
> --- TurbineTemplateService.java 2001/04/01 18:28:48 1.31
> +++ TurbineTemplateService.java 2001/04/05 06:25:49 1.32
> @@ -1,832 +1,896 @@
> -package org.apache.turbine.services.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 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 java.io.File;
> -
> -import java.util.ArrayList;
> -import java.util.HashMap;
> -import java.util.Hashtable;
> -import java.util.Iterator;
> -import java.util.List;
> -import java.util.Map;
> -import java.util.Stack;
> -import java.util.StringTokenizer;
> -
> -import org.apache.turbine.modules.NavigationLoader;
> -import org.apache.turbine.modules.ScreenLoader;
> -
> -import org.apache.turbine.util.Log;
> -import org.apache.turbine.util.RunData;
> -
> -import org.apache.turbine.services.TurbineBaseService;
> -import org.apache.turbine.services.TurbineServices;
> -import org.apache.turbine.services.InitializationException;
> -import org.apache.turbine.services.resources.TurbineResources;
> -import org.apache.turbine.services.resources.ResourceService;
> -import org.apache.turbine.services.servlet.TurbineServlet;
> -
> -import org.apache.velocity.runtime.configuration.Configuration;
> -
> -/**
> - * 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:john.mcnally@clearink.com">John D. McNally</a>
> - * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
> - * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
> - * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
> - * @version $Id: TurbineTemplateService.java,v 1.31 2001/04/01 18:28:48 jvanzyl Exp $
> - */
> -public class TurbineTemplateService extends TurbineBaseService
> - implements TemplateService
> -{
> - /**
> - * The default file extension used as a registry key when a
> - * template's file extension cannot be determined.
> - */
> - private static final String NO_FILE_EXT = "";
> -
> - /**
> - * The hashtable used to cache Screen names.
> - */
> - private Hashtable screenCache = null;
> -
> - /**
> - * The hashtable used to cache screen template names.
> - */
> - private Hashtable templateCache = null;
> -
> - /**
> - * The hashtable used to cache Navigation names.
> - */
> - private Hashtable navigationCache = null;
> -
> - /**
> - * The hashtable used to cache layout template names.
> - */
> - private Hashtable layoutCache = null;
> -
> - /**
> - * Flag set if cache is to be used.
> - */
> - private boolean useCache = false;
> -
> - /**
> - * Default extension for templates.
> - */
> - private String extension;
> -
> - /**
> - * Default layout template.
> - */
> - private String defaultLayoutTemplate;
> -
> - /**
> - * Default Navigation module.
> - */
> - private String defaultNavigation;
> -
> - /**
> - * Default Screen module.
> - */
> - private String defaultScreen;
> -
> - /**
> - * The mappings of template file extensions to {@link
> - * org.apache.turbine.services.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()
> - {
> - templateEngineRegistry = new HashMap();
> - }
> -
> - /**
> - * Called the first time the Service is used.
> - *
> - * @exception InitializationException
> - */
> - public void init(RunData data) throws InitializationException
> - {
> - try
> - {
> - initTemplate();
> - setInit(true);
> - }
> - catch (Exception e)
> - {
> - throw new InitializationException(
> - "TurbineTemplateService failed to initialize", e);
> - }
> - }
> -
> - /**
> - * Initialize the template service.
> - *
> - * @param config A ServletConfig.
> - * @exception Exception, a generic exception.
> - */
> - private void initTemplate() throws Exception
> - {
> - /*
> - * Get the configuration for the template service.
> - */
> - Configuration config = getConfiguration();
> -
> - /*
> - * Check to see if we are going to be caching modules.
> - */
> - useCache = TurbineResources.getBoolean("module.cache", true);
> -
> - if (useCache)
> - {
> - int layoutSize = config.getInt("layout.cache.size", 5);
> - int navigationSize = config.getInt("navigation.cache.size", 10);
> - int screenSize = config.getInt("screen.cache.size", 5);
> - int templateSize = config.getInt("screen.cache.size", 50);
> -
> - layoutCache = new Hashtable((int) (1.25 * layoutSize) + 1);
> - navigationCache = new Hashtable((int) (1.25 * navigationSize) + 1);
> - screenCache = new Hashtable((int) (1.25 * screenSize) + 1);
> - templateCache = new Hashtable((int) (1.25 * templateSize) + 1);
> - }
> - }
> -
> - /**
> - * The {@link org.apache.turbine.services.template.TemplateEngineService}
> - * associated with the specified template's file extension.
> - */
> - private TemplateEngineService getTemplateEngineService(String template)
> - {
> - int dotIndex = template.lastIndexOf('.');
> - String ext =
> - (dotIndex == -1 ? NO_FILE_EXT : template.substring(dotIndex + 1));
> - return (TemplateEngineService) templateEngineRegistry.get(ext);
> - }
> -
> - /**
> - * Take a set of template paths and translate them
> - * to the webapp space. This can be used by all
> - * template engine service implementations.
> - *
> - * @param String[] array of template paths to translate
> - * @return String[] array of translated template paths
> - */
> - public String[] translateTemplatePaths(String[] templatePaths)
> - {
> - for (int i = 0; i < templatePaths.length; i++)
> - {
> - templatePaths[i] = TurbineServlet.getRealPath(templatePaths[i]);
> - }
> -
> - return templatePaths;
> - }
> -
> - /**
> - * Adds the object into the hashtable.
> - *
> - * @param key The String key for the object.
> - * @param value The Object.
> - * @param h The Hashtable.
> - */
> - private void addToCache ( String key,
> - Object value,
> - Hashtable h )
> - {
> - if (useCache && value != null)
> - {
> - h.put(key, value);
> - }
> - }
> -
> - /**
> - * Get the Screen template given in the properties file.
> - *
> - * @return A String which is the value of the TemplateService
> - * default.screen property.
> - */
> - public String getDefaultScreen()
> - {
> - return defaultScreen;
> - }
> -
> - /**
> - * Get the default Navigation given in the properties file.
> - *
> - * @return A String which is the value of the TemplateService
> - * default.navigation property.
> - */
> - public String getDefaultNavigation()
> - {
> - return defaultNavigation;
> - }
> -
> - /**
> - * Get the default layout template given in the properties file.
> - *
> - * @return A String which is the value of the TemplateService
> - * default.layout.template property.
> - */
> - public String getDefaultLayoutTemplate()
> - {
> - return defaultLayoutTemplate;
> - }
> -
> - /**
> - * Locate and return the name of a screen template.
> - *
> - *
> - * @param name A String which is the key to the template.
> - * @return A String with the screen template path.
> - * @exception Exception, a generic exception.
> - */
> - public String getScreenTemplateName(String key) throws Exception
> - {
> - if (key == null)
> - {
> - throw new Exception ("TurbineTemplateService: " +
> - "getScreenTemplateName() was passed in a null value.");
> - }
> -
> - String screenTemplate = null;
> -
> - if ( useCache && templateCache.containsKey(key) )
> - {
> - screenTemplate = (String) templateCache.get(key);
> - }
> - else
> - {
> - String[] names = parseScreenTemplate(key);
> - screenTemplate = names[2];
> - addToCache( key, names[0], screenCache );
> - addToCache( key, names[1], layoutCache );
> - addToCache( key, names[2], templateCache );
> - }
> - return screenTemplate;
> - }
> -
> - /**
> - * Locate and return the name of a layout template.
> - *
> - *
> - * @param name A String with the name of the template.
> - * @return A String with the layout template path.
> - * @exception Exception, a generic exception.
> - */
> - public String getLayoutTemplateName(String key) throws Exception
> - {
> - if (key == null)
> - {
> - throw new Exception ("TurbineTemplateService: " +
> - "getLayoutTemplateName() was passed in a null value.");
> - }
> -
> - String layoutName = null;
> -
> - if ( useCache && layoutCache.containsKey(key) )
> - {
> - layoutName = (String) layoutCache.get(key);
> - }
> - else
> - {
> - String[] names = parseScreenTemplate(key);
> - layoutName = names[1];
> - addToCache( key, names[0], screenCache );
> - addToCache( key, names[1], layoutCache );
> - addToCache( key, names[2], templateCache );
> - }
> - return layoutName;
> - }
> -
> - /**
> - * Locate and return the name of a Navigation module.
> - *
> - * @param name A String with the name of the template.
> - * @return A String with the name of the navigation.
> - * @exception Exception, a generic exception.
> - */
> - public String getNavigationName(String key) throws Exception
> - {
> - if (key == null)
> - {
> - throw new Exception ("TurbineTemplateService: " +
> - "getNavigationName() was passed in a null value.");
> - }
> -
> - String navigationName = null;
> -
> - if ( useCache && navigationCache.containsKey(key) )
> - {
> - navigationName = (String) navigationCache.get(key);
> - }
> - else
> - {
> - navigationName = parseNavigationTemplate(key);
> - addToCache( key, navigationName, navigationCache );
> - }
> - return navigationName;
> - }
> -
> - /**
> - * Locate and return the name of a Screen module.
> - *
> - * @param name A String with the name of the template.
> - * @return A String with the name of the screen.
> - * @exception Exception, a generic exception.
> - */
> - public String getScreenName(String name) throws Exception
> - {
> -
> - if (name == null)
> - {
> - throw new Exception ("TurbineTemplateService: " +
> - "getScreenName() was passed in a null value.");
> - }
> -
> - String screenName = null;
> -
> - if ( useCache && screenCache.containsKey(name) )
> - {
> - screenName = (String)screenCache.get(name);
> - }
> - else
> - {
> - String[] names = parseScreenTemplate(name);
> - screenName = names[0];
> - addToCache( name, names[0], screenCache );
> - addToCache( name, names[1], layoutCache );
> - addToCache( name, names[2], templateCache );
> - }
> - return screenName;
> - }
> -
> - /**
> - * Get the default extension given in the properties file.
> - *
> - * @return A String with the extension.
> - */
> - public String getDefaultExtension()
> - {
> - return extension;
> - }
> -
> - /**
> - * This method takes the template parameter and parses it, so that
> - * relevant Screen/Layout-template information can be extracted.
> - *
> - * @param template A String with the template name.
> - * @return A String[] where the first element is the Screen name
> - * and the second element is the layout template.
> - */
> - protected String[] parseScreenTemplate( String template ) throws Exception
> - {
> - /*
> - * Check if an extension was included.
> - * If not, add the default. This shouldn't happen,
> - * an extension should always be present I think (jvz).
> - */
> - if ( template.indexOf('.') == -1 )
> - {
> - template = template + "." + getDefaultExtension();
> - }
> -
> - /*
> - * Remove any leading "/" characters because we
> - * are already appending one here. Don't want
> - * the duplicate "//" sequence in the path.
> - */
> - if (template.startsWith("/"))
> - {
> - template = template.substring(1);
> - }
> -
> - String templateToFind = "screens/" + template;
> -
> - if (!getTemplateEngineService(template).templateExists(templateToFind))
> - {
> - /*
> - * We should ask the template engine where it
> - * looked for templates.
> - */
> - throw new Exception("can't find template: " + templateToFind);
> - }
> -
> - Stack tokens = processTemplateName(template);
> - String className = (String) tokens.pop();
> - String fileName = (String) tokens.pop();
> -
> - /*
> - * Here we are producing two arrays, the first is
> - * a list of the paths where the template might
> - * live, and the second is a list of where the
> - * screen classes may live in the classpath.
> - */
> - String[] paths = new String[tokens.size() + 2];
> - String[] pkgs = new String[tokens.size() + 2];
> - int arrayIndex = 0;
> - for (int i=tokens.size(); i>=0; i--)
> - {
> - StringBuffer path = new StringBuffer();
> - StringBuffer pkg = new StringBuffer();
> - for (int j=0; j<i; j++)
> - {
> - path.append("/").append((String)tokens.get(j));
> - pkg.append((String)tokens.get(j)).append('.');
> - }
> - if ( i == tokens.size() )
> - {
> - StringBuffer distinctPath = new StringBuffer(path.toString());
> - StringBuffer distinctPkg = new StringBuffer(pkg.toString());
> - paths[arrayIndex] =
> - distinctPath.append('/').append(fileName).toString();
> - pkgs[arrayIndex] = distinctPkg.append(className).toString();
> - arrayIndex++;
> - }
> - paths[arrayIndex] = path.append(defaultLayoutTemplate).toString();
> - pkgs[arrayIndex] = pkg.append("Default").toString();
> - arrayIndex++;
> - }
> -
> - String[] holder = new String[3];
> - holder[0] = getScreenName(pkgs, template);
> - holder[1] = getLayoutTemplateName(paths);
> - holder[2] = template;
> - return holder;
> - }
> -
> - /**
> - * @see org.apache.turbine.services.template.TemplateService#templateExists
> - */
> - public boolean templateExists(String template, String[] templatePaths)
> - {
> - /*
> - * Make sure the template exists and determine the correct
> - * templateRoot path.
> - */
> - String pathRoot = null;
> - String pathSep = System.getProperty("path.separator");
> -
> - for (int i = 0; i < templatePaths.length; i++)
> - {
> - if (new File(templatePaths[i], template).exists())
> - {
> - pathRoot = templatePaths[i];
> - break;
> - }
> - }
> - return (pathRoot != null);
> - }
> -
> - /**
> - * Parse the template name out to a package path to locate the
> - * Navigation module. This is different than the Screen/Layout
> - * parser in that it only looks for packages. Note: If caching is
> - * enabled, this is only performed once for each unique template.
> - *
> - * @param String The template name (i.e folder/headernav.wm).
> - * @return A String with the name of the Navigation module to use
> - * for the template.
> - */
> - protected String parseNavigationTemplate( String template )
> - {
> - Stack tokens = processTemplateName(template);
> - String className = (String) tokens.pop();
> - String fileName = (String) tokens.pop();
> -
> - String[] pkgs = new String[tokens.size() + 2];
> - int arrayIndex = 0;
> - for (int i=tokens.size(); i>=0; i--)
> - {
> - StringBuffer pkg = new StringBuffer();
> - for (int j=0; j<i; j++)
> - {
> - pkg.append((String)tokens.get(j)).append('.');
> - }
> - if ( i == tokens.size() )
> - {
> - StringBuffer distinctPkg = new StringBuffer(pkg.toString());
> - pkgs[arrayIndex] = distinctPkg.append(className).toString();
> - arrayIndex++;
> - }
> - pkgs[arrayIndex] = pkg.append("Default").toString();
> - arrayIndex++;
> - }
> - return getNavigationName(pkgs, template);
> - }
> -
> - /**
> - * Process the template name and return a List that
> - * contains the tokenized bits of the path to
> - * the template, the fileName of the template,
> - * and the class name of the screen that goes
> - * along with the template.
> - *
> - * @param String template
> - * @return List
> - */
> - private Stack processTemplateName(String template)
> - {
> - /*
> - * Look at the template name that we have been given,
> - * say for example:
> - *
> - * /about_us/directions/driving.vm
> - *
> - * and try to determine where a matching Screen
> - * class might be found.
> - */
> - StringTokenizer st = new StringTokenizer(template, "/");
> - Stack tokens = new Stack();
> -
> - /*
> - * Using our example above we are going to turn:
> - *
> - * /about_us/directions/driving.vm
> - *
> - * into
> - *
> - * [about_us, directions, driving.vm]
> - */
> - while(st.hasMoreTokens())
> - {
> - String token = st.nextToken();
> - if (!token.equals(""))
> - {
> - tokens.add(token);
> - }
> - }
> -
> - /*
> - * The last element in the list is the name of
> - * our template.
> - */
> - String fileName = (String)tokens.pop();
> - int dot = fileName.lastIndexOf('.');
> - String className = null;
> -
> - if (dot > 0)
> - {
> - className = fileName.substring(0, dot);
> - }
> - else
> - {
> - className = fileName;
> - }
> -
> - tokens.push(fileName);
> - tokens.push(className);
> -
> - return tokens;
> - }
> -
> - /**
> - * Extract possible layouts paths.
> - *
> - * @param possiblePaths A String[] with possible paths to search.
> - * @return A String with the name of the layout template.
> - */
> - private String getLayoutTemplateName(String[] possiblePaths)
> - {
> - /*
> - * Assume at least one path. We should figure out a
> - * better way to get hold of the engine then having
> - * to use the template name everytime.
> - */
> - TemplateEngineService tes =
> - getTemplateEngineService(possiblePaths[0]);
> -
> - for (int i = 0; i < possiblePaths.length; i++)
> - {
> - /*
> - * We are not adjusting anything here "/" character
> - * wise because the possiblePaths are coming in
> - * with a prefix of "/".
> - */
> - if ( tes.templateExists("layouts" + possiblePaths[i]) )
> - {
> - return possiblePaths[i];
> - }
> - }
> -
> - /*
> - * Now we have to return the default layout for
> - * the template engine in question.
> - */
> - return (String) tes.getTemplateEngineServiceConfiguration()
> - .get(TemplateEngineService.DEFAULT_LAYOUT_TEMPLATE);
> - }
> -
> - /**
> - * Extract a possible Screen from the packages.
> - *
> - * @param possibleScreens A String[] with possible paths to
> - * search.
> - * @return A String with the name of the Screen class to use.
> - */
> - private String getScreenName(String[] possibleScreens, String template)
> - {
> - for (int i = 0; i < possibleScreens.length; i++)
> - {
> - try
> - {
> - ScreenLoader.getInstance().getInstance(possibleScreens[i]);
> - return possibleScreens[i];
> - }
> - catch (Exception e)
> - {
> - /*
> - * do nothing.
> - */
> - }
> - }
> -
> - /*
> - * Need a way to return the template engine default
> - * screen.
> - */
> - TemplateEngineService tes = getTemplateEngineService(template);
> -
> - return (String) tes.getTemplateEngineServiceConfiguration()
> - .get(TemplateEngineService.DEFAULT_SCREEN);
> -
> - }
> -
> -
> - /**
> - * Seaches for the Navigation class that may match the
> - * name of the Navigation template.
> - *
> - * @param possibleNavigations A String[] with possible navigation
> - * packages.
> - * @return A String with the name of the Navigation class to use.
> - */
> - private String getNavigationName(String[] possibleNavigations,
> - String template)
> - {
> - for (int i = 0; i < possibleNavigations.length; i++)
> - {
> - try
> - {
> - NavigationLoader.getInstance().getInstance(
> - possibleNavigations[i]);
> - return possibleNavigations[i];
> - }
> - catch (Exception e)
> - {
> - /*
> - * do nothing.
> - */
> - }
> - }
> -
> - /*
> - * Need a way to return the template engine default
> - * navigation.
> - */
> - TemplateEngineService tes = getTemplateEngineService(template);
> -
> - return (String) tes.getTemplateEngineServiceConfiguration()
> - .get(TemplateEngineService.DEFAULT_NAVIGATION);
> -
> - }
> -
> - /**
> - * @see org.apache.turbine.services.template.TemplateService#registerTemplateEngineService
> - */
> - public synchronized void registerTemplateEngineService(TemplateEngineService service)
> - {
> - // Clone the registry to write to non-sync'd Map implementations.
> - HashMap registry = (HashMap) templateEngineRegistry.clone();
> - String[] exts = service.getAssociatedFileExtensions();
> - for (int i = 0; i < exts.length; i++)
> - {
> - registry.put(exts[i], service);
> - }
> - templateEngineRegistry = registry;
> - }
> -
> - /**
> - * @see org.apache.turbine.services.template.TemplateService#getDefaultPageName
> - */
> - public String getDefaultPageName(RunData data)
> - {
> - String template = data.getParameters().get("template");
> -
> - TemplateEngineService tes =
> - getTemplateEngineService(template);
> -
> - if (tes == null)
> - {
> - return null;
> - }
> -
> - return (String) tes.getTemplateEngineServiceConfiguration()
> - .get(TemplateEngineService.DEFAULT_PAGE);
> - }
> -
> - /**
> - * @see org.apache.turbine.services.template.TemplateService#getDefaultLayoutName
> - */
> - public String getDefaultLayoutName(RunData data)
> - {
> - String template = data.getParameters().get("template");
> -
> - TemplateEngineService tes =
> - getTemplateEngineService(template);
> -
> - if (tes == null)
> - {
> - return null;
> - }
> -
> - return (String) tes.getTemplateEngineServiceConfiguration()
> - .get(TemplateEngineService.DEFAULT_LAYOUT);
> - }
> -}
> +package org.apache.turbine.services.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 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 java.io.File;
> +
> +import java.util.HashMap;
> +import java.util.Hashtable;
> +
> +import org.apache.turbine.modules.ScreenLoader;
> +import org.apache.turbine.modules.LayoutLoader;
> +import org.apache.turbine.modules.NavigationLoader;
> +
> +import org.apache.turbine.util.RunData;
> +
> +import org.apache.turbine.services.TurbineBaseService;
> +import org.apache.turbine.services.InitializationException;
> +import org.apache.turbine.services.resources.TurbineResources;
> +import org.apache.turbine.services.servlet.TurbineServlet;
> +
> +import org.apache.velocity.runtime.configuration.Configuration;
> +
> +/**
> + * 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:john.mcnally@clearink.com">John D. McNally</a>
> + * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
> + * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
> + * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
> + * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
> + * @version $Id: TurbineTemplateService.java,v 1.32 2001/04/05 06:25:49 jvanzyl Exp $
> + */
> +public class TurbineTemplateService extends TurbineBaseService
> + implements TemplateService
> +{
> + /**
> + * The default file extension used as a registry key when a
> + * template's file extension cannot be determined.
> + */
> + private static final String NO_FILE_EXT = "";
> +
> + /**
> + * The keys of template objects provided by the service.
> + */
> + private static final int PAGE_KEY = 0;
> + private static final int SCREEN_KEY = 1;
> + private static final int LAYOUT_KEY = 2;
> + private static final int NAVIGATION_KEY = 3;
> + private static final int LAYOUT_TEMPLATE_KEY = 4;
> + private static final int SCREEN_TEMPLATE_KEY = 5;
> +
> + /**
> + * The properties for default template object names.
> + */
> + private String[] defaultNameProperties = new String[]
> + {
> + TemplateEngineService.DEFAULT_PAGE,
> + TemplateEngineService.DEFAULT_SCREEN,
> + TemplateEngineService.DEFAULT_LAYOUT,
> + TemplateEngineService.DEFAULT_NAVIGATION,
> + TemplateEngineService.DEFAULT_LAYOUT_TEMPLATE
> + };
> +
> + /**
> + * The hashtables used to cache template object names.
> + */
> + private Hashtable[] templateNameCache = new Hashtable[6];
> +
> + /**
> + * Flag set if cache is to be used.
> + */
> + private boolean useCache = false;
> +
> + /**
> + * Default extension for templates.
> + */
> + private String defaultExtension;
> +
> + /**
> + * Default template with the default extension.
> + */
> + private String defaultTemplate;
> +
> + /**
> + * The mappings of template file extensions to {@link
> + * org.apache.turbine.services.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(RunData data) throws InitializationException
> + {
> + initTemplate();
> + setInit(true);
> + }
> +
> + /**
> + * Get the default template name extension specified
> + * in the template service properties.
> + *
> + * @return The default the extension.
> + */
> + public String getDefaultExtension()
> + {
> + return defaultExtension;
> + }
> +
> + /**
> + * Get the default page module name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default page module name.
> + */
> + public String getDefaultPage()
> + {
> + return getDefaultPageName(defaultTemplate);
> + }
> +
> + /**
> + * Get the default screen module name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default screen module name.
> + */
> + public String getDefaultScreen()
> + {
> + return getDefaultScreenName(defaultTemplate);
> + }
> +
> + /**
> + * Get the default layout module name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default layout module name.
> + */
> + public String getDefaultLayout()
> + {
> + return getDefaultLayoutName(defaultTemplate);
> + }
> +
> + /**
> + * Get the default navigation module name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default navigation module name.
> + */
> + public String getDefaultNavigation()
> + {
> + return getDefaultNavigationName(defaultTemplate);
> + }
> +
> + /**
> + * Get the default layout template name of the template engine
> + * service corresponding to the default template name extension.
> + *
> + * @return The default layout template name.
> + */
> + public String getDefaultLayoutTemplate()
> + {
> + return getDefaultLayoutTemplateName(defaultTemplate);
> + }
> +
> + /**
> + * Get the default page module name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default page module name.
> + */
> + public String getDefaultPageName(String template)
> + {
> + return getDefaultModuleName(template,PAGE_KEY);
> + }
> +
> + /**
> + * Get the default screen module name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default screen module name.
> + */
> + public String getDefaultScreenName(String template)
> + {
> + return getDefaultModuleName(template,SCREEN_KEY);
> + }
> +
> + /**
> + * Get the default layout module name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default layout module name.
> + */
> + public String getDefaultLayoutName(String template)
> + {
> + return getDefaultModuleName(template,LAYOUT_KEY);
> + }
> +
> + /**
> + * Get the default navigation module name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default navigation module name.
> + */
> + public String getDefaultNavigationName(String template)
> + {
> + return getDefaultModuleName(template,NAVIGATION_KEY);
> + }
> +
> + /**
> + * Get the default layout template name of the template engine
> + * service corresponding to the template name extension of
> + * the named template.
> + *
> + * @param template The template name.
> + * @return The default layout template name.
> + */
> + public String getDefaultLayoutTemplateName(String template)
> + {
> + String layoutTemplate =
> + getDefaultModuleName(template,LAYOUT_TEMPLATE_KEY);
> + if ((layoutTemplate != null) &&
> + (layoutTemplate.indexOf('.') < 0))
> + {
> + int dotIndex = template.indexOf('.');
> + layoutTemplate += dotIndex >= 0 ?
> + template.substring(dotIndex) : '.' + defaultExtension;
> + }
> + return layoutTemplate;
> + }
> +
> + /**
> + * Find the default page module name for the given request.
> + *
> + * @param data The encapsulation of the request to retrieve the
> + * default page for.
> + * @return The default page module name.
> + */
> + public String getDefaultPageName(RunData data)
> + {
> + String template = data.getParameters().get("template");
> + return template != null ?
> + getDefaultPageName(template) : getDefaultPage();
> + }
> +
> + /**
> + * Find the default layout module name for the given request.
> + *
> + * @param data The encapsulation of the request to retrieve the
> + * default layout for.
> + * @return The default layout module name.
> + */
> + public String getDefaultLayoutName(RunData data)
> + {
> + String template = data.getParameters().get("template");
> + return template != null ?
> + getDefaultLayoutName(template) : getDefaultLayout();
> + }
> +
> + /**
> + * Locate and return the name of the screen module to be used
> + * with the named screen template.
> + *
> + * @param template The screen template name.
> + * @return The found screen module name.
> + * @exception Exception, a generic exception.
> + */
> + public String getScreenName(String template)
> + throws Exception
> + {
> + return getCachedName(template,SCREEN_KEY);
> + }
> +
> + /**
> + * Locate and return the name of the layout module to be used
> + * with the named layout template.
> + *
> + * @param template The layout template name.
> + * @return The found layout module name.
> + * @exception Exception, a generic exception.
> + */
> + public String getLayoutName(String template)
> + throws Exception
> + {
> + return getCachedName(template,LAYOUT_KEY);
> + }
> +
> + /**
> + * Locate and return the name of the navigation module to be used
> + * with the named navigation template.
> + *
> + * @param template The navigation template name.
> + * @return The found navigation module name.
> + * @exception Exception, a generic exception.
> + */
> + public String getNavigationName(String template)
> + throws Exception
> + {
> + return getCachedName(template,NAVIGATION_KEY);
> + }
> +
> + /**
> + * Locate and return the name of the screen template corresponding
> + * to the given template name parameter.
> + *
> + * @param template The template name parameter.
> + * @return The found screen template name.
> + * @exception Exception, a generic exception.
> + */
> + public String getScreenTemplateName(String template)
> + throws Exception
> + {
> + return getCachedName(template,SCREEN_TEMPLATE_KEY);
> + }
> +
> + /**
> + * Locate and return the name of the layout template corresponding
> + * to the given screen template name parameter.
> + *
> + * @param template The template name parameter.
> + * @return The found screen template name.
> + * @exception Exception, a generic exception.
> + */
> + public String getLayoutTemplateName(String template)
> + throws Exception
> + {
> + return getCachedName(template,LAYOUT_TEMPLATE_KEY);
> + }
> +
> + /**
> + * 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] = TurbineServlet.getRealPath(templatePaths[i]);
> + }
> + return templatePaths;
> + }
> +
> + /**
> + * Delegates to the appropriate {@link
> + * org.apache.turbine.services.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;
> + }
> +
> + /**
> + * 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.turbine.services.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)
> + {
> + int dotIndex = template.lastIndexOf('.');
> + String ext = dotIndex == -1 ?
> + defaultExtension : template.substring(dotIndex + 1);
> + return (TemplateEngineService) registry.get(ext);
> + }
> + else
> + {
> + return null;
> + }
> + }
> +
> + /**
> + * 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 String getDefaultModuleName(String template,
> + int key)
> + {
> + TemplateEngineService tes = getTemplateEngineService(template);
> + return tes != null ?
> + (String) tes.getTemplateEngineServiceConfiguration().
> + get(defaultNameProperties[key]) : null;
> + }
> +
> + /**
> + * Get the cached template object name.
> + * If caching is not in use or the name is not in the cache,
> + * try to find the name from the template directories.
> + *
> + * @param template The template name.
> + * @param key The object type key.
> + * @return the cached name.
> + * @exception Exception, a generic exception.
> + */
> + protected String getCachedName(String template,
> + int key)
> + throws Exception
> + {
> + /*
> + * Add a leading slash if missing.
> + */
> + if (template.charAt(0) != '/')
> + {
> + template = '/' + template;
> + }
> +
> + /*
> + * Add a default extension if missing.
> + */
> + if (template.indexOf('.') < 0)
> + {
> + template += '.' + defaultExtension;
> + }
> +
> + /*
> + * Check the cache first.
> + */
> + String found;
> + if (useCache)
> + {
> + found = (String) templateNameCache[key].get(template);
> + if (found != null)
> + {
> + return found;
> + }
> + }
> +
> + /*
> + * Not in the cache, try to find it.
> + */
> + if (key == SCREEN_TEMPLATE_KEY)
> + {
> + TemplateEngineService tes = getTemplateEngineService(template);
> + if ((tes == null) ||
> + !tes.templateExists("screens" + template))
> + {
> + throw new Exception(
> + "Screen template '" + template + "' not found");
> + }
> + found = template;
> + }
> + else if (key == LAYOUT_TEMPLATE_KEY)
> + {
> + found = getParsedLayoutTemplateName(template);
> + }
> + else
> + {
> + found = getParsedModuleName(template,key);
> + }
> +
> + /*
> + * Put the found name to the cache.
> + */
> + if (useCache)
> + {
> + templateNameCache[key].put(template,found);
> + }
> +
> + return found;
> + }
> +
> + /**
> + * 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 String getParsedModuleName(String template,
> + int key)
> + throws Exception
> + {
> + /*
> + * 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);
> + }
> + 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();
> + try
> + {
> + switch (key)
> + {
> + case SCREEN_KEY:
> + ScreenLoader.getInstance().getInstance(module);
> + return module;
> +
> + case LAYOUT_KEY:
> + LayoutLoader.getInstance().getInstance(module);
> + return module;
> +
> + case NAVIGATION_KEY:
> + NavigationLoader.getInstance().getInstance(module);
> + return module;
> + }
> + }
> + catch (Exception x)
> + {
> + }
> +
> + 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 getDefaultModuleName(template,key);
> + }
> +
> + /**
> + * Get the parsed layout template name for the specified template.
> + *
> + * @param template The template name.
> + * @return The parsed layout template name.
> + * @exception Exception, a generic exception.
> + */
> + protected String getParsedLayoutTemplateName(String template)
> + throws Exception
> + {
> + /*
> + * Parse the template name.
> + */
> + StringBuffer path = new StringBuffer();
> + int i = parseTemplatePath(template,path);
> + if (path.charAt(0) != '/')
> + {
> + path.insert(0,'/');
> + i++;
> + }
> + path.insert(0,"layouts");
> + i += 7;
> +
> + /*
> + * Try first an exact match for a layout template having the same
> + * name as the input template, traverse then upper level
> + * directories to find the default layout template.
> + */
> + TemplateEngineService tes = getTemplateEngineService(template);
> + if (tes == null)
> + {
> + throw new Exception(
> + "Layout template '" + template + "' not found");
> + }
> +
> + String defaultLayout = getDefaultLayoutTemplateName(template);
> + if ((defaultLayout != null) &&
> + (defaultLayout.length() == 0))
> + {
> + defaultLayout = null;
> + }
> + else if (defaultLayout.charAt(0) != '/')
> + {
> + defaultLayout = '/' + defaultLayout;
> + }
> +
> + int j = 9999;
> + String layoutTemplate;
> + while (j-- > 0)
> + {
> + layoutTemplate = path.toString();
> + System.out.println("Search for layout: " + layoutTemplate);
> + if (tes.templateExists(layoutTemplate))
> + {
> + return layoutTemplate.substring(7);
> + }
> +
> + if (defaultLayout == null)
> + {
> + throw new Exception(
> + "Layout template '" + template + "' not found");
> + }
> +
> + path.setLength(i);
> + if (i > 8)
> + {
> + /*
> + * We have still directories to traverse.
> + */
> + for (i = path.length() - 2; i >= 8; i--)
> + {
> + if (path.charAt(i) == '/')
> + {
> + break;
> + }
> + }
> + }
> + else if (j > 0)
> + {
> + /*
> + * Only the main level left.
> + */
> + j = 1;
> + }
> + path.append(defaultLayout);
> + }
> +
> + /*
> + * Not found, return the default layout
> + * template as such.
> + */
> + return defaultLayout;
> + }
> +
> + /**
> + * Parse the template name.
> + *
> + * @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, a generic exception.
> + */
> + private int parseTemplatePath(String template,
> + StringBuffer buffer)
> + throws Exception
> + {
> + buffer.setLength(0);
> + int ind = template.lastIndexOf('/');
> + if ((template.length() == 0) ||
> + ((ind >= 0) &&
> + (template.length() <= (ind + 1))))
> + {
> + throw new Exception(
> + "Syntax error in template name '" + template + "'");
> + }
> +
> + buffer.append(template);
> + if (ind >= 0)
> + {
> + int j = 0;
> + while (j < buffer.length())
> + {
> + if (buffer.charAt(j) == '/')
> + {
> + if (buffer.charAt(j + 1) == '/')
> + {
> + buffer.deleteCharAt(j);
> + ind--;
> + continue;
> + }
> + }
> + j++;
> + }
> + }
> + return ind;
> + }
> +
> + /**
> + * Initialize the template service.
> + */
> + private void initTemplate()
> + {
> + /*
> + * Get the configuration for the template service.
> + */
> + Configuration config = getConfiguration();
> +
> + /*
> + * Get the default extension to use if nothing else is applicable.
> + */
> + defaultExtension = config.getString("default.extension",NO_FILE_EXT);
> + defaultTemplate = "Default." + defaultExtension;
> +
> + /*
> + * Check to see if we are going to be caching modules.
> + */
> + useCache = TurbineResources.getBoolean("module.cache",true);
> +
> + if (useCache)
> + {
> + int screenSize = config.getInt("screen.cache.size",5);
> + int screenTemplateSize = config.getInt("screen.cache.size",50);
> + int layoutSize = config.getInt("layout.cache.size",5);
> + int layoutTemplateSize = config.getInt("layout.cache.size",5);
> + int navigationSize = config.getInt("navigation.cache.size",10);
> +
> + /*
> + * Create hashtables for each object type,
> + * the first one for pages is not used.
> + */
> + templateNameCache[PAGE_KEY] = null;
> + templateNameCache[SCREEN_KEY] =
> + new Hashtable((int) (1.25 * screenSize) + 1);
> + templateNameCache[SCREEN_TEMPLATE_KEY] =
> + new Hashtable((int) (1.25 * screenTemplateSize) + 1);
> + templateNameCache[LAYOUT_KEY] =
> + new Hashtable((int) (1.25 * layoutSize) + 1);
> + templateNameCache[LAYOUT_TEMPLATE_KEY] =
> + new Hashtable((int) (1.25 * layoutTemplateSize) + 1);
> + templateNameCache[NAVIGATION_KEY] =
> + new Hashtable((int) (1.25 * navigationSize) + 1);
> + }
> + }
> +}
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: turbine-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org