001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017:
018: package org.jpublish;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.jpublish.page.PageInstance;
023: import org.jpublish.page.PageProperty;
024:
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.Locale;
028: import java.util.Map;
029:
030: /**
031: * This class represents the current Page. A new Page object is created
032: * for each request. Each Page object actually wraps a shared PageInstance
033: * object. Methods which modify the Page's state are only effective for the
034: * current request. To change the shared PageInstance state use the
035: * <code>getPageInstance()</code> method to retrieve the PageInstance object
036: * and make changes.
037: *
038: * @author Anthony Eden
039: * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
040: */
041:
042: public class Page {
043:
044: private static final Log log = LogFactory.getLog(Page.class);
045:
046: private PageInstance pageInstance;
047:
048: private Map properties;
049: private String templateName;
050: private Locale locale = Locale.getDefault();
051:
052: /**
053: * Construct a new Page.
054: *
055: * @param pageInstance The wrapped PageInstance
056: */
057: public Page(PageInstance pageInstance) {
058: this .pageInstance = pageInstance;
059:
060: this .properties = new HashMap();
061: this .locale = Locale.getDefault();
062: }
063:
064: /**
065: * Get the request path.
066: *
067: * @return The request path
068: */
069: public String getPath() {
070: return pageInstance.getPath();
071: }
072:
073: /**
074: * Get the page name.
075: *
076: * @return The page name
077: */
078: public String getPageName() {
079: return pageInstance.getPageName();
080: }
081:
082: /**
083: * Get the page type.
084: *
085: * @return The page type
086: */
087: public String getPageType() {
088: return pageInstance.getPageType();
089: }
090:
091: /**
092: * Get the wrapped PageInstance.
093: *
094: * @return The PageInstance
095: */
096: public PageInstance getPageInstance() {
097: return pageInstance;
098: }
099:
100: /**
101: * Return the page title. Initially the page title is extracted from
102: * the page's definition document, however it can be set programtically
103: * at runtime.
104: * <p/>
105: * <p>This method is deprecated. Use getProperty("title") instead and
106: * include a property named title in the page configuration file. The
107: * old <title> element and this method will be removed for the
108: * 1.0 release.
109: *
110: * @return The page title
111: * @deprecated Use getProperty("title") instead
112: */
113: public String getTitle() {
114: return getProperty("title");
115: }
116:
117: /**
118: * Set the title. This will temporarily alter the page's title.
119: * <p/>
120: * <p>This method is deprecated. The old <title> element
121: * and this method will be removed for the 1.0 release.
122: *
123: * @param title The page title
124: * @deprecated
125: */
126: public void setTitle(String title) {
127: setProperty("title", title);
128: }
129:
130: /**
131: * Get the full template file name, with the .suffix attached.
132: *
133: * @return The full template name
134: */
135: public String getFullTemplateName() {
136: return getTemplateName() + "." + getPageType();
137: }
138:
139: /**
140: * Get the template name. If the template name is not specified in the
141: * page configuration then the default template as specified in the
142: * SiteContext will be used.
143: *
144: * @return The template name
145: */
146: public String getTemplateName() {
147: if (templateName == null) {
148: templateName = pageInstance.getTemplateName();
149: }
150: return templateName;
151: }
152:
153: /**
154: * Set the template name. Invoking this method with a null value will
155: * reset the template to the default template as specified in the
156: * SiteContext.
157: *
158: * @param templateName The new template name or null to reset
159: */
160: public void setTemplateName(String templateName) {
161: this .templateName = templateName;
162: }
163:
164: /**
165: * Get a List of page actions. To add an action to the page just add
166: * the action to this List. Page actions are triggered each time the
167: * page is requested.
168: *
169: * @return A List of page actions
170: */
171: public List getPageActions() {
172: return pageInstance.getPageActions();
173: }
174:
175: /**
176: * Get the named page property using the default Locale. If the
177: * property is not found then return null.
178: *
179: * @param name The property name
180: * @return The value or null
181: */
182: public String getProperty(String name) {
183: return getProperty(name, getLocale());
184: }
185:
186: /**
187: * Get the Locale-specific value for the given named property. If
188: * the property is not found then return null. This method will try to find
189: * the most suitable locale by searching the property values in the
190: * following manner:
191: * <p/>
192: * <p/>
193: * language + "_" + country + "_" + variant<br>
194: * language + "_" + country<br>
195: * langauge<br>
196: * ""
197: * </p>
198: *
199: * @param name The property name
200: * @param locale The locale
201: * @return The value
202: */
203: public String getProperty(String name, Locale locale) {
204: if (log.isDebugEnabled())
205: log.debug("getProperty(" + name + "," + locale + ")");
206:
207: PageProperty pageProperty = (PageProperty) properties.get(name);
208: if (pageProperty == null) {
209: log.debug("Looking for property in PageInstance");
210: String value = pageInstance.getProperty(name, locale);
211: if (value != null) {
212: log.debug("Found property in page instance");
213: setProperty(name, value, locale);
214: return value;
215: }
216: }
217:
218: if (pageProperty == null) {
219: return null;
220: } else {
221: return pageProperty.getValue(locale);
222: }
223: }
224:
225: /**
226: * Get the named property. This method is equivilent to the
227: * <code>getProperty(name)</code> method. This method is provided
228: * as a convenience to view code.
229: *
230: * @param name The property name
231: * @return The value
232: */
233: public String get(String name) {
234: return getProperty(name);
235: }
236:
237: /**
238: * Execute the page actions using the given context.
239: *
240: * @param context The current context
241: * @return A redirection value or null if there is no redirection
242: * @throws Exception Any Exception which occurs while executing the action
243: */
244: public String executeActions(JPublishContext context)
245: throws Exception {
246: return pageInstance.executeActions(context);
247: }
248:
249: /**
250: * Get the current page's locale.
251: *
252: * @return The Locale
253: */
254: public Locale getLocale() {
255: return locale;
256: }
257:
258: /**
259: * Set the current page's locale. This locale is used when retrieving
260: * page properties.
261: *
262: * @param locale
263: */
264: public void setLocale(Locale locale) {
265: if (locale == null) {
266: locale = Locale.getDefault();
267: }
268: this .locale = locale;
269: }
270:
271: /**
272: * Set the property value.
273: *
274: * @param name The property name
275: * @param value The property value
276: */
277: public void setProperty(String name, String value) {
278: setProperty(name, value, getLocale());
279: }
280:
281: /**
282: * Set the property value.
283: *
284: * @param name The property name
285: * @param value The property value
286: * @param locale The Locale
287: */
288:
289: public void setProperty(String name, String value, Locale locale) {
290: // if(log.isDebugEnabled())
291: // log.debug("setProperty(" + name + "," + value + "," + locale + ")");
292: //
293: PageProperty property = (PageProperty) properties.get(name);
294: if (property == null) {
295: // named property not in property map
296: property = new PageProperty(name);
297: properties.put(name, property);
298: }
299: property.setValue(value, locale);
300: }
301:
302: /**
303: * @return a read/write Map containing the page properties
304: */
305: public Map getProperties() {
306: return this.properties;
307: }
308: }
|