1. /*
  2. * $Id: DocumentBuilderImpl.java,v 1.5 2001/03/16 19:17:02 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.jaxp;
  59. import java.io.IOException;
  60. import javax.xml.parsers.DocumentBuilder;
  61. import javax.xml.parsers.DocumentBuilderFactory;
  62. import javax.xml.parsers.ParserConfigurationException;
  63. import org.w3c.dom.Document;
  64. import org.w3c.dom.DOMImplementation;
  65. import org.apache.crimson.parser.XMLReaderImpl;
  66. import org.apache.crimson.tree.XmlDocument;
  67. import org.apache.crimson.tree.XmlDocumentBuilder;
  68. import org.apache.crimson.tree.XmlDocumentBuilderNS;
  69. import org.apache.crimson.tree.DOMImplementationImpl;
  70. import org.xml.sax.XMLReader;
  71. import org.xml.sax.InputSource;
  72. import org.xml.sax.SAXException;
  73. import org.xml.sax.SAXParseException;
  74. import org.xml.sax.EntityResolver;
  75. import org.xml.sax.ErrorHandler;
  76. import org.xml.sax.helpers.DefaultHandler;
  77. /**
  78. * @author Rajiv Mordani
  79. * @version $Revision: 1.5 $
  80. */
  81. public class DocumentBuilderImpl extends DocumentBuilder {
  82. private DocumentBuilderFactory dbf;
  83. private EntityResolver er = null;
  84. private ErrorHandler eh = null;
  85. private XMLReader xmlReader = null;
  86. private XmlDocumentBuilder builder = null;
  87. private boolean namespaceAware = false;
  88. private boolean validating = false;
  89. DocumentBuilderImpl(DocumentBuilderFactory dbf)
  90. throws ParserConfigurationException
  91. {
  92. this.dbf = dbf;
  93. namespaceAware = dbf.isNamespaceAware();
  94. xmlReader = new XMLReaderImpl();
  95. try {
  96. // Validation
  97. validating = dbf.isValidating();
  98. String validation = "http://xml.org/sax/features/validation";
  99. xmlReader.setFeature(validation, validating);
  100. // If validating, provide a default ErrorHandler that prints
  101. // validation errors with a warning telling the user to set an
  102. // ErrorHandler
  103. if (validating) {
  104. setErrorHandler(new DefaultValidationErrorHandler());
  105. }
  106. // SAX2 namespace-prefixes should be true for either builder
  107. String nsPrefixes =
  108. "http://xml.org/sax/features/namespace-prefixes";
  109. xmlReader.setFeature(nsPrefixes, true);
  110. // Set SAX2 namespaces feature appropriately
  111. String namespaces = "http://xml.org/sax/features/namespaces";
  112. xmlReader.setFeature(namespaces, namespaceAware);
  113. // Use the appropriate DOM builder based on "namespaceAware"
  114. if (namespaceAware) {
  115. builder = new XmlDocumentBuilderNS();
  116. } else {
  117. builder = new XmlDocumentBuilder();
  118. }
  119. // Use builder as the ContentHandler
  120. xmlReader.setContentHandler(builder);
  121. // org.xml.sax.ext.LexicalHandler
  122. String lexHandler = "http://xml.org/sax/properties/lexical-handler";
  123. xmlReader.setProperty(lexHandler, builder);
  124. // org.xml.sax.ext.DeclHandler
  125. String declHandler =
  126. "http://xml.org/sax/properties/declaration-handler";
  127. xmlReader.setProperty(declHandler, builder);
  128. // DTDHandler
  129. xmlReader.setDTDHandler(builder);
  130. } catch (SAXException e) {
  131. // Handles both SAXNotSupportedException, SAXNotRecognizedException
  132. throw new ParserConfigurationException(e.getMessage());
  133. }
  134. // Set various builder properties obtained from DocumentBuilderFactory
  135. builder.setIgnoreWhitespace(dbf.isIgnoringElementContentWhitespace());
  136. builder.setExpandEntityReferences(dbf.isExpandEntityReferences());
  137. builder.setIgnoreComments(dbf.isIgnoringComments());
  138. builder.setPutCDATAIntoText(dbf.isCoalescing());
  139. }
  140. public Document newDocument() {
  141. return new XmlDocument();
  142. }
  143. public DOMImplementation getDOMImplementation() {
  144. return DOMImplementationImpl.getDOMImplementation();
  145. }
  146. public Document parse(InputSource is) throws SAXException, IOException {
  147. if (is == null) {
  148. throw new IllegalArgumentException("InputSource cannot be null");
  149. }
  150. if (er != null) {
  151. xmlReader.setEntityResolver(er);
  152. }
  153. if (eh != null) {
  154. xmlReader.setErrorHandler(eh);
  155. }
  156. xmlReader.parse(is);
  157. return builder.getDocument();
  158. }
  159. public boolean isNamespaceAware() {
  160. return namespaceAware;
  161. }
  162. public boolean isValidating() {
  163. return validating;
  164. }
  165. public void setEntityResolver(org.xml.sax.EntityResolver er) {
  166. this.er = er;
  167. }
  168. public void setErrorHandler(org.xml.sax.ErrorHandler eh) {
  169. // If app passes in a ErrorHandler of null, then ignore all errors
  170. // and warnings
  171. this.eh = (eh == null) ? new DefaultHandler() : eh;
  172. }
  173. }