001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999,2000 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057: package org.apache.html.dom;
058:
059: import org.w3c.dom.*;
060: import org.w3c.dom.html.*;
061: import org.apache.xerces.dom.ElementImpl;
062:
063: /**
064: * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
065: * will only appear inside HTML documents. This element extends {@link
066: * org.apache.xerces.dom.ElementImpl} by adding methods for directly
067: * manipulating HTML-specific attributes. All HTML elements gain access to
068: * the <code>id</code>, <code>title</code>, <code>lang</code>,
069: * <code>dir</code> and <code>class</code> attributes. Other elements
070: * add their own specific attributes.
071: *
072: *
073: * @version $Revision: 1.4 $ $Date: 2000/12/21 00:33:40 $
074: * @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
075: * @see org.w3c.dom.html.HTMLElement
076: */
077: public class HTMLElementImpl extends ElementImpl implements HTMLElement {
078:
079: /**
080: * Constructor required owner document and element tag name. Will be called
081: * by the constructor of specific element types but with a known tag name.
082: * Assures that the owner document is an HTML element.
083: *
084: * @param owner The owner HTML document
085: * @param tagName The element's tag name
086: */
087: HTMLElementImpl(HTMLDocumentImpl owner, String tagName) {
088: super (owner, tagName.toUpperCase());
089: }
090:
091: public String getId() {
092: return getAttribute("id");
093: }
094:
095: public void setId(String id) {
096: setAttribute("id", id);
097: }
098:
099: public String getTitle() {
100: return getAttribute("title");
101: }
102:
103: public void setTitle(String title) {
104: setAttribute("title", title);
105: }
106:
107: public String getLang() {
108: return getAttribute("lang");
109: }
110:
111: public void setLang(String lang) {
112: setAttribute("lang", lang);
113: }
114:
115: public String getDir() {
116: return getAttribute("dir");
117: }
118:
119: public void setDir(String dir) {
120: setAttribute("dir", dir);
121: }
122:
123: public String getClassName() {
124: return getAttribute("class");
125: }
126:
127: public void setClassName(String className) {
128: setAttribute("class", className);
129: }
130:
131: /**
132: * Convenience method used to translate an attribute value into an integer
133: * value. Returns the integer value or zero if the attribute is not a
134: * valid numeric string.
135: *
136: * @param value The value of the attribute
137: * @return The integer value, or zero if not a valid numeric string
138: */
139: int getInteger(String value) {
140: try {
141: return Integer.parseInt(value);
142: } catch (NumberFormatException except) {
143: return 0;
144: }
145: }
146:
147: /**
148: * Convenience method used to translate an attribute value into a boolean
149: * value. If the attribute has an associated value (even an empty string),
150: * it is set and true is returned. If the attribute does not exist, false
151: * is returend.
152: *
153: * @param value The value of the attribute
154: * @return True or false depending on whether the attribute has been set
155: */
156: boolean getBinary(String name) {
157: return (getAttributeNode(name) != null);
158: }
159:
160: /**
161: * Convenience method used to set a boolean attribute. If the value is true,
162: * the attribute is set to an empty string. If the value is false, the attribute
163: * is removed. HTML 4.0 understands empty strings as set attributes.
164: *
165: * @param name The name of the attribute
166: * @param value The value of the attribute
167: */
168: void setAttribute(String name, boolean value) {
169: if (value)
170: setAttribute(name, name);
171: else
172: removeAttribute(name);
173: }
174:
175: public Attr getAttributeNode(String attrName) {
176: return super .getAttributeNode(attrName.toLowerCase());
177: }
178:
179: public Attr getAttributeNodeNS(String namespaceURI, String localName) {
180: if (namespaceURI != null && namespaceURI.length() > 0)
181: return super .getAttributeNodeNS(namespaceURI, localName);
182: else
183: return super .getAttributeNode(localName.toLowerCase());
184: }
185:
186: public String getAttribute(String attrName) {
187: return super .getAttribute(attrName.toLowerCase());
188: }
189:
190: public String getAttributeNS(String namespaceURI, String localName) {
191: if (namespaceURI != null && namespaceURI.length() > 0)
192: return super .getAttributeNS(namespaceURI, localName);
193: else
194: return super .getAttribute(localName.toLowerCase());
195: }
196:
197: public final NodeList getElementsByTagName(String tagName) {
198: return super .getElementsByTagName(tagName.toUpperCase());
199: }
200:
201: public final NodeList getElementsByTagNameNS(String namespaceURI,
202: String localName) {
203: if (namespaceURI != null && namespaceURI.length() > 0)
204: return super .getElementsByTagNameNS(namespaceURI, localName
205: .toUpperCase());
206: else
207: return super .getElementsByTagName(localName.toUpperCase());
208: }
209:
210: /**
211: * Convenience method used to capitalize a one-off attribute value before it
212: * is returned. For example, the align values "LEFT" and "left" will both
213: * return as "Left".
214: *
215: * @param value The value of the attribute
216: * @return The capitalized value
217: */
218: String capitalize(String value) {
219: char[] chars;
220: int i;
221:
222: // Convert string to charactares. Convert the first one to upper case,
223: // the other characters to lower case, and return the converted string.
224: chars = value.toCharArray();
225: if (chars.length > 0) {
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: * Convenience method used to capitalize a one-off attribute value before it
236: * is returned. For example, the align values "LEFT" and "left" will both
237: * return as "Left".
238: *
239: * @param name The name of the attribute
240: * @return The capitalized value
241: */
242: String getCapitalized(String name) {
243: String value;
244: char[] chars;
245: int i;
246:
247: value = getAttribute(name);
248: if (value != null) {
249: // Convert string to charactares. Convert the first one to upper case,
250: // the other characters to lower case, and return the converted string.
251: chars = value.toCharArray();
252: if (chars.length > 0) {
253: chars[0] = Character.toUpperCase(chars[0]);
254: for (i = 1; i < chars.length; ++i)
255: chars[i] = Character.toLowerCase(chars[i]);
256: return String.valueOf(chars);
257: }
258: }
259: return value;
260: }
261:
262: /**
263: * Convenience method returns the form in which this form element is contained.
264: * This method is exposed for form elements through the DOM API, but other
265: * elements have no access to it through the API.
266: */
267: public HTMLFormElement getForm() {
268: Node parent;
269:
270: parent = getParentNode();
271: while (parent != null) {
272: if (parent instanceof HTMLFormElement)
273: return (HTMLFormElement) parent;
274: parent = parent.getParentNode();
275: }
276: return null;
277: }
278:
279: }
|