View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.kew.xml;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  
21  import org.apache.log4j.Logger;
22  import org.xml.sax.EntityResolver;
23  import org.xml.sax.InputSource;
24  import org.xml.sax.SAXException;
25  
26  /**
27   * Internal workflow EntityResolver which resolves system ids with the
28   * "resource:" prefix to ClassLoader resources
29   * 
30   * TODO: maybe prefix should be changed from "resource:" to "internal:" or just "workflow:"
31   * given that it can be resolved in arbitrary ways other than ClassLoader "resources"
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class ClassLoaderEntityResolver implements EntityResolver {
36      private static final Logger LOG = Logger.getLogger(ClassLoaderEntityResolver.class);
37  
38      /**
39       * This contains definitions for items in the core "xml" schema, i.e. base, id, lang, and space attributes. 
40       */
41      private static final String XML_NAMESPACE_SCHEMA = "http://www.w3.org/2001/xml.xsd";
42      private static final String XSD_NAMESPACE_SCHEMA = "http://www.w3.org/2001/XMLSchema.xsd";
43      
44      private final String base;
45      public ClassLoaderEntityResolver() {
46          this.base = "schema";
47      }
48      public ClassLoaderEntityResolver(String base) {
49          this.base = base;
50      }
51      public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
52          LOG.debug("Resolving '" + publicId + "' / '" + systemId + "'");
53          String path = "";
54          if (systemId.equals(XML_NAMESPACE_SCHEMA)) {
55              path = base + "/xml.xsd";
56          } else if (systemId.equals(XSD_NAMESPACE_SCHEMA)) {
57              path = base + "/XMLSchema.xsd";
58          } else if (systemId.startsWith("resource")) {
59              /* It turns out that the stock XMLSchema.xsd refers to XMLSchema.dtd in a relative
60                 fashion which results in the parser qualifying it to some local file:// path
61                 which breaks our detection here.
62                 So I have made a small mod to the stock XMLSchema.xsd so that it instead refers to
63                 resource:XMLSchema.dtd which can be looked up locally.
64                 The same is true for XMLSchema.dtd with regard to datatypes.dtd, so I have also
65                 modified XMLSchema.dtd to refer to resource:datatypes.dtd.
66                 An alternative would be to rely on publicId, however that would essentially hard code
67                 the lookup to always be in the classpath and rule out being able to redirect the location
68                 of the physical resource through the systemId, which is useful.
69              */
70  
71              // TODO: revisit making this more sophisticated than just the classloader
72              // of this class (thread context classloader? plugin classloader?)
73              path = base + "/" + systemId.substring("resource:".length());
74              // ok, if the path does not itself end in .xsd or .dtd, it is bare/abstract
75              // so realize it by appending .xsd
76              // this allows us to support looking up files ending with ".dtd" through resource: without
77              // having extra logic to attempt to look up both suffixes for every single resource:
78              // (all of which except XMLSchema.dtd and datatypes.dtd at this point are .xsd files)
79              if (!(systemId.endsWith(".xsd") || systemId.endsWith(".dtd"))) {
80                  path += ".xsd";
81              }
82          } else {
83              LOG.error("Unable to resolve system id '" + systemId + "' locally...delegating to default resolution strategy.");
84              return null;
85          }
86          InputStream is = getClass().getClassLoader().getResourceAsStream(path);
87          if (is == null) {
88              String message = "Unable to find schema (" + path + ") for: " + systemId;
89              LOG.error(message);
90              throw new SAXException(message);
91          }
92          return new InputSource(is);
93      }
94  }