001: // ========================================================================
002: // $Id: Element.java,v 1.10 2005/08/13 00:01:23 gregwilkins Exp $
003: // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.html;
017:
018: import java.io.IOException;
019: import java.io.OutputStream;
020: import java.io.OutputStreamWriter;
021: import java.io.StringWriter;
022: import java.io.Writer;
023: import java.util.Enumeration;
024: import java.util.Hashtable;
025:
026: import org.mortbay.log.Log;
027:
028: /* -------------------------------------------------------------------- */
029: /** HTML Element.
030: * <p>This abstract class is the base for all HTML Elements.
031: * The feature of an abstract HTML Element is that it can be added to
032: * HTML Pages, HTML Composites and several other HTML Elements derivations.
033: * Elements may also have attributes set, which are handled by the derived
034: * Element.
035: * @deprecated Unless somebody steps forward to update and maintain this package
036: * @see Page
037: * @see Composite
038: * @version $Id: Element.java,v 1.10 2005/08/13 00:01:23 gregwilkins Exp $
039: * @author Greg Wilkins
040: */
041: public abstract class Element {
042:
043: /* ----------------------------------------------------------------- */
044: public static final String noAttributes = "", ALIGN = "align",
045: LEFT = "left", RIGHT = "right", CENTER = "center",
046: VALIGN = "valign", TOP = "top", BOTTOM = "bottom",
047: MIDDLE = "middle", WIDTH = "width", HEIGHT = "height",
048: SIZE = "size", COLOR = "color", BGCOLOR = "bgcolor",
049: STYLE = "style", CLASS = "class", ID = "id";
050:
051: /* ----------------------------------------------------------------- */
052: /** Dimensions >=0 if set*/
053: private int width = -1;
054: private int height = -1;
055: private int size = -1;
056:
057: /* ----------------------------------------------------------------- */
058: /** The space separated string of HTML element attributes.
059: */
060: private String attributes = null;
061: protected Hashtable attributeMap = null;
062:
063: /* ----------------------------------------------------------------- */
064: /** Default constructor.
065: */
066: public Element() {
067: }
068:
069: /* ----------------------------------------------------------------- */
070: /** Construct with attributes.
071: * @param attributes The initial attributes of the element
072: */
073: public Element(String attributes) {
074: attribute(attributes);
075: }
076:
077: /* ----------------------------------------------------------------- */
078: /** Write element to a Writer.
079: * This abstract method is called by the Page or other containing
080: * Element to write the HTML for this element. This must be implemented
081: * by the derived Element classes.
082: * @param out Writer to write the element to.
083: */
084: public abstract void write(Writer out) throws IOException;
085:
086: /* ----------------------------------------------------------------- */
087: /** Write Element to an OutputStream.
088: * Calls print(Writer) and checks errors
089: * Elements that override this method should also override
090: * write(Writer) to avoid infinite recursion.
091: * @param out OutputStream to write the element to.
092: */
093: public void write(OutputStream out) throws IOException {
094: Writer writer = new OutputStreamWriter(out);
095: write(writer);
096: writer.flush();
097: }
098:
099: /* ----------------------------------------------------------------- */
100: /** Write Element to an OutputStream.
101: * Calls print(Writer) and checks errors
102: * Elements that override this method should also override
103: * write(Writer) to avoid infinite recursion.
104: * @param out OutputStream to write the element to.
105: */
106: public void write(OutputStream out, String encoding)
107: throws IOException {
108: Writer writer = new OutputStreamWriter(out, encoding);
109: write(writer);
110: writer.flush();
111: }
112:
113: /* ----------------------------------------------------------------- */
114: public String attributes() {
115: if (attributes == null && attributeMap == null)
116: return noAttributes;
117:
118: StringBuffer buf = new StringBuffer(128);
119: synchronized (buf) {
120: if (attributeMap != null) {
121: Enumeration e = attributeMap.keys();
122: while (e.hasMoreElements()) {
123: buf.append(' ');
124: String a = (String) e.nextElement();
125: buf.append(a);
126: buf.append('=');
127: buf.append(attributeMap.get(a).toString());
128: }
129: }
130:
131: if (attributes != null && attributes.length() > 0) {
132: if (!attributes.startsWith(" "))
133: buf.append(' ');
134: buf.append(attributes);
135: }
136: }
137:
138: return buf.toString();
139: }
140:
141: /* ----------------------------------------------------------------- */
142: /** Add element Attributes.
143: * The attributes are added to the Element attributes (separated with
144: * a space). The attributes are available to the derived class in the
145: * protected member String <I>attributes</I>
146: * @deprecated Use attribute(String).
147: * @param attributes String of HTML attributes to add to the element.
148: * @return This Element so calls can be chained.
149: */
150: public Element attributes(String attributes) {
151: if (attributes == null) {
152: this .attributes = null;
153: return this ;
154: }
155:
156: if (attributes == noAttributes)
157: return this ;
158:
159: if (this .attributes == null)
160: this .attributes = attributes;
161: else
162: this .attributes += ' ' + attributes;
163: return this ;
164: }
165:
166: /* ------------------------------------------------------------ */
167: /** Set attributes from another Element.
168: * @param e Element
169: * @return This Element
170: */
171: public Element setAttributesFrom(Element e) {
172: attributes = e.attributes;
173: attributeMap = (Hashtable) e.attributeMap.clone();
174: return this ;
175: }
176:
177: /* ----------------------------------------------------------------- */
178: /** Add element Attributes.
179: * The attributes are added to the Element attributes (separated with
180: * a space). The attributes are available to the derived class in the
181: * protected member String <I>attributes</I>
182: * @param attributes String of HTML attributes to add to the element.
183: * A null attribute clears the current attributes.
184: * @return This Element so calls can be chained.
185: */
186: public Element attribute(String attributes) {
187: if (attributes == null || this .attributes == null
188: || this .attributes == noAttributes
189: || this .attributes.length() == 0)
190: this .attributes = attributes;
191: else
192: this .attributes += ' ' + attributes;
193: return this ;
194: }
195:
196: /* ----------------------------------------------------------------- */
197: /** Add quoted element Attributes and value.
198: * @param attribute String of HTML attribute tag
199: * @param value String value of the attribute to be quoted
200: * @return This Element so calls can be chained.
201: */
202: public Element attribute(String attribute, Object value) {
203: if (attributeMap == null)
204: attributeMap = new Hashtable(10);
205:
206: if (value != null) {
207: if (value instanceof String
208: && ((String) value).indexOf('"') != -1) {
209: String s = (String) value;
210: int q = 0;
211: while ((q = s.indexOf('"', q)) >= 0) {
212: s = s.substring(0, q) + """ + s.substring(++q);
213: q += 6;
214: }
215: value = s;
216: }
217:
218: attributeMap.put(attribute, "\"" + value + '"');
219: }
220: return this ;
221: }
222:
223: /* ----------------------------------------------------------------- */
224: /** Add quoted element Attributes and value.
225: * @param attribute String of HTML attribute tag
226: * @param value String value of the attribute to be quoted
227: * @return This Element so calls can be chained.
228: */
229: public Element attribute(String attribute, long value) {
230: if (attributeMap == null)
231: attributeMap = new Hashtable(10);
232:
233: attributeMap.put(attribute, Long.toString(value));
234: return this ;
235: }
236:
237: /* ----------------------------------------------------------------- */
238: /** Convert Element to String.
239: * Uses write() to convert the HTML Element to a string.
240: * @return String of the HTML element
241: */
242: public String toString() {
243: try {
244: StringWriter out = new StringWriter();
245: write(out);
246: out.flush();
247: return out.toString();
248: } catch (IOException e) {
249: Log.ignore(e);
250: }
251: return null;
252: }
253:
254: /* ----------------------------------------------------------------- */
255: /** left justify.
256: * Convenience method equivalent to attribute("align","left"). Not
257: * applicable to all Elements.
258: */
259: public Element left() {
260: return attribute(ALIGN, LEFT);
261: }
262:
263: /* ----------------------------------------------------------------- */
264: /** right justify.
265: * Convenience method equivalent to attribute("align","right"). Not
266: * applicable to all Elements.
267: */
268: public Element right() {
269: return attribute(ALIGN, RIGHT);
270: }
271:
272: /* ----------------------------------------------------------------- */
273: /** Center.
274: * Convenience method equivalent to attribute("align","center"). Not
275: * applicable to all Elements.
276: */
277: public Element center() {
278: return attribute(ALIGN, CENTER);
279: }
280:
281: /* ----------------------------------------------------------------- */
282: /** Top align.
283: * Convenience method equivalent to attribute("valign","top"). Not
284: * applicable to all Elements.
285: */
286: public Element top() {
287: return attribute(VALIGN, TOP);
288: }
289:
290: /* ----------------------------------------------------------------- */
291: /** Bottom align.
292: * Convenience method equivalent to attribute("valign","bottom"). Not
293: * applicable to all Elements.
294: */
295: public Element bottom() {
296: return attribute(VALIGN, BOTTOM);
297: }
298:
299: /* ----------------------------------------------------------------- */
300: /** Middle align.
301: * Convenience method equivalent to attribute("valign","middle"). Not
302: * applicable to all Elements.
303: */
304: public Element middle() {
305: return attribute(VALIGN, MIDDLE);
306: }
307:
308: /* ----------------------------------------------------------------- */
309: /** set width.
310: * Convenience method equivalent to attribute("width",w). Not
311: * applicable to all Elements.
312: */
313: public Element width(int w) {
314: width = w;
315: return attribute(WIDTH, w);
316: }
317:
318: /* ----------------------------------------------------------------- */
319: /** set width.
320: * Convenience method equivalent to attribute("width",w). Not
321: * applicable to all Elements.
322: */
323: public Element width(String w) {
324: width = -1;
325: return attribute(WIDTH, w);
326: }
327:
328: /* ----------------------------------------------------------------- */
329: public int width() {
330: return width;
331: }
332:
333: /* ----------------------------------------------------------------- */
334: /** set height.
335: * Convenience method equivalent to attribute("height",h). Not
336: * applicable to all Elements.
337: */
338: public Element height(int h) {
339: height = h;
340: return attribute(HEIGHT, h);
341: }
342:
343: /* ----------------------------------------------------------------- */
344: /** set height.
345: * Convenience method equivalent to attribute("height",h). Not
346: * applicable to all Elements.
347: */
348: public Element height(String h) {
349: height = -1;
350: return attribute(HEIGHT, h);
351: }
352:
353: /* ----------------------------------------------------------------- */
354: public int height() {
355: return height;
356: }
357:
358: /* ----------------------------------------------------------------- */
359: /** set size.
360: * Convenience method equivalent to attribute("size",s). Not
361: * applicable to all Elements.
362: */
363: public Element size(int s) {
364: size = s;
365: return attribute(SIZE, s);
366: }
367:
368: /* ----------------------------------------------------------------- */
369: /** set size.
370: * Convenience method equivalent to attribute("size",s). Not
371: * applicable to all Elements.
372: */
373: public Element size(String s) {
374: size = -1;
375: return attribute(SIZE, s);
376: }
377:
378: /* ----------------------------------------------------------------- */
379: public int size() {
380: return size;
381: }
382:
383: /* ----------------------------------------------------------------- */
384: /** set color.
385: * Convenience method equivalent to attribute("color",color). Not
386: * applicable to all Elements.
387: */
388: public Element color(String color) {
389: return attribute(COLOR, color);
390: }
391:
392: /* ----------------------------------------------------------------- */
393: /** set BGCOLOR.
394: * Convenience method equivalent to attribute("bgcolor",color). Not
395: * applicable to all Elements.
396: */
397: public Element bgColor(String color) {
398: return attribute(BGCOLOR, color);
399: }
400:
401: /* ----------------------------------------------------------------- */
402: /** set CSS CLASS.
403: */
404: public Element cssClass(String c) {
405: return attribute(CLASS, c);
406: }
407:
408: /* ----------------------------------------------------------------- */
409: /** set CSS ID.
410: * Convenience method equivalent to attribute("id",id).
411: */
412: public Element cssID(String id) {
413: return attribute(ID, id);
414: }
415:
416: /* ----------------------------------------------------------------- */
417: /** set Style.
418: * Convenience method equivalent to attribute("style",style).
419: */
420: public Element style(String style) {
421: return attribute(STYLE, style);
422: }
423: }
|