1 57 58 package com.sun.org.apache.xerces.internal.parsers; 59 60 import java.io.IOException ; 61 62 import com.sun.org.apache.xerces.internal.impl.Constants; 63 import com.sun.org.apache.xerces.internal.util.EntityResolverWrapper; 64 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper; 65 import com.sun.org.apache.xerces.internal.util.SymbolTable; 66 import com.sun.org.apache.xerces.internal.xni.XNIException; 67 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 68 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 69 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 70 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 71 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 72 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException; 73 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 74 import org.w3c.dom.Node ; 75 import org.xml.sax.EntityResolver ; 76 import org.xml.sax.ErrorHandler ; 77 import org.xml.sax.InputSource ; 78 import org.xml.sax.SAXException ; 79 import org.xml.sax.SAXNotRecognizedException ; 80 import org.xml.sax.SAXNotSupportedException ; 81 import org.xml.sax.SAXParseException ; 82 import org.xml.sax.helpers.LocatorImpl ; 83 import com.sun.org.apache.xerces.internal.util.EntityResolver2Wrapper; 84 import org.xml.sax.ext.EntityResolver2 ; 85 86 96 public class DOMParser 97 extends AbstractDOMParser { 98 99 103 105 106 protected static final String SYMBOL_TABLE = 107 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 108 109 110 protected static final String XMLGRAMMAR_POOL = 111 Constants.XERCES_PROPERTY_PREFIX+Constants.XMLGRAMMAR_POOL_PROPERTY; 112 113 114 private static final String [] RECOGNIZED_PROPERTIES = { 115 SYMBOL_TABLE, 116 XMLGRAMMAR_POOL, 117 }; 118 119 123 126 public DOMParser(XMLParserConfiguration config) { 127 super(config); 128 } 130 133 public DOMParser() { 134 this(null, null); 135 } 137 140 public DOMParser(SymbolTable symbolTable) { 141 this(symbolTable, null); 142 } 144 145 149 public DOMParser(SymbolTable symbolTable, XMLGrammarPool grammarPool) { 150 super((XMLParserConfiguration)ObjectFactory.createObject( 151 "com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration", 152 "com.sun.org.apache.xerces.internal.parsers.XIncludeParserConfiguration" 153 )); 154 155 fConfiguration.addRecognizedProperties(RECOGNIZED_PROPERTIES); 157 if (symbolTable != null) { 158 fConfiguration.setProperty(SYMBOL_TABLE, symbolTable); 159 } 160 if (grammarPool != null) { 161 fConfiguration.setProperty(XMLGRAMMAR_POOL, grammarPool); 162 } 163 164 } 166 170 183 public void parse(String systemId) throws SAXException , IOException { 184 185 XMLInputSource source = new XMLInputSource(null, systemId, null); 187 try { 188 parse(source); 189 } 190 191 catch (XMLParseException e) { 193 Exception ex = e.getException(); 194 if (ex == null) { 195 LocatorImpl locatorImpl = new LocatorImpl (); 198 locatorImpl.setPublicId(e.getPublicId()); 199 locatorImpl.setSystemId(e.getExpandedSystemId()); 200 locatorImpl.setLineNumber(e.getLineNumber()); 201 locatorImpl.setColumnNumber(e.getColumnNumber()); 202 throw new SAXParseException (e.getMessage(), locatorImpl); 203 } 204 if (ex instanceof SAXException ) { 205 throw (SAXException )ex; 207 } 208 if (ex instanceof IOException ) { 209 throw (IOException )ex; 210 } 211 throw new SAXException (ex); 212 } 213 catch (XNIException e) { 214 e.printStackTrace(); 215 Exception ex = e.getException(); 216 if (ex == null) { 217 throw new SAXException (e.getMessage()); 218 } 219 if (ex instanceof SAXException ) { 220 throw (SAXException )ex; 221 } 222 if (ex instanceof IOException ) { 223 throw (IOException )ex; 224 } 225 throw new SAXException (ex); 226 } 227 228 } 230 238 public void parse(InputSource inputSource) 239 throws SAXException , IOException { 240 241 try { 243 XMLInputSource xmlInputSource = 244 new XMLInputSource(inputSource.getPublicId(), 245 inputSource.getSystemId(), 246 null); 247 xmlInputSource.setByteStream(inputSource.getByteStream()); 248 xmlInputSource.setCharacterStream(inputSource.getCharacterStream()); 249 xmlInputSource.setEncoding(inputSource.getEncoding()); 250 parse(xmlInputSource); 251 } 252 253 catch (XMLParseException e) { 255 Exception ex = e.getException(); 256 if (ex == null) { 257 LocatorImpl locatorImpl = new LocatorImpl (); 260 locatorImpl.setPublicId(e.getPublicId()); 261 locatorImpl.setSystemId(e.getExpandedSystemId()); 262 locatorImpl.setLineNumber(e.getLineNumber()); 263 locatorImpl.setColumnNumber(e.getColumnNumber()); 264 throw new SAXParseException (e.getMessage(), locatorImpl); 265 } 266 if (ex instanceof SAXException ) { 267 throw (SAXException )ex; 269 } 270 if (ex instanceof IOException ) { 271 throw (IOException )ex; 272 } 273 throw new SAXException (ex); 274 } 275 catch (XNIException e) { 276 Exception ex = e.getException(); 277 if (ex == null) { 278 throw new SAXException (e.getMessage()); 279 } 280 if (ex instanceof SAXException ) { 281 throw (SAXException )ex; 282 } 283 if (ex instanceof IOException ) { 284 throw (IOException )ex; 285 } 286 throw new SAXException (ex); 287 } 288 289 } 291 298 public void setEntityResolver(EntityResolver resolver) { 299 300 try { 301 if(resolver instanceof EntityResolver2 ){ 302 fConfiguration.setProperty(ENTITY_RESOLVER, new EntityResolver2Wrapper((EntityResolver2 )resolver)); 303 }else{ 304 fConfiguration.setProperty(ENTITY_RESOLVER, new EntityResolverWrapper(resolver)); 305 } 306 } 307 catch (XMLConfigurationException e) { 308 } 310 311 } 313 320 public EntityResolver getEntityResolver() { 321 322 EntityResolver entityResolver = null; 323 try { 324 XMLEntityResolver xmlEntityResolver = 325 (XMLEntityResolver)fConfiguration.getProperty(ENTITY_RESOLVER); 326 if (xmlEntityResolver != null){ 327 if(xmlEntityResolver instanceof EntityResolverWrapper) { 328 entityResolver = ((EntityResolverWrapper)xmlEntityResolver).getEntityResolver(); 329 }else if(xmlEntityResolver instanceof EntityResolver2Wrapper){ 330 entityResolver = ((EntityResolver2Wrapper)xmlEntityResolver).getEntityResolver(); 331 } 332 } 333 }catch (XMLConfigurationException e) { 334 } 336 return entityResolver; 337 338 } 340 358 public void setErrorHandler(ErrorHandler errorHandler) { 359 360 try { 361 fConfiguration.setProperty(ERROR_HANDLER, 362 new ErrorHandlerWrapper(errorHandler)); 363 } 364 catch (XMLConfigurationException e) { 365 } 367 368 } 370 377 public ErrorHandler getErrorHandler() { 378 379 ErrorHandler errorHandler = null; 380 try { 381 XMLErrorHandler xmlErrorHandler = 382 (XMLErrorHandler)fConfiguration.getProperty(ERROR_HANDLER); 383 if (xmlErrorHandler != null && 384 xmlErrorHandler instanceof ErrorHandlerWrapper) { 385 errorHandler = ((ErrorHandlerWrapper)xmlErrorHandler).getErrorHandler(); 386 } 387 } 388 catch (XMLConfigurationException e) { 389 } 391 return errorHandler; 392 393 } 395 409 public void setFeature(String featureId, boolean state) 410 throws SAXNotRecognizedException , SAXNotSupportedException { 411 412 try { 413 fConfiguration.setFeature(featureId, state); 414 } 415 catch (XMLConfigurationException e) { 416 String message = e.getMessage(); 417 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 418 throw new SAXNotRecognizedException (message); 419 } 420 else { 421 throw new SAXNotSupportedException (message); 422 } 423 } 424 425 } 427 441 public boolean getFeature(String featureId) 442 throws SAXNotRecognizedException , SAXNotSupportedException { 443 444 try { 445 return fConfiguration.getFeature(featureId); 446 } 447 catch (XMLConfigurationException e) { 448 String message = e.getMessage(); 449 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 450 throw new SAXNotRecognizedException (message); 451 } 452 else { 453 throw new SAXNotSupportedException (message); 454 } 455 } 456 457 } 459 474 public void setProperty(String propertyId, Object value) 475 throws SAXNotRecognizedException , SAXNotSupportedException { 476 477 try { 478 fConfiguration.setProperty(propertyId, value); 479 } 480 catch (XMLConfigurationException e) { 481 String message = e.getMessage(); 482 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 483 throw new SAXNotRecognizedException (message); 484 } 485 else { 486 throw new SAXNotSupportedException (message); 487 } 488 } 489 490 } 492 506 public Object getProperty(String propertyId) 507 throws SAXNotRecognizedException , SAXNotSupportedException { 508 509 if (propertyId.equals(CURRENT_ELEMENT_NODE)) { 510 boolean deferred = false; 511 try { 512 deferred = getFeature(DEFER_NODE_EXPANSION); 513 } 514 catch (XMLConfigurationException e){ 515 } 517 if (deferred) { 518 throw new SAXNotSupportedException ("Current element node cannot be queried when node expansion is deferred."); 519 } 520 return (fCurrentNode!=null && 521 fCurrentNode.getNodeType() == Node.ELEMENT_NODE)? fCurrentNode:null; 522 } 523 524 try { 525 return fConfiguration.getProperty(propertyId); 526 } 527 catch (XMLConfigurationException e) { 528 String message = e.getMessage(); 529 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 530 throw new SAXNotRecognizedException (message); 531 } 532 else { 533 throw new SAXNotSupportedException (message); 534 } 535 } 536 537 } 539 } | Popular Tags |