View Javadoc

1   package org.apache.turbine.modules.pages;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.turbine.services.template.TurbineTemplate;
20  import org.apache.turbine.util.RunData;
21  import org.apache.turbine.util.TurbineException;
22  
23  /***
24   * When building sites using templates, Screens need only be defined
25   * for templates which require dynamic (database or object) data.
26   *
27   * <p>
28   *
29   * This page can be used on sites where the number of Screens can be
30   * much less than the number of templates.  The templates can be
31   * grouped in directories with common layouts.  Screen modules are
32   * then expected to be placed in packages corresponding with the
33   * templates' directories and follow a specific naming scheme.
34   *
35   * <p>
36   *
37   * The template parameter is parsed and and a Screen whose package
38   * matches the templates path and shares the same name minus any
39   * extension and beginning with a capital letter is searched for.  If
40   * not found, a Screen in a package matching the template's path with
41   * name Default is searched for.  If still not found, a Screen with
42   * name Default is looked for in packages corresponding to parent
43   * directories in the template's path until a match is found.
44   *
45   * <p>
46   *
47   * For example if data.getParameters().getString("template") returns
48   * /about_us/directions/driving.wm, the search follows
49   * about_us.directions.Driving, about_us.directions.Default,
50   * about_us.Default, Default, WebMacroSiteScreen (i.e. the default
51   * screen set in TurbineResources).
52   *
53   * <p>
54   *
55   * Only one Layout module is used, since it is expected that any
56   * dynamic content will be placed in navigations and screens.  The
57   * layout template to be used is found in a similar way to the Screen.
58   * For example the following paths will be searched in the layouts
59   * subdirectory: /about_us/directions/driving.wm,
60   * /about_us/directions/default.wm, /about_us/default.wm, /default.wm,
61   * where wm is the value of the template.default.extension property.
62   *
63   * <p>
64   *
65   * This approach allows a site with largely static content to be
66   * updated and added to regularly by those with little Java
67   * experience.
68   *
69   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
70   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
71   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
72   * @version $Id: TemplatePage.java 264148 2005-08-29 14:21:04Z henning $
73   */
74  public class TemplatePage
75      extends DefaultPage
76  {
77      /***
78       * Works with TemplateService to set up default templates and
79       * corresponding class modules.
80       *
81       * @param data Turbine information.
82       * @exception Exception, a generic exception.
83       */
84      protected void doBuildAfterAction(RunData data)
85          throws Exception
86      {
87          // The Template Service at this point must fetch the Screen class
88          // to match a given template. If the Screen class has already been
89          // set by an action, skip this, because the user has the already
90          // specified the Screen class he wants to use.
91          if (!data.hasScreen())
92          {
93              // This is effectively getting the "template" parameter
94              // from the parameter parser in rundata. This is coming
95              // from the request for a template.
96              String template = data.getTemplateInfo().getScreenTemplate();
97  
98              // Get the layout template and the correct Screen.
99              String layoutTemplate =
100                     TurbineTemplate.getLayoutTemplateName(template);
101             data.getTemplateInfo().setLayoutTemplate(layoutTemplate);
102 
103             String screen = TurbineTemplate.getScreenName(template);
104 
105             if (screen == null)
106             {
107                 String errMsg = "Couldn't map Template "
108                     + template + " to any Screen class!";
109                 log.error(errMsg);
110                 throw new TurbineException(errMsg);
111             }
112             data.setScreen(screen);
113         }
114     }
115 }