View Javadoc
1   /**
2    * Copyright 2005-2015 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 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   * Resource resolver for SchemaFactory.  For now used during validation of NotificationRequest content element.
31   * Looks up XSD from classloader.
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class ClassLoaderLSResourceResolver extends ClassLoaderResourceResolver implements LSResourceResolver {
35      /**
36       * Constructs a ClassLoaderLSResourceResolver.java.
37       * @param base
38       * @param prefix
39       */
40      public ClassLoaderLSResourceResolver(String base, String prefix) {
41          super(base, prefix);
42      }
43  
44      /**
45       * @see org.w3c.dom.ls.LSResourceResolver#resolveResource(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
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/*SAXException*/(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  }