1. /*
  2. * $Id: XmlDocumentBuilderNS.java,v 1.1 2001/03/16 19:17:01 edwingo Exp $
  3. *
  4. * The Apache Software License, Version 1.1
  5. *
  6. *
  7. * Copyright (c) 2000 The Apache Software Foundation. All rights
  8. * reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The end-user documentation included with the redistribution,
  23. * if any, must include the following acknowledgment:
  24. * "This product includes software developed by the
  25. * Apache Software Foundation (http://www.apache.org/)."
  26. * Alternately, this acknowledgment may appear in the software itself,
  27. * if and wherever such third-party acknowledgments normally appear.
  28. *
  29. * 4. The names "Crimson" and "Apache Software Foundation" must
  30. * not be used to endorse or promote products derived from this
  31. * software without prior written permission. For written
  32. * permission, please contact apache@apache.org.
  33. *
  34. * 5. Products derived from this software may not be called "Apache",
  35. * nor may "Apache" appear in their name, without prior written
  36. * permission of the Apache Software Foundation.
  37. *
  38. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  39. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  40. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  45. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  46. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  48. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. * SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This software consists of voluntary contributions made by many
  53. * individuals on behalf of the Apache Software Foundation and was
  54. * originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
  55. * http://www.sun.com. For more information on the Apache Software
  56. * Foundation, please see <http://www.apache.org/>.
  57. */
  58. package org.apache.crimson.tree;
  59. import org.xml.sax.SAXException;
  60. import org.xml.sax.SAXParseException;
  61. import org.xml.sax.Attributes;
  62. import org.w3c.dom.DOMException;
  63. import org.apache.crimson.parser.AttributesEx;
  64. /**
  65. * This class implements a Namespace aware DOM tree builder which uses NS
  66. * versions of the DOM Level 2 create methods and assumes disableNamespaces
  67. * is false, ie. JAXP namespaceAware is true.
  68. */
  69. public class XmlDocumentBuilderNS extends XmlDocumentBuilder
  70. {
  71. /**
  72. * Receive notification of the beginning of an element.
  73. */
  74. public void startElement(String namespaceURI, String localName,
  75. String qName, Attributes attributes)
  76. throws SAXException
  77. {
  78. //
  79. // Convert set of attributes to DOM representation.
  80. //
  81. AttributeSet attSet = null;
  82. int length = attributes.getLength();
  83. if (length != 0) {
  84. try {
  85. attSet = AttributeSet.createAttributeSet2(attributes);
  86. } catch (DOMException ex) {
  87. throw new SAXParseException(getMessage("XDB-002",
  88. new Object[] { ex.getMessage() }), locator, ex);
  89. }
  90. }
  91. //
  92. // Then create the element, associate its attributes, and
  93. // stack it for later addition.
  94. //
  95. ElementNode2 e = null;
  96. try {
  97. // Translate a SAX empty string to mean no namespaceURI
  98. if ("".equals(namespaceURI)) {
  99. namespaceURI = null;
  100. }
  101. e = (ElementNode2)document.createElementNS(namespaceURI, qName);
  102. } catch (DOMException ex) {
  103. throw new SAXParseException(getMessage("XDB-004",
  104. new Object[] { ex.getMessage() }), locator, ex);
  105. }
  106. if (attributes instanceof AttributesEx) {
  107. e.setIdAttributeName(
  108. ((AttributesEx)attributes).getIdAttributeName());
  109. }
  110. if (length != 0) {
  111. e.setAttributes(attSet);
  112. }
  113. elementStack[topOfStack++].appendChild(e);
  114. elementStack[topOfStack] = e;
  115. //
  116. // Division of responsibility for namespace processing is (being
  117. // revised so) that the DOM builder reports errors when namespace
  118. // constraints are violated, and the parser is ignorant of them.
  119. //
  120. // XXX check duplicate attributes here ???
  121. }
  122. /**
  123. * Receive notification of a processing instruction.
  124. */
  125. public void processingInstruction(String name, String instruction)
  126. throws SAXException
  127. {
  128. if (name.indexOf (':') != -1) {
  129. throw new SAXParseException((getMessage ("XDB-010")), locator);
  130. }
  131. super.processingInstruction(name, instruction);
  132. }
  133. //////////////////////////////////////////////////////////////////////
  134. // org.xml.sax.ext.DeclHandler callbacks
  135. //////////////////////////////////////////////////////////////////////
  136. /**
  137. * Report an internal entity declaration.
  138. */
  139. public void internalEntityDecl(String name, String value)
  140. throws SAXException
  141. {
  142. if (name.indexOf (':') != -1) {
  143. throw new SAXParseException((getMessage("XDB-012")), locator);
  144. }
  145. super.internalEntityDecl(name, value);
  146. }
  147. /**
  148. * Report a parsed external entity declaration.
  149. */
  150. public void externalEntityDecl(String name, String publicId,
  151. String systemId)
  152. throws SAXException
  153. {
  154. if (name.indexOf (':') != -1) {
  155. throw new SAXParseException((getMessage("XDB-012")), locator);
  156. }
  157. super.externalEntityDecl(name, publicId, systemId);
  158. }
  159. //////////////////////////////////////////////////////////////////////
  160. // org.xml.sax.DTDHandler callbacks
  161. //////////////////////////////////////////////////////////////////////
  162. /**
  163. * Receive notification of a notation declaration event.
  164. */
  165. public void notationDecl(String n, String p, String s)
  166. throws SAXException
  167. {
  168. if (n.indexOf(':') != -1) {
  169. throw new SAXParseException((getMessage("XDB-013")), locator);
  170. }
  171. super.notationDecl(n, p, s);
  172. }
  173. /**
  174. * Receive notification of an unparsed entity declaration event.
  175. */
  176. public void unparsedEntityDecl(String name, String publicId,
  177. String systemId, String notation)
  178. throws SAXException
  179. {
  180. if (name.indexOf(':') != -1) {
  181. throw new SAXParseException((getMessage("XDB-012")), locator);
  182. }
  183. super.unparsedEntityDecl(name, publicId, systemId, notation);
  184. }
  185. }