001: package hero.xpdl;
002:
003: import java.io.IOException;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: import org.xml.sax.EntityResolver;
010: import org.xml.sax.InputSource;
011: import org.xml.sax.SAXException;
012:
013: /**
014: * This class defines the entity resolver used to resolve DTDs/Schemas during
015: * the xml parsing
016: * @author Florent Benoit
017: */
018: public class JEntityResolver implements EntityResolver {
019:
020: /**
021: * Map which contains DTDs and Schemas available in local
022: */
023: private HashMap entries = new HashMap();
024:
025: /**
026: * Constructor without JDigester instance (used in WsGen only)
027: */
028: public JEntityResolver() {
029: super ();
030: }
031:
032: /**
033: * Add a local schema to the known entries
034: * @param path path to a local schema
035: */
036: private void addSchema(String path) {
037: String id = path.substring(path.lastIndexOf('/') + 1);
038: entries.put(id, path);
039: }
040:
041: /**
042: * The Parser will call this method before opening any external entity except
043: * the top-level document entity.
044: * @param publicId The public identifier of the external entity being referenced,
045: * or null if none was supplied.
046: * @param systemId The system identifier of the external entity being referenced.
047: * @return An InputSource object describing the new input source, or null to request that
048: * the parser open a regular URI connection to the system identifier.
049: * @throws SAXException Any SAX exception, possibly wrapping another exception.
050: * @throws IOException A Java-specific IO exception, possibly the result of creating
051: * a new InputStream or Reader for the InputSource.
052: */
053: public InputSource resolveEntity(String publicId, String systemId)
054: throws IOException, SAXException {
055:
056: // DTD : check if we got this entry
057: String localPath = null;
058: if (publicId != null) {
059: localPath = (String) entries.get(publicId);
060: } else if (systemId != null) {
061: // Can be a schema
062: if (systemId.toLowerCase().endsWith(".xsd")) {
063: // Retrieve basename
064: String baseName = systemId.substring(systemId
065: .lastIndexOf('/') + 1);
066:
067: // Registred ?
068: System.out.println("LOCALPATH: "
069: + (String) entries.get(baseName));
070: localPath = (String) entries.get(baseName);
071: }
072: }
073:
074: if (localPath == null) {
075: // This DTD/Schema was not added to this DTD/Schame local repository
076: return null;
077: }
078:
079: // Return the local path source
080: return (new InputSource(localPath));
081: }
082:
083: /**
084: * Add the mapping between a public Id and the local path of the DTD
085: * @param dtds Object containing the mapping PublicId --> Local URL
086: */
087: public void addDtds(DTDs dtds) {
088: if (dtds != null) {
089: Map dtdsMapping = dtds.getMapping();
090: if (dtdsMapping != null) {
091: for (Iterator it = dtdsMapping.entrySet().iterator(); it
092: .hasNext();) {
093: Map.Entry entry = (Map.Entry) it.next();
094: String publicId = (String) entry.getKey();
095: String localURL = (String) entry.getValue();
096: entries.put(publicId, localURL);
097: }
098: }
099: }
100: }
101:
102: /**
103: * Add the given Schemas to the schemas available for this resolver
104: * @param schemas Definition of the schemas to add to this resolver
105: */
106: public void addSchemas(Schemas schemas) {
107: // no schemas
108: if (schemas == null) {
109: return;
110: }
111:
112: List localSchemas = schemas.getlocalSchemas();
113:
114: // there are schemas
115: if (localSchemas != null) {
116: for (Iterator it = localSchemas.iterator(); it.hasNext();) {
117: String schema = (String) it.next();
118: addSchema(schema);
119: }
120: }
121: }
122:
123: }
|