1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.beans.factory.xml;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import javax.xml.parsers.DocumentBuilder;
  20. import javax.xml.parsers.DocumentBuilderFactory;
  21. import javax.xml.parsers.ParserConfigurationException;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.w3c.dom.Document;
  25. import org.xml.sax.EntityResolver;
  26. import org.xml.sax.ErrorHandler;
  27. import org.xml.sax.SAXException;
  28. import org.xml.sax.SAXParseException;
  29. import org.springframework.beans.BeanUtils;
  30. import org.springframework.beans.BeansException;
  31. import org.springframework.beans.factory.BeanDefinitionStoreException;
  32. import org.springframework.beans.factory.support.AbstractBeanDefinitionReader;
  33. import org.springframework.beans.factory.support.BeanDefinitionRegistry;
  34. import org.springframework.core.io.Resource;
  35. /**
  36. * Bean definition reader for Spring's default XML bean definition format.
  37. * Typically applied to a DefaultListableBeanFactory.
  38. *
  39. * <p>The structure, element and attribute names of the required XML document
  40. * are hard-coded in this class. (Of course a transform could be run if necessary
  41. * to produce this format). "beans" doesn't need to be the root element of the XML
  42. * document: This class will parse all bean definition elements in the XML file.
  43. *
  44. * <p>This class registers each bean definition with the given bean factory superclass,
  45. * and relies on the latter's implementation of the BeanDefinitionRegistry interface.
  46. * It supports singletons, prototypes, and references to either of these kinds of bean.
  47. * @author Juergen Hoeller
  48. * @since 26.11.2003
  49. * @see #setParserClass
  50. * @version $Id: XmlBeanDefinitionReader.java,v 1.8 2004/03/18 02:46:12 trisberg Exp $
  51. */
  52. public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
  53. protected final Log logger = LogFactory.getLog(getClass());
  54. private boolean validating = true;
  55. private EntityResolver entityResolver;
  56. private Class parserClass = DefaultXmlBeanDefinitionParser.class;
  57. /**
  58. * Create new XmlBeanDefinitionReader for the given bean factory.
  59. */
  60. public XmlBeanDefinitionReader(BeanDefinitionRegistry beanFactory) {
  61. super(beanFactory);
  62. }
  63. /**
  64. * Set if the XML parser should validate the document and thus enforce a DTD.
  65. */
  66. public void setValidating(boolean validating) {
  67. this.validating = validating;
  68. }
  69. /**
  70. * Set a SAX entity resolver to be used for parsing. By default, BeansDtdResolver
  71. * will be used. Can be overridden for custom entity resolution, e.g. relative
  72. * to some specific base path.
  73. * @see org.springframework.beans.factory.xml.BeansDtdResolver
  74. */
  75. public void setEntityResolver(EntityResolver entityResolver) {
  76. this.entityResolver = entityResolver;
  77. }
  78. /**
  79. * Set the XmlBeanDefinitionParser implementation to use.
  80. * Default is DefaultXmlBeanDefinitionParser.
  81. * @see XmlBeanDefinitionParser
  82. * @see DefaultXmlBeanDefinitionParser
  83. */
  84. public void setParserClass(Class parserClass) {
  85. if (this.parserClass == null || !XmlBeanDefinitionParser.class.isAssignableFrom(parserClass)) {
  86. throw new IllegalArgumentException("parserClass must be a XmlBeanDefinitionParser");
  87. }
  88. this.parserClass = parserClass;
  89. }
  90. /**
  91. * Load bean definitions from the specified XML file.
  92. * @param resource the resource descriptor for the XML file
  93. * @throws BeansException in case of loading or parsing errors
  94. */
  95. public void loadBeanDefinitions(Resource resource) throws BeansException {
  96. if (resource == null) {
  97. throw new BeanDefinitionStoreException("Resource cannot be null: expected an XML file");
  98. }
  99. InputStream is = null;
  100. try {
  101. logger.info("Loading XML bean definitions from " + resource + "");
  102. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  103. logger.debug("Using JAXP implementation [" + factory + "]");
  104. factory.setValidating(this.validating);
  105. DocumentBuilder docBuilder = factory.newDocumentBuilder();
  106. docBuilder.setErrorHandler(new BeansErrorHandler());
  107. docBuilder.setEntityResolver(this.entityResolver != null ? this.entityResolver : new BeansDtdResolver());
  108. is = resource.getInputStream();
  109. Document doc = docBuilder.parse(is);
  110. registerBeanDefinitions(doc, resource);
  111. }
  112. catch (ParserConfigurationException ex) {
  113. throw new BeanDefinitionStoreException("Parser configuration exception parsing XML from " + resource, ex);
  114. }
  115. catch (SAXParseException ex) {
  116. throw new BeanDefinitionStoreException("Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
  117. }
  118. catch (SAXException ex) {
  119. throw new BeanDefinitionStoreException("XML document from " + resource + " is invalid", ex);
  120. }
  121. catch (IOException ex) {
  122. throw new BeanDefinitionStoreException("IOException parsing XML document from " + resource, ex);
  123. }
  124. finally {
  125. if (is != null) {
  126. try {
  127. is.close();
  128. }
  129. catch (IOException ex) {
  130. logger.warn("Could not close InputStream", ex);
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Register the bean definitions contained in the given DOM document.
  137. * All calls go through this.
  138. * @param doc the DOM document
  139. * @throws BeansException in case of parsing errors
  140. */
  141. public void registerBeanDefinitions(Document doc, Resource resource) throws BeansException {
  142. XmlBeanDefinitionParser parser = (XmlBeanDefinitionParser) BeanUtils.instantiateClass(this.parserClass);
  143. parser.registerBeanDefinitions(getBeanFactory(), getBeanClassLoader(), doc, resource);
  144. }
  145. /**
  146. * Private implementation of SAX ErrorHandler used when validating XML.
  147. */
  148. private static class BeansErrorHandler implements ErrorHandler {
  149. /**
  150. * We can't use the enclosing class' logger as it's protected and inherited.
  151. */
  152. private final static Log logger = LogFactory.getLog(XmlBeanFactory.class);
  153. public void error(SAXParseException ex) throws SAXException {
  154. throw ex;
  155. }
  156. public void fatalError(SAXParseException ex) throws SAXException {
  157. throw ex;
  158. }
  159. public void warning(SAXParseException ex) throws SAXException {
  160. logger.warn("Ignored XML validation warning: " + ex);
  161. }
  162. }
  163. }