1. /*
  2. * $Id: SAXParserImpl.java,v 1.8 2001/11/06 23:48:41 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 javax.xml.parsers.SAXParser;
  60. import javax.xml.parsers.SAXParserFactory;
  61. import javax.xml.parsers.ParserConfigurationException;
  62. import org.xml.sax.SAXException;
  63. import org.xml.sax.SAXParseException;
  64. import org.xml.sax.HandlerBase;
  65. import org.xml.sax.Parser;
  66. import org.xml.sax.XMLReader;
  67. import org.xml.sax.helpers.XMLReaderAdapter;
  68. import org.xml.sax.helpers.DefaultHandler;
  69. import org.xml.sax.SAXNotRecognizedException;
  70. import org.xml.sax.SAXNotSupportedException;
  71. import java.util.*;
  72. /**
  73. * @author Rajiv Mordani
  74. * @author Edwin Goei
  75. * @version $Revision: 1.8 $
  76. */
  77. /**
  78. * This is the implementation specific class for the
  79. * <code>javax.xml.parsers.SAXParser</code>.
  80. */
  81. public class SAXParserImpl extends SAXParser {
  82. private XMLReader xmlReader;
  83. private Parser parser = null;
  84. private boolean validating = false;
  85. private boolean namespaceAware = false;
  86. /**
  87. * Create a SAX parser with the associated features
  88. * @param features Hashtable of SAX features, may be null
  89. */
  90. SAXParserImpl(SAXParserFactory spf, Hashtable features)
  91. throws SAXException
  92. {
  93. // Instantiate an XMLReader directly and not through SAX so that we
  94. // use the right ClassLoader
  95. xmlReader = new org.apache.crimson.parser.XMLReaderImpl();
  96. // Validation
  97. validating = spf.isValidating();
  98. String validation = "http://xml.org/sax/features/validation";
  99. // If validating, provide a default ErrorHandler that prints
  100. // validation errors with a warning telling the user to set an
  101. // ErrorHandler. Note: this does not handle all cases.
  102. if (validating) {
  103. xmlReader.setErrorHandler(new DefaultValidationErrorHandler());
  104. }
  105. // Allow SAX parser to use a different ErrorHandler if it wants to
  106. xmlReader.setFeature(validation, validating);
  107. // "namespaceAware" == SAX Namespaces feature
  108. // Note: there is a compatibility problem here with default values:
  109. // JAXP default is false while SAX 2 default is true!
  110. namespaceAware = spf.isNamespaceAware();
  111. String namespaces = "http://xml.org/sax/features/namespaces";
  112. xmlReader.setFeature(namespaces, namespaceAware);
  113. // SAX "namespaces" and "namespace-prefixes" features must not both
  114. // be false as specified by SAX
  115. if (namespaceAware == false) {
  116. String prefixes = "http://xml.org/sax/features/namespace-prefixes";
  117. xmlReader.setFeature(prefixes, true);
  118. }
  119. setFeatures(features);
  120. }
  121. /**
  122. * Set any features of our XMLReader based on any features set on the
  123. * SAXParserFactory.
  124. *
  125. * XXX Does not handle possible conflicts between SAX feature names and
  126. * JAXP specific feature names, eg. SAXParserFactory.isValidating()
  127. */
  128. private void setFeatures(Hashtable features)
  129. throws SAXNotSupportedException, SAXNotRecognizedException
  130. {
  131. if (features != null) {
  132. for (Enumeration e = features.keys(); e.hasMoreElements();) {
  133. String feature = (String)e.nextElement();
  134. boolean value = ((Boolean)features.get(feature)).booleanValue();
  135. xmlReader.setFeature(feature, value);
  136. }
  137. }
  138. }
  139. public Parser getParser() throws SAXException {
  140. if (parser == null) {
  141. // Adapt a SAX2 XMLReader into a SAX1 Parser
  142. parser = new XMLReaderAdapter(xmlReader);
  143. // Set a DocumentHandler that does nothing to avoid getting
  144. // exceptions if no DocumentHandler is set by the app
  145. parser.setDocumentHandler(new HandlerBase());
  146. }
  147. return parser;
  148. }
  149. /**
  150. * Returns the XMLReader that is encapsulated by the implementation of
  151. * this class.
  152. */
  153. public XMLReader getXMLReader() {
  154. return xmlReader;
  155. }
  156. public boolean isNamespaceAware() {
  157. return namespaceAware;
  158. }
  159. public boolean isValidating() {
  160. return validating;
  161. }
  162. /**
  163. * Sets the particular property in the underlying implementation of
  164. * org.xml.sax.XMLReader.
  165. */
  166. public void setProperty(String name, Object value)
  167. throws SAXNotRecognizedException, SAXNotSupportedException
  168. {
  169. xmlReader.setProperty(name, value);
  170. }
  171. /**
  172. * returns the particular property requested for in the underlying
  173. * implementation of org.xml.sax.XMLReader.
  174. */
  175. public Object getProperty(String name)
  176. throws SAXNotRecognizedException, SAXNotSupportedException
  177. {
  178. return xmlReader.getProperty(name);
  179. }
  180. }