001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.engine.builder.xml;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020:
021: import javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023: import javax.xml.parsers.ParserConfigurationException;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.springframework.core.io.Resource;
028: import org.springframework.util.xml.SimpleSaxErrorHandler;
029: import org.w3c.dom.Document;
030: import org.xml.sax.EntityResolver;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * The default document loader strategy for XSD-based XML documents with
035: * validation enabled by default.
036: * <p>
037: * Note: full XSD support requires JDK 5.0 or a capable parser such as Xerces
038: * 2.0. JDK 1.4 or < do not fully support XSD out of the box. To use this
039: * implementation on JDK 1.4 make sure Xerces is available in your classpath or
040: * disable XSD validation by
041: * {@link #setValidating(boolean) setting the validating property to false}.
042: *
043: * @author Keith Donald
044: */
045: public class DefaultDocumentLoader implements DocumentLoader {
046:
047: private static final Log logger = LogFactory
048: .getLog(DefaultDocumentLoader.class);
049:
050: /**
051: * JAXP attribute used to configure the schema language for validation.
052: */
053: private static final String SCHEMA_LANGUAGE_ATTRIBUTE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
054:
055: /**
056: * JAXP attribute value indicating the XSD schema language.
057: */
058: private static final String XSD_SCHEMA_LANGUAGE = "http://www.w3.org/2001/XMLSchema";
059:
060: /**
061: * Flag indicating if the XML document parser will perform schema
062: * validation.
063: */
064: private boolean validating = true;
065:
066: /**
067: * The spring-webflow schema resolution strategy.
068: */
069: private EntityResolver entityResolver = new WebFlowEntityResolver();
070:
071: /**
072: * Returns whether or not the XML parser will validate the document.
073: */
074: public boolean isValidating() {
075: return validating;
076: }
077:
078: /**
079: * Set if the XML parser should validate the document and thus enforce a
080: * schema. Defaults to true.
081: */
082: public void setValidating(boolean validating) {
083: this .validating = validating;
084: }
085:
086: /**
087: * Returns the SAX entity resolver used by the XML parser.
088: */
089: public EntityResolver getEntityResolver() {
090: return entityResolver;
091: }
092:
093: /**
094: * Set a SAX entity resolver to be used for parsing. Can be overridden for
095: * custom entity resolution, for example relative to some specific base
096: * path.
097: * @see org.springframework.webflow.engine.builder.xml.WebFlowEntityResolver
098: */
099: public void setEntityResolver(EntityResolver entityResolver) {
100: this .entityResolver = entityResolver;
101: }
102:
103: public Document loadDocument(Resource resource) throws IOException,
104: ParserConfigurationException, SAXException {
105: InputStream is = null;
106: try {
107: is = resource.getInputStream();
108: DocumentBuilderFactory factory = DocumentBuilderFactory
109: .newInstance();
110: factory.setValidating(isValidating());
111: factory.setNamespaceAware(true);
112: try {
113: factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE,
114: XSD_SCHEMA_LANGUAGE);
115: } catch (IllegalArgumentException ex) {
116: throw new IllegalStateException(
117: "Unable to validate using XSD: Your JAXP provider ["
118: + factory
119: + "] does not support XML Schema. "
120: + "Are you running on Java 1.4 or below with Apache Crimson? "
121: + "If so you must upgrade to Apache Xerces (or Java 5 or >) for full XSD support.");
122: }
123: DocumentBuilder docBuilder = factory.newDocumentBuilder();
124: docBuilder
125: .setErrorHandler(new SimpleSaxErrorHandler(logger));
126: docBuilder.setEntityResolver(getEntityResolver());
127: return docBuilder.parse(is);
128: } finally {
129: if (is != null) {
130: is.close();
131: }
132: }
133: }
134: }
|