Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ClassLoaderResourceResolver |
|
| 3.6666666666666665;3.667 |
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.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 | 0 | 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 | 0 | public ClassLoaderResourceResolver() { |
49 | 0 | this.base = "schema"; |
50 | 0 | this.prefix = ""; |
51 | 0 | } |
52 | ||
53 | /** | |
54 | * Constructs a ClassLoaderResourceResolver.java. | |
55 | * @param base | |
56 | * @param prefix | |
57 | */ | |
58 | 0 | public ClassLoaderResourceResolver(String base, String prefix) { |
59 | 0 | this.base = base; |
60 | 0 | this.prefix = prefix; |
61 | 0 | } |
62 | ||
63 | /** | |
64 | * @param systemId | |
65 | * @return String | |
66 | */ | |
67 | protected String resolveSystemId(String systemId) { | |
68 | 0 | if (systemId.equals(XML_NAMESPACE_SCHEMA)) { |
69 | 0 | return base + "/xml.xsd"; |
70 | 0 | } else if (systemId.equals(XSD_NAMESPACE_SCHEMA)) { |
71 | 0 | return base + "/XMLSchema.xsd"; |
72 | 0 | } 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 | 0 | 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 | 0 | if (!(systemId.endsWith(".xsd") || systemId.endsWith(".dtd"))) { |
92 | 0 | path += ".xsd"; |
93 | } | |
94 | 0 | return path; |
95 | } else { | |
96 | 0 | return null; |
97 | } | |
98 | } | |
99 | } |