1 3 56 57 package org.jboss.util.xml.catalog.readers; 58 59 import java.util.Hashtable ; 60 import java.io.IOException ; 61 import java.io.FileNotFoundException ; 62 import java.io.InputStream ; 63 import java.net.URL ; 64 import java.net.URLConnection ; 65 import java.net.MalformedURLException ; 66 import java.net.UnknownHostException ; 67 68 import javax.xml.parsers.ParserConfigurationException ; 69 import javax.xml.parsers.SAXParserFactory ; 70 import javax.xml.parsers.SAXParser ; 71 72 import org.xml.sax.AttributeList ; 73 import org.xml.sax.Attributes ; 74 import org.xml.sax.ContentHandler ; 75 import org.xml.sax.DocumentHandler ; 76 import org.xml.sax.EntityResolver ; 77 import org.xml.sax.InputSource ; 78 import org.xml.sax.Locator ; 79 import org.xml.sax.Parser ; 80 import org.xml.sax.SAXException ; 81 82 import org.jboss.util.xml.catalog.Catalog; 83 import org.jboss.util.xml.catalog.CatalogException; 84 import org.jboss.util.xml.catalog.CatalogManager; 85 import org.jboss.util.xml.catalog.helpers.Debug; 86 import org.jboss.util.xml.catalog.readers.CatalogReader; 87 88 116 public class SAXCatalogReader implements CatalogReader, ContentHandler , DocumentHandler { 117 118 protected SAXParserFactory parserFactory = null; 119 120 121 protected String parserClass = null; 122 123 130 protected Hashtable namespaceMap = new Hashtable (); 131 132 133 private SAXCatalogParser saxParser = null; 134 135 138 private boolean abandonHope = false; 139 140 141 private Catalog catalog; 142 143 145 public void setParserFactory(SAXParserFactory parserFactory) { 146 this.parserFactory = parserFactory; 147 } 148 149 151 public void setParserClass(String parserClass) { 152 this.parserClass = parserClass; 153 } 154 155 156 public SAXParserFactory getParserFactory() { 157 return parserFactory; 158 } 159 160 161 public String getParserClass() { 162 return parserClass; 163 } 164 165 174 protected Debug debug = CatalogManager.getStaticManager().debug; 175 176 177 public SAXCatalogReader() { 178 parserFactory = null; 179 parserClass = null; 180 } 181 182 183 public SAXCatalogReader(SAXParserFactory parserFactory) { 184 this.parserFactory = parserFactory; 185 } 186 187 188 public SAXCatalogReader(String parserClass) { 189 this.parserClass = parserClass; 190 } 191 192 195 public void setCatalogParser(String namespaceURI, 196 String rootElement, 197 String parserClass) { 198 if (namespaceURI == null) { 199 namespaceMap.put(rootElement, parserClass); 200 } else { 201 namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass); 202 } 203 } 204 205 208 public String getCatalogParser(String namespaceURI, 209 String rootElement) { 210 if (namespaceURI == null) { 211 return (String ) namespaceMap.get(rootElement); 212 } else { 213 return (String ) namespaceMap.get("{"+namespaceURI+"}"+rootElement); 214 } 215 } 216 217 226 public void readCatalog(Catalog catalog, String fileUrl) 227 throws MalformedURLException , IOException , 228 CatalogException { 229 230 URL url = null; 231 232 try { 233 url = new URL (fileUrl); 234 } catch (MalformedURLException e) { 235 url = new URL ("file:///" + fileUrl); 236 } 237 238 debug = catalog.getCatalogManager().debug; 239 240 try { 241 URLConnection urlCon = url.openConnection(); 242 readCatalog(catalog, urlCon.getInputStream()); 243 } catch (FileNotFoundException e) { 244 catalog.getCatalogManager().debug.message(1, "Failed to load catalog, file not found", 245 url.toString()); 246 } 247 } 248 249 259 public void readCatalog(Catalog catalog, InputStream is) 260 throws IOException , CatalogException { 261 262 if (parserFactory == null && parserClass == null) { 264 debug.message(1, "Cannot read SAX catalog without a parser"); 265 throw new CatalogException(CatalogException.UNPARSEABLE); 266 } 267 268 debug = catalog.getCatalogManager().debug; 269 EntityResolver bResolver = catalog.getCatalogManager().getBootstrapResolver(); 270 271 this.catalog = catalog; 272 273 try { 274 if (parserFactory != null) { 275 SAXParser parser = parserFactory.newSAXParser(); 276 SAXParserHandler spHandler = new SAXParserHandler(); 277 spHandler.setContentHandler(this); 278 if (bResolver != null) { 279 spHandler.setEntityResolver(bResolver); 280 } 281 parser.parse(new InputSource (is), spHandler); 282 } else { 283 Parser parser = (Parser ) Class.forName(parserClass).newInstance(); 284 parser.setDocumentHandler(this); 285 if (bResolver != null) { 286 parser.setEntityResolver(bResolver); 287 } 288 parser.parse(new InputSource (is)); 289 } 290 } catch (ClassNotFoundException cnfe) { 291 throw new CatalogException(CatalogException.UNPARSEABLE); 292 } catch (IllegalAccessException iae) { 293 throw new CatalogException(CatalogException.UNPARSEABLE); 294 } catch (InstantiationException ie) { 295 throw new CatalogException(CatalogException.UNPARSEABLE); 296 } catch (ParserConfigurationException pce) { 297 throw new CatalogException(CatalogException.UNKNOWN_FORMAT); 298 } catch (SAXException se) { 299 Exception e = se.getException(); 300 UnknownHostException uhe = new UnknownHostException (); 302 FileNotFoundException fnfe = new FileNotFoundException (); 303 if (e != null) { 304 if (e.getClass() == uhe.getClass()) { 305 throw new CatalogException(CatalogException.PARSE_FAILED, 306 e.toString()); 307 } else if (e.getClass() == fnfe.getClass()) { 308 throw new CatalogException(CatalogException.PARSE_FAILED, 309 e.toString()); 310 } 311 } 312 throw new CatalogException(se); 313 } 314 } 315 316 319 320 public void setDocumentLocator (Locator locator) { 321 if (saxParser != null) { 322 saxParser.setDocumentLocator(locator); 323 } 324 } 325 326 327 public void startDocument () throws SAXException { 328 saxParser = null; 329 abandonHope = false; 330 return; 331 } 332 333 334 public void endDocument ()throws SAXException { 335 if (saxParser != null) { 336 saxParser.endDocument(); 337 } 338 } 339 340 346 public void startElement (String name, 347 AttributeList atts) 348 throws SAXException { 349 350 if (abandonHope) { 351 return; 352 } 353 354 if (saxParser == null) { 355 String prefix = ""; 356 if (name.indexOf(':') > 0) { 357 prefix = name.substring(0, name.indexOf(':')); 358 } 359 360 String localName = name; 361 if (localName.indexOf(':') > 0) { 362 localName = localName.substring(localName.indexOf(':')+1); 363 } 364 365 String namespaceURI = null; 366 if (prefix.equals("")) { 367 namespaceURI = atts.getValue("xmlns"); 368 } else { 369 namespaceURI = atts.getValue("xmlns:" + prefix); 370 } 371 372 String saxParserClass = getCatalogParser(namespaceURI, 373 localName); 374 375 if (saxParserClass == null) { 376 abandonHope = true; 377 if (namespaceURI == null) { 378 debug.message(2, "No Catalog parser for " + name); 379 } else { 380 debug.message(2, "No Catalog parser for " 381 + "{" + namespaceURI + "}" 382 + name); 383 } 384 return; 385 } 386 387 try { 388 saxParser = (SAXCatalogParser) 389 Class.forName(saxParserClass).newInstance(); 390 391 saxParser.setCatalog(catalog); 392 saxParser.startDocument(); 393 saxParser.startElement(name, atts); 394 } catch (ClassNotFoundException cnfe) { 395 saxParser = null; 396 abandonHope = true; 397 debug.message(2, cnfe.toString()); 398 } catch (InstantiationException ie) { 399 saxParser = null; 400 abandonHope = true; 401 debug.message(2, ie.toString()); 402 } catch (IllegalAccessException iae) { 403 saxParser = null; 404 abandonHope = true; 405 debug.message(2, iae.toString()); 406 } catch (ClassCastException cce ) { 407 saxParser = null; 408 abandonHope = true; 409 debug.message(2, cce.toString()); 410 } 411 } else { 412 saxParser.startElement(name, atts); 413 } 414 } 415 416 422 public void startElement (String namespaceURI, 423 String localName, 424 String qName, 425 Attributes atts) 426 throws SAXException { 427 428 if (abandonHope) { 429 return; 430 } 431 432 if (saxParser == null) { 433 String saxParserClass = getCatalogParser(namespaceURI, 434 localName); 435 436 if (saxParserClass == null) { 437 abandonHope = true; 438 if (namespaceURI == null) { 439 debug.message(2, "No Catalog parser for " + localName); 440 } else { 441 debug.message(2, "No Catalog parser for " 442 + "{" + namespaceURI + "}" 443 + localName); 444 } 445 return; 446 } 447 448 try { 449 saxParser = (SAXCatalogParser) 450 Class.forName(saxParserClass).newInstance(); 451 452 saxParser.setCatalog(catalog); 453 saxParser.startDocument(); 454 saxParser.startElement(namespaceURI, localName, qName, atts); 455 } catch (ClassNotFoundException cnfe) { 456 saxParser = null; 457 abandonHope = true; 458 debug.message(2, cnfe.toString()); 459 } catch (InstantiationException ie) { 460 saxParser = null; 461 abandonHope = true; 462 debug.message(2, ie.toString()); 463 } catch (IllegalAccessException iae) { 464 saxParser = null; 465 abandonHope = true; 466 debug.message(2, iae.toString()); 467 } catch (ClassCastException cce ) { 468 saxParser = null; 469 abandonHope = true; 470 debug.message(2, cce.toString()); 471 } 472 } else { 473 saxParser.startElement(namespaceURI, localName, qName, atts); 474 } 475 } 476 477 478 public void endElement (String name) throws SAXException { 479 if (saxParser != null) { 480 saxParser.endElement(name); 481 } 482 } 483 484 485 public void endElement (String namespaceURI, 486 String localName, 487 String qName) throws SAXException { 488 if (saxParser != null) { 489 saxParser.endElement(namespaceURI, localName, qName); 490 } 491 } 492 493 494 public void characters (char ch[], int start, int length) 495 throws SAXException { 496 if (saxParser != null) { 497 saxParser.characters(ch, start, length); 498 } 499 } 500 501 502 public void ignorableWhitespace (char ch[], int start, int length) 503 throws SAXException { 504 if (saxParser != null) { 505 saxParser.ignorableWhitespace(ch, start, length); 506 } 507 } 508 509 510 public void processingInstruction (String target, String data) 511 throws SAXException { 512 if (saxParser != null) { 513 saxParser.processingInstruction(target, data); 514 } 515 } 516 517 518 public void startPrefixMapping (String prefix, String uri) 519 throws SAXException { 520 if (saxParser != null) { 521 saxParser.startPrefixMapping (prefix, uri); 522 } 523 } 524 525 526 public void endPrefixMapping (String prefix) 527 throws SAXException { 528 if (saxParser != null) { 529 saxParser.endPrefixMapping (prefix); 530 } 531 } 532 533 534 public void skippedEntity (String name) 535 throws SAXException { 536 if (saxParser != null) { 537 saxParser.skippedEntity(name); 538 } 539 } 540 } 541 | Popular Tags |