View Javadoc

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