KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > xml > util > LocalEntityResolver


1 package org.jbpm.bpel.xml.util;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.util.Hashtable JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.xml.sax.EntityResolver JavaDoc;
12 import org.xml.sax.InputSource JavaDoc;
13 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc {
21   
22   private static final Log log = LogFactory.getLog(LocalEntityResolver.class);
23   
24   private static Hashtable JavaDoc entityRegistry = new Hashtable JavaDoc();
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   /**
39    * Register the mapping of an entity's public id to its local file name.
40    * @param publicId the public ID of the entity
41    * @param fileName the entity local file name
42    */

43   public static void registerEntity(String JavaDoc publicId, String JavaDoc fileName) {
44       entityRegistry.put(publicId, fileName);
45   }
46          
47   /**
48    * Returns entity inputSource.
49    *
50    * @param publicId the public ID of the entity
51    * @param systemId the system ID of the entity
52    * @return InputSource of entity
53   */

54   public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
55       if (publicId == null && systemId == null) return null;
56       InputSource JavaDoc inputSource = null;
57       String JavaDoc entityFileName = getLocalEntityName(publicId, systemId);
58       if (entityFileName != null) {
59           try {
60               URL JavaDoc url = LocalEntityResolver.class.getResource(entityFileName);
61               InputStream JavaDoc 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 JavaDoc e) {
67                       log.debug("Failed to open url stream", e);
68                   }
69               }
70               if (inputStream != null) {
71                 inputSource = new InputSource JavaDoc(inputStream);
72               }
73           } catch (Exception JavaDoc e) {
74               log.error("Cannot load local entity: " + entityFileName);
75           }
76       }
77       return inputSource;
78   }
79    
80   /**
81    * Get the local entity name by looking it up in the local registry.
82    * The publicID is used first. If not found, the entity is searched with
83    * the systemID.
84    * @param publicId the public ID of the entity, might be null
85    * @param systemId the system ID of the entity, might be null
86    * @return the local filename
87   */

88   private String JavaDoc getLocalEntityName(String JavaDoc publicId, String JavaDoc systemId) {
89       String JavaDoc filename = null;
90       if (publicId != null) filename = (String JavaDoc)entityRegistry.get(publicId);
91       if (filename == null && systemId != null) filename = (String JavaDoc)entityRegistry.get(systemId);
92       if (filename == null && systemId != null) {
93           try {
94               URL JavaDoc url = new URL JavaDoc(systemId);
95               String JavaDoc path = url.getPath();
96               int slash = path.lastIndexOf('/');
97             filename = path.substring(slash + 1);
98         } catch (MalformedURLException JavaDoc 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