001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.services.html;
066:
067: import com.jcorporate.expresso.core.misc.ConfigManager;
068: import com.jcorporate.expresso.core.misc.StringUtil;
069: import com.jcorporate.expresso.kernel.util.FastStringBuffer;
070: import com.jcorporate.expresso.services.dbobj.Setup;
071: import org.apache.log4j.Logger;
072:
073: import javax.servlet.http.HttpServletRequest;
074: import javax.servlet.http.HttpServletResponse;
075: import java.io.IOException;
076: import java.io.PrintWriter;
077: import java.util.Enumeration;
078:
079: /**
080: * An HTML page - a page element contains all other elements and is what
081: * actually gets sent to the client
082: *
083: * @author Michael Nash
084: * @version $Revision: 1.13 $ $Date: 2004/11/17 20:48:18 $
085: */
086: public class Page extends HtmlElement {
087:
088: private static final String this Class = Page.class.getName() + ".";
089: private String title = ("No Title");
090: private String bgColor = null;
091: private String dbName = "default";
092: private static Logger log = Logger.getLogger(Page.class);
093:
094: /**
095: * Constructor
096: *
097: * @throws HtmlException If the superclass constructor fails
098: */
099: public Page() throws HtmlException {
100: super ();
101: } /* Page() */
102:
103: /**
104: * Constructor
105: * Create a new page object with the given title
106: *
107: * @param newTitle Title for the page
108: * @throws HtmlException If the parameter is invalid
109: */
110: public Page(String newTitle) throws HtmlException {
111: super (newTitle);
112: setTitle(newTitle);
113: } /* Page(String) */
114:
115: /**
116: * Add a new element to the page
117: *
118: * @param newElement Element to add
119: * @throws HtmlException If the parameter is invalid
120: */
121: public synchronized void add(HtmlElement newElement)
122: throws HtmlException {
123: String myName = (this Class + "add(HtmlElement)");
124:
125: if (newElement instanceof Page) {
126: throw new HtmlException(myName
127: + ":Can't add a Page to Page " + getName());
128: }
129:
130: super .add(newElement);
131: } /* add(HtmlElement) */
132:
133: public synchronized void display(HttpServletRequest req,
134: HttpServletResponse res, String charset)
135: throws HtmlException {
136: if (StringUtil.notNull(charset).equals("")) {
137: charset = "ISO-8859-1";
138: }
139:
140: res.setContentType("text/html; charset=" + charset);
141:
142: try {
143: PrintWriter out = res.getWriter();
144: display(out);
145: } catch (IOException ie) {
146: throw new HtmlException(ie);
147: }
148: } /* display(HttpServletRequest, HttpServletResponse, String) */
149:
150: /**
151: * Raw display after content type and charset has been set.
152: * Make sure you've done both if you call this direct
153: * depth is ignored for a page because depth = 0;
154: *
155: * @param depth the number of tabs to indent
156: * @param out the output print writer
157: */
158: public synchronized void display(PrintWriter out, int depth)
159: throws HtmlException {
160: display(out);
161: }
162:
163: /**
164: * Display the page to the client
165: * Once a page is displayed it cannot be displayed again or new
166: * element added to it. This used to be the "public" method, but
167: * display(HttpServletRequest, HttpServletResponse, String) should be used
168: * now instead.
169: *
170: * @param out PrintWriter to the client
171: * @throws HtmlException If the page or it's contents cannot be displayed
172: */
173: protected synchronized void display(PrintWriter out)
174: throws HtmlException {
175: String myName = (this Class + "display(PrintWriter)");
176:
177: if (contents.size() == 0) {
178: throw new HtmlException(myName + ":Page " + getName()
179: + " has no contents");
180: }
181:
182: out.println("<html>");
183: out.println("<head>");
184: out.println("<title>" + title + "</title>");
185: out
186: .println("<META NAME=\"generator\" CONTENT=\"JCorporate's Expresso\">");
187:
188: String contextPath = ConfigManager.getContextPath();
189: String cssSuffix = null;
190: try {
191: cssSuffix = Setup.getValue(dbName, "defaultCSS");
192: } catch (com.jcorporate.expresso.core.db.DBException dbe) {
193: log
194: .warn(
195: "Unable to get setup value 'defaultCSS'. You may need to re-run DBCreate get the entry added.",
196: dbe);
197: }
198:
199: FastStringBuffer styleURL = FastStringBuffer.getInstance();
200: try {
201: styleURL
202: .append("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
203: styleURL.append(contextPath);
204: styleURL.append("/expresso");
205: styleURL.append("/style/");
206: styleURL.append("default");
207:
208: if (cssSuffix != null && cssSuffix.length() > 0) {
209: styleURL.append("-");
210: styleURL.append(cssSuffix);
211: }
212: styleURL.append(".css");
213:
214: styleURL.append("\">");
215: out.println(styleURL.toString());
216: } finally {
217: styleURL.release();
218: styleURL = null;
219: }
220:
221: // String styleSheet = "";
222: //
223: // try {
224: // styleSheet = StringUtil.notNull(ConfigManager.getContext(dbName).getStyleSheet());
225: // } catch (ConfigurationException ce) {
226: // throw new HtmlException(ce);
227: // }
228: // //This needs to be getValueRequired or we won't be able to determine that
229: // //Expresso has not been set up yet and we should use a default getContextPath()
230: // if (styleSheet.equals("")) {
231: // try {
232: // styleSheet = StringUtil.notNull(Setup.getValueRequired(dbName,
233: // "ContextPath")) +
234: // "/" +
235: // ConfigManager.getProperty(dbName, "expressoDir") +
236: // "/style/default.css";
237: // } catch (DBException de) {
238: // log.warn(myName + ":Cannot determine stylesheet location", de);
239: // styleSheet = ConfigManager.getContextPath() +
240: // "/expresso/style/default.css";
241: // }
242: // }
243: // if (!styleSheet.equals("")) {
244: // out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"" +
245: // styleSheet + " \">");
246: // }
247:
248: out.println("</head>");
249: out.print("<body");
250:
251: if (cSSClass != null) {
252: out.print(" class=\"" + cSSClass + "\"");
253: }
254: if (cSSID != null) {
255: out.print(" id=\"" + cSSID + "\"");
256: }
257: if (bgColor != null) {
258: out.println(" bgcolor=\"" + bgColor + "\"");
259: }
260:
261: out.println(">");
262:
263: HtmlElement oneElement = null;
264:
265: for (Enumeration e = contents.elements(); e.hasMoreElements();) {
266: oneElement = (HtmlElement) e.nextElement();
267: oneElement.display(out, 1);
268: }
269:
270: out.println("</body>");
271: out.println("</html>");
272: setDisplayed();
273: } /* display(PrintWriter) */
274:
275: public synchronized void displayPartial(HttpServletRequest req,
276: HttpServletResponse res, String charset)
277: throws HtmlException {
278: if (StringUtil.notNull(charset).equals("")) {
279: charset = "ISO-8859-1";
280: }
281:
282: res.setContentType("text/html; charset=" + charset);
283:
284: try {
285: PrintWriter out = res.getWriter();
286: displayPartial(out);
287: } catch (IOException ie) {
288: throw new HtmlException(ie);
289: }
290: }
291:
292: /**
293: * Display a partial page to the client, which can
294: * be included in another page such as a jsp page.
295: * Once a page is displayed it cannot be displayed again or new
296: * element added to it.
297: * This used to be a public method, but
298: * Use displayPartial(HttpServletRequest, HttpServletResponse, String) instead now.
299: *
300: * @param out PrintWriter to the client
301: * @throws HtmlException If the page or it's contents cannot be displayed
302: */
303: protected synchronized void displayPartial(PrintWriter out)
304: throws HtmlException {
305: String myName = (this Class + "display(PrintWriter)");
306:
307: if (contents.size() == 0) {
308: throw new HtmlException(myName + ":Page " + getName()
309: + " has no contents");
310: }
311:
312: HtmlElement oneElement = null;
313:
314: for (Enumeration e = contents.elements(); e.hasMoreElements();) {
315: oneElement = (HtmlElement) e.nextElement();
316: oneElement.display(out, 0);
317: }
318:
319: setDisplayed();
320: } /* displayPartial(PrintWriter) */
321:
322: /**
323: * Set the background color for the page
324: *
325: * @param newColor Color for the background of this page
326: * @throws HtmlException If the parameter is invalid
327: */
328: public synchronized void setBGColor(String newColor)
329: throws HtmlException {
330: String myName = (this Class + "setBGColor(String)");
331:
332: if (newColor == null) {
333: throw new HtmlException(myName
334: + ":Background color cannot be null");
335: }
336:
337: bgColor = newColor;
338: } /* setBGColor(String) */
339:
340: /**
341: * Set a db name for this page - if no DB name is specified, "default" is used.
342: * This name is used to select the context/db used for reading the setup value
343: * ContextPath, which is used to find the path to the stylesheet for this
344: * application
345: *
346: * @param newDBName the new database name
347: */
348: public synchronized void setDBName(String newDBName) {
349: if (StringUtil.notNull(newDBName).equals("")) {
350: dbName = "default";
351: } else {
352: dbName = newDBName;
353: }
354: } /* setDBName(String) */
355:
356: /**
357: * Set a new title for this page
358: *
359: * @param newTitle Title for the page
360: */
361: public synchronized void setTitle(String newTitle) {
362: if (newTitle != null) {
363: title = newTitle;
364: }
365: } /* setTitle(String) */
366:
367: } /* Page */
|