1 16 17 package org.apache.xerces.parsers; 18 19 import java.io.IOException ; 20 21 import org.apache.xerces.impl.Constants; 22 import org.apache.xerces.util.EntityResolverWrapper; 23 import org.apache.xerces.util.EntityResolver2Wrapper; 24 import org.apache.xerces.util.ErrorHandlerWrapper; 25 import org.apache.xerces.util.SAXMessageFormatter; 26 import org.apache.xerces.util.SymbolTable; 27 import org.apache.xerces.xni.XNIException; 28 import org.apache.xerces.xni.grammars.XMLGrammarPool; 29 import org.apache.xerces.xni.parser.XMLConfigurationException; 30 import org.apache.xerces.xni.parser.XMLEntityResolver; 31 import org.apache.xerces.xni.parser.XMLErrorHandler; 32 import org.apache.xerces.xni.parser.XMLInputSource; 33 import org.apache.xerces.xni.parser.XMLParseException; 34 import org.apache.xerces.xni.parser.XMLParserConfiguration; 35 import org.w3c.dom.Node ; 36 import org.xml.sax.EntityResolver ; 37 import org.xml.sax.ErrorHandler ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.SAXNotRecognizedException ; 41 import org.xml.sax.SAXNotSupportedException ; 42 import org.xml.sax.SAXParseException ; 43 import org.xml.sax.ext.EntityResolver2 ; 44 import org.xml.sax.helpers.LocatorImpl ; 45 46 56 public class DOMParser 57 extends AbstractDOMParser { 58 59 63 65 66 protected static final String USE_ENTITY_RESOLVER2 = 67 Constants.SAX_FEATURE_PREFIX + Constants.USE_ENTITY_RESOLVER2_FEATURE; 68 69 71 72 protected static final String SYMBOL_TABLE = 73 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 74 75 76 protected static final String XMLGRAMMAR_POOL = 77 Constants.XERCES_PROPERTY_PREFIX+Constants.XMLGRAMMAR_POOL_PROPERTY; 78 79 80 private static final String [] RECOGNIZED_PROPERTIES = { 81 SYMBOL_TABLE, 82 XMLGRAMMAR_POOL, 83 }; 84 85 89 91 92 protected boolean fUseEntityResolver2 = true; 93 94 98 101 public DOMParser(XMLParserConfiguration config) { 102 super(config); 103 } 105 108 public DOMParser() { 109 this(null, null); 110 } 112 115 public DOMParser(SymbolTable symbolTable) { 116 this(symbolTable, null); 117 } 119 120 124 public DOMParser(SymbolTable symbolTable, XMLGrammarPool grammarPool) { 125 super((XMLParserConfiguration)ObjectFactory.createObject( 126 "org.apache.xerces.xni.parser.XMLParserConfiguration", 127 "org.apache.xerces.parsers.XIncludeAwareParserConfiguration" 128 )); 129 130 fConfiguration.addRecognizedProperties(RECOGNIZED_PROPERTIES); 132 if (symbolTable != null) { 133 fConfiguration.setProperty(SYMBOL_TABLE, symbolTable); 134 } 135 if (grammarPool != null) { 136 fConfiguration.setProperty(XMLGRAMMAR_POOL, grammarPool); 137 } 138 139 } 141 145 158 public void parse(String systemId) throws SAXException , IOException { 159 160 XMLInputSource source = new XMLInputSource(null, systemId, null); 162 try { 163 parse(source); 164 } 165 166 catch (XMLParseException e) { 168 Exception ex = e.getException(); 169 if (ex == null) { 170 LocatorImpl locatorImpl = new LocatorImpl (); 173 locatorImpl.setPublicId(e.getPublicId()); 174 locatorImpl.setSystemId(e.getExpandedSystemId()); 175 locatorImpl.setLineNumber(e.getLineNumber()); 176 locatorImpl.setColumnNumber(e.getColumnNumber()); 177 throw new SAXParseException (e.getMessage(), locatorImpl); 178 } 179 if (ex instanceof SAXException ) { 180 throw (SAXException )ex; 182 } 183 if (ex instanceof IOException ) { 184 throw (IOException )ex; 185 } 186 throw new SAXException (ex); 187 } 188 catch (XNIException e) { 189 e.printStackTrace(); 190 Exception ex = e.getException(); 191 if (ex == null) { 192 throw new SAXException (e.getMessage()); 193 } 194 if (ex instanceof SAXException ) { 195 throw (SAXException )ex; 196 } 197 if (ex instanceof IOException ) { 198 throw (IOException )ex; 199 } 200 throw new SAXException (ex); 201 } 202 203 } 205 213 public void parse(InputSource inputSource) 214 throws SAXException , IOException { 215 216 try { 218 XMLInputSource xmlInputSource = 219 new XMLInputSource(inputSource.getPublicId(), 220 inputSource.getSystemId(), 221 null); 222 xmlInputSource.setByteStream(inputSource.getByteStream()); 223 xmlInputSource.setCharacterStream(inputSource.getCharacterStream()); 224 xmlInputSource.setEncoding(inputSource.getEncoding()); 225 parse(xmlInputSource); 226 } 227 228 catch (XMLParseException e) { 230 Exception ex = e.getException(); 231 if (ex == null) { 232 LocatorImpl locatorImpl = new LocatorImpl (); 235 locatorImpl.setPublicId(e.getPublicId()); 236 locatorImpl.setSystemId(e.getExpandedSystemId()); 237 locatorImpl.setLineNumber(e.getLineNumber()); 238 locatorImpl.setColumnNumber(e.getColumnNumber()); 239 throw new SAXParseException (e.getMessage(), locatorImpl); 240 } 241 if (ex instanceof SAXException ) { 242 throw (SAXException )ex; 244 } 245 if (ex instanceof IOException ) { 246 throw (IOException )ex; 247 } 248 throw new SAXException (ex); 249 } 250 catch (XNIException e) { 251 Exception ex = e.getException(); 252 if (ex == null) { 253 throw new SAXException (e.getMessage()); 254 } 255 if (ex instanceof SAXException ) { 256 throw (SAXException )ex; 257 } 258 if (ex instanceof IOException ) { 259 throw (IOException )ex; 260 } 261 throw new SAXException (ex); 262 } 263 264 } 266 273 public void setEntityResolver(EntityResolver resolver) { 274 275 try { 276 XMLEntityResolver xer = (XMLEntityResolver) fConfiguration.getProperty(ENTITY_RESOLVER); 277 if (fUseEntityResolver2 && resolver instanceof EntityResolver2 ) { 278 if (xer instanceof EntityResolver2Wrapper) { 279 EntityResolver2Wrapper er2w = (EntityResolver2Wrapper) xer; 280 er2w.setEntityResolver((EntityResolver2 ) resolver); 281 } 282 else { 283 fConfiguration.setProperty(ENTITY_RESOLVER, 284 new EntityResolver2Wrapper((EntityResolver2 ) resolver)); 285 } 286 } 287 else { 288 if (xer instanceof EntityResolverWrapper) { 289 EntityResolverWrapper erw = (EntityResolverWrapper) xer; 290 erw.setEntityResolver(resolver); 291 } 292 else { 293 fConfiguration.setProperty(ENTITY_RESOLVER, 294 new EntityResolverWrapper(resolver)); 295 } 296 } 297 } 298 catch (XMLConfigurationException e) { 299 } 301 302 } 304 311 public EntityResolver getEntityResolver() { 312 313 EntityResolver entityResolver = null; 314 try { 315 XMLEntityResolver xmlEntityResolver = 316 (XMLEntityResolver)fConfiguration.getProperty(ENTITY_RESOLVER); 317 if (xmlEntityResolver != null) { 318 if (xmlEntityResolver instanceof EntityResolverWrapper) { 319 entityResolver = 320 ((EntityResolverWrapper) xmlEntityResolver).getEntityResolver(); 321 } 322 else if (xmlEntityResolver instanceof EntityResolver2Wrapper) { 323 entityResolver = 324 ((EntityResolver2Wrapper) xmlEntityResolver).getEntityResolver(); 325 } 326 } 327 } 328 catch (XMLConfigurationException e) { 329 } 331 return entityResolver; 332 333 } 335 353 public void setErrorHandler(ErrorHandler errorHandler) { 354 355 try { 356 XMLErrorHandler xeh = (XMLErrorHandler) fConfiguration.getProperty(ERROR_HANDLER); 357 if (xeh instanceof ErrorHandlerWrapper) { 358 ErrorHandlerWrapper ehw = (ErrorHandlerWrapper) xeh; 359 ehw.setErrorHandler(errorHandler); 360 } 361 else { 362 fConfiguration.setProperty(ERROR_HANDLER, 363 new ErrorHandlerWrapper(errorHandler)); 364 } 365 } 366 catch (XMLConfigurationException e) { 367 } 369 370 } 372 379 public ErrorHandler getErrorHandler() { 380 381 ErrorHandler errorHandler = null; 382 try { 383 XMLErrorHandler xmlErrorHandler = 384 (XMLErrorHandler)fConfiguration.getProperty(ERROR_HANDLER); 385 if (xmlErrorHandler != null && 386 xmlErrorHandler instanceof ErrorHandlerWrapper) { 387 errorHandler = ((ErrorHandlerWrapper)xmlErrorHandler).getErrorHandler(); 388 } 389 } 390 catch (XMLConfigurationException e) { 391 } 393 return errorHandler; 394 395 } 397 411 public void setFeature(String featureId, boolean state) 412 throws SAXNotRecognizedException , SAXNotSupportedException { 413 414 try { 415 416 if (featureId.equals(USE_ENTITY_RESOLVER2)) { 421 if (state != fUseEntityResolver2) { 422 fUseEntityResolver2 = state; 423 setEntityResolver(getEntityResolver()); 425 } 426 return; 427 } 428 429 433 fConfiguration.setFeature(featureId, state); 434 } 435 catch (XMLConfigurationException e) { 436 String identifier = e.getIdentifier(); 437 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 438 throw new SAXNotRecognizedException ( 439 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 440 "feature-not-recognized", new Object [] {identifier})); 441 } 442 else { 443 throw new SAXNotSupportedException ( 444 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 445 "feature-not-supported", new Object [] {identifier})); 446 } 447 } 448 449 } 451 465 public boolean getFeature(String featureId) 466 throws SAXNotRecognizedException , SAXNotSupportedException { 467 468 try { 469 470 if (featureId.equals(USE_ENTITY_RESOLVER2)) { 475 return fUseEntityResolver2; 476 } 477 478 482 return fConfiguration.getFeature(featureId); 483 } 484 catch (XMLConfigurationException e) { 485 String identifier = e.getIdentifier(); 486 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 487 throw new SAXNotRecognizedException ( 488 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 489 "feature-not-recognized", new Object [] {identifier})); 490 } 491 else { 492 throw new SAXNotSupportedException ( 493 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 494 "feature-not-supported", new Object [] {identifier})); 495 } 496 } 497 498 } 500 515 public void setProperty(String propertyId, Object value) 516 throws SAXNotRecognizedException , SAXNotSupportedException { 517 518 try { 519 fConfiguration.setProperty(propertyId, value); 520 } 521 catch (XMLConfigurationException e) { 522 String identifier = e.getIdentifier(); 523 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 524 throw new SAXNotRecognizedException ( 525 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 526 "property-not-recognized", new Object [] {identifier})); 527 } 528 else { 529 throw new SAXNotSupportedException ( 530 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 531 "property-not-supported", new Object [] {identifier})); 532 } 533 } 534 535 } 537 551 public Object getProperty(String propertyId) 552 throws SAXNotRecognizedException , SAXNotSupportedException { 553 554 if (propertyId.equals(CURRENT_ELEMENT_NODE)) { 555 boolean deferred = false; 556 try { 557 deferred = getFeature(DEFER_NODE_EXPANSION); 558 } 559 catch (XMLConfigurationException e){ 560 } 562 if (deferred) { 563 throw new SAXNotSupportedException ("Current element node cannot be queried when node expansion is deferred."); 564 } 565 return (fCurrentNode!=null && 566 fCurrentNode.getNodeType() == Node.ELEMENT_NODE)? fCurrentNode:null; 567 } 568 569 try { 570 return fConfiguration.getProperty(propertyId); 571 } 572 catch (XMLConfigurationException e) { 573 String identifier = e.getIdentifier(); 574 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 575 throw new SAXNotRecognizedException ( 576 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 577 "property-not-recognized", new Object [] {identifier})); 578 } 579 else { 580 throw new SAXNotSupportedException ( 581 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 582 "property-not-supported", new Object [] {identifier})); 583 } 584 } 585 586 } 588 591 public XMLParserConfiguration getXMLParserConfiguration() { 592 return fConfiguration; 593 } 595 } | Popular Tags |