DomHTMLElement.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* DomHTMLElement.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.dom.html2;
  32. import gnu.xml.dom.DomDOMException;
  33. import gnu.xml.dom.DomElement;
  34. import gnu.xml.dom.DomEvent;
  35. import org.w3c.dom.DOMException;
  36. import org.w3c.dom.NamedNodeMap;
  37. import org.w3c.dom.Node;
  38. import org.w3c.dom.events.UIEvent;
  39. import org.w3c.dom.html2.HTMLElement;
  40. /**
  41. * Abstract implementation of an HTML element node.
  42. *
  43. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  44. */
  45. public abstract class DomHTMLElement
  46. extends DomElement
  47. implements HTMLElement
  48. {
  49. protected DomHTMLElement(DomHTMLDocument owner, String namespaceURI,
  50. String name)
  51. {
  52. super(owner, namespaceURI, name);
  53. }
  54. /**
  55. * Returns the value of the specified attribute.
  56. * The attribute name is case insensitive.
  57. */
  58. protected String getHTMLAttribute(String name)
  59. {
  60. if (hasAttributes())
  61. {
  62. NamedNodeMap attrs = getAttributes();
  63. int len = attrs.getLength();
  64. for (int i = 0; i < len; i++)
  65. {
  66. Node attr = attrs.item(i);
  67. String attrName = attr.getLocalName();
  68. if (attrName == null)
  69. {
  70. attrName = attr.getNodeName();
  71. }
  72. if (attrName.equalsIgnoreCase(name))
  73. {
  74. return attr.getNodeValue();
  75. }
  76. }
  77. }
  78. return "";
  79. }
  80. protected int getIntHTMLAttribute(String name)
  81. {
  82. String value = getHTMLAttribute(name);
  83. if (value == null)
  84. {
  85. return -1;
  86. }
  87. try
  88. {
  89. return Integer.parseInt(value);
  90. }
  91. catch (NumberFormatException e)
  92. {
  93. return -1;
  94. }
  95. }
  96. protected boolean getBooleanHTMLAttribute(String name)
  97. {
  98. String value = getHTMLAttribute(name);
  99. return value != null;
  100. }
  101. /**
  102. * Sets the value of the specified attribute.
  103. * The attribute name is case insensitive.
  104. */
  105. protected void setHTMLAttribute(String name, String value)
  106. {
  107. Node attr;
  108. NamedNodeMap attrs = getAttributes();
  109. int len = attrs.getLength();
  110. for (int i = 0; i < len; i++)
  111. {
  112. attr = attrs.item(i);
  113. String attrName = attr.getLocalName();
  114. if (attrName == null)
  115. {
  116. attrName = attr.getNodeName();
  117. }
  118. if (attrName.equalsIgnoreCase(name))
  119. {
  120. if (value != null)
  121. {
  122. attr.setNodeValue(value);
  123. }
  124. else
  125. {
  126. attrs.removeNamedItem(attr.getNodeName());
  127. }
  128. return;
  129. }
  130. }
  131. if (value != null)
  132. {
  133. // Create a new attribute
  134. DomHTMLDocument doc = (DomHTMLDocument) getOwnerDocument();
  135. // XXX namespace URI for attribute?
  136. attr = doc.createAttribute(name);
  137. attr.setNodeValue(value);
  138. }
  139. }
  140. protected void setIntHTMLAttribute(String name, int value)
  141. {
  142. setHTMLAttribute(name, Integer.toString(value));
  143. }
  144. protected void setBooleanHTMLAttribute(String name, boolean value)
  145. {
  146. setHTMLAttribute(name, value ? name : null);
  147. }
  148. /**
  149. * Returns the first parent element with the specified name.
  150. */
  151. protected Node getParentElement(String name)
  152. {
  153. for (Node parent = getParentNode(); parent != null;
  154. parent = parent.getParentNode())
  155. {
  156. String parentName = parent.getLocalName();
  157. if (parentName == null)
  158. {
  159. parentName = parent.getNodeName();
  160. }
  161. if (name.equalsIgnoreCase(parentName))
  162. {
  163. return parent;
  164. }
  165. }
  166. return null;
  167. }
  168. /**
  169. * Returns the first child element with the specified name.
  170. */
  171. protected Node getChildElement(String name)
  172. {
  173. for (Node child = getFirstChild(); child != null;
  174. child = child.getNextSibling())
  175. {
  176. String childName = child.getLocalName();
  177. if (childName == null)
  178. {
  179. childName = child.getLocalName();
  180. }
  181. if (name.equalsIgnoreCase(childName))
  182. {
  183. return child;
  184. }
  185. }
  186. return null;
  187. }
  188. /**
  189. * Returns the index of this element among elements of the same name,
  190. * relative to its parent.
  191. */
  192. protected int getIndex()
  193. {
  194. int index = 0;
  195. Node parent = getParentNode();
  196. if (parent != null)
  197. {
  198. for (Node ctx = parent.getFirstChild(); ctx != null;
  199. ctx = ctx.getNextSibling())
  200. {
  201. if (ctx == this)
  202. {
  203. return index;
  204. }
  205. index++;
  206. }
  207. }
  208. throw new DomDOMException(DOMException.NOT_FOUND_ERR);
  209. }
  210. protected void dispatchUIEvent(String name)
  211. {
  212. UIEvent event = new DomEvent.DomUIEvent(name);
  213. dispatchEvent(event);
  214. }
  215. public String getId()
  216. {
  217. return getHTMLAttribute("id");
  218. }
  219. public void setId(String id)
  220. {
  221. setHTMLAttribute("id", id);
  222. }
  223. public String getTitle()
  224. {
  225. return getHTMLAttribute("title");
  226. }
  227. public void setTitle(String title)
  228. {
  229. setHTMLAttribute("title", title);
  230. }
  231. public String getLang()
  232. {
  233. return getHTMLAttribute("lang");
  234. }
  235. public void setLang(String lang)
  236. {
  237. setHTMLAttribute("lang", lang);
  238. }
  239. public String getDir()
  240. {
  241. return getHTMLAttribute("dir");
  242. }
  243. public void setDir(String dir)
  244. {
  245. setHTMLAttribute("dir", dir);
  246. }
  247. public String getClassName()
  248. {
  249. return getHTMLAttribute("class");
  250. }
  251. public void setClassName(String className)
  252. {
  253. setHTMLAttribute("class", className);
  254. }
  255. }