KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom > html > HTMLElementImpl


1 /**
2  * org/ozone-db/xml/dom/html/HTMLElementImpl.java
3  *
4  * The contents of this file are subject to the OpenXML Public
5  * License Version 1.0; you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at
7  * http://www.openxml.org/license.html
8  *
9  * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
10  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
11  * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
12  * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
13  * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
14  * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
15  *
16  * The Initial Developer of this code under the License is Assaf Arkin.
17  * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
18  * All Rights Reserved.
19  */

20
21
22 package org.ozoneDB.xml.dom.html;
23
24
25 import org.ozoneDB.xml.dom.DOMExceptionImpl;
26 import org.ozoneDB.xml.dom.ElementImpl;
27 import org.ozoneDB.xml.dom.NodeImpl;
28 import org.w3c.dom.*;
29 import org.w3c.dom.html.HTMLElement;
30 import org.w3c.dom.html.HTMLFormElement;
31
32
33 /**
34  * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
35  * will only appear inside HTML documents. This element extends {@link
36  * org.openxml.dom.ElementImpl} by adding methods for directly manipulating
37  * HTML-specific attributes. All HTML elements gain access to the
38  * <code>id</code>, <code>title</code>, <code>lang</code>, <code>dir</code>
39  * and <code>class</code> attributes. Other elements add their own specific
40  * attributes.
41  * <P>
42  * Note that some support is provided by {@link NodeImpl}
43  * directly: translating all tag names to upper case and all attribute names
44  * to lower case.
45  *
46  *
47  * @version $Revision: 1.2 $ $Date: 2003/11/20 23:18:42 $
48  * @author <a HREF="mailto:arkin@trendline.co.il">Assaf Arkin</a>
49  * @see org.w3c.dom.html.HTMLElement
50  * @see ElementImpl
51  */

52 public class HTMLElementImpl extends ElementImpl implements HTMLElement {
53
54
55     public String JavaDoc getId() {
56         return getAttribute( "id" );
57     }
58
59
60     public void setId( String JavaDoc id ) {
61         setAttribute( "id", id );
62     }
63
64
65     public String JavaDoc getTitle() {
66         return getAttribute( "title" );
67     }
68
69
70     public void setTitle( String JavaDoc title ) {
71         setAttribute( "title", title );
72     }
73
74
75     public String JavaDoc getLang() {
76         return getAttribute( "lang" );
77     }
78
79
80     public void setLang( String JavaDoc lang ) {
81         setAttribute( "lang", lang );
82     }
83
84
85     public String JavaDoc getDir() {
86         return getAttribute( "dir" );
87     }
88
89
90     public void setDir( String JavaDoc dir ) {
91         setAttribute( "dir", dir );
92     }
93
94
95     public String JavaDoc getClassName() {
96         return getAttribute( "class" );
97     }
98
99
100     public void setClassName( String JavaDoc className ) {
101         setAttribute( "class", className );
102     }
103
104
105     /**
106      * Convenience method used to translate an attribute value into an integer
107      * value. Returns the integer value or zero if the attribute is not a
108      * valid numeric string.
109      *
110      * @param value The value of the attribute
111      * @return The integer value, or zero if not a valid numeric string
112      */

113     int toInteger( String JavaDoc value ) {
114         try {
115             return Integer.parseInt( value );
116         } catch (NumberFormatException JavaDoc except) {
117             return 0;
118         }
119     }
120
121
122     /**
123      * Convenience method used to translate an attribute value into a boolean
124      * value. If the attribute has an associated value (even an empty string),
125      * it is set and true is returned. If the attribute does not exist, false
126      * is returend.
127      *
128      * @param name The value of the attribute
129      * @return True or false depending on whether the attribute has been set
130      */

131     boolean toBoolean( String JavaDoc name ) {
132         return getAttribute( name ) != null;
133     }
134
135
136     /**
137      * Convenience method used to set a boolean attribute. If the value is true,
138      * the attribute is set to an empty string. If the value is false, the attribute
139      * is removed. HTML 4.0 understands empty strings as set attributes.
140      *
141      * @param name The name of the attribute
142      * @param value The value of the attribute
143      */

144     void setAttribute( String JavaDoc name, boolean value ) {
145         if (value) {
146             setAttribute( name, "" );
147         } else {
148             removeAttribute( name );
149         }
150     }
151
152
153     /**
154      * Convenience method used to capitalize a one-off attribute value before it
155      * is returned. For example, the align values "LEFT" and "left" will both
156      * return as "Left".
157      *
158      * @param value The value of the attribute
159      * @return The capitalized value
160      */

161     String JavaDoc capitalize( String JavaDoc value ) {
162         char[] chars;
163         int i;
164
165         // Convert string to charactares. Convert the first one to upper case,
166
// the other characters to lower case, and return the converted string.
167
chars = value.toCharArray();
168         if (chars.length > 0) {
169             chars[0] = Character.toUpperCase( chars[0] );
170             for (i = 1; i < chars.length; ++i) {
171                 chars[i] = Character.toLowerCase( chars[i] );
172             }
173             return String.valueOf( chars );
174         }
175         return value;
176     }
177
178
179     /**
180      * Convenience method used to capitalize a one-off attribute value before it
181      * is returned. For example, the align values "LEFT" and "left" will both
182      * return as "Left".
183      *
184      * @param name The name of the attribute
185      * @return The capitalized value
186      */

187     String JavaDoc getCapitalized( String JavaDoc name ) {
188         String JavaDoc value;
189         char[] chars;
190         int i;
191
192         value = getAttribute( name );
193         if (value != null) {
194             // Convert string to charactares. Convert the first one to upper case,
195
// the other characters to lower case, and return the converted string.
196
chars = value.toCharArray();
197             if (chars.length > 0) {
198                 chars[0] = Character.toUpperCase( chars[0] );
199                 for (i = 1; i < chars.length; ++i) {
200                     chars[i] = Character.toLowerCase( chars[i] );
201                 }
202                 return String.valueOf( chars );
203             }
204         }
205         return value;
206     }
207
208
209     /**
210      * Convenience method returns the form in which this form element is contained.
211      * This method is exposed for form elements through the DOM API, but other
212      * elements have no access to it through the API.
213      */

214     public HTMLFormElement getForm() {
215         Node parent;
216
217         parent = getParentNode();
218         while (parent != null) {
219             if (parent instanceof HTMLFormElement) {
220                 return (HTMLFormElement)parent;
221             }
222             parent = parent.getParentNode();
223         }
224         return null;
225     }
226
227
228     protected Node castNewChild( Node newChild ) throws DOMException {
229         // Same method appears in HTMLElementImpl and HTMLDocumentImpl.
230

231         if (newChild == null) {
232             throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR, "Child reference is null." );
233         }
234         if (!(newChild instanceof NodeImpl)) {
235             throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR,
236                     "Child is not a compatible type for this node." );
237         }
238
239         // newChild must be HTMLElement, Text, Comment, DocumentFragment or
240
// ProcessingInstruction. CDATASection and EntityReference not supported
241
// in HTML documents.
242
if (!(newChild instanceof ElementImpl || newChild instanceof Comment || newChild instanceof Text
243                 || newChild instanceof DocumentFragment || newChild instanceof ProcessingInstruction)) {
244             throw new DOMExceptionImpl( DOMException.HIERARCHY_REQUEST_ERR,
245                     "Child is not a compatible type for this node." );
246         }
247         return (NodeImpl)newChild;
248     }
249
250
251     /**
252      * Constructor required owner document and element tag name. Will be called
253      * by the constructor of specific element types but with a known tag name.
254      * Assures that the owner document is an HTML element.
255      *
256      * @param owner The owner HTML document
257      * @param tagName The element's tag name
258      */

259     HTMLElementImpl( HTMLDocumentImpl owner, String JavaDoc tagName ) {
260         super( owner, tagName.toUpperCase() );
261     }
262
263
264 }
265
Popular Tags