KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > html > dom > HTMLElementImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999,2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package org.enhydra.apache.html.dom;
58
59
60 import org.enhydra.apache.xerces.dom.ElementImpl;
61 import org.w3c.dom.Attr JavaDoc;
62 import org.w3c.dom.Node JavaDoc;
63 import org.w3c.dom.NodeList JavaDoc;
64 import org.w3c.dom.html.HTMLElement;
65 import org.w3c.dom.html.HTMLFormElement;
66
67
68 /**
69  * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
70  * will only appear inside HTML documents. This element extends {@link
71  * org.enhydra.apache.xerces.dom.ElementImpl} by adding methods for directly
72  * manipulating HTML-specific attributes. All HTML elements gain access to
73  * the <code>id</code>, <code>title</code>, <code>lang</code>,
74  * <code>dir</code> and <code>class</code> attributes. Other elements
75  * add their own specific attributes.
76  *
77  *
78  * @version $Revision: 1.2 $ $Date: 2005/01/26 08:28:44 $
79  * @author <a HREF="mailto:arkin@exoffice.com">Assaf Arkin</a>
80  * @see org.w3c.dom.html.HTMLElement
81  */

82 public class HTMLElementImpl
83     extends ElementImpl
84     implements HTMLElement
85 {
86
87
88     /**
89      * Constructor required owner document and element tag name. Will be called
90      * by the constructor of specific element types but with a known tag name.
91      * Assures that the owner document is an HTML element.
92      *
93      * @param owner The owner HTML document
94      * @param tagName The element's tag name
95      */

96     public HTMLElementImpl( HTMLDocumentImpl owner, String JavaDoc tagName )
97     {
98         super( owner, tagName.toUpperCase() );
99     }
100     
101     
102     public String JavaDoc getId()
103     {
104         return getAttribute( "id" );
105     }
106     
107     
108     public void setId( String JavaDoc id )
109     {
110         setAttribute( "id", id );
111     }
112     
113     
114     public String JavaDoc getTitle()
115     {
116         return getAttribute( "title" );
117     }
118     
119     
120     public void setTitle( String JavaDoc title )
121     {
122         setAttribute( "title", title );
123     }
124     
125     
126     public String JavaDoc getLang()
127     {
128         return getAttribute( "lang" );
129     }
130     
131     
132     public void setLang( String JavaDoc lang )
133     {
134         setAttribute( "lang", lang );
135     }
136     
137     
138     public String JavaDoc getDir()
139     {
140         return getAttribute( "dir" );
141     }
142     
143     
144     public void setDir( String JavaDoc dir )
145     {
146         setAttribute( "dir", dir );
147     }
148
149     
150     public String JavaDoc getClassName()
151     {
152         return getAttribute( "class" );
153     }
154
155     
156     public void setClassName( String JavaDoc className )
157     {
158         setAttribute( "class", className );
159     }
160     
161     
162     /**
163      * Convenience method used to translate an attribute value into an integer
164      * value. Returns the integer value or zero if the attribute is not a
165      * valid numeric string.
166      *
167      * @param value The value of the attribute
168      * @return The integer value, or zero if not a valid numeric string
169      */

170     int getInteger( String JavaDoc value )
171     {
172         try
173         {
174             return Integer.parseInt( value );
175         }
176         catch ( NumberFormatException JavaDoc except )
177         {
178             return 0;
179         }
180     }
181     
182     
183     /**
184      * Convenience method used to translate an attribute value into a boolean
185      * value. If the attribute has an associated value (even an empty string),
186      * it is set and true is returned. If the attribute does not exist, false
187      * is returend.
188      *
189      * @param value The value of the attribute
190      * @return True or false depending on whether the attribute has been set
191      */

192     boolean getBinary( String JavaDoc name )
193     {
194         return ( getAttributeNode( name ) != null );
195     }
196     
197     
198     /**
199      * Convenience method used to set a boolean attribute. If the value is true,
200      * the attribute is set to an empty string. If the value is false, the attribute
201      * is removed. HTML 4.0 understands empty strings as set attributes.
202      *
203      * @param name The name of the attribute
204      * @param value The value of the attribute
205      */

206     void setAttribute( String JavaDoc name, boolean value )
207     {
208         if ( value )
209             setAttribute( name, name );
210         else
211             removeAttribute( name );
212     }
213
214
215     public Attr JavaDoc getAttributeNode( String JavaDoc attrName )
216     {
217     return super.getAttributeNode( attrName.toLowerCase() );
218     }
219
220
221     public Attr JavaDoc getAttributeNodeNS( String JavaDoc namespaceURI,
222                     String JavaDoc localName )
223     {
224     if ( namespaceURI != null && namespaceURI.length() > 0 )
225         return super.getAttributeNodeNS( namespaceURI, localName );
226     else
227         return super.getAttributeNode( localName.toLowerCase() );
228     }
229     
230     
231     public String JavaDoc getAttribute( String JavaDoc attrName )
232     {
233     return super.getAttribute( attrName.toLowerCase() );
234     }
235
236
237     public String JavaDoc getAttributeNS( String JavaDoc namespaceURI,
238                   String JavaDoc localName )
239     {
240     if ( namespaceURI != null && namespaceURI.length() > 0 )
241         return super.getAttributeNS( namespaceURI, localName );
242     else
243         return super.getAttribute( localName.toLowerCase() );
244     }
245
246
247     public final NodeList JavaDoc getElementsByTagName( String JavaDoc tagName )
248     {
249     return super.getElementsByTagName( tagName.toUpperCase() );
250     }
251
252
253     public final NodeList JavaDoc getElementsByTagNameNS( String JavaDoc namespaceURI,
254                               String JavaDoc localName )
255     {
256     if ( namespaceURI != null && namespaceURI.length() > 0 )
257         return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase() );
258     else
259         return super.getElementsByTagName( localName.toUpperCase() );
260     }
261
262
263     /**
264      * Convenience method used to capitalize a one-off attribute value before it
265      * is returned. For example, the align values "LEFT" and "left" will both
266      * return as "Left".
267      *
268      * @param value The value of the attribute
269      * @return The capitalized value
270      */

271     String JavaDoc capitalize( String JavaDoc value )
272     {
273         char[] chars;
274         int i;
275         
276         // Convert string to charactares. Convert the first one to upper case,
277
// the other characters to lower case, and return the converted string.
278
chars = value.toCharArray();
279         if ( chars.length > 0 )
280         {
281             chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
282             for ( i = 1 ; i < chars.length ; ++i )
283                 chars[ i ] = Character.toLowerCase( chars[ i ] );
284             return String.valueOf( chars );
285         }
286         return value;
287     }
288     
289
290     /**
291      * Convenience method used to capitalize a one-off attribute value before it
292      * is returned. For example, the align values "LEFT" and "left" will both
293      * return as "Left".
294      *
295      * @param name The name of the attribute
296      * @return The capitalized value
297      */

298     String JavaDoc getCapitalized( String JavaDoc name )
299     {
300         String JavaDoc value;
301         char[] chars;
302         int i;
303         
304         value = getAttribute( name );
305         if ( value != null )
306         {
307             // Convert string to charactares. Convert the first one to upper case,
308
// the other characters to lower case, and return the converted string.
309
chars = value.toCharArray();
310             if ( chars.length > 0 )
311             {
312                 chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
313                 for ( i = 1 ; i < chars.length ; ++i )
314                     chars[ i ] = Character.toLowerCase( chars[ i ] );
315                 return String.valueOf( chars );
316             }
317         }
318         return value;
319     }
320
321     
322     /**
323      * Convenience method returns the form in which this form element is contained.
324      * This method is exposed for form elements through the DOM API, but other
325      * elements have no access to it through the API.
326      */

327     public HTMLFormElement getForm()
328     {
329         Node JavaDoc parent;
330         
331         parent = getParentNode();
332         while ( parent != null )
333         {
334             if ( parent instanceof HTMLFormElement )
335                 return (HTMLFormElement) parent;
336             parent = parent.getParentNode();
337         }
338         return null;
339     }
340
341
342 }
343
344
Popular Tags