001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.html.dom;
018:
019: import java.util.Locale;
020:
021: import org.apache.xerces.dom.ElementImpl;
022: import org.w3c.dom.Attr;
023: import org.w3c.dom.Node;
024: import org.w3c.dom.NodeList;
025: import org.w3c.dom.html.HTMLElement;
026: import org.w3c.dom.html.HTMLFormElement;
027:
028: /**
029: * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
030: * will only appear inside HTML documents. This element extends {@link
031: * org.apache.xerces.dom.ElementImpl} by adding methods for directly
032: * manipulating HTML-specific attributes. All HTML elements gain access to
033: * the <code>id</code>, <code>title</code>, <code>lang</code>,
034: * <code>dir</code> and <code>class</code> attributes. Other elements
035: * add their own specific attributes.
036: *
037: * @xerces.internal
038: *
039: * @version $Revision: 449313 $ $Date: 2006-09-23 18:01:43 -0400 (Sat, 23 Sep 2006) $
040: * @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
041: * @see org.w3c.dom.html.HTMLElement
042: */
043: public class HTMLElementImpl extends ElementImpl implements HTMLElement {
044:
045: private static final long serialVersionUID = 5283925246324423495L;
046:
047: /**
048: * Constructor required owner document and element tag name. Will be called
049: * by the constructor of specific element types but with a known tag name.
050: * Assures that the owner document is an HTML element.
051: *
052: * @param owner The owner HTML document
053: * @param tagName The element's tag name
054: */
055: public HTMLElementImpl(HTMLDocumentImpl owner, String tagName) {
056: super (owner, tagName.toUpperCase(Locale.ENGLISH));
057: }
058:
059: public String getId() {
060: return getAttribute("id");
061: }
062:
063: public void setId(String id) {
064: setAttribute("id", id);
065: }
066:
067: public String getTitle() {
068: return getAttribute("title");
069: }
070:
071: public void setTitle(String title) {
072: setAttribute("title", title);
073: }
074:
075: public String getLang() {
076: return getAttribute("lang");
077: }
078:
079: public void setLang(String lang) {
080: setAttribute("lang", lang);
081: }
082:
083: public String getDir() {
084: return getAttribute("dir");
085: }
086:
087: public void setDir(String dir) {
088: setAttribute("dir", dir);
089: }
090:
091: public String getClassName() {
092: return getAttribute("class");
093: }
094:
095: public void setClassName(String className) {
096: setAttribute("class", className);
097: }
098:
099: /**
100: * Convenience method used to translate an attribute value into an integer
101: * value. Returns the integer value or zero if the attribute is not a
102: * valid numeric string.
103: *
104: * @param value The value of the attribute
105: * @return The integer value, or zero if not a valid numeric string
106: */
107: int getInteger(String value) {
108: try {
109: return Integer.parseInt(value);
110: } catch (NumberFormatException except) {
111: return 0;
112: }
113: }
114:
115: /**
116: * Convenience method used to translate an attribute value into a boolean
117: * value. If the attribute has an associated value (even an empty string),
118: * it is set and true is returned. If the attribute does not exist, false
119: * is returend.
120: *
121: * @param value The value of the attribute
122: * @return True or false depending on whether the attribute has been set
123: */
124: boolean getBinary(String name) {
125: return (getAttributeNode(name) != null);
126: }
127:
128: /**
129: * Convenience method used to set a boolean attribute. If the value is true,
130: * the attribute is set to an empty string. If the value is false, the attribute
131: * is removed. HTML 4.0 understands empty strings as set attributes.
132: *
133: * @param name The name of the attribute
134: * @param value The value of the attribute
135: */
136: void setAttribute(String name, boolean value) {
137: if (value) {
138: setAttribute(name, name);
139: } else {
140: removeAttribute(name);
141: }
142: }
143:
144: public Attr getAttributeNode(String attrName) {
145: return super .getAttributeNode(attrName
146: .toLowerCase(Locale.ENGLISH));
147: }
148:
149: public Attr getAttributeNodeNS(String namespaceURI, String localName) {
150: if (namespaceURI != null && namespaceURI.length() > 0) {
151: return super .getAttributeNodeNS(namespaceURI, localName);
152: }
153: return super .getAttributeNode(localName
154: .toLowerCase(Locale.ENGLISH));
155: }
156:
157: public String getAttribute(String attrName) {
158: return super .getAttribute(attrName.toLowerCase(Locale.ENGLISH));
159: }
160:
161: public String getAttributeNS(String namespaceURI, String localName) {
162: if (namespaceURI != null && namespaceURI.length() > 0) {
163: return super .getAttributeNS(namespaceURI, localName);
164: }
165: return super
166: .getAttribute(localName.toLowerCase(Locale.ENGLISH));
167: }
168:
169: public final NodeList getElementsByTagName(String tagName) {
170: return super .getElementsByTagName(tagName
171: .toUpperCase(Locale.ENGLISH));
172: }
173:
174: public final NodeList getElementsByTagNameNS(String namespaceURI,
175: String localName) {
176: if (namespaceURI != null && namespaceURI.length() > 0) {
177: return super .getElementsByTagNameNS(namespaceURI, localName
178: .toUpperCase(Locale.ENGLISH));
179: }
180: return super .getElementsByTagName(localName
181: .toUpperCase(Locale.ENGLISH));
182: }
183:
184: /**
185: * Convenience method used to capitalize a one-off attribute value before it
186: * is returned. For example, the align values "LEFT" and "left" will both
187: * return as "Left".
188: *
189: * @param value The value of the attribute
190: * @return The capitalized value
191: */
192: String capitalize(String value) {
193:
194: char[] chars;
195: int i;
196:
197: // Convert string to charactares. Convert the first one to upper case,
198: // the other characters to lower case, and return the converted string.
199: chars = value.toCharArray();
200: if (chars.length > 0) {
201: chars[0] = Character.toUpperCase(chars[0]);
202: for (i = 1; i < chars.length; ++i) {
203: chars[i] = Character.toLowerCase(chars[i]);
204: }
205: return String.valueOf(chars);
206: }
207: return value;
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 name The name of the attribute
216: * @return The capitalized value
217: */
218: String getCapitalized(String name) {
219: String value;
220: char[] chars;
221: int i;
222:
223: value = getAttribute(name);
224: if (value != null) {
225: // Convert string to charactares. Convert the first one to upper case,
226: // the other characters to lower case, and return the converted string.
227: chars = value.toCharArray();
228: if (chars.length > 0) {
229: chars[0] = Character.toUpperCase(chars[0]);
230: for (i = 1; i < chars.length; ++i) {
231: chars[i] = Character.toLowerCase(chars[i]);
232: }
233: return String.valueOf(chars);
234: }
235: }
236: return value;
237: }
238:
239: /**
240: * Convenience method returns the form in which this form element is contained.
241: * This method is exposed for form elements through the DOM API, but other
242: * elements have no access to it through the API.
243: */
244: public HTMLFormElement getForm() {
245: Node parent = getParentNode();
246: while (parent != null) {
247: if (parent instanceof HTMLFormElement) {
248: return (HTMLFormElement) parent;
249: }
250: parent = parent.getParentNode();
251: }
252: return null;
253: }
254:
255: }
|