KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > parser > AbstractPage


1 /*
2  * Title: AbstractPage
3  * Description:
4  *
5  * This software is published under the terms of the OpenSymphony Software
6  * License version 1.1, of which a copy has been included with this
7  * distribution in the LICENSE.txt file.
8  */

9
10 package com.opensymphony.module.sitemesh.parser;
11
12 import com.opensymphony.module.sitemesh.Page;
13
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
16 import java.io.*;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 /**
22  * Abstract implementation of {@link com.opensymphony.module.sitemesh.Page} .
23  *
24  * <p>Contains base methods for storing and accessing page properties.
25  * Also stores {@link #pageData} as byte[] and implements write???()
26  * methods.</p>
27  *
28  * <p>Concrete implementations need only set the {@link #pageData} and
29  * call {@link #addProperty(java.lang.String,java.lang.String)} to
30  * add all the required information.</p>
31  *
32  * @author <a HREF="joe@truemesh.com">Joe Walnes</a>
33  * @version $Revision: 1.4 $
34  *
35  * @see com.opensymphony.module.sitemesh.Page
36  */

37 public abstract class AbstractPage implements Page {
38     /**
39      * Map of all properties.
40      * Key is String. Value is java.util.List of multiple String values.
41      */

42     private Map JavaDoc properties = new HashMap JavaDoc();
43
44     /** Date of page contents. */
45     protected char[] pageData = new char[0];
46
47     /** RequestURI of original Page. */
48     private HttpServletRequest JavaDoc request;
49
50     public void writePage(Writer out) throws IOException {
51         out.write(pageData);
52     }
53
54     public String JavaDoc getPage() {
55         try {
56             StringWriter writer = new StringWriter();
57             writePage(writer);
58             return writer.toString();
59         } catch (IOException e) {
60             throw new IllegalStateException JavaDoc("Could not get page " + e.getMessage());
61         }
62     }
63
64     /**
65      * Write data of html <code>&lt;body&gt;</code> tag.
66      *
67      * <p>Must be implemented. Data written should not actually contain the
68      * body tags, but all the data in between.
69      */

70     public abstract void writeBody(Writer out) throws IOException;
71
72     public String JavaDoc getBody() {
73         try {
74             StringWriter writer = new StringWriter();
75             writeBody(writer);
76             return writer.toString();
77         } catch (IOException e) {
78             throw new IllegalStateException JavaDoc("Could not get body " + e.getMessage());
79         }
80     }
81
82     /** Return title of from "title" property. Never returns null. */
83     public String JavaDoc getTitle() {
84         return noNull(getProperty("title"));
85     }
86
87     public int getContentLength() {
88         return pageData.length;
89     }
90
91     public String JavaDoc getProperty(String JavaDoc name) {
92         if (!isPropertySet(name)) return null;
93         return (String JavaDoc)properties.get(name);
94     }
95
96     public int getIntProperty(String JavaDoc name) {
97         try {
98             return Integer.parseInt(noNull(getProperty(name)));
99         }
100         catch (NumberFormatException JavaDoc e) {
101             return 0;
102         }
103     }
104
105     public long getLongProperty(String JavaDoc name) {
106         try {
107             return Long.parseLong(noNull(getProperty(name)));
108         }
109         catch (NumberFormatException JavaDoc e) {
110             return 0;
111         }
112     }
113
114     public boolean getBooleanProperty(String JavaDoc name) {
115         String JavaDoc property = getProperty(name);
116         if (property == null || property.trim().length() == 0) return false;
117         switch (property.charAt(0)) {
118             case '1':
119             case 't':
120             case 'T':
121             case 'y':
122             case 'Y':
123                 return true;
124             default:
125                 return false;
126         }
127     }
128
129     public boolean isPropertySet(String JavaDoc name) {
130         return properties.containsKey(name);
131     }
132
133     public String JavaDoc[] getPropertyKeys() {
134         synchronized(properties) {
135             Set JavaDoc keys = properties.keySet();
136             return (String JavaDoc[])keys.toArray(new String JavaDoc[keys.size()]);
137         }
138     }
139
140     public Map JavaDoc getProperties() {
141         return properties;
142     }
143
144     /** @see com.opensymphony.module.sitemesh.Page#getRequest() */
145     public HttpServletRequest JavaDoc getRequest() {
146         return request;
147     }
148
149     /**
150      * Create snapshot of Request.
151      *
152      * @see com.opensymphony.module.sitemesh.Page#getRequest()
153      */

154     public void setRequest(HttpServletRequest JavaDoc request) {
155         this.request = new PageRequest(request);
156     }
157
158     /**
159      * Add a property to the properties list.
160      *
161      * @param name Name of property
162      * @param value Value of property
163      */

164     public void addProperty(String JavaDoc name, String JavaDoc value) {
165         properties.put(name, value);
166     }
167
168     /** Return String as is, or "" if null. (Prevents NullPointerExceptions) */
169     protected static String JavaDoc noNull(String JavaDoc in) {
170         return in == null ? "" : in;
171     }
172 }
173
174 class PageRequest extends HttpServletRequestWrapper JavaDoc {
175
176     private String JavaDoc requestURI, method, pathInfo, pathTranslated, queryString, servletPath;
177
178     public PageRequest(HttpServletRequest JavaDoc request) {
179         super(request);
180         requestURI = request.getRequestURI();
181         method = request.getMethod();
182         pathInfo = request.getPathInfo();
183         pathTranslated = request.getPathTranslated();
184         queryString = request.getQueryString();
185         servletPath = request.getServletPath();
186     }
187
188     public String JavaDoc getRequestURI() {
189         return requestURI;
190     }
191
192     public String JavaDoc getMethod() {
193         return method;
194     }
195
196     public String JavaDoc getPathInfo() {
197         return pathInfo;
198     }
199
200     public String JavaDoc getPathTranslated() {
201         return pathTranslated;
202     }
203
204     public String JavaDoc getQueryString() {
205         return queryString;
206     }
207
208     public String JavaDoc getServletPath() {
209         return servletPath;
210     }
211 }
212
Popular Tags