KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > lazydom > html > LazyHTMLElement


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: LazyHTMLElement.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23 package org.enhydra.xml.lazydom.html;
24
25
26 import org.enhydra.xml.lazydom.LazyElement;
27 import org.enhydra.xml.lazydom.LazyElementNoNS;
28 import org.w3c.dom.Attr JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31 import org.w3c.dom.html.HTMLElement;
32 import org.w3c.dom.html.HTMLFormElement;
33
34 /*
35  * LazyDOM: This is a modified version of org.apache.html.dom.HTMLDocumentImpl
36  * modified to implement the LazyDOM. While most of the HTMLElement classes
37  * are created automatically using a sed script, this was a bit complex for
38  * this class, so it was done by hand. A diff with the Xerces class should
39  * make an upgrade of this class easy.
40  */

41
42 /**
43  * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
44  * will only appear inside HTML documents. This element extends {@link
45  * org.enhydra.apache.xerces.dom.ElementImpl} by adding methods for directly
46  * manipulating HTML-specific attributes. All HTML elements gain access to
47  * the <code>id</code>, <code>title</code>, <code>lang</code>,
48  * <code>dir</code> and <code>class</code> attributes. Other elements
49  * add their own specific attributes.
50  *
51  *
52  * @version $Revision: 1.2 $ $Date: 2005/01/26 08:29:24 $
53  * @author <a HREF="mailto:arkin@exoffice.com">Assaf Arkin</a>
54  * @see org.w3c.dom.html.HTMLElement
55  */

56 public class LazyHTMLElement
57     extends LazyElementNoNS
58     implements HTMLElement
59 {
60
61
62     /**
63      * Constructor required owner document and element tag name. Will be called
64      * by the constructor of specific element types but with a known tag name.
65      * Assures that the owner document is an HTML element.
66      *
67      * @param owner The owner HTML document
68      * @param template If not null, then this template will be used to
69      * initialize the Element.
70      * @param tagName The element's tag name
71      */

72     LazyHTMLElement( LazyHTMLDocument owner, LazyElement template, String JavaDoc tagName )
73     {
74         // LazyDOM: removed tagName.toUpperCase(); always uppper case here.
75
super( owner, template, tagName );
76     }
77     
78     
79     public String JavaDoc getId()
80     {
81         return getAttribute( "id" );
82     }
83     
84     
85     public void setId( String JavaDoc id )
86     {
87         setAttribute( "id", id );
88     }
89     
90     
91     public String JavaDoc getTitle()
92     {
93         return getAttribute( "title" );
94     }
95     
96     
97     public void setTitle( String JavaDoc title )
98     {
99         setAttribute( "title", title );
100     }
101     
102     
103     public String JavaDoc getLang()
104     {
105         return getAttribute( "lang" );
106     }
107     
108     
109     public void setLang( String JavaDoc lang )
110     {
111         setAttribute( "lang", lang );
112     }
113     
114     
115     public String JavaDoc getDir()
116     {
117         return getAttribute( "dir" );
118     }
119     
120     
121     public void setDir( String JavaDoc dir )
122     {
123         setAttribute( "dir", dir );
124     }
125
126     
127     public String JavaDoc getClassName()
128     {
129         return getAttribute( "class" );
130     }
131
132     
133     public void setClassName( String JavaDoc className )
134     {
135         setAttribute( "class", className );
136     }
137     
138     
139     /**
140      * Convenience method used to translate an attribute value into an integer
141      * value. Returns the integer value or zero if the attribute is not a
142      * valid numeric string.
143      *
144      * @param value The value of the attribute
145      * @return The integer value, or zero if not a valid numeric string
146      */

147     int getInteger( String JavaDoc value )
148     {
149         try
150         {
151             return Integer.parseInt( value );
152         }
153         catch ( NumberFormatException JavaDoc except )
154         {
155             return 0;
156         }
157     }
158     
159     
160     /**
161      * Convenience method used to translate an attribute value into a boolean
162      * value. If the attribute has an associated value (even an empty string),
163      * it is set and true is returned. If the attribute does not exist, false
164      * is returend.
165      *
166      * @param value The value of the attribute
167      * @return True or false depending on whether the attribute has been set
168      */

169     boolean getBinary( String JavaDoc name )
170     {
171         return ( getAttributeNode( name ) != null );
172     }
173     
174     
175     /**
176      * Convenience method used to set a boolean attribute. If the value is true,
177      * the attribute is set to an empty string. If the value is false, the attribute
178      * is removed. HTML 4.0 understands empty strings as set attributes.
179      *
180      * @param name The name of the attribute
181      * @param value The value of the attribute
182      */

183     void setAttribute( String JavaDoc name, boolean value )
184     {
185         if ( value )
186             setAttribute( name, name );
187         else
188             removeAttribute( name );
189     }
190
191
192     public Attr JavaDoc getAttributeNode( String JavaDoc attrName )
193     {
194     return super.getAttributeNode( attrName.toLowerCase() );
195     }
196
197
198     public Attr JavaDoc getAttributeNodeNS( String JavaDoc namespaceURI,
199                     String JavaDoc localName )
200     {
201     if ( namespaceURI != null && namespaceURI.length() > 0 )
202         return super.getAttributeNodeNS( namespaceURI, localName );
203     else
204         return super.getAttributeNode( localName.toLowerCase() );
205     }
206     
207     
208     public String JavaDoc getAttribute( String JavaDoc attrName )
209     {
210     return super.getAttribute( attrName.toLowerCase() );
211     }
212
213
214     public String JavaDoc getAttributeNS( String JavaDoc namespaceURI,
215                   String JavaDoc localName )
216     {
217     if ( namespaceURI != null && namespaceURI.length() > 0 )
218         return super.getAttributeNS( namespaceURI, localName );
219     else
220         return super.getAttribute( localName.toLowerCase() );
221     }
222
223
224     public final NodeList JavaDoc getElementsByTagName( String JavaDoc tagName )
225     {
226     return super.getElementsByTagName( tagName.toUpperCase() );
227     }
228
229
230     public final NodeList JavaDoc getElementsByTagNameNS( String JavaDoc namespaceURI,
231                               String JavaDoc localName )
232     {
233     if ( namespaceURI != null && namespaceURI.length() > 0 )
234         return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase() );
235     else
236         return super.getElementsByTagName( localName.toUpperCase() );
237     }
238
239
240     /**
241      * Convenience method used to capitalize a one-off attribute value before it
242      * is returned. For example, the align values "LEFT" and "left" will both
243      * return as "Left".
244      *
245      * @param value The value of the attribute
246      * @return The capitalized value
247      */

248     String JavaDoc capitalize( String JavaDoc value )
249     {
250         char[] chars;
251         int i;
252         
253         // Convert string to charactares. Convert the first one to upper case,
254
// the other characters to lower case, and return the converted string.
255
chars = value.toCharArray();
256         if ( chars.length > 0 )
257         {
258             chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
259             for ( i = 1 ; i < chars.length ; ++i )
260                 chars[ i ] = Character.toLowerCase( chars[ i ] );
261             return String.valueOf( chars );
262         }
263         return value;
264     }
265     
266
267     /**
268      * Convenience method used to capitalize a one-off attribute value before it
269      * is returned. For example, the align values "LEFT" and "left" will both
270      * return as "Left".
271      *
272      * @param name The name of the attribute
273      * @return The capitalized value
274      */

275     String JavaDoc getCapitalized( String JavaDoc name )
276     {
277         String JavaDoc value;
278         char[] chars;
279         int i;
280         
281         value = getAttribute( name );
282         if ( value != null )
283         {
284             // Convert string to charactares. Convert the first one to upper case,
285
// the other characters to lower case, and return the converted string.
286
chars = value.toCharArray();
287             if ( chars.length > 0 )
288             {
289                 chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
290                 for ( i = 1 ; i < chars.length ; ++i )
291                     chars[ i ] = Character.toLowerCase( chars[ i ] );
292                 return String.valueOf( chars );
293             }
294         }
295         return value;
296     }
297
298     
299     /**
300      * Convenience method returns the form in which this form element is contained.
301      * This method is exposed for form elements through the DOM API, but other
302      * elements have no access to it through the API.
303      */

304     public HTMLFormElement getForm()
305     {
306         Node JavaDoc parent;
307         
308         parent = getParentNode();
309         while ( parent != null )
310         {
311             if ( parent instanceof HTMLFormElement )
312                 return (HTMLFormElement) parent;
313             parent = parent.getParentNode();
314         }
315         return null;
316     }
317
318
319 }
320
321
Popular Tags