PageRenderTime 104ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/java/src/org/jpublish/page/PageInstance.java

http://jpublish.googlecode.com/
Java | 394 lines | 160 code | 58 blank | 176 comment | 33 complexity | e8de0370e28312f1a0a6070edadc4422 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. * Copyright 2004-2007 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.jpublish.page;
  18. import com.anthonyeden.lib.config.Configuration;
  19. import com.anthonyeden.lib.config.ConfigurationException;
  20. import com.anthonyeden.lib.config.XMLConfiguration;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.jpublish.JPublishContext;
  24. import org.jpublish.SiteContext;
  25. import org.jpublish.action.Action;
  26. import org.jpublish.action.ActionWrapper;
  27. import java.io.InputStream;
  28. import java.util.*;
  29. /**
  30. * A representation of a single web page. A page is defined by an XML
  31. * document in the pages directory. Each page has a template associated
  32. * with the page and can have 0 or more actions attached to the page.
  33. * <p/>
  34. * <p>Actions attached to a page will be triggered each time the page
  35. * is requested. Actions will be triggered in the order that they are
  36. * listed within the Page's configuration.</p>
  37. * <p/>
  38. * <p>There should only be a single PageInstance in memory for each path.
  39. * Each PageInstance is actually wrapped in a Page class which provides
  40. * request-specific features.
  41. *
  42. * @author Anthony Eden
  43. * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
  44. */
  45. public class PageInstance {
  46. private static Log log = LogFactory.getLog(PageInstance.class);
  47. private SiteContext siteContext;
  48. private List pageActions;
  49. private String path;
  50. private String pageName;
  51. private String pageType;
  52. private String templateName;
  53. private Map properties;
  54. private Configuration configuration = null;
  55. /**
  56. * Construct a new Page for the given path. The name of the page is
  57. * the last part of the path (i.e. the file component) without the
  58. * dot ending. The page type is the dot-ending.
  59. *
  60. * @param siteContext The SiteContext
  61. * @param path The request path
  62. * @param pageName The name of the page
  63. * @param pageType The page type
  64. */
  65. public PageInstance(SiteContext siteContext, String path, String pageName, String pageType) {
  66. this.siteContext = siteContext;
  67. this.path = path;
  68. this.pageName = pageName;
  69. this.pageType = pageType;
  70. this.pageActions = new ArrayList();
  71. this.templateName = siteContext.getDefaultTemplate();
  72. this.properties = new HashMap();
  73. }
  74. /**
  75. * Get the request path.
  76. *
  77. * @return The request path
  78. */
  79. public String getPath() {
  80. return path;
  81. }
  82. /**
  83. * Get the page name.
  84. *
  85. * @return The page name
  86. */
  87. public String getPageName() {
  88. return pageName;
  89. }
  90. /**
  91. * Get the page type.
  92. *
  93. * @return The page type
  94. */
  95. public String getPageType() {
  96. return pageType;
  97. }
  98. /**
  99. * Return the page title. Initially the page title is extracted from
  100. * the page's definition document, however it can be set programtically
  101. * at runtime.
  102. * <p/>
  103. * <p>This method is deprecated. Use getProperty("title") instead and
  104. * include a property named title in the page configuration file. The
  105. * old &lt;title&gt; element and this method will be removed for the
  106. * 1.0 release.
  107. *
  108. * @return The page title
  109. * @deprecated Use getProperty("title") instead
  110. */
  111. public String getTitle() {
  112. String title = getProperty("Title");
  113. if (title == null) {
  114. title = getProperty("title");
  115. }
  116. return title;
  117. }
  118. /**
  119. * Set the title. This will temporarily alter the page's title.
  120. * <p/>
  121. * <p>This method is deprecated. The old &lt;title&gt; element
  122. * and this method will be removed for the 1.0 release.
  123. *
  124. * @param title The page title
  125. * @deprecated
  126. */
  127. public void setTitle(String title) {
  128. if (title != null) {
  129. if (log.isDebugEnabled())
  130. log.debug("setTitle(" + title + ")");
  131. setProperty("title", title, null);
  132. }
  133. }
  134. /**
  135. * Get the full template file name, with the .suffix attached.
  136. *
  137. * @return The full template name
  138. */
  139. public String getFullTemplateName() {
  140. return templateName + "." + pageType;
  141. }
  142. /**
  143. * Get the template name. If the template name is not specified in the
  144. * page configuration then the default template as specified in the
  145. * SiteContext will be used.
  146. *
  147. * @return The template name
  148. */
  149. public String getTemplateName() {
  150. return templateName;
  151. }
  152. /**
  153. * Set the template name. Invoking this method with a null value will
  154. * reset the template to the default template as specified in the
  155. * SiteContext.
  156. *
  157. * @param templateName The new template name or null to reset
  158. */
  159. public synchronized void setTemplateName(String templateName) {
  160. if (log.isDebugEnabled())
  161. log.debug("setTemplateName(" + templateName + ")");
  162. if (templateName == null) {
  163. templateName = siteContext.getDefaultTemplate();
  164. if (log.isDebugEnabled())
  165. log.debug("Using default template: " + templateName);
  166. }
  167. this.templateName = templateName;
  168. }
  169. /**
  170. * Get a List of page actions. To add an action to the page just add
  171. * the action to this List. Page actions are triggered each time the
  172. * page is requested.
  173. *
  174. * @return A List of page actions
  175. */
  176. public List getPageActions() {
  177. return pageActions;
  178. }
  179. /**
  180. * Get the named page property using the default Locale. If the
  181. * property is not found then return null.
  182. *
  183. * @param name The property name
  184. * @return The value or null
  185. */
  186. public String getProperty(String name) {
  187. return getProperty(name, Locale.getDefault());
  188. }
  189. /**
  190. * Get the Locale-specific value for the given named property. If
  191. * the property is not found then return null. This method will try to
  192. * find the most suitable locale by searching the property values in the
  193. * following manner:
  194. * <p/>
  195. * <p/>
  196. * language + "_" + country + "_" + variant<br>
  197. * language + "_" + country<br>
  198. * langauge<br>
  199. * ""
  200. * </p>
  201. *
  202. * @param name The property name
  203. * @param locale The locale
  204. * @return The value
  205. */
  206. public String getProperty(String name, Locale locale) {
  207. if (log.isDebugEnabled())
  208. log.debug("Get property [name=" + name + ",locale=" + locale + "]");
  209. PageProperty property = (PageProperty) properties.get(name);
  210. if (property != null) {
  211. return property.getValue(locale);
  212. } else {
  213. return null;
  214. }
  215. }
  216. /**
  217. * Get the named property. This method is equivilent to the
  218. * <code>getProperty(name)</code> method. This method is provided
  219. * as a convenience to view code.
  220. *
  221. * @param name The property name
  222. * @return The value
  223. */
  224. public String get(String name) {
  225. return getProperty(name);
  226. }
  227. /**
  228. * Execute the page actions using the given context.
  229. *
  230. * @param context The current context
  231. * @return A redirection value or null if there is no redirection
  232. * @throws Exception Any Exception which occurs while executing the action
  233. */
  234. public String executeActions(JPublishContext context) throws Exception {
  235. Iterator pageActions = getPageActions().iterator();
  236. while (pageActions.hasNext()) {
  237. ((ActionWrapper) pageActions.next()).execute(context);
  238. String redirect = (String) context.get("redirect");
  239. if (redirect != null) {
  240. return redirect;
  241. }
  242. }
  243. return null;
  244. }
  245. /**
  246. * Load the page configuration from the page's XML stream.
  247. *
  248. * @param in The InputStream
  249. * @throws Exception Any exception
  250. */
  251. public synchronized void load(InputStream in) throws Exception {
  252. if (log.isDebugEnabled())
  253. log.debug("Loading page: " + path);
  254. Configuration configuration = new XMLConfiguration(in);
  255. loadConfiguration(configuration);
  256. }
  257. /**
  258. * Load the page configuration from the given Configuration object.
  259. *
  260. * @param configuration The Configuration object
  261. * @throws ConfigurationException
  262. */
  263. public synchronized void loadConfiguration(Configuration configuration) throws ConfigurationException {
  264. if (log.isDebugEnabled())
  265. log.debug("Loading page: " + path);
  266. this.configuration = configuration;
  267. setTitle(configuration.getChildValue("title"));
  268. setTemplateName(configuration.getChildValue("template"));
  269. // load page actions
  270. if (log.isDebugEnabled())
  271. log.debug("Looping through page-action elements.");
  272. Iterator pageActionElements = configuration.getChildren("page-action").iterator();
  273. while (pageActionElements.hasNext()) {
  274. Configuration pageActionElement = (Configuration) pageActionElements.next();
  275. String name = pageActionElement.getAttribute("name");
  276. if (name == null) {
  277. throw new ConfigurationException("Error configuring page-action.");
  278. }
  279. Action action = siteContext.getActionManager().findAction(name);
  280. if (action == null) {
  281. throw new ConfigurationException("Action " + name + " not defined");
  282. }
  283. pageActions.add(new ActionWrapper(action, pageActionElement));
  284. }
  285. // load page properties
  286. if (log.isDebugEnabled())
  287. log.debug("Loading page properties");
  288. Iterator propertyElements = configuration.getChildren("property").iterator();
  289. String value = null;
  290. while (propertyElements.hasNext()) {
  291. Configuration propertyElement = (Configuration) propertyElements.next();
  292. value = propertyElement.getAttribute("value");
  293. setProperty(
  294. propertyElement.getAttribute("name"),
  295. value == null ? propertyElement.getValue() : value, propertyElement.getAttribute("locale"));
  296. }
  297. }
  298. /**
  299. * Set the property value.
  300. *
  301. * @param name The property name
  302. * @param value The value
  303. * @param locale The locale String or null
  304. */
  305. public void setProperty(String name, String value, String locale) {
  306. if (log.isDebugEnabled())
  307. log.debug("setProperty() [name=" + name + ",value=" + value +
  308. ",locale=" + locale);
  309. PageProperty property = (PageProperty) properties.get(name);
  310. if (property == null) {
  311. // named property not in property map
  312. property = new PageProperty(name);
  313. properties.put(name, property);
  314. }
  315. property.setValue(value, locale);
  316. }
  317. /**
  318. * expose a readOnly view containing the current page instance properties
  319. *
  320. * @return a readonly view to properties [Florin]
  321. */
  322. public Map getProperties() {
  323. Map readOnlyProperties = null;
  324. if (properties != null && !properties.isEmpty())
  325. readOnlyProperties = Collections.unmodifiableMap(properties);
  326. return readOnlyProperties;
  327. }
  328. /**
  329. * offers access to the page configuration
  330. *
  331. * @return the page configuration
  332. */
  333. public Configuration getConfiguration() {
  334. return configuration;
  335. }
  336. }