1 23 package org.objectweb.jorm.xml2mi.lib; 24 25 import org.apache.xerces.xni.parser.XMLInputSource; 26 import org.apache.xerces.xni.parser.XMLEntityResolver; 27 import org.apache.xerces.xni.XMLResourceIdentifier; 28 import org.apache.xerces.xni.XNIException; 29 import org.apache.tools.ant.types.DTDLocation; 30 import org.objectweb.util.monolog.api.Logger; 31 import org.objectweb.util.monolog.api.BasicLevel; 32 import org.objectweb.util.monolog.api.LoggerFactory; 33 import org.objectweb.jorm.util.api.Loggable; 34 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.FileInputStream ; 38 import java.io.FileNotFoundException ; 39 import java.util.Iterator ; 40 import java.util.ArrayList ; 41 import java.util.zip.ZipEntry ; 42 import java.util.jar.JarFile ; 43 import java.net.URL ; 44 45 48 public class DTDResolver implements XMLEntityResolver, Loggable { 49 50 private ArrayList dtdLocations = null; 51 private Logger logger = null; 52 53 public DTDResolver(ArrayList dtdLocations) { 54 this.dtdLocations = dtdLocations; 55 } 56 57 public DTDResolver(ArrayList dtdLocations, Logger logger) { 58 this.dtdLocations = dtdLocations; 59 this.logger = logger; 60 } 61 62 64 public XMLInputSource resolveEntity(XMLResourceIdentifier identifier) throws XNIException, IOException { 65 String publicid = identifier.getPublicId(); 66 String location = null; 67 if (publicid == null) { 68 location = identifier.getBaseSystemId(); 69 } else { 70 Iterator it = dtdLocations.iterator(); 71 logger.log(BasicLevel.DEBUG, "Looking for the publicId: " + publicid); 72 while (it.hasNext()) { 73 DTDLocation dtdloc = (DTDLocation) it.next(); 74 String current = dtdloc.getPublicId(); 75 logger.log(BasicLevel.DEBUG, "Current public id: " + current); 76 if (current.equals(publicid)) { 77 location = dtdloc.getLocation(); 78 break; 79 } 80 } 81 } 82 if (location == null) { 83 logger.log(BasicLevel.WARN, "No dtd location found for the publicId:" + publicid); 84 if (identifier.getBaseSystemId() == null) 85 throw new IOException ("Could not resolve publicid: [" + publicid + "]"); 86 logger.log(BasicLevel.WARN, "Resolved " + publicid + " to with default location " + identifier.getBaseSystemId()); 87 return new XMLInputSource(identifier); 88 } 89 90 InputStream is = null; 91 92 try { 94 is = new FileInputStream (location); 95 logger.log(BasicLevel.DEBUG, "Resolved " + publicid + " to local file " + location); 96 } catch (FileNotFoundException e) { 97 logger.log(BasicLevel.DEBUG, location + " is not a local file "); 98 } 99 100 if (is == null) { 101 URL urldtd = null; 102 try { 103 urldtd = new URL (location); 104 is = urldtd.openStream(); 105 logger.log(BasicLevel.DEBUG, "Resolved " + publicid + " to url " + urldtd); 106 } catch (IOException e) { 107 logger.log(BasicLevel.DEBUG, location + " is not an URL "); 108 } 109 } 110 111 if (is == null) { 113 is = getClass().getClassLoader().getResourceAsStream(location); 114 if (is != null) { 115 logger.log(BasicLevel.DEBUG, "Resolved " + publicid + " to resource " + location); 116 } else { 117 logger.log(BasicLevel.DEBUG, location + " is not availlable in the classpath"); 118 } 119 } 120 121 if (is == null) { 123 int idx = location.indexOf(".jar!"); 124 if (idx != -1) { 125 try { 126 JarFile jarfile = new JarFile (location.substring(0, idx)); 127 if (jarfile != null) { 129 ZipEntry zipentry = jarfile.getEntry( 130 location.substring(idx + 5, location.length())); 131 if (zipentry != null) { 134 is = jarfile.getInputStream(zipentry); 135 } 136 } 137 } catch (Exception e) { 138 logger.log(BasicLevel.DEBUG, location + " is not an jar file"); 139 } 140 } 141 } 142 143 if (is != null) 144 return new XMLInputSource(publicid, 145 identifier.getLiteralSystemId(), 146 identifier.getBaseSystemId(), 147 is, 148 null); 149 else 150 throw new IOException ("Could not resolve publicid: [" 151 + publicid + "] location [" + location + "]"); 152 } 153 154 public Logger getLogger() { 156 return logger; 157 } 158 159 public LoggerFactory getLoggerFactory() { 160 return null; 161 } 162 163 public void setLogger(Logger logger) { 164 this.logger = logger; 165 } 166 167 public void setLoggerFactory(LoggerFactory loggerfactory) { 168 } 169 } 170 | Popular Tags |