KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > html > Element


1 // ========================================================================
2
// $Id: Element.java,v 1.10 2005/08/13 00:01:23 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.html;
17 import java.io.IOException JavaDoc;
18 import java.io.OutputStream JavaDoc;
19 import java.io.OutputStreamWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.io.Writer JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.mortbay.log.LogFactory;
27 import org.mortbay.util.LogSupport;
28
29 /* -------------------------------------------------------------------- */
30 /** HTML Element.
31  * <p>This abstract class is the base for all HTML Elements.
32  * The feature of an abstract HTML Element is that it can be added to
33  * HTML Pages, HTML Composites and several other HTML Elements derivations.
34  * Elements may also have attributes set, which are handled by the derived
35  * Element.
36  * @see Page
37  * @see Composite
38  * @version $Id: Element.java,v 1.10 2005/08/13 00:01:23 gregwilkins Exp $
39  * @author Greg Wilkins
40 */

41 public abstract class Element
42 {
43     private static Log log = LogFactory.getLog(Element.class);
44
45     /* ----------------------------------------------------------------- */
46     public static final String JavaDoc
47         noAttributes="",
48         ALIGN="align",
49         LEFT="left",
50         RIGHT="right",
51         CENTER="center",
52         VALIGN="valign",
53         TOP="top",
54         BOTTOM="bottom",
55         MIDDLE="middle",
56         WIDTH="width",
57         HEIGHT="height",
58         SIZE="size",
59         COLOR="color",
60         BGCOLOR="bgcolor",
61         STYLE="style",
62         CLASS="class",
63         ID="id";
64     
65         
66     
67     /* ----------------------------------------------------------------- */
68     /** Dimensions >=0 if set*/
69     private int width=-1;
70     private int height=-1;
71     private int size=-1;
72
73     /* ----------------------------------------------------------------- */
74     /** The space separated string of HTML element attributes.
75      */

76     private String JavaDoc attributes=null;
77     protected Hashtable JavaDoc attributeMap=null;
78
79     /* ----------------------------------------------------------------- */
80     /** Default constructor.
81      */

82     public Element(){}
83
84     /* ----------------------------------------------------------------- */
85     /** Construct with attributes.
86      * @param attributes The initial attributes of the element
87      */

88     public Element(String JavaDoc attributes)
89     {
90         attribute(attributes);
91     }
92
93     /* ----------------------------------------------------------------- */
94     /** Write element to a Writer.
95      * This abstract method is called by the Page or other containing
96      * Element to write the HTML for this element. This must be implemented
97      * by the derived Element classes.
98      * @param out Writer to write the element to.
99      */

100     public abstract void write(Writer JavaDoc out)
101          throws IOException JavaDoc;
102
103     /* ----------------------------------------------------------------- */
104     /** Write Element to an OutputStream.
105      * Calls print(Writer) and checks errors
106      * Elements that override this method should also override
107      * write(Writer) to avoid infinite recursion.
108      * @param out OutputStream to write the element to.
109      */

110     public void write(OutputStream JavaDoc out)
111          throws IOException JavaDoc
112     {
113         Writer JavaDoc writer = new OutputStreamWriter JavaDoc(out);
114         write(writer);
115         writer.flush();
116     }
117     
118     /* ----------------------------------------------------------------- */
119     /** Write Element to an OutputStream.
120      * Calls print(Writer) and checks errors
121      * Elements that override this method should also override
122      * write(Writer) to avoid infinite recursion.
123      * @param out OutputStream to write the element to.
124      */

125     public void write(OutputStream JavaDoc out, String JavaDoc encoding)
126          throws IOException JavaDoc
127     {
128         Writer JavaDoc writer = new OutputStreamWriter JavaDoc(out,encoding);
129         write(writer);
130         writer.flush();
131     }
132
133     /* ----------------------------------------------------------------- */
134     public String JavaDoc attributes()
135     {
136         if (attributes==null && attributeMap==null)
137             return noAttributes;
138
139         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(128);
140         synchronized(buf)
141         {
142             if (attributeMap!=null)
143             {
144                 Enumeration JavaDoc e = attributeMap.keys();
145                 while (e.hasMoreElements())
146                 {
147                     buf.append(' ');
148                     String JavaDoc a = (String JavaDoc)e.nextElement();
149                     buf.append(a);
150                     buf.append('=');
151                     buf.append(attributeMap.get(a).toString());
152                 }
153             }
154             
155             if(attributes!=null && attributes.length()>0)
156             {
157                 if (!attributes.startsWith(" "))
158                     buf.append(' ');
159                 buf.append(attributes);
160             }
161         }
162
163         return buf.toString();
164     }
165
166     /* ----------------------------------------------------------------- */
167     /** Add element Attributes.
168      * The attributes are added to the Element attributes (separated with
169      * a space). The attributes are available to the derived class in the
170      * protected member String <I>attributes</I>
171      * @deprecated Use attribute(String).
172      * @param attributes String of HTML attributes to add to the element.
173      * @return This Element so calls can be chained.
174      */

175     public Element attributes(String JavaDoc attributes)
176     {
177         if (log.isDebugEnabled() && attributes!=null && attributes.indexOf('=')>=0)
178             log.debug("Set attribute with old method: "+attributes+
179                          " on " + getClass().getName());
180
181         if (attributes==null)
182         {
183             this.attributes=null;
184             return this;
185         }
186         
187         if (attributes==noAttributes)
188             return this;
189         
190         if (this.attributes==null)
191             this.attributes=attributes;
192         else
193             this.attributes += ' '+attributes;
194         return this;
195     }
196
197     /* ------------------------------------------------------------ */
198     /** Set attributes from another Element.
199      * @param e Element
200      * @return This Element
201      */

202     public Element setAttributesFrom(Element e)
203     {
204         attributes=e.attributes;
205         attributeMap=(Hashtable JavaDoc)e.attributeMap.clone();
206         return this;
207     }
208
209     
210     /* ----------------------------------------------------------------- */
211     /** Add element Attributes.
212      * The attributes are added to the Element attributes (separated with
213      * a space). The attributes are available to the derived class in the
214      * protected member String <I>attributes</I>
215      * @param attributes String of HTML attributes to add to the element.
216      * A null attribute clears the current attributes.
217      * @return This Element so calls can be chained.
218      */

219     public Element attribute(String JavaDoc attributes)
220     {
221         if (log.isDebugEnabled() && attributes!=null && attributes.indexOf('=')>=0)
222             log.warn("Set attribute with old method: "+attributes+
223                          " on " + getClass().getName());
224         
225         if (attributes==null ||
226             this.attributes==null ||
227             this.attributes==noAttributes ||
228             this.attributes.length()==0)
229             this.attributes=attributes;
230         else
231             this.attributes += ' '+attributes;
232         return this;
233     }
234     
235     /* ----------------------------------------------------------------- */
236     /** Add quoted element Attributes and value.
237      * @param attribute String of HTML attribute tag
238      * @param value String value of the attribute to be quoted
239      * @return This Element so calls can be chained.
240      */

241     public Element attribute(String JavaDoc attribute, Object JavaDoc value)
242     {
243         if (attributeMap==null)
244             attributeMap=new Hashtable JavaDoc(10);
245         
246         if (value!=null)
247         {
248             if (value instanceof String JavaDoc && ((String JavaDoc)value).indexOf('"')!=-1)
249             {
250                 String JavaDoc s=(String JavaDoc)value;
251                 int q=0;
252                 while((q=s.indexOf('"',q))>=0)
253                 {
254                     s=s.substring(0,q)+"&quot;"+s.substring(++q);
255                     q+=6;
256                 }
257                 value=s;
258             }
259             
260             attributeMap.put(attribute,"\""+value+'"');
261         }
262         return this;
263     }
264     
265     /* ----------------------------------------------------------------- */
266     /** Add quoted element Attributes and value.
267      * @param attribute String of HTML attribute tag
268      * @param value String value of the attribute to be quoted
269      * @return This Element so calls can be chained.
270      */

271     public Element attribute(String JavaDoc attribute, long value)
272     {
273         if (attributeMap==null)
274             attributeMap=new Hashtable JavaDoc(10);
275         
276         attributeMap.put(attribute,Long.toString(value));
277         return this;
278     }
279
280     /* ----------------------------------------------------------------- */
281     /** Convert Element to String.
282      * Uses write() to convert the HTML Element to a string.
283      * @return String of the HTML element
284      */

285     public String JavaDoc toString()
286     {
287         try{
288             StringWriter JavaDoc out = new StringWriter JavaDoc();
289             write(out);
290             out.flush();
291             return out.toString();
292         }
293         catch(IOException JavaDoc e){
294             LogSupport.ignore(log,e);
295         }
296         return null;
297     }
298     
299     /* ----------------------------------------------------------------- */
300     /** left justify.
301      * Convenience method equivalent to attribute("align","left"). Not
302      * applicable to all Elements.
303      */

304     public Element left()
305     {
306         return attribute(ALIGN,LEFT);
307     }
308     
309     /* ----------------------------------------------------------------- */
310     /** right justify.
311      * Convenience method equivalent to attribute("align","right"). Not
312      * applicable to all Elements.
313      */

314     public Element right()
315     {
316         return attribute(ALIGN,RIGHT);
317     }
318     
319     /* ----------------------------------------------------------------- */
320     /** Center.
321      * Convenience method equivalent to attribute("align","center"). Not
322      * applicable to all Elements.
323      */

324     public Element center()
325     {
326         return attribute(ALIGN,CENTER);
327     }
328     
329     /* ----------------------------------------------------------------- */
330     /** Top align.
331      * Convenience method equivalent to attribute("valign","top"). Not
332      * applicable to all Elements.
333      */

334     public Element top()
335     {
336         return attribute(VALIGN,TOP);
337     }
338     
339     /* ----------------------------------------------------------------- */
340     /** Bottom align.
341      * Convenience method equivalent to attribute("valign","bottom"). Not
342      * applicable to all Elements.
343      */

344     public Element bottom()
345     {
346         return attribute(VALIGN,BOTTOM);
347     }
348     
349     /* ----------------------------------------------------------------- */
350     /** Middle align.
351      * Convenience method equivalent to attribute("valign","middle"). Not
352      * applicable to all Elements.
353      */

354     public Element middle()
355     {
356         return attribute(VALIGN,MIDDLE);
357     }
358     
359     /* ----------------------------------------------------------------- */
360     /** set width.
361      * Convenience method equivalent to attribute("width",w). Not
362      * applicable to all Elements.
363      */

364     public Element width(int w)
365     {
366         width=w;
367         return attribute(WIDTH,w);
368     }
369     
370     /* ----------------------------------------------------------------- */
371     /** set width.
372      * Convenience method equivalent to attribute("width",w). Not
373      * applicable to all Elements.
374      */

375     public Element width(String JavaDoc w)
376     {
377         width=-1;
378         return attribute(WIDTH,w);
379     }
380     
381     /* ----------------------------------------------------------------- */
382     public int width()
383     {
384         return width;
385     }
386     
387     /* ----------------------------------------------------------------- */
388     /** set height.
389      * Convenience method equivalent to attribute("height",h). Not
390      * applicable to all Elements.
391      */

392     public Element height(int h)
393     {
394         height=h;
395         return attribute(HEIGHT,h);
396     }
397     
398     /* ----------------------------------------------------------------- */
399     /** set height.
400      * Convenience method equivalent to attribute("height",h). Not
401      * applicable to all Elements.
402      */

403     public Element height(String JavaDoc h)
404     {
405         height=-1;
406         return attribute(HEIGHT,h);
407     }
408     
409     /* ----------------------------------------------------------------- */
410     public int height()
411     {
412         return height;
413     }
414     
415     /* ----------------------------------------------------------------- */
416     /** set size.
417      * Convenience method equivalent to attribute("size",s). Not
418      * applicable to all Elements.
419      */

420     public Element size(int s)
421     {
422         size=s;
423         return attribute(SIZE,s);
424     }
425     
426     /* ----------------------------------------------------------------- */
427     /** set size.
428      * Convenience method equivalent to attribute("size",s). Not
429      * applicable to all Elements.
430      */

431     public Element size(String JavaDoc s)
432     {
433         size=-1;
434         return attribute(SIZE,s);
435     }
436     
437     /* ----------------------------------------------------------------- */
438     public int size()
439     {
440         return size;
441     }
442     
443     /* ----------------------------------------------------------------- */
444     /** set color.
445      * Convenience method equivalent to attribute("color",color). Not
446      * applicable to all Elements.
447      */

448     public Element color(String JavaDoc color)
449     {
450         return attribute(COLOR,color);
451     }
452     
453     /* ----------------------------------------------------------------- */
454     /** set BGCOLOR.
455      * Convenience method equivalent to attribute("bgcolor",color). Not
456      * applicable to all Elements.
457      */

458     public Element bgColor(String JavaDoc color)
459     {
460         return attribute(BGCOLOR,color);
461     }
462     
463     /* ----------------------------------------------------------------- */
464     /** set CSS CLASS.
465      */

466     public Element cssClass(String JavaDoc c)
467     {
468         return attribute(CLASS,c);
469     }
470     
471     /* ----------------------------------------------------------------- */
472     /** set CSS ID.
473      * Convenience method equivalent to attribute("id",id).
474      */

475     public Element cssID(String JavaDoc id)
476     {
477         return attribute(ID,id);
478     }
479     
480     /* ----------------------------------------------------------------- */
481     /** set Style.
482      * Convenience method equivalent to attribute("style",style).
483      */

484     public Element style(String JavaDoc style)
485     {
486         return attribute(STYLE,style);
487     }
488 }
489
490
491
492
493
Popular Tags