KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999,2000,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.html.dom;
17
18 import java.util.Locale JavaDoc;
19
20 import org.apache.xerces.dom.ElementImpl;
21 import org.w3c.dom.Attr JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.w3c.dom.html.HTMLElement;
25 import org.w3c.dom.html.HTMLFormElement;
26
27 /**
28  * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
29  * will only appear inside HTML documents. This element extends {@link
30  * org.apache.xerces.dom.ElementImpl} by adding methods for directly
31  * manipulating HTML-specific attributes. All HTML elements gain access to
32  * the <code>id</code>, <code>title</code>, <code>lang</code>,
33  * <code>dir</code> and <code>class</code> attributes. Other elements
34  * add their own specific attributes.
35  *
36  * @xerces.internal
37  *
38  * @version $Revision: 1.9 $ $Date: 2005/04/18 00:41:07 $
39  * @author <a HREF="mailto:arkin@exoffice.com">Assaf Arkin</a>
40  * @see org.w3c.dom.html.HTMLElement
41  */

42 public class HTMLElementImpl
43     extends ElementImpl
44     implements HTMLElement
45 {
46
47     private static final long serialVersionUID = 3833188025499792690L;
48
49     /**
50      * Constructor required owner document and element tag name. Will be called
51      * by the constructor of specific element types but with a known tag name.
52      * Assures that the owner document is an HTML element.
53      *
54      * @param owner The owner HTML document
55      * @param tagName The element's tag name
56      */

57     HTMLElementImpl( HTMLDocumentImpl owner, String JavaDoc tagName )
58     {
59         super( owner, tagName.toUpperCase(Locale.ENGLISH) );
60     }
61     
62     
63     public String JavaDoc getId()
64     {
65         return getAttribute( "id" );
66     }
67     
68     
69     public void setId( String JavaDoc id )
70     {
71         setAttribute( "id", id );
72     }
73     
74     
75     public String JavaDoc getTitle()
76     {
77         return getAttribute( "title" );
78     }
79     
80     
81     public void setTitle( String JavaDoc title )
82     {
83         setAttribute( "title", title );
84     }
85     
86     
87     public String JavaDoc getLang()
88     {
89         return getAttribute( "lang" );
90     }
91     
92     
93     public void setLang( String JavaDoc lang )
94     {
95         setAttribute( "lang", lang );
96     }
97     
98     
99     public String JavaDoc getDir()
100     {
101         return getAttribute( "dir" );
102     }
103     
104     
105     public void setDir( String JavaDoc dir )
106     {
107         setAttribute( "dir", dir );
108     }
109
110     
111     public String JavaDoc getClassName()
112     {
113         return getAttribute( "class" );
114     }
115
116     
117     public void setClassName( String JavaDoc className )
118     {
119         setAttribute( "class", className );
120     }
121     
122     
123     /**
124      * Convenience method used to translate an attribute value into an integer
125      * value. Returns the integer value or zero if the attribute is not a
126      * valid numeric string.
127      *
128      * @param value The value of the attribute
129      * @return The integer value, or zero if not a valid numeric string
130      */

131     int getInteger( String JavaDoc value )
132     {
133         try
134         {
135             return Integer.parseInt( value );
136         }
137         catch ( NumberFormatException JavaDoc except )
138         {
139             return 0;
140         }
141     }
142     
143     
144     /**
145      * Convenience method used to translate an attribute value into a boolean
146      * value. If the attribute has an associated value (even an empty string),
147      * it is set and true is returned. If the attribute does not exist, false
148      * is returend.
149      *
150      * @param value The value of the attribute
151      * @return True or false depending on whether the attribute has been set
152      */

153     boolean getBinary( String JavaDoc name )
154     {
155         return ( getAttributeNode( name ) != null );
156     }
157     
158     
159     /**
160      * Convenience method used to set a boolean attribute. If the value is true,
161      * the attribute is set to an empty string. If the value is false, the attribute
162      * is removed. HTML 4.0 understands empty strings as set attributes.
163      *
164      * @param name The name of the attribute
165      * @param value The value of the attribute
166      */

167     void setAttribute( String JavaDoc name, boolean value )
168     {
169         if ( value )
170             setAttribute( name, name );
171         else
172             removeAttribute( name );
173     }
174
175
176     public Attr JavaDoc getAttributeNode( String JavaDoc attrName )
177     {
178     return super.getAttributeNode( attrName.toLowerCase(Locale.ENGLISH) );
179     }
180
181
182     public Attr JavaDoc getAttributeNodeNS( String JavaDoc namespaceURI,
183                     String JavaDoc localName )
184     {
185     if ( namespaceURI != null && namespaceURI.length() > 0 )
186         return super.getAttributeNodeNS( namespaceURI, localName );
187     else
188         return super.getAttributeNode( localName.toLowerCase(Locale.ENGLISH) );
189     }
190     
191     
192     public String JavaDoc getAttribute( String JavaDoc attrName )
193     {
194     return super.getAttribute( attrName.toLowerCase(Locale.ENGLISH) );
195     }
196
197
198     public String JavaDoc getAttributeNS( String JavaDoc namespaceURI,
199                   String JavaDoc localName )
200     {
201     if ( namespaceURI != null && namespaceURI.length() > 0 )
202         return super.getAttributeNS( namespaceURI, localName );
203     else
204         return super.getAttribute( localName.toLowerCase(Locale.ENGLISH) );
205     }
206
207
208     public final NodeList JavaDoc getElementsByTagName( String JavaDoc tagName )
209     {
210     return super.getElementsByTagName( tagName.toUpperCase(Locale.ENGLISH) );
211     }
212
213
214     public final NodeList JavaDoc getElementsByTagNameNS( String JavaDoc namespaceURI,
215                               String JavaDoc localName )
216     {
217     if ( namespaceURI != null && namespaceURI.length() > 0 )
218         return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase(Locale.ENGLISH) );
219     else
220         return super.getElementsByTagName( localName.toUpperCase(Locale.ENGLISH) );
221     }
222
223
224     /**
225      * Convenience method used to capitalize a one-off attribute value before it
226      * is returned. For example, the align values "LEFT" and "left" will both
227      * return as "Left".
228      *
229      * @param value The value of the attribute
230      * @return The capitalized value
231      */

232     String JavaDoc capitalize( String JavaDoc value )
233     {
234         char[] chars;
235         int i;
236         
237         // Convert string to charactares. Convert the first one to upper case,
238
// the other characters to lower case, and return the converted string.
239
chars = value.toCharArray();
240         if ( chars.length > 0 )
241         {
242             chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
243             for ( i = 1 ; i < chars.length ; ++i )
244                 chars[ i ] = Character.toLowerCase( chars[ i ] );
245             return String.valueOf( chars );
246         }
247         return value;
248     }
249     
250
251     /**
252      * Convenience method used to capitalize a one-off attribute value before it
253      * is returned. For example, the align values "LEFT" and "left" will both
254      * return as "Left".
255      *
256      * @param name The name of the attribute
257      * @return The capitalized value
258      */

259     String JavaDoc getCapitalized( String JavaDoc name )
260     {
261         String JavaDoc value;
262         char[] chars;
263         int i;
264         
265         value = getAttribute( name );
266         if ( value != null )
267         {
268             // Convert string to charactares. Convert the first one to upper case,
269
// the other characters to lower case, and return the converted string.
270
chars = value.toCharArray();
271             if ( chars.length > 0 )
272             {
273                 chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
274                 for ( i = 1 ; i < chars.length ; ++i )
275                     chars[ i ] = Character.toLowerCase( chars[ i ] );
276                 return String.valueOf( chars );
277             }
278         }
279         return value;
280     }
281
282     
283     /**
284      * Convenience method returns the form in which this form element is contained.
285      * This method is exposed for form elements through the DOM API, but other
286      * elements have no access to it through the API.
287      */

288     public HTMLFormElement getForm()
289     {
290         Node JavaDoc parent;
291         
292         parent = getParentNode();
293         while ( parent != null )
294         {
295             if ( parent instanceof HTMLFormElement )
296                 return (HTMLFormElement) parent;
297             parent = parent.getParentNode();
298         }
299         return null;
300     }
301
302
303 }
304
305
Popular Tags