DomNsNode.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* DomNsNode.java --
  2. Copyright (C) 1999,2000,2001,2004 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;
  32. import javax.xml.XMLConstants;
  33. import org.w3c.dom.DOMException;
  34. /**
  35. * <p> Abstract implemention of namespace support. This facilitates
  36. * sharing code for attribute and element nodes.
  37. *
  38. * @author David Brownell
  39. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  40. */
  41. public abstract class DomNsNode
  42. extends DomNode
  43. {
  44. private String name;
  45. private String namespace;
  46. private String prefix;
  47. private String localName;
  48. /**
  49. * Constructs a node associated with the specified document, and
  50. * with the specified namespace information.
  51. *
  52. * @param owner The document with which this entity is associated
  53. * @param namespaceURI Combined with the local part of the name,
  54. * this identifies a type of element or attribute; may be null.
  55. * If this is the empty string, it is reassigned as null so that
  56. * applications only need to test that case.
  57. * @param name Name of this node, which may include a prefix
  58. */
  59. // package private
  60. DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name)
  61. {
  62. super(nodeType, owner);
  63. setNodeName(name);
  64. setNamespaceURI(namespaceURI);
  65. }
  66. /**
  67. * Constructs a node associated with the specified document, and
  68. * with the specified namespace information. The prefix and local part
  69. * are given explicitly rather than being computed. This allows them
  70. * to be explicitly set to {@code null} as required by
  71. * {@link Document#createElement(String)}.
  72. *
  73. * @param owner The document with which this entity is associated
  74. * @param namespaceURI Combined with the local part of the name,
  75. * this identifies a type of element or attribute; may be null.
  76. * If this is the empty string, it is reassigned as null so that
  77. * applications only need to test that case.
  78. * @param name Name of this node, which may include a prefix
  79. * @param prefix the namespace prefix of the name. May be {@code null}.
  80. * @param localName the local part of the name. May be {@code null}.
  81. */
  82. // package private
  83. DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name,
  84. String prefix, String localName)
  85. {
  86. super(nodeType, owner);
  87. this.name = name.intern();
  88. this.prefix = prefix == null ? null : prefix.intern();
  89. this.localName = localName == null ? null : localName.intern();
  90. setNamespaceURI(namespaceURI);
  91. }
  92. /**
  93. * <b>DOM L1</b>
  94. * Returns the node's name, including any namespace prefix.
  95. */
  96. public final String getNodeName()
  97. {
  98. return name;
  99. }
  100. final void setNodeName(String name)
  101. {
  102. this.name = name.intern();
  103. int index = name.indexOf(':');
  104. if (index == -1)
  105. {
  106. prefix = null;
  107. localName = this.name;
  108. }
  109. else
  110. {
  111. prefix = name.substring(0, index).intern();
  112. localName = name.substring(index + 1).intern();
  113. }
  114. }
  115. /**
  116. * <b>DOM L2</b>
  117. * Returns the node's namespace URI
  118. * <em>or null</em> if the node name is not namespace scoped.
  119. */
  120. public final String getNamespaceURI()
  121. {
  122. return namespace;
  123. }
  124. final void setNamespaceURI(String namespaceURI)
  125. {
  126. if ("".equals(namespaceURI))
  127. {
  128. namespaceURI = null;
  129. }
  130. namespace = (namespaceURI == null) ? null : namespaceURI.intern();
  131. }
  132. /**
  133. * <b>DOM L2</b>
  134. * Returns any prefix part of the node's name (before any colon).
  135. */
  136. public final String getPrefix()
  137. {
  138. return prefix;
  139. }
  140. /**
  141. * <b>DOM L2</b>
  142. * Assigns the prefix part of the node's name (before any colon).
  143. */
  144. public final void setPrefix(String prefix)
  145. {
  146. if (readonly)
  147. {
  148. throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
  149. }
  150. if (prefix == null)
  151. {
  152. name = localName;
  153. return;
  154. }
  155. else if (namespace == null)
  156. {
  157. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  158. "can't set prefix, node has no namespace URI",
  159. this, 0);
  160. }
  161. DomDocument.checkName(prefix, "1.1".equals(owner.getXmlVersion()));
  162. if (prefix.indexOf (':') != -1)
  163. {
  164. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  165. "illegal prefix " + prefix, this, 0);
  166. }
  167. if (XMLConstants.XML_NS_PREFIX.equals(prefix)
  168. && !XMLConstants.XML_NS_URI.equals(namespace))
  169. {
  170. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  171. "xml namespace is always " +
  172. XMLConstants.XML_NS_URI, this, 0);
  173. }
  174. if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
  175. {
  176. if (namespace != null || getNodeType() != ATTRIBUTE_NODE)
  177. {
  178. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  179. "xmlns attribute prefix is reserved",
  180. this, 0);
  181. }
  182. }
  183. else if (getNodeType () == ATTRIBUTE_NODE
  184. && (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
  185. name.startsWith("xmlns:")))
  186. {
  187. throw new DomDOMException(DOMException.NAMESPACE_ERR,
  188. "namespace declarations can't change names",
  189. this, 0);
  190. }
  191. this.prefix = prefix.intern();
  192. }
  193. /**
  194. * <b>DOM L2</b>
  195. * Returns the local part of the node's name (after any colon).
  196. */
  197. public final String getLocalName()
  198. {
  199. return localName;
  200. }
  201. }