001: /**
002: * org/ozone-db/xml/dom/html/HTMLElementImpl.java
003: *
004: * The contents of this file are subject to the OpenXML Public
005: * License Version 1.0; you may not use this file except in compliance
006: * with the License. You may obtain a copy of the License at
007: * http://www.openxml.org/license.html
008: *
009: * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
010: * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
011: * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
012: * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
013: * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
014: * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
015: *
016: * The Initial Developer of this code under the License is Assaf Arkin.
017: * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
018: * All Rights Reserved.
019: */package org.ozoneDB.xml.dom.html;
020:
021: import org.ozoneDB.xml.dom.*;
022: import org.w3c.dom.*;
023: import org.w3c.dom.html.*;
024:
025: /**
026: * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
027: * will only appear inside HTML documents. This element extends {@link
028: * org.openxml.dom.ElementImpl} by adding methods for directly manipulating
029: * HTML-specific attributes. All HTML elements gain access to the
030: * <code>id</code>, <code>title</code>, <code>lang</code>, <code>dir</code>
031: * and <code>class</code> attributes. Other elements add their own specific
032: * attributes.
033: * <P>
034: * Note that some support is provided by {@link org.openxml.dom.NodeImpl}
035: * directly: translating all tag names to upper case and all attribute names
036: * to lower case.
037: *
038: *
039: * @version $Revision: 1.1 $ $Date: 2001/12/18 11:03:24 $
040: * @author <a href="mailto:arkin@trendline.co.il">Assaf Arkin</a>
041: * @see org.w3c.dom.html.HTMLElement
042: * @see org.openxml.dom.ElementImpl
043: */
044: public class HTMLElementImpl extends ElementImpl implements HTMLElement {
045:
046: public String getId() {
047: return getAttribute("id");
048: }
049:
050: public void setId(String id) {
051: setAttribute("id", id);
052: }
053:
054: public String getTitle() {
055: return getAttribute("title");
056: }
057:
058: public void setTitle(String title) {
059: setAttribute("title", title);
060: }
061:
062: public String getLang() {
063: return getAttribute("lang");
064: }
065:
066: public void setLang(String lang) {
067: setAttribute("lang", lang);
068: }
069:
070: public String getDir() {
071: return getAttribute("dir");
072: }
073:
074: public void setDir(String dir) {
075: setAttribute("dir", dir);
076: }
077:
078: public String getClassName() {
079: return getAttribute("class");
080: }
081:
082: public void setClassName(String className) {
083: setAttribute("class", className);
084: }
085:
086: /**
087: * Convenience method used to translate an attribute value into an integer
088: * value. Returns the integer value or zero if the attribute is not a
089: * valid numeric string.
090: *
091: * @param value The value of the attribute
092: * @return The integer value, or zero if not a valid numeric string
093: */
094: int toInteger(String value) {
095: try {
096: return Integer.parseInt(value);
097: } catch (NumberFormatException except) {
098: return 0;
099: }
100: }
101:
102: /**
103: * Convenience method used to translate an attribute value into a boolean
104: * value. If the attribute has an associated value (even an empty string),
105: * it is set and true is returned. If the attribute does not exist, false
106: * is returend.
107: *
108: * @param value The value of the attribute
109: * @return True or false depending on whether the attribute has been set
110: */
111: boolean toBoolean(String name) {
112: return getAttribute(name) != null;
113: }
114:
115: /**
116: * Convenience method used to set a boolean attribute. If the value is true,
117: * the attribute is set to an empty string. If the value is false, the attribute
118: * is removed. HTML 4.0 understands empty strings as set attributes.
119: *
120: * @param name The name of the attribute
121: * @param value The value of the attribute
122: */
123: void setAttribute(String name, boolean value) {
124: if (value) {
125: setAttribute(name, "");
126: } else {
127: removeAttribute(name);
128: }
129: }
130:
131: /**
132: * Convenience method used to capitalize a one-off attribute value before it
133: * is returned. For example, the align values "LEFT" and "left" will both
134: * return as "Left".
135: *
136: * @param value The value of the attribute
137: * @return The capitalized value
138: */
139: String capitalize(String value) {
140: char[] chars;
141: int i;
142:
143: // Convert string to charactares. Convert the first one to upper case,
144: // the other characters to lower case, and return the converted string.
145: chars = value.toCharArray();
146: if (chars.length > 0) {
147: chars[0] = Character.toUpperCase(chars[0]);
148: for (i = 1; i < chars.length; ++i) {
149: chars[i] = Character.toLowerCase(chars[i]);
150: }
151: return String.valueOf(chars);
152: }
153: return value;
154: }
155:
156: /**
157: * Convenience method used to capitalize a one-off attribute value before it
158: * is returned. For example, the align values "LEFT" and "left" will both
159: * return as "Left".
160: *
161: * @param name The name of the attribute
162: * @return The capitalized value
163: */
164: String getCapitalized(String name) {
165: String value;
166: char[] chars;
167: int i;
168:
169: value = getAttribute(name);
170: if (value != null) {
171: // Convert string to charactares. Convert the first one to upper case,
172: // the other characters to lower case, and return the converted string.
173: chars = value.toCharArray();
174: if (chars.length > 0) {
175: chars[0] = Character.toUpperCase(chars[0]);
176: for (i = 1; i < chars.length; ++i) {
177: chars[i] = Character.toLowerCase(chars[i]);
178: }
179: return String.valueOf(chars);
180: }
181: }
182: return value;
183: }
184:
185: /**
186: * Convenience method returns the form in which this form element is contained.
187: * This method is exposed for form elements through the DOM API, but other
188: * elements have no access to it through the API.
189: */
190: public HTMLFormElement getForm() {
191: Node parent;
192:
193: parent = getParentNode();
194: while (parent != null) {
195: if (parent instanceof HTMLFormElement) {
196: return (HTMLFormElement) parent;
197: }
198: parent = parent.getParentNode();
199: }
200: return null;
201: }
202:
203: protected Node castNewChild(Node newChild) throws DOMException {
204: // Same method appears in HTMLElementImpl and HTMLDocumentImpl.
205:
206: if (newChild == null) {
207: throw new DOMExceptionImpl(
208: DOMException.HIERARCHY_REQUEST_ERR,
209: "Child reference is null.");
210: }
211: if (!(newChild instanceof NodeImpl)) {
212: throw new DOMExceptionImpl(
213: DOMException.HIERARCHY_REQUEST_ERR,
214: "Child is not a compatible type for this node.");
215: }
216:
217: // newChild must be HTMLElement, Text, Comment, DocumentFragment or
218: // ProcessingInstruction. CDATASection and EntityReference not supported
219: // in HTML documents.
220: if (!(newChild instanceof ElementImpl
221: || newChild instanceof Comment
222: || newChild instanceof Text
223: || newChild instanceof DocumentFragment || newChild instanceof ProcessingInstruction)) {
224: throw new DOMExceptionImpl(
225: DOMException.HIERARCHY_REQUEST_ERR,
226: "Child is not a compatible type for this node.");
227: }
228: return (NodeImpl) newChild;
229: }
230:
231: /**
232: * Constructor required owner document and element tag name. Will be called
233: * by the constructor of specific element types but with a known tag name.
234: * Assures that the owner document is an HTML element.
235: *
236: * @param owner The owner HTML document
237: * @param tagName The element's tag name
238: */
239: HTMLElementImpl(HTMLDocumentImpl owner, String tagName) {
240: super(owner, tagName.toUpperCase());
241: }
242:
243: }
|