View Javadoc
1   package org.apache.turbine.modules.pages;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.turbine.annotation.TurbineLoader;
28  import org.apache.turbine.modules.Action;
29  import org.apache.turbine.modules.ActionLoader;
30  import org.apache.turbine.modules.Layout;
31  import org.apache.turbine.modules.LayoutLoader;
32  import org.apache.turbine.modules.Page;
33  import org.apache.turbine.modules.Screen;
34  import org.apache.turbine.modules.ScreenLoader;
35  import org.apache.turbine.pipeline.PipelineData;
36  import org.apache.turbine.util.RunData;
37  
38  /**
39   * When building sites using templates, Screens need only be defined
40   * for templates which require dynamic (database or object) data.
41   *
42   * <p>
43   *
44   * This page can be used on sites where the number of Screens can be
45   * much less than the number of templates.  The templates can be
46   * grouped in directories with common layouts.  Screen modules are
47   * then expected to be placed in packages corresponding with the
48   * templates' directories and follow a specific naming scheme.
49   *
50   * <p>
51   *
52   * The template parameter is parsed and and a Screen whose package
53   * matches the templates path and shares the same name minus any
54   * extension and beginning with a capital letter is searched for.  If
55   * not found, a Screen in a package matching the template's path with
56   * name Default is searched for.  If still not found, a Screen with
57   * name Default is looked for in packages corresponding to parent
58   * directories in the template's path until a match is found.
59   *
60   * <p>
61   *
62   * For example if data.getParameters().getString("template") returns
63   * /about_us/directions/driving.wm, the search follows
64   * about_us.directions.Driving, about_us.directions.Default,
65   * about_us.Default, Default, VelocitySiteScreen.
66   *
67   * <p>
68   *
69   * Only one Layout module is used, since it is expected that any
70   * dynamic content will be placed in navigations and screens.  The
71   * layout template to be used is found in a similar way to the Screen.
72   * For example the following paths will be searched in the layouts
73   * subdirectory: /about_us/directions/driving.wm,
74   * /about_us/directions/default.wm, /about_us/default.wm, /default.wm.
75   *
76   * <p>
77   *
78   * This approach allows a site with largely static content to be
79   * updated and added to regularly by those with little Java
80   * experience.
81   *
82   * <p>
83   *
84   * The code is an almost a complete clone of the FreeMarkerSitePage
85   * written by John McNally.  I've only modified it for Template use.
86   *
87   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
88   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
89   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
90   * @version $Id: DefaultPage.java 1773378 2016-12-09 13:19:59Z tv $
91   */
92  public class DefaultPage
93      extends Page
94  {
95      /** Logging */
96      protected Log log = LogFactory.getLog(this.getClass());
97  
98      /** Injected loader instance */
99      @TurbineLoader( Action.class )
100     protected ActionLoader actionLoader;
101 
102     /** Injected loader instance */
103     @TurbineLoader( Screen.class )
104     protected ScreenLoader screenLoader;
105 
106     /** Injected loader instance */
107     @TurbineLoader( Layout.class )
108     protected LayoutLoader layoutLoader;
109 
110     /**
111      * Builds the Page.
112      *
113      * @param pipelineData Turbine information.
114      * @throws Exception a generic exception.
115      */
116     @Override
117     public void doBuild(PipelineData pipelineData)
118             throws Exception
119     {
120         RunData data = getRunData(pipelineData);
121         // Template pages can use this to set up the context, so it is
122         // available to the Action and Screen.  It does nothing here.
123         doBuildBeforeAction(pipelineData);
124 
125         // If an action has been defined, execute it here.  Actions
126         // can re-define the template definition.
127         if (data.hasAction())
128         {
129             actionLoader.exec(pipelineData, data.getAction());
130         }
131 
132         // if a redirect was setup in data, don't do anything else
133         if (StringUtils.isNotEmpty(data.getRedirectURI()))
134         {
135             return;
136         }
137 
138         // Template pages can use this to set up default templates and
139         // associated class modules.  It does nothing here.
140         doBuildAfterAction(pipelineData);
141 
142         String screenName = data.getScreen();
143 
144         log.debug("Building " + screenName);
145 
146         // Ask the Screen for its Layout and then execute the Layout.
147         // The Screen can override the getLayout() method to re-define
148         // the Layout depending on data passed in via the
149         // data.parameters object.
150         Screen aScreen = screenLoader.getAssembler(screenName);
151         String layout = aScreen.getLayout(pipelineData);
152 
153         // If the Layout has been set to be null, attempt to execute
154         // the Screen that has been defined.
155         if (layout != null)
156         {
157             layoutLoader.exec(pipelineData, layout);
158         }
159         else
160         {
161             screenLoader.exec(pipelineData, screenName);
162         }
163 
164         // Do any post build actions (overridable by subclasses -
165         // does nothing here).
166         doPostBuild(pipelineData);
167     }
168 
169     /**
170      * Can be used by template Pages to stuff the Context into the
171      * PipelineData so that it is available to the Action module and the
172      * Screen module via getContext().  It does nothing here.
173      *
174      * @param pipelineData Turbine information.
175      * @throws Exception a generic exception.
176      */
177     protected void doBuildBeforeAction(PipelineData pipelineData)
178             throws Exception
179     {
180         // do nothing by default
181     }
182 
183     /**
184      * Can be overridden by template Pages to set up data needed to
185      * process a template.  It does nothing here.
186      *
187      * @param pipelineData Turbine information.
188      * @throws Exception a generic exception.
189      */
190     protected void doBuildAfterAction(PipelineData pipelineData)
191             throws Exception
192     {
193         // do nothing by default
194     }
195 
196     /**
197      * Can be overridden to perform actions when the request is
198      * fully processed. It does nothing here.
199      *
200      * @param pipelineData Turbine information.
201      * @throws Exception a generic exception.
202      */
203     protected void doPostBuild(PipelineData pipelineData)
204             throws Exception
205     {
206         // do nothing by default
207     }
208 }