KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xhtml > dom > xerces > XHTMLElementImpl


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.xml.xhtml.dom.xerces;
58
59 import org.enhydra.apache.xerces.dom.ElementNSImpl;
60 import org.w3c.dom.Attr JavaDoc;
61 import org.w3c.dom.Node JavaDoc;
62 import org.w3c.dom.NodeList JavaDoc;
63 import org.w3c.dom.html.HTMLElement;
64 import org.w3c.dom.html.HTMLFormElement;
65
66 /*
67  * Derived from Xerces HTMLDocument implementation.
68  * Author: Assaf Arkin <arkin@exoffice.com>
69  */

70
71 /**
72  * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
73  * will only appear inside HTML documents. This element extends {@link
74  * org.enhydra.apache.xerces.dom.ElementImpl} by adding methods for directly
75  * manipulating HTML-specific attributes. All HTML elements gain access to
76  * the <code>id</code>, <code>title</code>, <code>lang</code>,
77  * <code>dir</code> and <code>class</code> attributes. Other elements
78  * add their own specific attributes.
79  *
80  * @see org.w3c.dom.html.HTMLElement
81  */

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

95     XHTMLElementImpl( XHTMLDocumentBase owner, String JavaDoc namespaceURI, String JavaDoc tagName )
96     {
97         super( owner, namespaceURI, tagName );
98     }
99     
100     
101     public String JavaDoc getId()
102     {
103         return getAttribute( "id" );
104     }
105     
106     
107     public void setId( String JavaDoc id )
108     {
109         setAttribute( "id", id );
110     }
111     
112     
113     public String JavaDoc getTitle()
114     {
115         return getAttribute( "title" );
116     }
117     
118     
119     public void setTitle( String JavaDoc title )
120     {
121         setAttribute( "title", title );
122     }
123     
124     
125     public String JavaDoc getLang()
126     {
127         return getAttribute( "lang" );
128     }
129     
130     
131     public void setLang( String JavaDoc lang )
132     {
133         setAttribute( "lang", lang );
134     }
135     
136     
137     public String JavaDoc getDir()
138     {
139         return getAttribute( "dir" );
140     }
141     
142     
143     public void setDir( String JavaDoc dir )
144     {
145         setAttribute( "dir", dir );
146     }
147
148     
149     public String JavaDoc getClassName()
150     {
151         return getAttribute( "class" );
152     }
153
154     
155     public void setClassName( String JavaDoc className )
156     {
157         setAttribute( "class", className );
158     }
159
160     public Attr JavaDoc getAttributeNode( String JavaDoc attrName )
161     {
162     return super.getAttributeNode( attrName.toLowerCase() );
163     }
164
165
166     public Attr JavaDoc getAttributeNodeNS( String JavaDoc namespaceURI,
167                     String JavaDoc localName )
168     {
169     if ( namespaceURI != null && namespaceURI.length() > 0 )
170         return super.getAttributeNodeNS( namespaceURI, localName );
171     else
172         return super.getAttributeNode( localName.toLowerCase() );
173     }
174     
175     
176     public String JavaDoc getAttribute( String JavaDoc attrName )
177     {
178     return super.getAttribute( attrName.toLowerCase() );
179     }
180
181
182     public String JavaDoc getAttributeNS( String JavaDoc namespaceURI,
183                   String JavaDoc localName )
184     {
185     if ( namespaceURI != null && namespaceURI.length() > 0 )
186         return super.getAttributeNS( namespaceURI, localName );
187     else
188         return super.getAttribute( localName.toLowerCase() );
189     }
190
191
192     public final NodeList JavaDoc getElementsByTagName( String JavaDoc tagName )
193     {
194     return super.getElementsByTagName( tagName.toUpperCase() );
195     }
196
197
198     public final NodeList JavaDoc getElementsByTagNameNS( String JavaDoc namespaceURI,
199                               String JavaDoc localName )
200     {
201     if ( namespaceURI != null && namespaceURI.length() > 0 )
202         return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase() );
203     else
204         return super.getElementsByTagName( localName.toUpperCase() );
205     }
206
207
208     /**
209      * Convenience method used to capitalize a one-off attribute value before it
210      * is returned. For example, the align values "LEFT" and "left" will both
211      * return as "Left".
212      *
213      * @param value The value of the attribute
214      * @return The capitalized value
215      */

216     String JavaDoc capitalize( String JavaDoc value )
217     {
218         char[] chars;
219         int i;
220         
221         // Convert string to charactares. Convert the first one to upper case,
222
// the other characters to lower case, and return the converted string.
223
chars = value.toCharArray();
224         if ( chars.length > 0 )
225         {
226             chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
227             for ( i = 1 ; i < chars.length ; ++i )
228                 chars[ i ] = Character.toLowerCase( chars[ i ] );
229             return String.valueOf( chars );
230         }
231         return value;
232     }
233     
234
235     /**
236      * Convenience method used to capitalize a one-off attribute value before it
237      * is returned. For example, the align values "LEFT" and "left" will both
238      * return as "Left".
239      *
240      * @param name The name of the attribute
241      * @return The capitalized value
242      */

243     String JavaDoc getCapitalized( String JavaDoc name )
244     {
245         String JavaDoc value;
246         char[] chars;
247         int i;
248         
249         value = getAttribute( name );
250         if ( value != null )
251         {
252             // Convert string to charactares. Convert the first one to upper case,
253
// the other characters to lower case, and return the converted string.
254
chars = value.toCharArray();
255             if ( chars.length > 0 )
256             {
257                 chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
258                 for ( i = 1 ; i < chars.length ; ++i )
259                     chars[ i ] = Character.toLowerCase( chars[ i ] );
260                 return String.valueOf( chars );
261             }
262         }
263         return value;
264     }
265
266     
267     /**
268      * Convenience method returns the form in which this form element is contained.
269      * This method is exposed for form elements through the DOM API, but other
270      * elements have no access to it through the API.
271      */

272     public HTMLFormElement getForm()
273     {
274         Node JavaDoc parent;
275         
276         parent = getParentNode();
277         while ( parent != null )
278         {
279             if ( parent instanceof HTMLFormElement )
280                 return (HTMLFormElement) parent;
281             parent = parent.getParentNode();
282         }
283         return null;
284     }
285
286     
287     /*
288      * Get the value of a boolean attribute.
289      */

290     protected final boolean getBooleanAttribute(String JavaDoc attr,
291                                                 boolean defaultValue) {
292     String JavaDoc attrVal = getAttribute(attr);
293         if (attrVal != null) {
294             return (attrVal.equalsIgnoreCase("y")
295                     || attrVal.equalsIgnoreCase("yes")
296                     || attrVal.equalsIgnoreCase("true"));
297         } else {
298             return defaultValue;
299         }
300     }
301
302     /*
303      * Get the value of a boolean attribute.
304      */

305     protected final boolean getBooleanAttribute(String JavaDoc attr) {
306         return getBooleanAttribute(attr, false);
307     }
308
309     /*
310      * Set the value of a boolean attribute.
311      */

312     protected final void setAttribute(String JavaDoc attr,
313                                       boolean value) {
314         //setAttribute(attr, (value ? "true" : "false"));
315
//bring this in-line with HTMLElementImpl which
316
//already treats boolean attributes as
317
//attribute="attribute". "true" and "false" are
318
//not valid values for boolean attributes.
319
if ( value )
320             setAttribute( attr, attr );
321         else
322             removeAttribute( attr );
323     }
324
325     /*
326      * Get the value of a integer attribute
327      */

328     protected final int getIntAttribute(String JavaDoc attr,
329                                         int defaultValue) {
330     String JavaDoc attrVal = getAttribute(attr);
331     if (attrVal != null) {
332             return Integer.parseInt(attrVal);
333     } else {
334             return defaultValue;
335         }
336     }
337
338     /*
339      * Get the value of a integer attribute
340      */

341     protected final int getIntAttribute(String JavaDoc attr) {
342         return getIntAttribute(attr, 0);
343     }
344
345     /*
346      * Set the value of a integer attribute.
347      */

348     protected final void setAttribute(String JavaDoc attr,
349                                       int value) {
350         setAttribute(attr, Integer.toString(value));
351     }
352 }
353
354
Popular Tags