KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.Logger;
68
69 import java.io.OutputStream JavaDoc;
70 import java.io.PrintWriter JavaDoc;
71 import java.util.Hashtable JavaDoc;
72 import java.util.Vector JavaDoc;
73
74
75 /**
76  * Base class for all of the HTML elements on a page
77  *
78  * @author Michael Nash
79  * @version $Revision: 1.12 $ $Date: 2004/11/17 20:48:18 $
80  */

81 public abstract class HtmlElement {
82     protected Vector JavaDoc contents = new Vector JavaDoc(5);
83     private String JavaDoc thisClass = (this.getClass().getName() + ".");
84     private HtmlElement myParent = null;
85     private String JavaDoc myObjName = "No Name";
86     private boolean hasBeenDisplayed = false;
87     protected String JavaDoc cSSClass = "jc-default";
88     protected String JavaDoc cSSID = null;
89     protected Hashtable JavaDoc attributes = null;
90     private static Logger log = Logger.getLogger(HtmlElement.class);
91     private static char[] padding = new char[256];
92
93     //Static Initializer
94
{
95         for (int i = 0; i < padding.length; i++) {
96             padding[i] = '\t';
97         }
98     }
99
100     /**
101      * Constructor
102      *
103      * @throws HtmlException If the superclass constructor fails
104      */

105     public HtmlElement()
106             throws HtmlException {
107         super();
108     } /* HtmlElement() */
109
110     /**
111      * Constructor
112      * Create the element with the given element as it's contents
113      *
114      * @param newElement Element to use as contents of this new element
115      * @throws HtmlException If the element cannot be used or the paramter is invalid
116      */

117     public HtmlElement(HtmlElement newElement)
118             throws HtmlException {
119         super();
120         add(newElement);
121     } /* HtmlElement(HtmlElement) */
122
123     /**
124      * Constructor
125      * Create an element with a name
126      *
127      * @param elementName The name of the new element
128      * @throws HtmlException If the element cannot be created
129      */

130     public HtmlElement(String JavaDoc elementName)
131             throws HtmlException {
132         super();
133
134         if (elementName != null) {
135             setName(elementName);
136         } else {
137             setName("null");
138         }
139     } /* HtmlElement(String) */
140
141     /**
142      * Add a new element to this element's contents
143      *
144      * @param newElement Element to be added
145      * @throws HtmlException If the element cannot be added
146      */

147     public synchronized void add(HtmlElement newElement)
148             throws HtmlException {
149         String JavaDoc myName = (thisClass + "add(HtmlElement)");
150
151         if (newElement == null) {
152             throw new HtmlException(myName + ":Cannot add null element to '" +
153                     getName() + "'");
154         }
155
156         newElement.setParent(this);
157         contents.addElement(newElement);
158     } /* add(HtmlElement) */
159
160
161     /**
162      * Alternate display method using an output stream
163      *
164      * @param out Outputstream to use to display to the client
165      * @throws HtmlException If the element (or it's contents) cannot be displayed
166      */

167     protected void display(OutputStream JavaDoc out)
168             throws HtmlException {
169         display(new PrintWriter JavaDoc(out), 0);
170     } /* display(OutputStream) */
171
172
173     /**
174      * Display the element to the client
175      * Each HtmlElement must implement this method
176      *
177      * @param out PrintWriter to display to the client
178      * @param depth the number of tabs to indent
179      * @throws HtmlException If the element (or it's contents) cannot be displayed
180      */

181     protected abstract void display(PrintWriter JavaDoc out, int depth)
182             throws HtmlException;
183
184     /* display(PrintWriter) */
185     /**
186      * See if the object has been displayed
187      *
188      * @throws HtmlException if the object has never been displayed
189      */

190     protected void finalize()
191             throws HtmlException {
192         String JavaDoc myName = (thisClass + "finalize()");
193
194         if (!hasBeenDisplayed) {
195             throw new HtmlException(myName + ":WARNING: Object '" + getName() +
196                     "' was never displayed!");
197         }
198     } /* finalize() */
199
200
201     /**
202      * Get an attribute from this element
203      *
204      * @param attribName Name of the attribute to be set
205      * @return Value of the attribute
206      * @throws HtmlException If the attribute cannot be retrieved
207      */

208     public Object JavaDoc getAttribute(String JavaDoc attribName)
209             throws HtmlException {
210         if (attributes == null) {
211             return null;
212         }
213
214         return attributes.get(attribName);
215     } /* getAttribute(String) */
216
217
218     /**
219      * Return the count of contents in this item
220      *
221      * @return integer
222      */

223     public int getContentCount() {
224         return contents.size();
225     } /* getContentCount() */
226
227     /**
228      * @return CSS class
229      */

230     public String JavaDoc getCSSClass() {
231         return cSSClass;
232     } /* getCSSClass() */
233
234     /**
235      * Return the name of this element
236      *
237      * @return String The name
238      * @throws HtmlException If the name cannot be accessed or the object has itself
239      * as a parent
240      */

241     public String JavaDoc getName()
242             throws HtmlException {
243         String JavaDoc myName = (thisClass + "getName()");
244
245         if (myObjName == null) {
246             myObjName = ("Null Name");
247         }
248         if (myParent == this) {
249             throw new HtmlException(myName + ":Object " + myObjName +
250                     " cannot have itself as a parent");
251         }
252
253         return ("<strong>" + myObjName + "</strong>");
254     } /* getName() */
255
256
257     /**
258      * Pad the output with particular nesting tabs supplies in essence source
259      * formatting.
260      *
261      * @param out the output stream
262      * @param count the number of tabs to display
263      */

264     protected void padWithTabs(PrintWriter JavaDoc out, int count) {
265         if (count > padding.length) {
266             log.warn("Encountered > " + padding.length +
267                     " nesting. Source format will no longer be padded");
268             out.print(new String JavaDoc(padding, 0, count));
269         }
270     }
271
272     /**
273      * Set an attribute for this element
274      *
275      * @param attribName Name of the attribute to be set
276      * @param attribValue Value of the attribute to be set
277      * @throws HtmlException If the attribute cannot be set
278      */

279     public synchronized void setAttribute(String JavaDoc attribName,
280                                           Object JavaDoc attribValue)
281             throws HtmlException {
282         if (attributes == null) {
283             attributes = new Hashtable JavaDoc();
284         }
285
286         attributes.put(attribName, attribValue);
287     } /* setAttribute(String, Object) */
288
289
290     /**
291      * @param className the CSS class name
292      */

293     public void setCSSClass(String JavaDoc className) {
294         cSSClass = className;
295     } /* setCSSClass(String) */
296
297     /**
298      * @param id the CSS id
299      */

300     public void setCSSID(String JavaDoc id) {
301         cSSID = id;
302     } /* setCSSID(String) */
303
304     /**
305      * Mark this object as having been displayed
306      *
307      * @throws HtmlException If the object has already been displayed
308      */

309     protected void setDisplayed()
310             throws HtmlException {
311         String JavaDoc myName = (thisClass + "setDisplayed()");
312
313         if (hasBeenDisplayed) {
314             throw new HtmlException(myName + ":Element " + getName() +
315                     " has already been displayed - cannot display twice");
316         }
317     } /* setDisplayed() */
318
319
320     /**
321      * Set the name of this element
322      *
323      * @param newName The new name
324      * @throws HtmlException If the name cannot be set
325      */

326     public void setName(String JavaDoc newName)
327             throws HtmlException {
328         String JavaDoc myName = (thisClass + "setName(String)");
329
330         if (newName == null) {
331             throw new HtmlException(myName + ":Cannot set name to null");
332         }
333
334         myObjName = newName;
335     } /* setName(String) */
336
337
338     /**
339      * Set the parent of this element to the named element
340      *
341      * @param newParent The new parent element of this element
342      * @throws HtmlException If the given element cannot be used as a parent or the
343      * parameter is invalid
344      */

345     protected void setParent(HtmlElement newParent)
346             throws HtmlException {
347         String JavaDoc myName = (thisClass + "setParent(HtmlElement)");
348
349         if (newParent == null) {
350             throw new HtmlException(myName + ":Can't set parent of " +
351                     getName() + " to a null element");
352         }
353         if (myParent != null) {
354             if (newParent == myParent) {
355                 throw new HtmlException(myName + ":This element, " +
356                         getName() +
357                         ", has already been added to the element you are " +
358                         "adding it to: " +
359                         newParent.getName() +
360                         " - cannot add again");
361             } else {
362                 throw new HtmlException(myName + ":This element, '" +
363                         getName() +
364                         "', has already been added to another element:" +
365                         myParent.getName() +
366                         " - cannot add it to another elements");
367             }
368         } /* myParent is not null */
369
370
371         myParent = newParent;
372     } /* setParent(HtmlElement) */
373
374 } /* HtmlElement */
375
Popular Tags