1 package org.apache.turbine.modules.pages;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.Vector;
58
59 // Turbine Classes
60 import org.apache.turbine.modules.ActionLoader;
61 import org.apache.turbine.modules.LayoutLoader;
62 import org.apache.turbine.modules.Page;
63 import org.apache.turbine.modules.Screen;
64 import org.apache.turbine.modules.ScreenLoader;
65 import org.apache.turbine.services.resources.TurbineResources;
66 import org.apache.turbine.util.RunData;
67
68 // ECS Stuff
69 import org.apache.ecs.Doctype;
70
71
72 /***
73 * When building sites using templates, Screens need only be defined
74 * for templates which require dynamic (database or object) data.
75 *
76 * <p>
77 *
78 * This page can be used on sites where the number of Screens can be
79 * much less than the number of templates. The templates can be
80 * grouped in directories with common layouts. Screen modules are
81 * then expected to be placed in packages corresponding with the
82 * templates' directories and follow a specific naming scheme.
83 *
84 * <p>
85 *
86 * The template parameter is parsed and and a Screen whose package
87 * matches the templates path and shares the same name minus any
88 * extension and beginning with a capital letter is searched for. If
89 * not found, a Screen in a package matching the template's path with
90 * name Default is searched for. If still not found, a Screen with
91 * name Default is looked for in packages corresponding to parent
92 * directories in the template's path until a match is found.
93 *
94 * <p>
95 *
96 * For example if data.getParameters().getString("template") returns
97 * /about_us/directions/driving.wm, the search follows
98 * about_us.directions.Driving, about_us.directions.Default,
99 * about_us.Default, Default, WebMacroSiteScreen.
100 *
101 * <p>
102 *
103 * Only one Layout module is used, since it is expected that any
104 * dynamic content will be placed in navigations and screens. The
105 * layout template to be used is found in a similar way to the Screen.
106 * For example the following paths will be searched in the layouts
107 * subdirectory: /about_us/directions/driving.wm,
108 * /about_us/directions/default.wm, /about_us/default.wm, /default.wm.
109 *
110 * <p>
111 *
112 * This approach allows a site with largely static content to be
113 * updated and added to regularly by those with little Java
114 * experience.
115 *
116 * <p>
117 *
118 * The code is an almost a complete clone of the FreeMarkerSitePage
119 * written by John McNally. I've only modified it for WebMacro use.
120 *
121 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
122 * @version $Id: DefaultPage.java,v 1.2 2002/03/29 02:00:02 jmcnally Exp $
123 */
124 public class DefaultPage extends Page
125 {
126 /***
127 * Builds the Page.
128 *
129 * @param data Turbine information.
130 * @exception Exception, a generic exception.
131 */
132 public void doBuild( RunData data )
133 throws Exception
134 {
135 // Template pages can use this to set up the context, so it is
136 // available to the Action and Screen. It does nothing here.
137 doBuildBeforeAction(data);
138
139 // If an action has been defined, execute it here. Actions
140 // can re-define the template definition.
141 if ( data.hasAction() )
142 {
143 ActionLoader.getInstance().exec ( data, data.getAction() );
144 }
145
146 // if a redirect was setup in data, don't do anything else
147 if ((data.getRedirectURI() != null) && (data.getRedirectURI().length() > 0)) return;
148
149 // Set the default doctype from the value given in
150 // TurbineResources.properties.
151 setDefaultDoctype(data);
152
153 // Template pages can use this to set up default templates and
154 // associated class modules. It does nothing here.
155 doBuildAfterAction(data);
156
157 // Ask the Screen for its Layout and then execute the Layout.
158 // The Screen can override the getLayout() method to re-define
159 // the Layout depending on data passed in via the
160 // data.parameters object.
161 ScreenLoader sl = ScreenLoader.getInstance();
162 Screen aScreen = sl.getInstance(data.getScreen());
163 String layout = aScreen.getLayout(data);
164
165 // If the Layout has been set to be null, attempt to execute
166 // the Screen that has been defined.
167 if ( layout != null )
168 {
169 LayoutLoader.getInstance().exec ( data, layout );
170 }
171 else
172 {
173 ScreenLoader.getInstance().exec ( data, data.getScreen() );
174 }
175
176 // Do any post build actions (overridable by subclasses -
177 // does nothing here).
178 doPostBuild(data);
179 }
180
181 /***
182 * Can be used by template Pages to stuff the Context into the
183 * RunData so that it is available to the Action module and the
184 * Screen module via getContext(). It does nothing here.
185 *
186 * @param data Turbine information.
187 * @exception Exception, a generic exception.
188 */
189 protected void doBuildBeforeAction(RunData data)
190 throws Exception
191 {
192 }
193
194 /***
195 * Can be overridden by template Pages to set up data needed to
196 * process a template. It does nothing here.
197 *
198 * @param data Turbine information.
199 * @exception Exception, a generic exception.
200 */
201 protected void doBuildAfterAction(RunData data)
202 throws Exception
203 {
204 }
205
206 /***
207 * Can be overridden to perform actions when the request is
208 * fully processed. It does nothing here.
209 *
210 * @param data Turbine information.
211 * @exception Exception, a generic exception.
212 */
213 protected void doPostBuild(RunData data)
214 throws Exception
215 {
216 }
217
218 /***
219 * Set the default Doctype. If Doctype is set to null, it will
220 * not be added. The default Doctype can be set in
221 * TurbineResources by using the single strings: Html40Strict,
222 * Html40Transitional, or Html40Frameset. Additionally the
223 * default can be supplied as two strings giving the dtd and uri.
224 *
225 * @param data Turbine information.
226 * @exception Exception, a generic exception.
227 */
228 private void setDefaultDoctype( RunData data )
229 throws Exception
230 {
231 String errMsg =
232 "default.doctype property not set properly in TurbineResources.";
233 Vector doctypeProperty =
234 TurbineResources.getVector("default.doctype", null);
235 if (doctypeProperty != null && doctypeProperty.size() > 0)
236 {
237 if (doctypeProperty.size() == 1)
238 {
239 String doc = (String)doctypeProperty.firstElement();
240 if (doc.equalsIgnoreCase("Html40Transitional"))
241 {
242 data.getPage().setDoctype(new Doctype.Html40Transitional());
243 }
244 else if (doc.equalsIgnoreCase("Html40Strict"))
245 {
246 data.getPage().setDoctype(new Doctype.Html40Strict());
247 }
248 else if (doc.equalsIgnoreCase("Html40Frameset"))
249 {
250 data.getPage().setDoctype(new Doctype.Html40Frameset());
251 }
252 else
253 {
254 throw new Exception(errMsg);
255 }
256 }
257 else if (doctypeProperty.size() == 2)
258 {
259 data.getPage().setDoctype(
260 new Doctype()
261 .setIdentifier((String)doctypeProperty.elementAt(0))
262 .setUri((String)doctypeProperty.elementAt(1)));
263 }
264 else
265 {
266 throw new Exception(errMsg);
267 }
268 }
269 }
270 }
This page was automatically generated by Maven