KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > page > Page


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.page;
50
51 import java.util.HashMap JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Locale JavaDoc;
54 import java.util.Map JavaDoc;
55
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.jpublish.RequestContext;
59 import org.jpublish.util.Property;
60
61 /**
62  * This class represents the current Page. A new Page object is created for each request. Each Page object actually
63  * wraps a shared PageInstance object. Methods which modify the Page's state are only effective for the current request.
64  * To change the shared PageInstance state use the <code>getPageInstance()</code> method to retrieve the PageInstance
65  * object and make changes.
66  *
67  * <p>Note that the methods in the Page class are not synchronized because they should not be accessed from more than a
68  * single thread (the current request thread).
69  *
70  * @author Anthony Eden
71  */

72
73 public class Page {
74
75     private Log log = LogFactory.getLog(Page.class);
76
77     private PageInstance pageInstance = null;
78
79     private Map JavaDoc properties = new HashMap JavaDoc();
80     private String JavaDoc templateName = null;
81     private String JavaDoc viewRendererName = null;
82     private Locale JavaDoc locale = Locale.getDefault();
83     private String JavaDoc encoding = null;
84     private String JavaDoc pageType = null;
85     private String JavaDoc pageName = null;
86     private String JavaDoc pageParent = null;
87
88     /**
89      * Construct a new Page.
90      *
91      * @param pageInstance The wrapped PageInstance
92      */

93
94     public Page(PageInstance pageInstance) {
95         this.pageInstance = pageInstance;
96     }
97
98     /**
99      * Get the request path.
100      *
101      * @return The request path
102      */

103
104     public String JavaDoc getPath() {
105         if (pageName != null || pageType != null || pageParent != null) {
106             StringBuffer JavaDoc tempPath = new StringBuffer JavaDoc();
107             String JavaDoc pageParent = getPageParent();
108             log.debug("Page parent: " + pageParent);
109             if (!pageParent.equals("")) {
110                 tempPath.append(pageParent);
111                 tempPath.append("/");
112             }
113             String JavaDoc pageName = getPageName();
114             log.debug("Page name: " + pageName);
115             tempPath.append(pageName);
116             tempPath.append(".");
117             tempPath.append(getPageType());
118             log.debug("Generated path: " + tempPath);
119             return tempPath.toString();
120         } else {
121             return pageInstance.getPath();
122         }
123     }
124
125     /**
126      * Get the page name.
127      *
128      * @return The page name
129      */

130
131     public String JavaDoc getPageName() {
132         if (pageName != null) {
133             return pageName;
134         } else {
135             return pageInstance.getPageName();
136         }
137     }
138
139     /**
140      * Set the page name. This value will only be valid for the current request.
141      *
142      * @param pageName The page name
143      */

144
145     public void setPageName(String JavaDoc pageName) {
146         this.pageName = pageName;
147     }
148
149     /**
150      * Get the page type. The page type is used to determine which template and content to use for rendering. For
151      * example, in a request for index.html the page type is html and the page name is index.
152      *
153      * @return The page type
154      */

155
156     public String JavaDoc getPageType() {
157         if (pageType == null) {
158             return pageInstance.getPageType();
159         } else {
160             return pageType;
161         }
162     }
163
164     /**
165      * Set the page type. This value will only exist for this request.
166      *
167      * @param pageType The page type for this request
168      */

169
170     public void setPageType(String JavaDoc pageType) {
171         this.pageType = pageType;
172     }
173
174     /**
175      * Get the page parent.
176      *
177      * @return The the page parent
178      */

179
180     public String JavaDoc getPageParent() {
181         if (pageParent != null) {
182             return pageParent;
183         } else {
184             return pageInstance.getPageParent();
185         }
186     }
187
188     /**
189      * Set the page parent. Any modification of this value will only exist for the current request.
190      *
191      * @param pageParent The page parent path
192      */

193
194     public void setPageParent(String JavaDoc pageParent) {
195         this.pageParent = pageParent;
196     }
197
198     /**
199      * Get the wrapped PageInstance.
200      *
201      * @return The PageInstance
202      */

203
204     public PageInstance getPageInstance() {
205         return pageInstance;
206     }
207
208     /**
209      * Get the full template file name, with the .suffix attached.
210      *
211      * @return The full template name
212      */

213
214     public String JavaDoc getFullTemplateName() {
215         return getTemplateName() + "." + getPageType();
216     }
217
218     /**
219      * Get the template name. If the template name is not specified in the page configuration then the default template
220      * as specified in the SiteContext will be used.
221      *
222      * @return The template name
223      */

224
225     public String JavaDoc getTemplateName() {
226         if (templateName == null) {
227             templateName = pageInstance.getTemplateName();
228         }
229         return templateName;
230     }
231
232     /**
233      * Set the template name. Invoking this method with a null value will reset the template to the default template as
234      * specified in the SiteContext.
235      *
236      * @param templateName The new template name or null to reset
237      */

238
239     public void setTemplateName(String JavaDoc templateName) {
240         this.templateName = templateName;
241     }
242
243     /**
244      * Get the view renderer name. This method will return null if the page should use the default view renderer.
245      *
246      * @return The view renderer name or null
247      */

248
249     public String JavaDoc getViewRendererName() {
250         log.debug("getViewRendererName()");
251         if (viewRendererName == null) {
252             log.debug("Setting view renderer name to page instance value");
253             viewRendererName = pageInstance.getViewRendererName();
254             if (log.isDebugEnabled()) {
255                 log.debug("View renderer name = " + viewRendererName);
256             }
257         }
258         return viewRendererName;
259     }
260
261     /**
262      * Set the view renderer name. Set this value to null to specify that the default view renderer should be used.
263      *
264      * @param viewRendererName The view renderer name or null to reset
265      */

266
267     public void setViewRendererName(String JavaDoc viewRendererName) {
268         this.viewRendererName = viewRendererName;
269     }
270
271     /**
272      * Get a List of page actions. To add an action to the page just add the action to this List. Page actions are
273      * triggered each time the page is requested.
274      *
275      * @return A List of page actions
276      */

277
278     public List JavaDoc getPageActions() {
279         return pageInstance.getPageActions();
280     }
281
282     /**
283      * Get the named page property using the default Locale. If the property is not found then return null.
284      *
285      * @param name The property name
286      * @return The value or null
287      */

288
289     public String JavaDoc getProperty(String JavaDoc name) {
290         return getProperty(name, getLocale());
291     }
292
293     /**
294      * Get the Locale-specific value for the given named property. If the property is not found then return null. This
295      * method will try to find the most suitable locale by searching the property values in the following manner:
296      *
297      * <p> language + "_" + country + "_" + variant<br> language + "_" + country<br> langauge<br> "" </p>
298      *
299      * @param name The property name
300      * @param locale The locale
301      * @return The value
302      */

303
304     public String JavaDoc getProperty(String JavaDoc name, Locale JavaDoc locale) {
305         Property property = (Property) properties.get(name);
306         if (property == null) {
307             String JavaDoc value = pageInstance.getProperty(name, locale);
308             if (value != null) {
309                 setProperty(name, value, locale);
310                 return value;
311             }
312         }
313
314         if (property == null) {
315             return null;
316         } else {
317             return property.getValue(locale);
318         }
319     }
320
321     /**
322      * Get the named property. This method is equivilent to the <code>getProperty(name)</code> method. This method is
323      * provided as a convenience to view code so that views which support dot-notation or a a similar direct property
324      * access notation can use this.
325      *
326      * @param name The property name
327      * @return The value
328      */

329
330     public String JavaDoc get(String JavaDoc name) {
331         return getProperty(name);
332     }
333
334     /**
335      * Set the property value.
336      *
337      * @param name The property name
338      * @param value The property value
339      */

340
341     public void setProperty(String JavaDoc name, String JavaDoc value) {
342         setProperty(name, value, getLocale());
343     }
344
345     /**
346      * Set the property value.
347      *
348      * @param name The property name
349      * @param value The property value
350      * @param locale The Locale
351      */

352
353     public void setProperty(String JavaDoc name, String JavaDoc value, Locale JavaDoc locale) {
354         Property property = (Property) properties.get(name);
355         if (property == null) {
356             // named property not in property map
357
property = new Property(name);
358             properties.put(name, property);
359         }
360         property.setValue(value, locale);
361     }
362
363     /**
364      * Get the current page's locale.
365      *
366      * @return The Locale
367      */

368
369     public Locale JavaDoc getLocale() {
370         return locale;
371     }
372
373     /**
374      * Set the current page's locale. This locale is used when retrieving page properties. Set to null to use the
375      * system default locale.
376      *
377      * @param locale The new locale
378      */

379
380     public void setLocale(Locale JavaDoc locale) {
381         if (locale == null) {
382             locale = Locale.getDefault();
383         }
384         this.locale = locale;
385     }
386
387     /**
388      * Get the character encoding used for the page. This value is used as the character encoding for all content
389      * loaded by this page.
390      *
391      * @return The character encoding
392      */

393
394     public String JavaDoc getEncoding() {
395         if (encoding == null) {
396             encoding = pageInstance.getEncoding();
397         }
398         return encoding;
399     }
400
401     /**
402      * Set the character encoding for this page.
403      *
404      * @param encoding The new character encoding
405      */

406
407     public void setEncoding(String JavaDoc encoding) {
408         this.encoding = encoding;
409     }
410
411     /**
412      * Execute the page actions using the given context.
413      *
414      * @param context The current context
415      */

416
417     public void executeActions(RequestContext context) {
418         pageInstance.executeActions(context);
419     }
420
421 }
422
Popular Tags