KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > xml > DefaultDocumentLoader


1 /*
2  * Copyright 2002-2007 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
17 package org.springframework.beans.factory.xml;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.w3c.dom.Document JavaDoc;
26 import org.xml.sax.EntityResolver JavaDoc;
27 import org.xml.sax.ErrorHandler JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29
30 /**
31  * The default {@link DocumentLoader} implementation.
32  *
33  * <p>Simply loads {@link Document documents} using the standard JAXP-configured
34  * XML parser. If you want to change the {@link DocumentBuilder} that is used to
35  * load documents then one strategy is to use a Java define when starting your
36  * application. For example, to use the Oracle {@link DocumentBuilder}, one might
37  * start one's application like so:
38  *
39  * <pre code="class">java -Djavax.xml.parsers.DocumentBuilderFactory=oracle.xml.jaxp.JXDocumentBuilderFactory MyMainClass</pre>
40  *
41  * @author Rob Harrop
42  * @author Juergen Hoeller
43  * @since 2.0
44  */

45 public class DefaultDocumentLoader implements DocumentLoader {
46
47     /**
48      * JAXP attribute used to configure the schema language for validation.
49      */

50     private static final String JavaDoc SCHEMA_LANGUAGE_ATTRIBUTE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
51
52     /**
53      * JAXP attribute value indicating the XSD schema language.
54      */

55     private static final String JavaDoc XSD_SCHEMA_LANGUAGE = "http://www.w3.org/2001/XMLSchema";
56
57
58     private static final Log logger = LogFactory.getLog(DefaultDocumentLoader.class);
59
60
61     /**
62      * Load the {@link Document} at the supplied {@link InputSource} using the standard JAXP-configured
63      * XML parser.
64      */

65     public Document JavaDoc loadDocument(
66             InputSource JavaDoc inputSource, EntityResolver JavaDoc entityResolver,
67             ErrorHandler JavaDoc errorHandler, int validationMode, boolean namespaceAware)
68             throws Exception JavaDoc {
69
70         DocumentBuilderFactory JavaDoc factory =
71                 createDocumentBuilderFactory(validationMode, namespaceAware);
72         if (logger.isDebugEnabled()) {
73             logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
74         }
75         DocumentBuilder JavaDoc builder = createDocumentBuilder(factory, entityResolver, errorHandler);
76         return builder.parse(inputSource);
77     }
78
79     /**
80      * Create the {@link DocumentBuilderFactory} instance.
81      * @param validationMode the type of validation ({@link XmlBeanDefinitionReader#VALIDATION_NONE none}, {@link XmlBeanDefinitionReader#VALIDATION_DTD DTD}, or {@link XmlBeanDefinitionReader#VALIDATION_XSD XSD})
82      * @param namespaceAware <code>true</code> if the returned factory is to provide support for XML namespaces
83      * @throws ParserConfigurationException if we failed to build a proper DocumentBuilderFactory
84      */

85     protected DocumentBuilderFactory JavaDoc createDocumentBuilderFactory(int validationMode, boolean namespaceAware)
86             throws ParserConfigurationException JavaDoc {
87
88         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
89         factory.setNamespaceAware(namespaceAware);
90
91         if (validationMode != XmlBeanDefinitionReader.VALIDATION_NONE) {
92             factory.setValidating(true);
93
94             if (validationMode == XmlBeanDefinitionReader.VALIDATION_XSD) {
95                 // enforce namespace aware for XSD
96
factory.setNamespaceAware(true);
97                 try {
98                     factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE);
99                 }
100                 catch (IllegalArgumentException JavaDoc ex) {
101                     throw new ParserConfigurationException JavaDoc(
102                             "Unable to validate using XSD: Your JAXP provider [" + factory +
103                             "] does not support XML Schema. Are you running on Java 1.4 or below with " +
104                             "Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.");
105                 }
106             }
107         }
108
109         return factory;
110     }
111
112     /**
113      * Create a JAXP DocumentBuilder that this bean definition reader
114      * will use for parsing XML documents. Can be overridden in subclasses,
115      * adding further initialization of the builder.
116      * @param factory the JAXP DocumentBuilderFactory that the DocumentBuilder
117      * should be created with
118      * @return the JAXP DocumentBuilder
119      * @throws ParserConfigurationException if thrown by JAXP methods
120      */

121     protected DocumentBuilder JavaDoc createDocumentBuilder(
122             DocumentBuilderFactory JavaDoc factory, EntityResolver JavaDoc entityResolver, ErrorHandler JavaDoc errorHandler)
123             throws ParserConfigurationException JavaDoc {
124
125         DocumentBuilder JavaDoc docBuilder = factory.newDocumentBuilder();
126         if (entityResolver != null) {
127             docBuilder.setEntityResolver(entityResolver);
128         }
129         if (errorHandler != null) {
130             docBuilder.setErrorHandler(errorHandler);
131         }
132         return docBuilder;
133     }
134
135 }
136
Popular Tags