1 24 package org.objectweb.jorm.xml2mi.lib; 25 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.net.URL ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 import java.util.jar.JarFile ; 37 import java.util.zip.ZipEntry ; 38 39 import javax.xml.parsers.DocumentBuilder ; 40 import javax.xml.parsers.DocumentBuilderFactory ; 41 import javax.xml.parsers.ParserConfigurationException ; 42 43 import org.objectweb.util.monolog.api.BasicLevel; 44 import org.objectweb.util.monolog.api.Logger; 45 import org.w3c.dom.Document ; 46 import org.xml.sax.EntityResolver ; 47 import org.xml.sax.ErrorHandler ; 48 import org.xml.sax.InputSource ; 49 import org.xml.sax.SAXException ; 50 import org.xml.sax.SAXParseException ; 51 52 58 public class SAXParserHelper implements EntityResolver , ErrorHandler { 59 60 63 private Properties publicid2location = null; 64 65 68 private ClassLoader classLoader; 69 70 73 private List errors; 74 75 78 private DocumentBuilder parser; 79 80 private Logger logger; 81 82 88 public SAXParserHelper(Properties publicid2location, 89 Logger logger) throws SAXException { 90 this.publicid2location = publicid2location; 91 this.logger = logger; 92 this.classLoader = getClass().getClassLoader(); 93 if (classLoader == null) { 94 classLoader = ClassLoader.getSystemClassLoader(); 95 } 96 init(true); 97 } 98 99 106 public SAXParserHelper(Properties publicid2location, 107 Logger logger, 108 ClassLoader cl, 109 boolean dtdverify) throws SAXException { 110 this.publicid2location = publicid2location; 111 this.logger = logger; 112 this.classLoader = cl; 113 if (classLoader == null) { 114 classLoader = getClass().getClassLoader(); 115 if (classLoader == null) { 116 classLoader = ClassLoader.getSystemClassLoader(); 117 } 118 } 119 init(dtdverify); 120 } 121 122 134 public Document parse(String xmlFileName) throws SAXException { 135 InputStream is = null; 136 try { 137 is = new FileInputStream (xmlFileName); 138 } catch (FileNotFoundException e) { 139 logger.log(BasicLevel.DEBUG, xmlFileName + " is not a local file "); 140 } 141 142 if (is == null) { 143 URL urldtd = null; 144 try { 145 urldtd = new URL (xmlFileName); 146 is = urldtd.openStream(); 147 } catch (IOException e) { 148 logger.log(BasicLevel.DEBUG, xmlFileName + " is not an URL "); 149 } 150 } 151 152 if (is == null) { 154 is = classLoader.getResourceAsStream(xmlFileName); 155 if (is == null) { 156 logger.log(BasicLevel.DEBUG, xmlFileName + " is not availlable in the classpath"); 157 } 158 } 159 160 if (is == null) { 162 int idx = xmlFileName.indexOf(".jar!"); 163 if (idx != -1) { 164 try { 165 JarFile jarfile = new JarFile (xmlFileName.substring(0, idx)); 166 if (jarfile != null) { 168 ZipEntry zipentry = jarfile.getEntry( 169 xmlFileName.substring(idx + 5, xmlFileName.length())); 170 if (zipentry != null) { 173 is = jarfile.getInputStream(zipentry); 174 } 175 } 176 } catch (Exception e) { 177 logger.log(BasicLevel.DEBUG, xmlFileName + " is not in a jar file"); 178 } 179 } else { 180 logger.log(BasicLevel.DEBUG, xmlFileName + " is not in a jar file"); 181 } 182 } 183 184 if (is == null) { 185 throw new SAXException ("No XML file '" + xmlFileName + "' found"); 186 } else { 187 return parse(is); 188 } 189 190 } 191 192 199 public Document parse(InputStream is) throws SAXException { 200 if (errors != null) { 201 errors.clear(); 203 } 204 try { 206 Document d = parser.parse(is, "."); 207 if (d == null) { 208 throw new SAXException ("No document"); 209 } 210 return d; 211 } catch (IOException e) { 212 throw new SAXException (e); 213 } 214 } 215 216 221 private void init(boolean dtdverify)throws SAXException { 222 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 224 domFactory.setValidating(dtdverify); 225 try { 226 parser = domFactory.newDocumentBuilder(); 227 } catch (ParserConfigurationException e) { 228 throw new SAXException (e); 229 } 230 parser.setEntityResolver(this); 231 parser.setErrorHandler(this); 232 } 233 234 private List getErrorList() { 235 if (errors == null) { 236 errors = new ArrayList (); 237 } 238 return errors; 239 } 240 241 244 public void error(SAXParseException e) throws SAXException { 245 logger.log(BasicLevel.ERROR, e.getMessage(), e); 246 getErrorList().add(e); 247 } 248 public void fatalError(SAXParseException e) throws SAXException { 249 logger.log(BasicLevel.FATAL, e.getMessage(), e); 250 getErrorList().add(e); 251 } 252 public void warning(SAXParseException e) throws SAXException { 253 logger.log(BasicLevel.WARN, e.getMessage(), e); 254 getErrorList().add(e); 255 } 256 257 258 261 266 public InputSource resolveEntity (String publicId, String systemId) 267 throws SAXException { 268 String publicid = publicId; 269 String location = null; 270 if (publicid == null) { 271 location = systemId; 272 } else { 273 logger.log(BasicLevel.DEBUG, "Looking for the publicId: " + publicid); 274 for(Iterator it = publicid2location.entrySet().iterator(); it.hasNext();) { 275 Map.Entry me = (Map.Entry ) it.next(); 276 String current = (String ) me.getKey(); 277 logger.log(BasicLevel.DEBUG, "Current public id: " + current); 278 if (current.equals(publicid)) { 279 location = (String ) me.getValue(); 280 break; 281 } 282 } 283 } 284 if (location == null) { 285 logger.log(BasicLevel.INFO, 286 "No dtd location found for the publicId: " + publicid); 287 if (systemId == null) 288 throw new SAXException ("Could not resolve publicid: " + publicid); 289 logger.log(BasicLevel.INFO, "Try to resolve " + publicid 290 + " with the default location " + systemId); 291 location = systemId; 292 } 293 294 InputStream is = null; 295 296 try { 298 is = new FileInputStream (location); 299 logger.log(BasicLevel.DEBUG, "Resolved " + publicid 300 + " to local file " + location); 301 } catch (FileNotFoundException e) { 302 logger.log(BasicLevel.DEBUG, location + " is not a local file "); 303 } 304 305 if (is == null) { 306 URL urldtd = null; 307 try { 308 urldtd = new URL (location); 309 is = urldtd.openStream(); 310 logger.log(BasicLevel.DEBUG, "Resolved " + publicid 311 + " to url " + urldtd); 312 } catch (IOException e) { 313 logger.log(BasicLevel.DEBUG, location + " is not an URL "); 314 } 315 } 316 317 if (is == null) { 319 is = classLoader.getResourceAsStream(location); 320 if (is != null) { 321 logger.log(BasicLevel.DEBUG, "Resolved " + publicid 322 + " to resource " + location); 323 } else { 324 logger.log(BasicLevel.DEBUG, location 325 + " is not availlable in the classpath"); 326 } 327 } 328 329 if (is == null) { 331 int idx = location.indexOf(".jar!"); 332 if (idx != -1) { 333 try { 334 JarFile jarfile = new JarFile (location.substring(0, idx)); 335 if (jarfile != null) { 337 ZipEntry zipentry = jarfile.getEntry( 338 location.substring(idx + 5, location.length())); 339 if (zipentry != null) { 342 is = jarfile.getInputStream(zipentry); 343 } 344 } 345 } catch (Exception e) { 346 logger.log(BasicLevel.DEBUG, location + " is not an jar file"); 347 } 348 } 349 } 350 351 if (is != null) 352 return new InputSource (is); 353 else 354 throw new SAXException ("Could not resolve publicid: [" 355 + publicid + "] location [" + location + "]"); 356 } 357 } 358 | Popular Tags |