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.IOException;
019import java.io.InputStream;
020
021import org.xml.sax.EntityResolver;
022import org.xml.sax.InputSource;
023import org.xml.sax.SAXException;
024
025/**
026 * Internal notification EntityResolver which resolves system ids with the "resource:" prefix to ClassLoader resources
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029public class ClassLoaderEntityResolver extends ClassLoaderResourceResolver implements EntityResolver {
030    /**
031     * Constructs a ClassLoaderEntityResolver.java.
032     */
033    public ClassLoaderEntityResolver() {
034        super();
035    }
036
037    /**
038     * Constructs a ClassLoaderEntityResolver.java.
039     * @param base
040     * @param prefix
041     */
042    public ClassLoaderEntityResolver(String base, String prefix) {
043        super(base, prefix);
044    }
045    
046    /**
047     * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
048     */
049    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
050        LOG.debug("Resolving '" + publicId + "' / '" + systemId + "'");
051        String path = resolveSystemId(systemId);
052        if (path == null) {
053            LOG.error("Unable to resolve system id '" + systemId + "' locally...delegating to default resolution strategy.");
054            return null;
055        }
056        LOG.debug("Looking up resource '" + path + "' for entity '" + systemId + "'");
057        InputStream is = getClass().getClassLoader().getResourceAsStream(path);
058        if (is == null) {
059            String message = "Unable to find schema (" + path + ") for: " + systemId;
060            LOG.warn(message);
061            // necessary if processContents is lax, because lax doesn't care...if it doesn't resolve it won't validate
062            // (not quite clear, as lax could be interpreted as *if the namespace is valid*, treating a present, but invalid
063            // namespace as a fatal error. instead, apparently a present but invalid namespace is ignored with 'lax'
064            // so w should use strict to ensure that is an error instead of throwing an exception here gratuitously
065            // which will screw up compound resolution
066            //throw new SAXException(message);
067            return null;
068        }
069        return new InputSource(is);
070    }
071
072    /**
073     * @see java.lang.Object#toString()
074     */
075    public String toString() {
076        return "[ClassLoaderEntityResolver: base=" + base + ", prefix=" + prefix + "]";
077    }
078}