001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.xml;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021:
022: import org.apache.log4j.Logger;
023: import org.xml.sax.EntityResolver;
024: import org.xml.sax.InputSource;
025: import org.xml.sax.SAXException;
026:
027: /**
028: * Internal workflow EntityResolver which resolves system ids with the
029: * "resource:" prefix to ClassLoader resources
030: *
031: * TODO: maybe prefix should be changed from "resource:" to "internal:" or just "workflow:"
032: * given that it can be resolved in arbitrary ways other than ClassLoader "resources"
033: *
034: * @author Aaron Hamid (arh14 at cornell dot edu)
035: */
036: public class ClassLoaderEntityResolver implements EntityResolver {
037: private static final Logger LOG = Logger
038: .getLogger(ClassLoaderEntityResolver.class);
039:
040: /**
041: * This contains definitions for items in the core "xml" schema, i.e. base, id, lang, and space attributes.
042: */
043: private static final String XML_NAMESPACE_SCHEMA = "http://www.w3.org/2001/xml.xsd";
044: private static final String XSD_NAMESPACE_SCHEMA = "http://www.w3.org/2001/XMLSchema.xsd";
045:
046: private final String base;
047:
048: public ClassLoaderEntityResolver() {
049: this .base = "schema";
050: }
051:
052: public ClassLoaderEntityResolver(String base) {
053: this .base = base;
054: }
055:
056: public InputSource resolveEntity(String publicId, String systemId)
057: throws SAXException, IOException {
058: LOG.debug("Resolving '" + publicId + "' / '" + systemId + "'");
059: String path = "";
060: if (systemId.equals(XML_NAMESPACE_SCHEMA)) {
061: path = base + "/xml.xsd";
062: } else if (systemId.equals(XSD_NAMESPACE_SCHEMA)) {
063: path = base + "/XMLSchema.xsd";
064: } else if (systemId.startsWith("resource")) {
065: /* It turns out that the stock XMLSchema.xsd refers to XMLSchema.dtd in a relative
066: fashion which results in the parser qualifying it to some local file:// path
067: which breaks our detection here.
068: So I have made a small mod to the stock XMLSchema.xsd so that it instead refers to
069: resource:XMLSchema.dtd which can be looked up locally.
070: The same is true for XMLSchema.dtd with regard to datatypes.dtd, so I have also
071: modified XMLSchema.dtd to refer to resource:datatypes.dtd.
072: An alternative would be to rely on publicId, however that would essentially hard code
073: the lookup to always be in the classpath and rule out being able to redirect the location
074: of the physical resource through the systemId, which is useful.
075: */
076:
077: // TODO: revisit making this more sophisticated than just the classloader
078: // of this class (thread context classloader? plugin classloader?)
079: path = base + "/"
080: + systemId.substring("resource:".length());
081: // ok, if the path does not itself end in .xsd or .dtd, it is bare/abstract
082: // so realize it by appending .xsd
083: // this allows us to support looking up files ending with ".dtd" through resource: without
084: // having extra logic to attempt to look up both suffixes for every single resource:
085: // (all of which except XMLSchema.dtd and datatypes.dtd at this point are .xsd files)
086: if (!(systemId.endsWith(".xsd") || systemId
087: .endsWith(".dtd"))) {
088: path += ".xsd";
089: }
090: } else {
091: LOG
092: .error("Unable to resolve system id '"
093: + systemId
094: + "' locally...delegating to default resolution strategy.");
095: return null;
096: }
097: InputStream is = getClass().getClassLoader()
098: .getResourceAsStream(path);
099: if (is == null) {
100: String message = "Unable to find schema (" + path
101: + ") for: " + systemId;
102: LOG.error(message);
103: throw new SAXException(message);
104: }
105: return new InputSource(is);
106: }
107: }
|