1. /*
  2. *
  3. * The Apache Software License, Version 1.1
  4. *
  5. *
  6. * Copyright (c) 2000-2004 The Apache Software Foundation. All rights
  7. * reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. The end-user documentation included with the redistribution,
  22. * if any, must include the following acknowledgment:
  23. * "This product includes software developed by the
  24. * Apache Software Foundation (http://www.apache.org/)."
  25. * Alternately, this acknowledgment may appear in the software itself,
  26. * if and wherever such third-party acknowledgments normally appear.
  27. *
  28. * 4. The names "Xerces" and "Apache Software Foundation" must
  29. * not be used to endorse or promote products derived from this
  30. * software without prior written permission. For written
  31. * permission, please contact apache@apache.org.
  32. *
  33. * 5. Products derived from this software may not be called "Apache",
  34. * nor may "Apache" appear in their name, without prior written
  35. * permission of the Apache Software Foundation.
  36. *
  37. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  38. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  39. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  43. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  44. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  45. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  47. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48. * SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This software consists of voluntary contributions made by many
  52. * individuals on behalf of the Apache Software Foundation and was
  53. * originally based on software copyright (c) 1999, Sun Microsystems, Inc.,
  54. * http://www.sun.com. For more information on the Apache Software
  55. * Foundation, please see <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.jaxp;
  58. import java.io.IOException;
  59. import java.util.Enumeration;
  60. import java.util.Hashtable;
  61. import javax.xml.parsers.DocumentBuilder;
  62. import javax.xml.parsers.DocumentBuilderFactory;
  63. import javax.xml.validation.Schema;
  64. import com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl;
  65. import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
  66. import com.sun.org.apache.xerces.internal.impl.Constants;
  67. import com.sun.org.apache.xerces.internal.parsers.DOMParser;
  68. import com.sun.org.apache.xerces.internal.parsers.JAXPConfiguration;
  69. import com.sun.org.apache.xerces.internal.util.SecurityManager;
  70. import com.sun.org.apache.xerces.internal.xni.XNIException;
  71. import org.w3c.dom.DOMImplementation;
  72. import org.w3c.dom.Document;
  73. import org.xml.sax.EntityResolver;
  74. import org.xml.sax.ErrorHandler;
  75. import org.xml.sax.InputSource;
  76. import org.xml.sax.SAXException;
  77. import org.xml.sax.SAXNotRecognizedException;
  78. import org.xml.sax.SAXNotSupportedException;
  79. import org.xml.sax.helpers.DefaultHandler;
  80. /**
  81. * @author Rajiv Mordani
  82. * @author Edwin Goei
  83. * @version $Id: DocumentBuilderImpl.java,v 1.24 2004/02/24 23:15:58 mrglavas Exp $
  84. */
  85. public class DocumentBuilderImpl extends DocumentBuilder
  86. implements JAXPConstants
  87. {
  88. private EntityResolver er = null;
  89. private ErrorHandler eh = null;
  90. private final DOMParser domParser;
  91. private boolean enableSP = true;
  92. private final Schema grammar;
  93. /**
  94. * null if the secure processing is disabled.
  95. * otherwise a valid {@link SecureProcessing} object.
  96. */
  97. private final SecurityManager secureProcessing ;
  98. private final boolean xincludeAware;
  99. protected DocumentBuilderImpl(DocumentBuilderFactory dbf, Hashtable dbfAttrs)
  100. throws SAXNotRecognizedException, SAXNotSupportedException
  101. {
  102. grammar = dbf.getSchema();
  103. secureProcessing = new SecurityManager();
  104. this.domParser = new DOMParser(new JAXPConfiguration(grammar));
  105. this.xincludeAware = dbf.isXIncludeAware();
  106. domParser.setFeature(
  107. Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE,
  108. xincludeAware);
  109. // If validating, provide a default ErrorHandler that prints
  110. // validation errors with a warning telling the user to set an
  111. // ErrorHandler
  112. if (dbf.isValidating()) {
  113. setErrorHandler(new DefaultValidationErrorHandler());
  114. }
  115. domParser.setFeature(Constants.SAX_FEATURE_PREFIX +
  116. Constants.VALIDATION_FEATURE, dbf.isValidating());
  117. // "namespaceAware" == SAX Namespaces feature
  118. domParser.setFeature(Constants.SAX_FEATURE_PREFIX +
  119. Constants.NAMESPACES_FEATURE,
  120. dbf.isNamespaceAware());
  121. // Set various parameters obtained from DocumentBuilderFactory
  122. domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
  123. Constants.INCLUDE_IGNORABLE_WHITESPACE,
  124. !dbf.isIgnoringElementContentWhitespace());
  125. domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
  126. Constants.CREATE_ENTITY_REF_NODES_FEATURE,
  127. !dbf.isExpandEntityReferences());
  128. domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
  129. Constants.INCLUDE_COMMENTS_FEATURE,
  130. !dbf.isIgnoringComments());
  131. domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
  132. Constants.CREATE_CDATA_NODES_FEATURE,
  133. !dbf.isCoalescing());
  134. setDocumentBuilderFactoryAttributes(dbfAttrs);
  135. if( enableSP)
  136. domParser.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, secureProcessing);
  137. }
  138. /**
  139. * <p>Reset this <code>DocumentBuilder</code> to its original configuration.</p>
  140. *
  141. * <p><code>DocumentBuilder</code> is reset to the same state as when it was created with
  142. * {@link DocumentBuilderFactory#newDocumentBuilder()}.
  143. * <code>reset()</code> is designed to allow the reuse of existing <code>DocumentBuilder</code>s
  144. * thus saving resources associated with the creation of new <code>DocumentBuilder</code>s.</p>
  145. *
  146. * <p>The reset <code>DocumentBuilder</code> is not guaranteed to have the same {@link EntityResolver} or {@link ErrorHandler}
  147. * <code>Object</code>s, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal
  148. * <code>EntityResolver</code> and <code>ErrorHandler</code>.</p>
  149. *
  150. * @since 1.5
  151. */
  152. public void reset(){
  153. if(domParser != null){
  154. try{
  155. //we dont need to worry about any properties being set on this object because
  156. //DocumentBuilder doesn't provide any way to set the properties
  157. //once it is created.
  158. domParser.reset();
  159. }
  160. //xxx: underlying implementation reset throws XNIException what should we do in this case ?
  161. //other question is why underlying implementation should throw an exception is it because
  162. //of properties being set.. if there was any propery that is not being supported
  163. //exception would have been thrown when setting it on the underlying implementation.
  164. catch(XNIException ex){
  165. //coninue.
  166. }
  167. }
  168. }
  169. /**
  170. * Set any DocumentBuilderFactory attributes of our underlying DOMParser
  171. *
  172. * Note: code does not handle possible conflicts between DOMParser
  173. * attribute names and JAXP specific attribute names,
  174. * eg. DocumentBuilderFactory.setValidating()
  175. */
  176. private void setDocumentBuilderFactoryAttributes(Hashtable dbfAttrs)
  177. throws SAXNotSupportedException, SAXNotRecognizedException
  178. {
  179. if (dbfAttrs == null) {
  180. // Nothing to do
  181. return;
  182. }
  183. // TODO: reroute those properties to use new JAXP1.3 API. -KK
  184. for (Enumeration e = dbfAttrs.keys(); e.hasMoreElements();) {
  185. String name = (String)e.nextElement();
  186. Object val = dbfAttrs.get(name);
  187. if (val instanceof Boolean) {
  188. // Assume feature
  189. if (Constants.FEATURE_SECURE_PROCESSING.equals(name)){
  190. enableSP = ((Boolean)val).booleanValue();
  191. }else
  192. domParser.setFeature(name, ((Boolean)val).booleanValue());
  193. } else {
  194. // Assume property
  195. if (JAXP_SCHEMA_LANGUAGE.equals(name)) {
  196. // JAXP 1.2 support
  197. //None of the properties will take effect till the setValidating(true) has been called
  198. if ( W3C_XML_SCHEMA.equals(val) ) {
  199. if( isValidating() ) {
  200. domParser.setFeature(
  201. Constants.XERCES_FEATURE_PREFIX +
  202. Constants.SCHEMA_VALIDATION_FEATURE, true);
  203. //also set the schema full checking to true.
  204. domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
  205. Constants.SCHEMA_FULL_CHECKING,
  206. true);
  207. // this should allow us not to emit DTD errors, as expected by the
  208. // spec when schema validation is enabled
  209. domParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
  210. }
  211. }
  212. } else if(JAXP_SCHEMA_SOURCE.equals(name)){
  213. if( isValidating() ) {
  214. String value=(String)dbfAttrs.get(JAXP_SCHEMA_LANGUAGE);
  215. if(value !=null && W3C_XML_SCHEMA.equals(value)){
  216. domParser.setProperty(name, val);
  217. }else{
  218. throw new IllegalArgumentException(
  219. DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
  220. "jaxp-order-not-supported",
  221. new Object[] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE}));
  222. }
  223. }
  224. }
  225. /*else if(name.equals(Constants.ENTITY_EXPANSION_LIMIT)){
  226. String elimit = (String)value;
  227. if(elimit != null && elimit != ""){
  228. int val = Integer.parseInt(elimit);
  229. secureProcessing.setEntityExpansionLimit(val);
  230. }
  231. }else if(name.equals(Constants.MAX_OCCUR_LIMIT)){
  232. String mlimit = (String)value;
  233. if(mlimit != null && mlimit != ""){
  234. int val = Integer.parseInt(mlimit);
  235. secureProcessing.setMaxOccurNodeLimit(val);
  236. }
  237. }*/ else {
  238. // Let Xerces code handle the property
  239. domParser.setProperty(name, val);
  240. }
  241. }
  242. }
  243. }
  244. /**
  245. * Non-preferred: use the getDOMImplementation() method instead of this
  246. * one to get a DOM Level 2 DOMImplementation object and then use DOM
  247. * Level 2 methods to create a DOM Document object.
  248. */
  249. public Document newDocument() {
  250. return new com.sun.org.apache.xerces.internal.dom.DocumentImpl();
  251. }
  252. public DOMImplementation getDOMImplementation() {
  253. return DOMImplementationImpl.getDOMImplementation();
  254. }
  255. public Document parse(InputSource is) throws SAXException, IOException {
  256. if (is == null) {
  257. throw new IllegalArgumentException(
  258. DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
  259. "jaxp-null-input-source", null));
  260. }
  261. if (er != null) {
  262. domParser.setEntityResolver(er);
  263. }
  264. if (eh != null) {
  265. domParser.setErrorHandler(eh);
  266. }
  267. domParser.parse(is);
  268. return domParser.getDocument();
  269. }
  270. public boolean isNamespaceAware() {
  271. try {
  272. return domParser.getFeature(Constants.SAX_FEATURE_PREFIX +
  273. Constants.NAMESPACES_FEATURE);
  274. } catch (SAXException x) {
  275. throw new IllegalStateException(x.getMessage());
  276. }
  277. }
  278. public boolean isValidating() {
  279. try {
  280. return domParser.getFeature(Constants.SAX_FEATURE_PREFIX +
  281. Constants.VALIDATION_FEATURE);
  282. } catch (SAXException x) {
  283. throw new IllegalStateException(x.getMessage());
  284. }
  285. }
  286. public void setEntityResolver(org.xml.sax.EntityResolver er) {
  287. this.er = er;
  288. }
  289. public void setErrorHandler(org.xml.sax.ErrorHandler eh) {
  290. // If app passes in a ErrorHandler of null, then ignore all errors
  291. // and warnings
  292. this.eh = (eh == null) ? new DefaultHandler() : eh;
  293. }
  294. /** <p>Get a reference to the the <code>GrammarCache</code> being used by
  295. * the XML processor.</p>
  296. *
  297. * <p>If no cache is being used, <code>null</code> is returned.</p>
  298. *
  299. * @return <code>GrammarCache</code> being used or <code>null</code>
  300. * if none in use
  301. */
  302. public Schema getSchema(){
  303. return grammar;
  304. }
  305. public boolean isXIncludeAware() {
  306. return xincludeAware;
  307. }
  308. final DOMParser getDOMParser() {
  309. return domParser;
  310. }
  311. }