JEntityResolver.java

00001 
00027 package org.objectweb.jonas_lib.deployment.digester;
00028 
00029 
00030 import java.io.IOException;
00031 import java.util.HashMap;
00032 import java.util.Iterator;
00033 import java.util.List;
00034 import java.util.Map;
00035 
00036 import org.xml.sax.EntityResolver;
00037 import org.xml.sax.InputSource;
00038 import org.xml.sax.SAXException;
00039 
00040 import org.objectweb.jonas_lib.deployment.api.DTDs;
00041 import org.objectweb.jonas_lib.deployment.api.Schemas;
00042 
00048 public class JEntityResolver implements EntityResolver {
00049 
00053     private HashMap entries = new HashMap();
00054 
00058     private JDigester jDigester = null;
00059 
00060 
00066     public JEntityResolver(JDigester jd) {
00067         super();
00068         jDigester = jd;
00069     }
00070 
00074     public JEntityResolver() {
00075         super();
00076     }
00077 
00082     private void addSchema(String path) {
00083         String id = path.substring(path.lastIndexOf('/') + 1);
00084         entries.put(id, path);
00085     }
00086 
00087 
00088 
00101     public InputSource resolveEntity(String publicId, String systemId)
00102         throws IOException, SAXException {
00103 
00104         // If we have a JDigester instance
00105         if (jDigester != null) {
00106             jDigester.setPublicId(publicId);
00107         }
00108 
00109         // DTD : check if we got this entry
00110         String localPath = null;
00111         if (publicId != null) {
00112             localPath = (String) entries.get(publicId);
00113         } else if (systemId != null) {
00114             // Can be a schema
00115             if (systemId.toLowerCase().endsWith(".xsd")) {
00116                 // Retrieve basename
00117                 String baseName = systemId.substring(systemId.lastIndexOf('/') + 1);
00118 
00119                 // Registred ?
00120                 localPath = (String) entries.get(baseName);
00121             }
00122         }
00123 
00124         if (localPath == null) {
00125             // This DTD/Schema was not added to this DTD/Schame local repository
00126             return null;
00127         }
00128 
00129         // Return the local path source
00130         return (new InputSource(localPath));
00131     }
00132 
00133 
00138     public void addDtds(DTDs dtds) {
00139         if (dtds != null) {
00140             Map dtdsMapping = dtds.getMapping();
00141             if (dtdsMapping != null) {
00142                 for (Iterator it = dtdsMapping.entrySet().iterator(); it.hasNext();) {
00143                     Map.Entry entry = (Map.Entry) it.next();
00144                     String publicId = (String) entry.getKey();
00145                     String localURL = (String) entry.getValue();
00146                     entries.put(publicId, localURL);
00147                 }
00148             }
00149         }
00150     }
00151 
00152 
00157     public void addSchemas(Schemas schemas) {
00158         // no schemas
00159         if (schemas == null) {
00160             return;
00161         }
00162 
00163         List localSchemas = schemas.getlocalSchemas();
00164 
00165         // there are schemas
00166         if (localSchemas != null) {
00167             for (Iterator it = localSchemas.iterator(); it.hasNext();) {
00168                String schema = (String) it.next();
00169                addSchema(schema);
00170             }
00171         }
00172     }
00173 
00174 
00175 
00176 }
00177 

Generated on Tue Feb 15 15:05:50 2005 for JOnAS by  doxygen 1.3.9.1