001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ken.util;
017
018import java.io.InputStream;
019
020import javax.xml.XMLConstants;
021import javax.xml.parsers.DocumentBuilder;
022import javax.xml.parsers.DocumentBuilderFactory;
023
024import org.w3c.dom.DOMImplementation;
025import org.w3c.dom.ls.DOMImplementationLS;
026import org.w3c.dom.ls.LSInput;
027import org.w3c.dom.ls.LSResourceResolver;
028
029/**
030 * Resource resolver for SchemaFactory.  For now used during validation of NotificationRequest content element.
031 * Looks up XSD from classloader.
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class ClassLoaderLSResourceResolver extends ClassLoaderResourceResolver implements LSResourceResolver {
035    /**
036     * Constructs a ClassLoaderLSResourceResolver.java.
037     * @param base
038     * @param prefix
039     */
040    public ClassLoaderLSResourceResolver(String base, String prefix) {
041        super(base, prefix);
042    }
043
044    /**
045     * @see org.w3c.dom.ls.LSResourceResolver#resolveResource(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
046     */
047    public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
048        if (!type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
049            return null;
050        }
051        LOG.error(type);
052        LOG.error(namespaceURI);
053        LOG.error(publicId);
054        LOG.error(systemId);
055        LOG.error(baseURI);
056        String path = resolveSystemId(systemId);
057        if (path == null) {
058            return null;
059        }
060        LOG.debug("Looking up resource '" + path + "' for system id '" + systemId + "'");
061        InputStream is = getClass().getClassLoader().getResourceAsStream(path);
062        if (is == null) {
063            String message = "Unable to find schema (" + path + ") for: " + systemId;
064            LOG.error(message);
065            throw new RuntimeException/*SAXException*/(message);
066        }
067        try {
068            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
069            DocumentBuilder builder = factory.newDocumentBuilder();
070            DOMImplementation domImpl = builder.getDOMImplementation();
071            DOMImplementationLS dils = (DOMImplementationLS) domImpl;
072            LSInput input = dils.createLSInput();
073            input.setByteStream(is);
074            return input;
075        } catch (Exception e) {
076            throw new RuntimeException(e);
077        }
078    }
079}