KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > html > internal > 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 com.sun.org.apache.html.internal.dom;
58
59
60 import java.util.Locale JavaDoc;
61
62 import com.sun.org.apache.xerces.internal.dom.ElementImpl;
63 import org.w3c.dom.Attr JavaDoc;
64 import org.w3c.dom.Node JavaDoc;
65 import org.w3c.dom.NodeList JavaDoc;
66 import org.w3c.dom.html.HTMLElement;
67 import org.w3c.dom.html.HTMLFormElement;
68
69
70 /**
71  * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
72  * will only appear inside HTML documents. This element extends {@link
73  * com.sun.org.apache.xerces.internal.dom.ElementImpl} by adding methods for directly
74  * manipulating HTML-specific attributes. All HTML elements gain access to
75  * the <code>id</code>, <code>title</code>, <code>lang</code>,
76  * <code>dir</code> and <code>class</code> attributes. Other elements
77  * add their own specific attributes.
78  *
79  *
80  * @version $Revision: 1.6 $ $Date: 2003/05/08 20:13:09 $
81  * @author <a HREF="mailto:arkin@exoffice.com">Assaf Arkin</a>
82  * @see org.w3c.dom.html.HTMLElement
83  */

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

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

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

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

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

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

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

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