View Javadoc
1   /**
2    * Copyright 2005-2016 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.ken.util;
17  
18  import javax.xml.XMLConstants;
19  
20  import org.apache.log4j.Logger;
21  
22  /**
23   * Utility base class for Entity and LSResource Resolvers that should resolve
24   * arguments to resources in the ClassLoader.
25   * @author Kuali Rice Team (rice.collab@kuali.org)
26   */
27  public class ClassLoaderResourceResolver {
28      protected final Logger LOG = Logger.getLogger(getClass());
29  
30      /**
31       * This contains definitions for items in the core "xml" schema, i.e. base, id, lang, and space attributes. 
32       */
33      private static final String XML_NAMESPACE_SCHEMA = "http://www.w3.org/2001/xml.xsd";
34      private static final String XSD_NAMESPACE_SCHEMA = XMLConstants.W3C_XML_SCHEMA_NS_URI;
35  
36      /**
37       * Root path in class loader.  Defaults to "schema".
38       */
39      protected final String base;
40      /**
41       * Prefix of resources to honor.  Defaults to "" (no specific prefix).
42       */
43      protected final String prefix;
44  
45      /**
46       * Constructs a ClassLoaderResourceResolver.java.
47       */
48      public ClassLoaderResourceResolver() {
49          this.base = "schema";
50          this.prefix = "";
51      }
52      
53      /**
54       * Constructs a ClassLoaderResourceResolver.java.
55       * @param base
56       * @param prefix
57       */
58      public ClassLoaderResourceResolver(String base, String prefix) {
59          this.base = base;
60          this.prefix = prefix;
61      }
62  
63      /**
64       * @param systemId
65       * @return String
66       */
67      protected String resolveSystemId(String systemId) {
68          if (systemId.equals(XML_NAMESPACE_SCHEMA)) {
69              return base + "/xml.xsd";
70          } else if (systemId.equals(XSD_NAMESPACE_SCHEMA)) {
71              return base + "/XMLSchema.xsd";
72          } else if (systemId.startsWith("resource:" + prefix +"/")) {
73              /* It turns out that the stock XMLSchema.xsd refers to XMLSchema.dtd in a relative
74                 fashion which results in the parser qualifying it to some local file:// path
75                 which breaks our detection here.
76                 So I have made a small mod to the stock XMLSchema.xsd so that it instead refers to
77                 resource:XMLSchema.dtd which can be looked up locally.
78                 The same is true for XMLSchema.dtd with regard to datatypes.dtd, so I have also
79                 modified XMLSchema.dtd to refer to resource:datatypes.dtd.
80                 An alternative would be to rely on publicId, however that would essentially hard code
81                 the lookup to always be in the classpath and rule out being able to redirect the location
82                 of the physical resource through the systemId, which is useful.
83              */
84  
85              String path = base + "/" + systemId.substring(("resource:" + prefix + "/").length());
86              // ok, if the path does not itself end in .xsd or .dtd, it is bare/abstract
87              // so realize it by appending .xsd
88              // this allows us to support looking up files ending with ".dtd" through resource: without
89              // having extra logic to attempt to look up both suffixes for every single resource:
90              // (all of which except XMLSchema.dtd and datatypes.dtd at this point are .xsd files)
91              if (!(systemId.endsWith(".xsd") || systemId.endsWith(".dtd"))) {
92                  path += ".xsd";
93              }
94              return path;
95          } else {
96              return null;
97          }
98      }
99  }