1 package org.jbpm.bpel.xml.util; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.net.MalformedURLException ; 6 import java.net.URL ; 7 import java.util.Hashtable ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.xml.sax.EntityResolver ; 12 import org.xml.sax.InputSource ; 13 import org.xml.sax.SAXException ; 14 15 import com.ibm.wsdl.Constants; 16 17 import org.jbpm.bpel.wsdl.xml.WsdlConstants; 18 import org.jbpm.bpel.xml.BpelConstants; 19 20 public class LocalEntityResolver implements EntityResolver { 21 22 private static final Log log = LogFactory.getLog(LocalEntityResolver.class); 23 24 private static Hashtable entityRegistry = new Hashtable (); 25 static { 26 registerEntity(BpelConstants.NS_VENDOR + "/bpel_definition_1_0.xsd", "bpel_definition_1_0.xsd"); 27 registerEntity(BpelConstants.NS_VENDOR + "/bpel_application_1_0.xsd", "bpel_application_1_0.xsd"); 28 registerEntity(Constants.NS_URI_WSDL, "wsdl.xsd"); 29 registerEntity("http://www.w3.org/2001/xml.xsd", "namespace.xsd"); 30 registerEntity(BpelConstants.NS_BPWS, "bpel_2_0.xsd"); 31 registerEntity(WsdlConstants.NS_PLNK, "plink_2_0.xsd"); 32 registerEntity(BpelConstants.NS_BPWS_1_1, "bpel_1_1.xsd"); 33 registerEntity(WsdlConstants.NS_PLNK_1_1, "plink_1_1.xsd"); 34 registerEntity("-//W3C//DTD XMLSCHEMA 200102//EN", "XMLSchema.dtd"); 35 registerEntity("datatypes", "datatypes.dtd"); 36 } 37 38 43 public static void registerEntity(String publicId, String fileName) { 44 entityRegistry.put(publicId, fileName); 45 } 46 47 54 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException { 55 if (publicId == null && systemId == null) return null; 56 InputSource inputSource = null; 57 String entityFileName = getLocalEntityName(publicId, systemId); 58 if (entityFileName != null) { 59 try { 60 URL url = LocalEntityResolver.class.getResource(entityFileName); 61 InputStream inputStream = null; 62 if (url != null) { 63 if (log.isTraceEnabled()) log.trace(entityFileName + " maps to URL: " + url); 64 try { 65 inputStream = url.openStream(); 66 } catch (IOException e) { 67 log.debug("Failed to open url stream", e); 68 } 69 } 70 if (inputStream != null) { 71 inputSource = new InputSource (inputStream); 72 } 73 } catch (Exception e) { 74 log.error("Cannot load local entity: " + entityFileName); 75 } 76 } 77 return inputSource; 78 } 79 80 88 private String getLocalEntityName(String publicId, String systemId) { 89 String filename = null; 90 if (publicId != null) filename = (String )entityRegistry.get(publicId); 91 if (filename == null && systemId != null) filename = (String )entityRegistry.get(systemId); 92 if (filename == null && systemId != null) { 93 try { 94 URL url = new URL (systemId); 95 String path = url.getPath(); 96 int slash = path.lastIndexOf('/'); 97 filename = path.substring(slash + 1); 98 } catch (MalformedURLException ignored) { 99 log.trace("SystemId is not a url: " + systemId, ignored); 100 return null; 101 } 102 } 103 if (!entityRegistry.values().contains(filename)) { 104 log.warn("Entity is not registered, publicId=" + publicId + " systemId=" + systemId); 105 } 106 return filename; 107 } 108 } | Popular Tags |