KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > builder > xml > DefaultDocumentLoader


1 /*
2  * Copyright 2002-2006 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.webflow.engine.builder.xml;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.springframework.core.io.Resource;
28 import org.springframework.util.xml.SimpleSaxErrorHandler;
29 import org.w3c.dom.Document JavaDoc;
30 import org.xml.sax.EntityResolver JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34  * The default document loader strategy for XSD-based XML documents with
35  * validation enabled by default.
36  * <p>
37  * Note: full XSD support requires JDK 5.0 or a capable parser such as Xerces
38  * 2.0. JDK 1.4 or < do not fully support XSD out of the box. To use this
39  * implementation on JDK 1.4 make sure Xerces is available in your classpath or
40  * disable XSD validation by
41  * {@link #setValidating(boolean) setting the validating property to false}.
42  *
43  * @author Keith Donald
44  */

45 public class DefaultDocumentLoader implements DocumentLoader {
46
47     private static final Log logger = LogFactory.getLog(DefaultDocumentLoader.class);
48
49     /**
50      * JAXP attribute used to configure the schema language for validation.
51      */

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

57     private static final String JavaDoc XSD_SCHEMA_LANGUAGE = "http://www.w3.org/2001/XMLSchema";
58
59     /**
60      * Flag indicating if the XML document parser will perform schema
61      * validation.
62      */

63     private boolean validating = true;
64
65     /**
66      * The spring-webflow schema resolution strategy.
67      */

68     private EntityResolver JavaDoc entityResolver = new WebFlowEntityResolver();
69
70     /**
71      * Returns whether or not the XML parser will validate the document.
72      */

73     public boolean isValidating() {
74         return validating;
75     }
76
77     /**
78      * Set if the XML parser should validate the document and thus enforce a
79      * schema. Defaults to true.
80      */

81     public void setValidating(boolean validating) {
82         this.validating = validating;
83     }
84
85     /**
86      * Returns the SAX entity resolver used by the XML parser.
87      */

88     public EntityResolver JavaDoc getEntityResolver() {
89         return entityResolver;
90     }
91
92     /**
93      * Set a SAX entity resolver to be used for parsing. Can be overridden for
94      * custom entity resolution, for example relative to some specific base
95      * path.
96      * @see org.springframework.webflow.engine.builder.xml.WebFlowEntityResolver
97      */

98     public void setEntityResolver(EntityResolver JavaDoc entityResolver) {
99         this.entityResolver = entityResolver;
100     }
101
102     public Document JavaDoc loadDocument(Resource resource) throws IOException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc {
103         InputStream JavaDoc is = null;
104         try {
105             is = resource.getInputStream();
106             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
107             factory.setValidating(isValidating());
108             factory.setNamespaceAware(true);
109             try {
110                 factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE);
111             }
112             catch (IllegalArgumentException JavaDoc ex) {
113                 throw new IllegalStateException JavaDoc("Unable to validate using XSD: Your JAXP provider [" + factory
114                         + "] does not support XML Schema. "
115                         + "Are you running on Java 1.4 or below with Apache Crimson? "
116                         + "If so you must upgrade to Apache Xerces (or Java 5 or >) for full XSD support.");
117             }
118             DocumentBuilder JavaDoc docBuilder = factory.newDocumentBuilder();
119             docBuilder.setErrorHandler(new SimpleSaxErrorHandler(logger));
120             docBuilder.setEntityResolver(getEntityResolver());
121             return docBuilder.parse(is);
122         }
123         finally {
124             if (is != null) {
125                 is.close();
126             }
127         }
128     }
129 }
Popular Tags