1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.util;
17
18 import java.io.InputStream;
19
20 import javax.xml.XMLConstants;
21 import javax.xml.parsers.DocumentBuilder;
22 import javax.xml.parsers.DocumentBuilderFactory;
23
24 import org.w3c.dom.DOMImplementation;
25 import org.w3c.dom.ls.DOMImplementationLS;
26 import org.w3c.dom.ls.LSInput;
27 import org.w3c.dom.ls.LSResourceResolver;
28
29
30
31
32
33
34 public class ClassLoaderLSResourceResolver extends ClassLoaderResourceResolver implements LSResourceResolver {
35
36
37
38
39
40 public ClassLoaderLSResourceResolver(String base, String prefix) {
41 super(base, prefix);
42 }
43
44
45
46
47 public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
48 if (!type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
49 return null;
50 }
51 LOG.error(type);
52 LOG.error(namespaceURI);
53 LOG.error(publicId);
54 LOG.error(systemId);
55 LOG.error(baseURI);
56 String path = resolveSystemId(systemId);
57 if (path == null) {
58 return null;
59 }
60 LOG.debug("Looking up resource '" + path + "' for system id '" + systemId + "'");
61 InputStream is = getClass().getClassLoader().getResourceAsStream(path);
62 if (is == null) {
63 String message = "Unable to find schema (" + path + ") for: " + systemId;
64 LOG.error(message);
65 throw new RuntimeException(message);
66 }
67 try {
68 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
69 DocumentBuilder builder = factory.newDocumentBuilder();
70 DOMImplementation domImpl = builder.getDOMImplementation();
71 DOMImplementationLS dils = (DOMImplementationLS) domImpl;
72 LSInput input = dils.createLSInput();
73 input.setByteStream(is);
74 return input;
75 } catch (Exception e) {
76 throw new RuntimeException(e);
77 }
78 }
79 }