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 com.sun.org.apache.html.internal.dom;
  58. import java.util.Locale;
  59. import com.sun.org.apache.xerces.internal.dom.ElementImpl;
  60. import org.w3c.dom.Attr;
  61. import org.w3c.dom.Node;
  62. import org.w3c.dom.NodeList;
  63. import org.w3c.dom.html.HTMLElement;
  64. import org.w3c.dom.html.HTMLFormElement;
  65. /**
  66. * Implements an HTML-specific element, an {@link org.w3c.dom.Element} that
  67. * will only appear inside HTML documents. This element extends {@link
  68. * com.sun.org.apache.xerces.internal.dom.ElementImpl} by adding methods for directly
  69. * manipulating HTML-specific attributes. All HTML elements gain access to
  70. * the <code>id</code>, <code>title</code>, <code>lang</code>,
  71. * <code>dir</code> and <code>class</code> attributes. Other elements
  72. * add their own specific attributes.
  73. *
  74. *
  75. * @version $Revision: 1.6 $ $Date: 2003/05/08 20:13:09 $
  76. * @author <a href="mailto:arkin@exoffice.com">Assaf Arkin</a>
  77. * @see org.w3c.dom.html.HTMLElement
  78. */
  79. public class HTMLElementImpl
  80. extends ElementImpl
  81. implements HTMLElement
  82. {
  83. /**
  84. * Constructor required owner document and element tag name. Will be called
  85. * by the constructor of specific element types but with a known tag name.
  86. * Assures that the owner document is an HTML element.
  87. *
  88. * @param owner The owner HTML document
  89. * @param tagName The element's tag name
  90. */
  91. HTMLElementImpl( HTMLDocumentImpl owner, String tagName )
  92. {
  93. super( owner, tagName.toUpperCase(Locale.ENGLISH) );
  94. }
  95. public String getId()
  96. {
  97. return getAttribute( "id" );
  98. }
  99. public void setId( String id )
  100. {
  101. setAttribute( "id", id );
  102. }
  103. public String getTitle()
  104. {
  105. return getAttribute( "title" );
  106. }
  107. public void setTitle( String title )
  108. {
  109. setAttribute( "title", title );
  110. }
  111. public String getLang()
  112. {
  113. return getAttribute( "lang" );
  114. }
  115. public void setLang( String lang )
  116. {
  117. setAttribute( "lang", lang );
  118. }
  119. public String getDir()
  120. {
  121. return getAttribute( "dir" );
  122. }
  123. public void setDir( String dir )
  124. {
  125. setAttribute( "dir", dir );
  126. }
  127. public String getClassName()
  128. {
  129. return getAttribute( "class" );
  130. }
  131. public void setClassName( String className )
  132. {
  133. setAttribute( "class", className );
  134. }
  135. /**
  136. * Convenience method used to translate an attribute value into an integer
  137. * value. Returns the integer value or zero if the attribute is not a
  138. * valid numeric string.
  139. *
  140. * @param value The value of the attribute
  141. * @return The integer value, or zero if not a valid numeric string
  142. */
  143. int getInteger( String value )
  144. {
  145. try
  146. {
  147. return Integer.parseInt( value );
  148. }
  149. catch ( NumberFormatException except )
  150. {
  151. return 0;
  152. }
  153. }
  154. /**
  155. * Convenience method used to translate an attribute value into a boolean
  156. * value. If the attribute has an associated value (even an empty string),
  157. * it is set and true is returned. If the attribute does not exist, false
  158. * is returend.
  159. *
  160. * @param value The value of the attribute
  161. * @return True or false depending on whether the attribute has been set
  162. */
  163. boolean getBinary( String name )
  164. {
  165. return ( getAttributeNode( name ) != null );
  166. }
  167. /**
  168. * Convenience method used to set a boolean attribute. If the value is true,
  169. * the attribute is set to an empty string. If the value is false, the attribute
  170. * is removed. HTML 4.0 understands empty strings as set attributes.
  171. *
  172. * @param name The name of the attribute
  173. * @param value The value of the attribute
  174. */
  175. void setAttribute( String name, boolean value )
  176. {
  177. if ( value )
  178. setAttribute( name, name );
  179. else
  180. removeAttribute( name );
  181. }
  182. public Attr getAttributeNode( String attrName )
  183. {
  184. return super.getAttributeNode( attrName.toLowerCase(Locale.ENGLISH) );
  185. }
  186. public Attr getAttributeNodeNS( String namespaceURI,
  187. String localName )
  188. {
  189. if ( namespaceURI != null && namespaceURI.length() > 0 )
  190. return super.getAttributeNodeNS( namespaceURI, localName );
  191. else
  192. return super.getAttributeNode( localName.toLowerCase(Locale.ENGLISH) );
  193. }
  194. public String getAttribute( String attrName )
  195. {
  196. return super.getAttribute( attrName.toLowerCase(Locale.ENGLISH) );
  197. }
  198. public String getAttributeNS( String namespaceURI,
  199. String localName )
  200. {
  201. if ( namespaceURI != null && namespaceURI.length() > 0 )
  202. return super.getAttributeNS( namespaceURI, localName );
  203. else
  204. return super.getAttribute( localName.toLowerCase(Locale.ENGLISH) );
  205. }
  206. public final NodeList getElementsByTagName( String tagName )
  207. {
  208. return super.getElementsByTagName( tagName.toUpperCase(Locale.ENGLISH) );
  209. }
  210. public final NodeList getElementsByTagNameNS( String namespaceURI,
  211. String localName )
  212. {
  213. if ( namespaceURI != null && namespaceURI.length() > 0 )
  214. return super.getElementsByTagNameNS( namespaceURI, localName.toUpperCase(Locale.ENGLISH) );
  215. else
  216. return super.getElementsByTagName( localName.toUpperCase(Locale.ENGLISH) );
  217. }
  218. /**
  219. * Convenience method used to capitalize a one-off attribute value before it
  220. * is returned. For example, the align values "LEFT" and "left" will both
  221. * return as "Left".
  222. *
  223. * @param value The value of the attribute
  224. * @return The capitalized value
  225. */
  226. String capitalize( String value )
  227. {
  228. char[] chars;
  229. int i;
  230. // Convert string to charactares. Convert the first one to upper case,
  231. // the other characters to lower case, and return the converted string.
  232. chars = value.toCharArray();
  233. if ( chars.length > 0 )
  234. {
  235. chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
  236. for ( i = 1 ; i < chars.length ; ++i )
  237. chars[ i ] = Character.toLowerCase( chars[ i ] );
  238. return String.valueOf( chars );
  239. }
  240. return value;
  241. }
  242. /**
  243. * Convenience method used to capitalize a one-off attribute value before it
  244. * is returned. For example, the align values "LEFT" and "left" will both
  245. * return as "Left".
  246. *
  247. * @param name The name of the attribute
  248. * @return The capitalized value
  249. */
  250. String getCapitalized( String name )
  251. {
  252. String value;
  253. char[] chars;
  254. int i;
  255. value = getAttribute( name );
  256. if ( value != null )
  257. {
  258. // Convert string to charactares. Convert the first one to upper case,
  259. // the other characters to lower case, and return the converted string.
  260. chars = value.toCharArray();
  261. if ( chars.length > 0 )
  262. {
  263. chars[ 0 ] = Character.toUpperCase( chars[ 0 ] );
  264. for ( i = 1 ; i < chars.length ; ++i )
  265. chars[ i ] = Character.toLowerCase( chars[ i ] );
  266. return String.valueOf( chars );
  267. }
  268. }
  269. return value;
  270. }
  271. /**
  272. * Convenience method returns the form in which this form element is contained.
  273. * This method is exposed for form elements through the DOM API, but other
  274. * elements have no access to it through the API.
  275. */
  276. public HTMLFormElement getForm()
  277. {
  278. Node parent;
  279. parent = getParentNode();
  280. while ( parent != null )
  281. {
  282. if ( parent instanceof HTMLFormElement )
  283. return (HTMLFormElement) parent;
  284. parent = parent.getParentNode();
  285. }
  286. return null;
  287. }
  288. }