1 25 26 package org.objectweb.easybeans.util.xml; 27 28 29 import java.io.IOException ; 30 import java.net.URL ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 34 import org.xml.sax.EntityResolver ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 38 42 public class SchemaEntityResolver implements EntityResolver { 43 44 47 private Map <String , String > schemasUrls = null; 48 49 53 public SchemaEntityResolver(final String [] schemas) { 54 schemasUrls = new HashMap <String , String >(); 55 URL url = null; 56 for (int i = 0; i < schemas.length; i++) { 57 url = SchemaEntityResolver.class.getResource("/" + schemas[i]); 58 if (url == null) { 59 throw new IllegalStateException ("'" + schemas[i] + "' was not found in the current classloader !"); 60 } 61 String urlString = url.toString(); 62 String id = urlString.substring(urlString.lastIndexOf('/') + 1); 63 schemasUrls.put(id, urlString); 64 65 } 66 } 67 68 83 public InputSource resolveEntity(final String publicId, final String systemId) throws IOException , SAXException { 84 85 String localPath = null; 86 87 if (systemId != null) { 88 if (systemId.toLowerCase().endsWith(".xsd")) { 90 String baseName = systemId.substring(systemId.lastIndexOf('/') + 1); 92 93 localPath = schemasUrls.get(baseName); 95 } 96 } 97 98 if (localPath == null) { 100 throw new SAXException ("No XSD found for '" + systemId + "'."); 101 } 102 103 return (new InputSource (localPath)); 105 } 106 107 } 108 | Popular Tags |