1 /** 2 * Copyright 2005-2012 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.IOException; 19 import java.io.InputStream; 20 21 import org.xml.sax.EntityResolver; 22 import org.xml.sax.InputSource; 23 import org.xml.sax.SAXException; 24 25 /** 26 * Internal notification EntityResolver which resolves system ids with the "resource:" prefix to ClassLoader resources 27 * @author Kuali Rice Team (rice.collab@kuali.org) 28 */ 29 public class ClassLoaderEntityResolver extends ClassLoaderResourceResolver implements EntityResolver { 30 /** 31 * Constructs a ClassLoaderEntityResolver.java. 32 */ 33 public ClassLoaderEntityResolver() { 34 super(); 35 } 36 37 /** 38 * Constructs a ClassLoaderEntityResolver.java. 39 * @param base 40 * @param prefix 41 */ 42 public ClassLoaderEntityResolver(String base, String prefix) { 43 super(base, prefix); 44 } 45 46 /** 47 * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String) 48 */ 49 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { 50 LOG.debug("Resolving '" + publicId + "' / '" + systemId + "'"); 51 String path = resolveSystemId(systemId); 52 if (path == null) { 53 LOG.error("Unable to resolve system id '" + systemId + "' locally...delegating to default resolution strategy."); 54 return null; 55 } 56 LOG.debug("Looking up resource '" + path + "' for entity '" + systemId + "'"); 57 InputStream is = getClass().getClassLoader().getResourceAsStream(path); 58 if (is == null) { 59 String message = "Unable to find schema (" + path + ") for: " + systemId; 60 LOG.warn(message); 61 // necessary if processContents is lax, because lax doesn't care...if it doesn't resolve it won't validate 62 // (not quite clear, as lax could be interpreted as *if the namespace is valid*, treating a present, but invalid 63 // namespace as a fatal error. instead, apparently a present but invalid namespace is ignored with 'lax' 64 // so w should use strict to ensure that is an error instead of throwing an exception here gratuitously 65 // which will screw up compound resolution 66 //throw new SAXException(message); 67 return null; 68 } 69 return new InputSource(is); 70 } 71 72 /** 73 * @see java.lang.Object#toString() 74 */ 75 public String toString() { 76 return "[ClassLoaderEntityResolver: base=" + base + ", prefix=" + prefix + "]"; 77 } 78 }