KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InputStream JavaDoc;
52 import java.io.InputStreamReader JavaDoc;
53 import java.io.UnsupportedEncodingException JavaDoc;
54 import java.util.*;
55
56 import com.anthonyeden.lib.config.Configuration;
57 import com.anthonyeden.lib.config.ConfigurationException;
58 import com.anthonyeden.lib.config.sax.SAXConfigurationFactory;
59 import com.anthonyeden.lib.util.MessageUtilities;
60 import org.apache.commons.logging.Log;
61 import org.apache.commons.logging.LogFactory;
62 import org.jpublish.JPublishEngine;
63 import org.jpublish.RequestContext;
64 import org.jpublish.SiteContext;
65 import org.jpublish.action.Action;
66 import org.jpublish.action.ActionWrapper;
67 import org.jpublish.util.Property;
68 import org.jpublish.util.encoding.CharacterEncodingManager;
69
70 /**
71  * A representation of a single web page. A page is defined by an XML document in the pages directory. Each page has a
72  * template associated with the page and can have 0 or more actions attached to the page.
73  *
74  * <p>Actions attached to a page will be triggered each time the page is requested. Actions will be triggered in the
75  * order that they are listed within the Page's configuration.</p>
76  *
77  * <p>There should only be a single PageInstance in memory for each path. Each PageInstance is actually wrapped in a
78  * Page class which provides request-specific features.</p>
79  *
80  * @author Anthony Eden
81  */

82
83 public class PageInstance {
84
85     private Log log = LogFactory.getLog(PageInstance.class);
86
87     private SiteContext siteContext = null;
88     private List pageActions = new ArrayList();
89     private String JavaDoc path = null;
90     private String JavaDoc pageName = null;
91     private String JavaDoc pageType = null;
92     private String JavaDoc pageParent = null;
93     private String JavaDoc templateName = null;
94     private String JavaDoc viewRendererName = null;
95     private String JavaDoc encoding = null;
96     private Map properties = new HashMap();
97
98     /**
99      * Construct a new Page for the given path. The name of the page is the last part of the path (i.e. the file
100      * component) without the dot ending. The page type is the dot-ending.
101      *
102      * @param siteContext The SiteContext
103      * @param path The original request path
104      * @param pageName The name of the page
105      * @param pageType The page type
106      * @param pageParent The page parent directory
107      */

108
109     public PageInstance(SiteContext siteContext, String JavaDoc path, String JavaDoc pageName,
110             String JavaDoc pageType, String JavaDoc pageParent) {
111         this.siteContext = siteContext;
112         this.path = path;
113         log.debug("Page path: " + path);
114         this.pageName = pageName;
115         this.pageType = pageType;
116         this.pageParent = pageParent;
117         log.debug("Page parent: " + pageParent);
118
119         this.templateName = siteContext.getDefaultTemplate();
120
121         CharacterEncodingManager encManager =
122                 siteContext.getCharacterEncodingManager();
123         this.encoding = encManager.getMap(path).getPageEncoding();
124     }
125
126     /**
127      * Get the original request path.
128      *
129      * @return The request path
130      */

131
132     public String JavaDoc getPath() {
133         return path;
134     }
135
136     /**
137      * Get the page name.
138      *
139      * @return The page name
140      */

141
142     public String JavaDoc getPageName() {
143         return pageName;
144     }
145
146     /**
147      * Get the page type.
148      *
149      * @return The page type
150      */

151
152     public String JavaDoc getPageType() {
153         return pageType;
154     }
155
156     /**
157      * Get the page parent.
158      *
159      * @return The page parent
160      */

161
162     public String JavaDoc getPageParent() {
163         return pageParent;
164     }
165
166     /**
167      * Get the full template file name, with the .suffix attached.
168      *
169      * @return The full template name
170      */

171
172     public String JavaDoc getFullTemplateName() {
173         return templateName + "." + pageType;
174     }
175
176     /**
177      * Get the template name. If the template name is not specified in the page configuration then the default template
178      * as specified in the SiteContext will be used.
179      *
180      * @return The template name
181      */

182
183     public String JavaDoc getTemplateName() {
184         return templateName;
185     }
186
187     /**
188      * Set the template name. Invoking this method with a null value will reset the template to the default template as
189      * specified in the SiteContext.
190      *
191      * @param templateName The new template name or null to reset
192      */

193
194     public synchronized void setTemplateName(String JavaDoc templateName) {
195         if (log.isDebugEnabled()) {
196             log.debug("setTemplateName(" + templateName + ")");
197         }
198         if (templateName == null) {
199             templateName = siteContext.getDefaultTemplate();
200             if (log.isDebugEnabled()) {
201                 log.debug("Using default template: " + templateName);
202             }
203         }
204         this.templateName = templateName;
205     }
206
207     /**
208      * Get the view renderer name. This method will return null if the page should use the default view renderer.
209      *
210      * @return The view renderer name or null
211      */

212
213     public String JavaDoc getViewRendererName() {
214         return viewRendererName;
215     }
216
217     /**
218      * Set the view renderer name. Set this value to null to specify that the default view renderer should be used.
219      *
220      * @param viewRendererName The view renderer name or null to reset
221      */

222
223     public void setViewRendererName(String JavaDoc viewRendererName) {
224         this.viewRendererName = viewRendererName;
225     }
226
227     /**
228      * Get a List of page actions. To add an action to the page just add the action to this List. Page actions are
229      * triggered each time the page is requested.
230      *
231      * @return A List of page actions
232      */

233
234     public List getPageActions() {
235         return pageActions;
236     }
237
238     /**
239      * Get the named page property using the default Locale. If the property is not found then return null.
240      *
241      * @param name The property name
242      * @return The value or null
243      */

244
245     public String JavaDoc getProperty(String JavaDoc name) {
246         return getProperty(name, Locale.getDefault());
247     }
248
249     /**
250      * Get the Locale-specific value for the given named property. If the property is not found then return null. This
251      * method will try to find the most suitable locale by searching the property values in the following manner:
252      *
253      * <p> language + "_" + country + "_" + variant<br> language + "_" + country<br> langauge<br> "" </p>
254      *
255      * @param name The property name
256      * @param locale The locale
257      * @return The value
258      */

259
260     public String JavaDoc getProperty(String JavaDoc name, Locale locale) {
261         if (log.isDebugEnabled()) {
262             log.debug("Get property [name=" + name + ",locale=" + locale + "]");
263         }
264         Property property = (Property) properties.get(name);
265         if (property != null) {
266             return property.getValue(locale);
267         } else {
268             return null;
269         }
270     }
271
272     /**
273      * Get the named property. This method is equivilent to the <code>getProperty(name)</code> method. This method is
274      * provided as a convenience to view code.
275      *
276      * @param name The property name
277      * @return The value
278      */

279
280     public String JavaDoc get(String JavaDoc name) {
281         if (log.isDebugEnabled()) {
282             log.debug("get(" + name + ") called to retrieve property");
283         }
284         return getProperty(name);
285     }
286
287     /**
288      * Get the character encoding used for the page. This value is used as the character encoding for all content
289      * loaded by this page.
290      *
291      * @return The character encoding
292      */

293
294     public String JavaDoc getEncoding() {
295         return encoding;
296     }
297
298     /**
299      * Set the character encoding used for the page.
300      *
301      * @param encoding The new character encoding
302      */

303
304     public void setEncoding(String JavaDoc encoding) {
305         this.encoding = encoding;
306     }
307
308     /**
309      * Execute the page actions using the given context.
310      *
311      * @param context The current context
312      */

313
314     public void executeActions(RequestContext context) {
315         Iterator pageActions = getPageActions().iterator();
316         while (pageActions.hasNext()) {
317             ((ActionWrapper) pageActions.next()).execute(context);
318             if (context.getStopProcessing() != null) return;
319         }
320     }
321
322     /**
323      * Load the page configuration from the page's XML stream.
324      *
325      * @param in The InputStream
326      * @throws ConfigurationException
327      */

328
329     public synchronized void load(InputStream JavaDoc in)
330             throws ConfigurationException {
331         try {
332             log.debug("Loading page with encoding " + getEncoding());
333             Configuration configuration =
334                     SAXConfigurationFactory.getInstance().getConfiguration(path,
335                             new InputStreamReader JavaDoc(in, getEncoding()));
336             loadConfiguration(configuration);
337         } catch (UnsupportedEncodingException JavaDoc e) {
338             throw new ConfigurationException("Error loading page configuration", e);
339         }
340     }
341
342     /**
343      * Load the page configuration from the given Configuration object.
344      *
345      * @param configuration The Configuration object
346      * @throws ConfigurationException
347      */

348
349     public synchronized void loadConfiguration(Configuration configuration)
350             throws ConfigurationException {
351         pageActions.clear();
352
353         setTemplateName(configuration.getChildValue("template"));
354         setViewRendererName(configuration.getChildValue("view-renderer"));
355         
356         // load page actions
357
log.debug("Looping through page-action elements.");
358         Iterator pageActionElements =
359                 configuration.getChildren("page-action").iterator();
360         while (pageActionElements.hasNext()) {
361             Configuration pageActionElement =
362                     (Configuration) pageActionElements.next();
363             String JavaDoc name = pageActionElement.getAttribute("name");
364
365             if (name == null) {
366                 Object JavaDoc[] args = {};
367                 String JavaDoc msg = MessageUtilities.getMessage(getClass(),
368                         JPublishEngine.MESSAGE_PACKAGE, "actionNameRequired",
369                         args);
370                 throw new ConfigurationException(msg, pageActionElement);
371             }
372
373             Action action = siteContext.getActionManager().findAction(name);
374
375             pageActions.add(new ActionWrapper(action, pageActionElement));
376         }
377         
378         // load page properties
379
log.debug("Loading page properties");
380         Iterator propertyElements =
381                 configuration.getChildren("property").iterator();
382         while (propertyElements.hasNext()) {
383             Configuration propertyElement =
384                     (Configuration) propertyElements.next();
385             setProperty(propertyElement.getAttribute("name"),
386                     propertyElement.getValue(),
387                     propertyElement.getAttribute("locale"));
388         }
389     }
390
391     /**
392      * Set the property value.
393      *
394      * @param name The property name
395      * @param value The value
396      * @param locale The locale String or null
397      */

398
399     private void setProperty(String JavaDoc name, String JavaDoc value, String JavaDoc locale) {
400         if (log.isDebugEnabled()) {
401             log.debug("setProperty() [name=" + name + ",value=" + value +
402                     ",locale=" + locale);
403         }
404         Property property = (Property) properties.get(name);
405         if (property == null) {
406             // named property not in property map
407
property = new Property(name);
408             properties.put(name, property);
409         }
410         property.setValue(value, locale);
411     }
412
413 }
414
Popular Tags