1 3 19 20 package com.sun.org.apache.xml.internal.resolver.readers; 21 22 import java.util.Hashtable ; 23 import java.io.IOException ; 24 import java.io.FileNotFoundException ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 import java.net.MalformedURLException ; 29 import java.net.UnknownHostException ; 30 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.parsers.SAXParserFactory ; 33 import javax.xml.parsers.SAXParser ; 34 35 import org.xml.sax.AttributeList ; 36 import org.xml.sax.Attributes ; 37 import org.xml.sax.ContentHandler ; 38 import org.xml.sax.DocumentHandler ; 39 import org.xml.sax.EntityResolver ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.Locator ; 42 import org.xml.sax.Parser ; 43 import org.xml.sax.SAXException ; 44 45 import com.sun.org.apache.xml.internal.resolver.Catalog; 46 import com.sun.org.apache.xml.internal.resolver.CatalogManager; 47 import com.sun.org.apache.xml.internal.resolver.CatalogException; 48 import com.sun.org.apache.xml.internal.resolver.readers.CatalogReader; 49 import com.sun.org.apache.xml.internal.resolver.helpers.Debug; 50 51 79 public class SAXCatalogReader implements CatalogReader, ContentHandler , DocumentHandler { 80 81 protected SAXParserFactory parserFactory = null; 82 83 84 protected String parserClass = null; 85 86 93 protected Hashtable namespaceMap = new Hashtable (); 94 95 96 private SAXCatalogParser saxParser = null; 97 98 101 private boolean abandonHope = false; 102 103 104 private Catalog catalog; 105 106 108 public void setParserFactory(SAXParserFactory parserFactory) { 109 this.parserFactory = parserFactory; 110 } 111 112 114 public void setParserClass(String parserClass) { 115 this.parserClass = parserClass; 116 } 117 118 119 public SAXParserFactory getParserFactory() { 120 return parserFactory; 121 } 122 123 124 public String getParserClass() { 125 return parserClass; 126 } 127 128 137 protected Debug debug = CatalogManager.getStaticManager().debug; 138 139 140 public SAXCatalogReader() { 141 parserFactory = null; 142 parserClass = null; 143 } 144 145 146 public SAXCatalogReader(SAXParserFactory parserFactory) { 147 this.parserFactory = parserFactory; 148 } 149 150 151 public SAXCatalogReader(String parserClass) { 152 this.parserClass = parserClass; 153 } 154 155 158 public void setCatalogParser(String namespaceURI, 159 String rootElement, 160 String parserClass) { 161 if (namespaceURI == null) { 162 namespaceMap.put(rootElement, parserClass); 163 } else { 164 namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass); 165 } 166 } 167 168 171 public String getCatalogParser(String namespaceURI, 172 String rootElement) { 173 if (namespaceURI == null) { 174 return (String ) namespaceMap.get(rootElement); 175 } else { 176 return (String ) namespaceMap.get("{"+namespaceURI+"}"+rootElement); 177 } 178 } 179 180 189 public void readCatalog(Catalog catalog, String fileUrl) 190 throws MalformedURLException , IOException , 191 CatalogException { 192 193 URL url = null; 194 195 try { 196 url = new URL (fileUrl); 197 } catch (MalformedURLException e) { 198 url = new URL ("file:///" + fileUrl); 199 } 200 201 debug = catalog.getCatalogManager().debug; 202 203 try { 204 URLConnection urlCon = url.openConnection(); 205 readCatalog(catalog, urlCon.getInputStream()); 206 } catch (FileNotFoundException e) { 207 catalog.getCatalogManager().debug.message(1, "Failed to load catalog, file not found", 208 url.toString()); 209 } 210 } 211 212 222 public void readCatalog(Catalog catalog, InputStream is) 223 throws IOException , CatalogException { 224 225 if (parserFactory == null && parserClass == null) { 227 debug.message(1, "Cannot read SAX catalog without a parser"); 228 throw new CatalogException(CatalogException.UNPARSEABLE); 229 } 230 231 debug = catalog.getCatalogManager().debug; 232 EntityResolver bResolver = catalog.getCatalogManager().getBootstrapResolver(); 233 234 this.catalog = catalog; 235 236 try { 237 if (parserFactory != null) { 238 SAXParser parser = parserFactory.newSAXParser(); 239 SAXParserHandler spHandler = new SAXParserHandler(); 240 spHandler.setContentHandler(this); 241 if (bResolver != null) { 242 spHandler.setEntityResolver(bResolver); 243 } 244 parser.parse(new InputSource (is), spHandler); 245 } else { 246 Parser parser = (Parser ) Class.forName(parserClass).newInstance(); 247 parser.setDocumentHandler(this); 248 if (bResolver != null) { 249 parser.setEntityResolver(bResolver); 250 } 251 parser.parse(new InputSource (is)); 252 } 253 } catch (ClassNotFoundException cnfe) { 254 throw new CatalogException(CatalogException.UNPARSEABLE); 255 } catch (IllegalAccessException iae) { 256 throw new CatalogException(CatalogException.UNPARSEABLE); 257 } catch (InstantiationException ie) { 258 throw new CatalogException(CatalogException.UNPARSEABLE); 259 } catch (ParserConfigurationException pce) { 260 throw new CatalogException(CatalogException.UNKNOWN_FORMAT); 261 } catch (SAXException se) { 262 Exception e = se.getException(); 263 UnknownHostException uhe = new UnknownHostException (); 265 FileNotFoundException fnfe = new FileNotFoundException (); 266 if (e != null) { 267 if (e.getClass() == uhe.getClass()) { 268 throw new CatalogException(CatalogException.PARSE_FAILED, 269 e.toString()); 270 } else if (e.getClass() == fnfe.getClass()) { 271 throw new CatalogException(CatalogException.PARSE_FAILED, 272 e.toString()); 273 } 274 } 275 throw new CatalogException(se); 276 } 277 } 278 279 282 283 public void setDocumentLocator (Locator locator) { 284 if (saxParser != null) { 285 saxParser.setDocumentLocator(locator); 286 } 287 } 288 289 290 public void startDocument () throws SAXException { 291 saxParser = null; 292 abandonHope = false; 293 return; 294 } 295 296 297 public void endDocument ()throws SAXException { 298 if (saxParser != null) { 299 saxParser.endDocument(); 300 } 301 } 302 303 309 public void startElement (String name, 310 AttributeList atts) 311 throws SAXException { 312 313 if (abandonHope) { 314 return; 315 } 316 317 if (saxParser == null) { 318 String prefix = ""; 319 if (name.indexOf(':') > 0) { 320 prefix = name.substring(0, name.indexOf(':')); 321 } 322 323 String localName = name; 324 if (localName.indexOf(':') > 0) { 325 localName = localName.substring(localName.indexOf(':')+1); 326 } 327 328 String namespaceURI = null; 329 if (prefix.equals("")) { 330 namespaceURI = atts.getValue("xmlns"); 331 } else { 332 namespaceURI = atts.getValue("xmlns:" + prefix); 333 } 334 335 String saxParserClass = getCatalogParser(namespaceURI, 336 localName); 337 338 if (saxParserClass == null) { 339 abandonHope = true; 340 if (namespaceURI == null) { 341 debug.message(2, "No Catalog parser for " + name); 342 } else { 343 debug.message(2, "No Catalog parser for " 344 + "{" + namespaceURI + "}" 345 + name); 346 } 347 return; 348 } 349 350 try { 351 saxParser = (SAXCatalogParser) 352 Class.forName(saxParserClass).newInstance(); 353 354 saxParser.setCatalog(catalog); 355 saxParser.startDocument(); 356 saxParser.startElement(name, atts); 357 } catch (ClassNotFoundException cnfe) { 358 saxParser = null; 359 abandonHope = true; 360 debug.message(2, cnfe.toString()); 361 } catch (InstantiationException ie) { 362 saxParser = null; 363 abandonHope = true; 364 debug.message(2, ie.toString()); 365 } catch (IllegalAccessException iae) { 366 saxParser = null; 367 abandonHope = true; 368 debug.message(2, iae.toString()); 369 } catch (ClassCastException cce ) { 370 saxParser = null; 371 abandonHope = true; 372 debug.message(2, cce.toString()); 373 } 374 } else { 375 saxParser.startElement(name, atts); 376 } 377 } 378 379 385 public void startElement (String namespaceURI, 386 String localName, 387 String qName, 388 Attributes atts) 389 throws SAXException { 390 391 if (abandonHope) { 392 return; 393 } 394 395 if (saxParser == null) { 396 String saxParserClass = getCatalogParser(namespaceURI, 397 localName); 398 399 if (saxParserClass == null) { 400 abandonHope = true; 401 if (namespaceURI == null) { 402 debug.message(2, "No Catalog parser for " + localName); 403 } else { 404 debug.message(2, "No Catalog parser for " 405 + "{" + namespaceURI + "}" 406 + localName); 407 } 408 return; 409 } 410 411 try { 412 saxParser = (SAXCatalogParser) 413 Class.forName(saxParserClass).newInstance(); 414 415 saxParser.setCatalog(catalog); 416 saxParser.startDocument(); 417 saxParser.startElement(namespaceURI, localName, qName, atts); 418 } catch (ClassNotFoundException cnfe) { 419 saxParser = null; 420 abandonHope = true; 421 debug.message(2, cnfe.toString()); 422 } catch (InstantiationException ie) { 423 saxParser = null; 424 abandonHope = true; 425 debug.message(2, ie.toString()); 426 } catch (IllegalAccessException iae) { 427 saxParser = null; 428 abandonHope = true; 429 debug.message(2, iae.toString()); 430 } catch (ClassCastException cce ) { 431 saxParser = null; 432 abandonHope = true; 433 debug.message(2, cce.toString()); 434 } 435 } else { 436 saxParser.startElement(namespaceURI, localName, qName, atts); 437 } 438 } 439 440 441 public void endElement (String name) throws SAXException { 442 if (saxParser != null) { 443 saxParser.endElement(name); 444 } 445 } 446 447 448 public void endElement (String namespaceURI, 449 String localName, 450 String qName) throws SAXException { 451 if (saxParser != null) { 452 saxParser.endElement(namespaceURI, localName, qName); 453 } 454 } 455 456 457 public void characters (char ch[], int start, int length) 458 throws SAXException { 459 if (saxParser != null) { 460 saxParser.characters(ch, start, length); 461 } 462 } 463 464 465 public void ignorableWhitespace (char ch[], int start, int length) 466 throws SAXException { 467 if (saxParser != null) { 468 saxParser.ignorableWhitespace(ch, start, length); 469 } 470 } 471 472 473 public void processingInstruction (String target, String data) 474 throws SAXException { 475 if (saxParser != null) { 476 saxParser.processingInstruction(target, data); 477 } 478 } 479 480 481 public void startPrefixMapping (String prefix, String uri) 482 throws SAXException { 483 if (saxParser != null) { 484 saxParser.startPrefixMapping (prefix, uri); 485 } 486 } 487 488 489 public void endPrefixMapping (String prefix) 490 throws SAXException { 491 if (saxParser != null) { 492 saxParser.endPrefixMapping (prefix); 493 } 494 } 495 496 497 public void skippedEntity (String name) 498 throws SAXException { 499 if (saxParser != null) { 500 saxParser.skippedEntity(name); 501 } 502 } 503 } 504 | Popular Tags |