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 org.apache.log4j.Logger;
068:
069: import java.io.OutputStream;
070: import java.io.PrintWriter;
071: import java.util.Hashtable;
072: import java.util.Vector;
073:
074: /**
075: * Base class for all of the HTML elements on a page
076: *
077: * @author Michael Nash
078: * @version $Revision: 1.12 $ $Date: 2004/11/17 20:48:18 $
079: */
080: public abstract class HtmlElement {
081: protected Vector contents = new Vector(5);
082: private String this Class = (this .getClass().getName() + ".");
083: private HtmlElement myParent = null;
084: private String myObjName = "No Name";
085: private boolean hasBeenDisplayed = false;
086: protected String cSSClass = "jc-default";
087: protected String cSSID = null;
088: protected Hashtable attributes = null;
089: private static Logger log = Logger.getLogger(HtmlElement.class);
090: private static char[] padding = new char[256];
091:
092: //Static Initializer
093: {
094: for (int i = 0; i < padding.length; i++) {
095: padding[i] = '\t';
096: }
097: }
098:
099: /**
100: * Constructor
101: *
102: * @throws HtmlException If the superclass constructor fails
103: */
104: public HtmlElement() throws HtmlException {
105: super ();
106: } /* HtmlElement() */
107:
108: /**
109: * Constructor
110: * Create the element with the given element as it's contents
111: *
112: * @param newElement Element to use as contents of this new element
113: * @throws HtmlException If the element cannot be used or the paramter is invalid
114: */
115: public HtmlElement(HtmlElement newElement) throws HtmlException {
116: super ();
117: add(newElement);
118: } /* HtmlElement(HtmlElement) */
119:
120: /**
121: * Constructor
122: * Create an element with a name
123: *
124: * @param elementName The name of the new element
125: * @throws HtmlException If the element cannot be created
126: */
127: public HtmlElement(String elementName) throws HtmlException {
128: super ();
129:
130: if (elementName != null) {
131: setName(elementName);
132: } else {
133: setName("null");
134: }
135: } /* HtmlElement(String) */
136:
137: /**
138: * Add a new element to this element's contents
139: *
140: * @param newElement Element to be added
141: * @throws HtmlException If the element cannot be added
142: */
143: public synchronized void add(HtmlElement newElement)
144: throws HtmlException {
145: String myName = (this Class + "add(HtmlElement)");
146:
147: if (newElement == null) {
148: throw new HtmlException(myName
149: + ":Cannot add null element to '" + getName() + "'");
150: }
151:
152: newElement.setParent(this );
153: contents.addElement(newElement);
154: } /* add(HtmlElement) */
155:
156: /**
157: * Alternate display method using an output stream
158: *
159: * @param out Outputstream to use to display to the client
160: * @throws HtmlException If the element (or it's contents) cannot be displayed
161: */
162: protected void display(OutputStream out) throws HtmlException {
163: display(new PrintWriter(out), 0);
164: } /* display(OutputStream) */
165:
166: /**
167: * Display the element to the client
168: * Each HtmlElement must implement this method
169: *
170: * @param out PrintWriter to display to the client
171: * @param depth the number of tabs to indent
172: * @throws HtmlException If the element (or it's contents) cannot be displayed
173: */
174: protected abstract void display(PrintWriter out, int depth)
175: throws HtmlException;
176:
177: /* display(PrintWriter) */
178: /**
179: * See if the object has been displayed
180: *
181: * @throws HtmlException if the object has never been displayed
182: */
183: protected void finalize() throws HtmlException {
184: String myName = (this Class + "finalize()");
185:
186: if (!hasBeenDisplayed) {
187: throw new HtmlException(myName + ":WARNING: Object '"
188: + getName() + "' was never displayed!");
189: }
190: } /* finalize() */
191:
192: /**
193: * Get an attribute from this element
194: *
195: * @param attribName Name of the attribute to be set
196: * @return Value of the attribute
197: * @throws HtmlException If the attribute cannot be retrieved
198: */
199: public Object getAttribute(String attribName) throws HtmlException {
200: if (attributes == null) {
201: return null;
202: }
203:
204: return attributes.get(attribName);
205: } /* getAttribute(String) */
206:
207: /**
208: * Return the count of contents in this item
209: *
210: * @return integer
211: */
212: public int getContentCount() {
213: return contents.size();
214: } /* getContentCount() */
215:
216: /**
217: * @return CSS class
218: */
219: public String getCSSClass() {
220: return cSSClass;
221: } /* getCSSClass() */
222:
223: /**
224: * Return the name of this element
225: *
226: * @return String The name
227: * @throws HtmlException If the name cannot be accessed or the object has itself
228: * as a parent
229: */
230: public String getName() throws HtmlException {
231: String myName = (this Class + "getName()");
232:
233: if (myObjName == null) {
234: myObjName = ("Null Name");
235: }
236: if (myParent == this ) {
237: throw new HtmlException(myName + ":Object " + myObjName
238: + " cannot have itself as a parent");
239: }
240:
241: return ("<strong>" + myObjName + "</strong>");
242: } /* getName() */
243:
244: /**
245: * Pad the output with particular nesting tabs supplies in essence source
246: * formatting.
247: *
248: * @param out the output stream
249: * @param count the number of tabs to display
250: */
251: protected void padWithTabs(PrintWriter out, int count) {
252: if (count > padding.length) {
253: log
254: .warn("Encountered > "
255: + padding.length
256: + " nesting. Source format will no longer be padded");
257: out.print(new String(padding, 0, count));
258: }
259: }
260:
261: /**
262: * Set an attribute for this element
263: *
264: * @param attribName Name of the attribute to be set
265: * @param attribValue Value of the attribute to be set
266: * @throws HtmlException If the attribute cannot be set
267: */
268: public synchronized void setAttribute(String attribName,
269: Object attribValue) throws HtmlException {
270: if (attributes == null) {
271: attributes = new Hashtable();
272: }
273:
274: attributes.put(attribName, attribValue);
275: } /* setAttribute(String, Object) */
276:
277: /**
278: * @param className the CSS class name
279: */
280: public void setCSSClass(String className) {
281: cSSClass = className;
282: } /* setCSSClass(String) */
283:
284: /**
285: * @param id the CSS id
286: */
287: public void setCSSID(String id) {
288: cSSID = id;
289: } /* setCSSID(String) */
290:
291: /**
292: * Mark this object as having been displayed
293: *
294: * @throws HtmlException If the object has already been displayed
295: */
296: protected void setDisplayed() throws HtmlException {
297: String myName = (this Class + "setDisplayed()");
298:
299: if (hasBeenDisplayed) {
300: throw new HtmlException(
301: myName
302: + ":Element "
303: + getName()
304: + " has already been displayed - cannot display twice");
305: }
306: } /* setDisplayed() */
307:
308: /**
309: * Set the name of this element
310: *
311: * @param newName The new name
312: * @throws HtmlException If the name cannot be set
313: */
314: public void setName(String newName) throws HtmlException {
315: String myName = (this Class + "setName(String)");
316:
317: if (newName == null) {
318: throw new HtmlException(myName + ":Cannot set name to null");
319: }
320:
321: myObjName = newName;
322: } /* setName(String) */
323:
324: /**
325: * Set the parent of this element to the named element
326: *
327: * @param newParent The new parent element of this element
328: * @throws HtmlException If the given element cannot be used as a parent or the
329: * parameter is invalid
330: */
331: protected void setParent(HtmlElement newParent)
332: throws HtmlException {
333: String myName = (this Class + "setParent(HtmlElement)");
334:
335: if (newParent == null) {
336: throw new HtmlException(myName + ":Can't set parent of "
337: + getName() + " to a null element");
338: }
339: if (myParent != null) {
340: if (newParent == myParent) {
341: throw new HtmlException(
342: myName
343: + ":This element, "
344: + getName()
345: + ", has already been added to the element you are "
346: + "adding it to: "
347: + newParent.getName()
348: + " - cannot add again");
349: } else {
350: throw new HtmlException(
351: myName
352: + ":This element, '"
353: + getName()
354: + "', has already been added to another element:"
355: + myParent.getName()
356: + " - cannot add it to another elements");
357: }
358: } /* myParent is not null */
359:
360: myParent = newParent;
361: } /* setParent(HtmlElement) */
362:
363: } /* HtmlElement */
|