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