KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > html > Page


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. 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 following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.html;
66
67 import com.jcorporate.expresso.core.misc.ConfigManager;
68 import com.jcorporate.expresso.core.misc.StringUtil;
69 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
70 import com.jcorporate.expresso.services.dbobj.Setup;
71 import org.apache.log4j.Logger;
72
73 import javax.servlet.http.HttpServletRequest JavaDoc;
74 import javax.servlet.http.HttpServletResponse JavaDoc;
75 import java.io.IOException JavaDoc;
76 import java.io.PrintWriter JavaDoc;
77 import java.util.Enumeration JavaDoc;
78
79
80 /**
81  * An HTML page - a page element contains all other elements and is what
82  * actually gets sent to the client
83  *
84  * @author Michael Nash
85  * @version $Revision: 1.13 $ $Date: 2004/11/17 20:48:18 $
86  */

87 public class Page
88         extends HtmlElement {
89
90     private static final String JavaDoc thisClass = Page.class.getName() + ".";
91     private String JavaDoc title = ("No Title");
92     private String JavaDoc bgColor = null;
93     private String JavaDoc dbName = "default";
94     private static Logger log = Logger.getLogger(Page.class);
95
96     /**
97      * Constructor
98      *
99      * @throws HtmlException If the superclass constructor fails
100      */

101     public Page()
102             throws HtmlException {
103         super();
104     } /* Page() */
105
106     /**
107      * Constructor
108      * Create a new page object with the given title
109      *
110      * @param newTitle Title for the page
111      * @throws HtmlException If the parameter is invalid
112      */

113     public Page(String JavaDoc newTitle)
114             throws HtmlException {
115         super(newTitle);
116         setTitle(newTitle);
117     } /* Page(String) */
118
119     /**
120      * Add a new element to the page
121      *
122      * @param newElement Element to add
123      * @throws HtmlException If the parameter is invalid
124      */

125     public synchronized void add(HtmlElement newElement)
126             throws HtmlException {
127         String JavaDoc myName = (thisClass + "add(HtmlElement)");
128
129         if (newElement instanceof Page) {
130             throw new HtmlException(myName + ":Can't add a Page to Page " +
131                     getName());
132         }
133
134         super.add(newElement);
135     } /* add(HtmlElement) */
136
137
138     public synchronized void display(HttpServletRequest JavaDoc req,
139                                      HttpServletResponse JavaDoc res, String JavaDoc charset)
140             throws HtmlException {
141         if (StringUtil.notNull(charset).equals("")) {
142             charset = "ISO-8859-1";
143         }
144
145         res.setContentType("text/html; charset=" + charset);
146
147         try {
148             PrintWriter JavaDoc out = res.getWriter();
149             display(out);
150         } catch (IOException JavaDoc ie) {
151             throw new HtmlException(ie);
152         }
153     } /* display(HttpServletRequest, HttpServletResponse, String) */
154
155
156     /**
157      * Raw display after content type and charset has been set.
158      * Make sure you've done both if you call this direct
159      * depth is ignored for a page because depth = 0;
160      *
161      * @param depth the number of tabs to indent
162      * @param out the output print writer
163      */

164     public synchronized void display(PrintWriter JavaDoc out, int depth)
165             throws HtmlException {
166         display(out);
167     }
168
169     /**
170      * Display the page to the client
171      * Once a page is displayed it cannot be displayed again or new
172      * element added to it. This used to be the "public" method, but
173      * display(HttpServletRequest, HttpServletResponse, String) should be used
174      * now instead.
175      *
176      * @param out PrintWriter to the client
177      * @throws HtmlException If the page or it's contents cannot be displayed
178      */

179     protected synchronized void display(PrintWriter JavaDoc out)
180             throws HtmlException {
181         String JavaDoc myName = (thisClass + "display(PrintWriter)");
182
183         if (contents.size() == 0) {
184             throw new HtmlException(myName + ":Page " + getName() +
185                     " has no contents");
186         }
187
188         out.println("<html>");
189         out.println("<head>");
190         out.println("<title>" + title + "</title>");
191         out.println("<META NAME=\"generator\" CONTENT=\"JCorporate's Expresso\">");
192
193         String JavaDoc contextPath = ConfigManager.getContextPath();
194         String JavaDoc cssSuffix = null;
195         try {
196             cssSuffix = Setup.getValue(dbName, "defaultCSS");
197         } catch (com.jcorporate.expresso.core.db.DBException dbe) {
198             log.warn("Unable to get setup value 'defaultCSS'. You may need to re-run DBCreate get the entry added."
199                     , dbe);
200         }
201
202         FastStringBuffer styleURL = FastStringBuffer.getInstance();
203         try {
204             styleURL.append("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"");
205             styleURL.append(contextPath);
206             styleURL.append("/expresso");
207             styleURL.append("/style/");
208             styleURL.append("default");
209
210             if (cssSuffix != null && cssSuffix.length() > 0) {
211                 styleURL.append("-");
212                 styleURL.append(cssSuffix);
213             }
214             styleURL.append(".css");
215
216             styleURL.append("\">");
217             out.println(styleURL.toString());
218         } finally {
219             styleURL.release();
220             styleURL = null;
221         }
222
223
224 // String styleSheet = "";
225
//
226
// try {
227
// styleSheet = StringUtil.notNull(ConfigManager.getContext(dbName).getStyleSheet());
228
// } catch (ConfigurationException ce) {
229
// throw new HtmlException(ce);
230
// }
231
// //This needs to be getValueRequired or we won't be able to determine that
232
// //Expresso has not been set up yet and we should use a default getContextPath()
233
// if (styleSheet.equals("")) {
234
// try {
235
// styleSheet = StringUtil.notNull(Setup.getValueRequired(dbName,
236
// "ContextPath")) +
237
// "/" +
238
// ConfigManager.getProperty(dbName, "expressoDir") +
239
// "/style/default.css";
240
// } catch (DBException de) {
241
// log.warn(myName + ":Cannot determine stylesheet location", de);
242
// styleSheet = ConfigManager.getContextPath() +
243
// "/expresso/style/default.css";
244
// }
245
// }
246
// if (!styleSheet.equals("")) {
247
// out.println("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"" +
248
// styleSheet + " \">");
249
// }
250

251         out.println("</head>");
252         out.print("<body");
253
254         if (cSSClass != null) {
255             out.print(" class=\"" + cSSClass + "\"");
256         }
257         if (cSSID != null) {
258             out.print(" id=\"" + cSSID + "\"");
259         }
260         if (bgColor != null) {
261             out.println(" bgcolor=\"" + bgColor + "\"");
262         }
263
264         out.println(">");
265
266         HtmlElement oneElement = null;
267
268         for (Enumeration JavaDoc e = contents.elements(); e.hasMoreElements();) {
269             oneElement = (HtmlElement) e.nextElement();
270             oneElement.display(out, 1);
271         }
272
273         out.println("</body>");
274         out.println("</html>");
275         setDisplayed();
276     } /* display(PrintWriter) */
277
278
279     public synchronized void displayPartial(HttpServletRequest JavaDoc req,
280                                             HttpServletResponse JavaDoc res,
281                                             String JavaDoc charset)
282             throws HtmlException {
283         if (StringUtil.notNull(charset).equals("")) {
284             charset = "ISO-8859-1";
285         }
286
287         res.setContentType("text/html; charset=" + charset);
288
289         try {
290             PrintWriter JavaDoc out = res.getWriter();
291             displayPartial(out);
292         } catch (IOException JavaDoc ie) {
293             throw new HtmlException(ie);
294         }
295     }
296
297     /**
298      * Display a partial page to the client, which can
299      * be included in another page such as a jsp page.
300      * Once a page is displayed it cannot be displayed again or new
301      * element added to it.
302      * This used to be a public method, but
303      * Use displayPartial(HttpServletRequest, HttpServletResponse, String) instead now.
304      *
305      * @param out PrintWriter to the client
306      * @throws HtmlException If the page or it's contents cannot be displayed
307      */

308     protected synchronized void displayPartial(PrintWriter JavaDoc out)
309             throws HtmlException {
310         String JavaDoc myName = (thisClass + "display(PrintWriter)");
311
312         if (contents.size() == 0) {
313             throw new HtmlException(myName + ":Page " + getName() +
314                     " has no contents");
315         }
316
317         HtmlElement oneElement = null;
318
319         for (Enumeration JavaDoc e = contents.elements(); e.hasMoreElements();) {
320             oneElement = (HtmlElement) e.nextElement();
321             oneElement.display(out, 0);
322         }
323
324         setDisplayed();
325     } /* displayPartial(PrintWriter) */
326
327
328     /**
329      * Set the background color for the page
330      *
331      * @param newColor Color for the background of this page
332      * @throws HtmlException If the parameter is invalid
333      */

334     public synchronized void setBGColor(String JavaDoc newColor)
335             throws HtmlException {
336         String JavaDoc myName = (thisClass + "setBGColor(String)");
337
338         if (newColor == null) {
339             throw new HtmlException(myName +
340                     ":Background color cannot be null");
341         }
342
343         bgColor = newColor;
344     } /* setBGColor(String) */
345
346
347     /**
348      * Set a db name for this page - if no DB name is specified, "default" is used.
349      * This name is used to select the context/db used for reading the setup value
350      * ContextPath, which is used to find the path to the stylesheet for this
351      * application
352      *
353      * @param newDBName the new database name
354      */

355     public synchronized void setDBName(String JavaDoc newDBName) {
356         if (StringUtil.notNull(newDBName).equals("")) {
357             dbName = "default";
358         } else {
359             dbName = newDBName;
360         }
361     } /* setDBName(String) */
362
363     /**
364      * Set a new title for this page
365      *
366      * @param newTitle Title for the page
367      */

368     public synchronized void setTitle(String JavaDoc newTitle) {
369         if (newTitle != null) {
370             title = newTitle;
371         }
372     } /* setTitle(String) */
373
374
375 } /* Page */
376
Popular Tags