1. /*
  2. *
  3. * The Apache Software License, Version 1.1
  4. *
  5. *
  6. * Copyright (c) 2000-2002 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.util.Hashtable;
  59. import javax.xml.parsers.DocumentBuilder;
  60. import javax.xml.parsers.DocumentBuilderFactory;
  61. import javax.xml.parsers.ParserConfigurationException;
  62. import javax.xml.validation.Schema;
  63. import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
  64. import com.sun.org.apache.xerces.internal.impl.Constants;
  65. import com.sun.org.apache.xerces.internal.parsers.DOMParser;
  66. import org.xml.sax.SAXException;
  67. /**
  68. * @author Rajiv Mordani
  69. * @author Edwin Goei
  70. * @version $Id: DocumentBuilderFactoryImpl.java,v 1.14 2004/02/24 23:15:58 mrglavas Exp $
  71. */
  72. public class DocumentBuilderFactoryImpl extends DocumentBuilderFactory {
  73. /** These are DocumentBuilderFactory attributes not DOM attributes */
  74. private Hashtable attributes;
  75. private Schema grammar;
  76. private boolean isXIncludeAware;
  77. /**
  78. * Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
  79. * using the currently configured parameters.
  80. */
  81. public DocumentBuilder newDocumentBuilder()
  82. throws ParserConfigurationException {
  83. // check the consistency between the specified schema and
  84. // the schema property. I thought about putting this into
  85. // DocumentBuilderImpl, but because of the hack in the getAttribute method,
  86. // we can't really do that. -KK
  87. if( attributes!= null && attributes.containsKey("http://java.sun.com/xml/jaxp/properties/schemaLanguage") && grammar!=null )
  88. throw new ParserConfigurationException(
  89. DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
  90. "jaxp-schema-support",null));
  91. try {
  92. return new DocumentBuilderImpl(this, attributes);
  93. } catch (SAXException se) {
  94. // Handles both SAXNotSupportedException, SAXNotRecognizedException
  95. throw new ParserConfigurationException(se.getMessage());
  96. }
  97. }
  98. /**
  99. * Allows the user to set specific attributes on the underlying
  100. * implementation.
  101. * @param name name of attribute
  102. * @param value null means to remove attribute
  103. */
  104. public void setAttribute(String name, Object value)
  105. throws IllegalArgumentException {
  106. // This handles removal of attributes
  107. if (value == null) {
  108. if (attributes != null) {
  109. attributes.remove(name);
  110. }
  111. // Unrecognized attributes do not cause an exception
  112. return;
  113. }
  114. // This is ugly. We have to collect the attributes and then
  115. // later create a DocumentBuilderImpl to verify the attributes.
  116. // Create Hashtable if none existed before
  117. if (attributes == null) {
  118. attributes = new Hashtable();
  119. }
  120. attributes.put(name, value);
  121. // Test the attribute name by possibly throwing an exception
  122. try {
  123. new DocumentBuilderImpl(this, attributes);
  124. } catch (Exception e) {
  125. attributes.remove(name);
  126. throw new IllegalArgumentException(e.getMessage());
  127. }
  128. }
  129. /**
  130. * Allows the user to retrieve specific attributes on the underlying
  131. * implementation.
  132. */
  133. public Object getAttribute(String name) throws IllegalArgumentException {
  134. // See if it's in the attributes Hashtable
  135. if (attributes != null) {
  136. Object val = attributes.get(name);
  137. if (val != null) {
  138. return val;
  139. }
  140. }
  141. DOMParser domParser = null;
  142. try {
  143. // We create a dummy DocumentBuilderImpl in case the attribute
  144. // name is not one that is in the attributes hashtable.
  145. domParser =
  146. new DocumentBuilderImpl(this, attributes).getDOMParser();
  147. return domParser.getProperty(name);
  148. } catch (SAXException se1) {
  149. // assert(name is not recognized or not supported), try feature
  150. try {
  151. boolean result = domParser.getFeature(name);
  152. // Must have been a feature
  153. return result ? Boolean.TRUE : Boolean.FALSE;
  154. } catch (SAXException se2) {
  155. // Not a property or a feature
  156. throw new IllegalArgumentException(se1.getMessage());
  157. }
  158. }
  159. }
  160. public Schema getSchema() {
  161. return grammar;
  162. }
  163. public void setSchema(Schema grammar) {
  164. this.grammar = grammar;
  165. }
  166. public boolean isXIncludeAware() {
  167. return this.isXIncludeAware;
  168. }
  169. public void setXIncludeAware(boolean state) {
  170. this.isXIncludeAware = state;
  171. }
  172. public void setFeature(String name, boolean value)
  173. throws ParserConfigurationException{
  174. //Revisit::
  175. //for now use attributes itself. we just support on feature.
  176. //If we need to use setFeature in full fledge we should
  177. //document what is supported by setAttribute
  178. //and what is by setFeature.
  179. //user should not use setAttribute("xyz",Boolean.TRUE)
  180. //instead of setFeature("xyz",true);
  181. if(attributes == null)
  182. attributes = new Hashtable();
  183. if(name.equals(Constants.FEATURE_SECURE_PROCESSING)){
  184. attributes.put(Constants.FEATURE_SECURE_PROCESSING,Boolean.valueOf(value));
  185. } else throw new ParserConfigurationException(
  186. DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
  187. "jaxp_feature_not_supported",
  188. new Object[] {name}));
  189. }
  190. public boolean getFeature(String name)
  191. throws ParserConfigurationException {
  192. if (name.equals(Constants.FEATURE_SECURE_PROCESSING)){
  193. Object ob = attributes.get(Constants.FEATURE_SECURE_PROCESSING);
  194. if(ob == null) return false;
  195. return ((Boolean)ob).booleanValue();
  196. }
  197. else
  198. throw new ParserConfigurationException(DOMMessageFormatter.formatMessage(
  199. DOMMessageFormatter.DOM_DOMAIN,"jaxp_feature_not_supported",
  200. new Object[] {name}));
  201. }
  202. }