1 16 17 package org.apache.xerces.jaxp; 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import javax.xml.XMLConstants ; 27 import javax.xml.validation.Schema ; 28 29 import org.apache.xerces.impl.Constants; 30 import org.apache.xerces.impl.validation.ValidationManager; 31 import org.apache.xerces.impl.xs.XMLSchemaValidator; 32 import org.apache.xerces.impl.xs.XSMessageFormatter; 33 import org.apache.xerces.jaxp.validation.XSGrammarPoolContainer; 34 import org.apache.xerces.util.SAXMessageFormatter; 35 import org.apache.xerces.util.SecurityManager; 36 import org.apache.xerces.xni.XMLDocumentHandler; 37 import org.apache.xerces.xni.parser.XMLComponent; 38 import org.apache.xerces.xni.parser.XMLComponentManager; 39 import org.apache.xerces.xni.parser.XMLConfigurationException; 40 import org.apache.xerces.xni.parser.XMLDTDFilter; 41 import org.apache.xerces.xni.parser.XMLDocumentSource; 42 import org.apache.xerces.xni.parser.XMLParserConfiguration; 43 import org.apache.xerces.xs.AttributePSVI; 44 import org.apache.xerces.xs.ElementPSVI; 45 import org.apache.xerces.xs.PSVIProvider; 46 import org.xml.sax.EntityResolver ; 47 import org.xml.sax.ErrorHandler ; 48 import org.xml.sax.InputSource ; 49 import org.xml.sax.Parser ; 50 import org.xml.sax.SAXException ; 51 import org.xml.sax.SAXNotRecognizedException ; 52 import org.xml.sax.SAXNotSupportedException ; 53 import org.xml.sax.XMLReader ; 54 55 64 public class SAXParserImpl extends javax.xml.parsers.SAXParser 65 implements JAXPConstants, PSVIProvider { 66 67 68 private static final String NAMESPACES_FEATURE = 69 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 70 71 72 private static final String NAMESPACE_PREFIXES_FEATURE = 73 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE; 74 75 76 private static final String VALIDATION_FEATURE = 77 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 78 79 80 private static final String XMLSCHEMA_VALIDATION_FEATURE = 81 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 82 83 84 private static final String XINCLUDE_FEATURE = 85 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FEATURE; 86 87 88 private static final String SECURITY_MANAGER = 89 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 90 91 private JAXPSAXParser xmlReader; 92 private String schemaLanguage = null; private final Schema grammar; 94 95 private XMLComponent fSchemaValidator; 96 private XMLComponentManager fSchemaValidatorComponentManager; 97 private ValidationManager fSchemaValidationManager; 98 99 100 private final ErrorHandler fInitErrorHandler; 101 102 103 private final EntityResolver fInitEntityResolver; 104 105 109 SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features) 110 throws SAXException { 111 this(spf, features, false); 112 } 113 114 118 SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features, boolean secureProcessing) 119 throws SAXException 120 { 121 xmlReader = new JAXPSAXParser(this); 123 124 xmlReader.setFeature0(NAMESPACES_FEATURE, spf.isNamespaceAware()); 128 129 xmlReader.setFeature0(NAMESPACE_PREFIXES_FEATURE, !spf.isNamespaceAware()); 133 134 if (spf.isXIncludeAware()) { 138 xmlReader.setFeature0(XINCLUDE_FEATURE, true); 139 } 140 141 if (secureProcessing) { 143 xmlReader.setProperty0(SECURITY_MANAGER, new SecurityManager ()); 144 } 145 146 setFeatures(features); 148 149 if (spf.isValidating()) { 153 fInitErrorHandler = new DefaultValidationErrorHandler(); 154 xmlReader.setErrorHandler(fInitErrorHandler); 155 } 156 else { 157 fInitErrorHandler = xmlReader.getErrorHandler(); 158 } 159 xmlReader.setFeature0(VALIDATION_FEATURE, spf.isValidating()); 160 161 this.grammar = spf.getSchema(); 163 if (grammar != null) { 164 XMLParserConfiguration config = xmlReader.getXMLParserConfiguration(); 165 XMLComponent validatorComponent = null; 166 167 if (grammar instanceof XSGrammarPoolContainer) { 168 validatorComponent = new XMLSchemaValidator(); 169 fSchemaValidationManager = new ValidationManager(); 170 XMLDTDFilter entityHandler = new UnparsedEntityHandler(fSchemaValidationManager); 171 config.setDTDHandler(entityHandler); 172 entityHandler.setDTDHandler(xmlReader); 173 xmlReader.setDTDSource(entityHandler); 174 fSchemaValidatorComponentManager = new SchemaValidatorConfiguration(config, 175 (XSGrammarPoolContainer) grammar, fSchemaValidationManager); 176 } 177 178 else { 179 validatorComponent = new JAXPValidatorComponent(grammar.newValidatorHandler()); 180 fSchemaValidatorComponentManager = config; 181 } 182 config.addRecognizedFeatures(validatorComponent.getRecognizedFeatures()); 183 config.addRecognizedProperties(validatorComponent.getRecognizedProperties()); 184 config.setDocumentHandler((XMLDocumentHandler) validatorComponent); 185 ((XMLDocumentSource)validatorComponent).setDocumentHandler(xmlReader); 186 xmlReader.setDocumentSource((XMLDocumentSource) validatorComponent); 187 fSchemaValidator = validatorComponent; 188 } 189 190 fInitEntityResolver = xmlReader.getEntityResolver(); 192 } 193 194 201 private void setFeatures(Hashtable features) 202 throws SAXNotSupportedException , SAXNotRecognizedException { 203 if (features != null) { 204 for (Enumeration e = features.keys(); e.hasMoreElements();) { 205 String feature = (String )e.nextElement(); 206 boolean value = ((Boolean )features.get(feature)).booleanValue(); 207 xmlReader.setFeature0(feature, value); 208 } 209 } 210 } 211 212 public Parser getParser() throws SAXException { 213 return (Parser ) xmlReader; 216 } 217 218 222 public XMLReader getXMLReader() { 223 return xmlReader; 224 } 225 226 public boolean isNamespaceAware() { 227 try { 228 return xmlReader.getFeature(NAMESPACES_FEATURE); 229 } 230 catch (SAXException x) { 231 throw new IllegalStateException (x.getMessage()); 232 } 233 } 234 235 public boolean isValidating() { 236 try { 237 return xmlReader.getFeature(VALIDATION_FEATURE); 238 } 239 catch (SAXException x) { 240 throw new IllegalStateException (x.getMessage()); 241 } 242 } 243 244 248 public boolean isXIncludeAware() { 249 try { 250 return xmlReader.getFeature(XINCLUDE_FEATURE); 251 } 252 catch (SAXException exc) { 253 return false; 254 } 255 } 256 257 261 public void setProperty(String name, Object value) 262 throws SAXNotRecognizedException , SAXNotSupportedException { 263 xmlReader.setProperty(name, value); 264 } 265 266 270 public Object getProperty(String name) 271 throws SAXNotRecognizedException , SAXNotSupportedException { 272 return xmlReader.getProperty(name); 273 } 274 275 public Schema getSchema() { 276 return grammar; 277 } 278 279 public void reset() { 280 try { 281 282 xmlReader.restoreInitState(); 283 } 284 catch (SAXException exc) { 285 } 289 290 xmlReader.setContentHandler(null); 291 xmlReader.setDTDHandler(null); 292 if (xmlReader.getErrorHandler() != fInitErrorHandler) { 293 xmlReader.setErrorHandler(fInitErrorHandler); 294 } 295 if (xmlReader.getEntityResolver() != fInitEntityResolver) { 296 xmlReader.setEntityResolver(fInitEntityResolver); 297 } 298 } 299 300 303 304 public ElementPSVI getElementPSVI() { 305 return ((PSVIProvider)xmlReader).getElementPSVI(); 306 } 307 308 public AttributePSVI getAttributePSVI(int index) { 309 return ((PSVIProvider)xmlReader).getAttributePSVI(index); 310 } 311 312 public AttributePSVI getAttributePSVIByName(String uri, String localname) { 313 return ((PSVIProvider)xmlReader).getAttributePSVIByName(uri, localname); 314 } 315 316 321 public static class JAXPSAXParser extends org.apache.xerces.parsers.SAXParser { 322 323 private HashMap fInitFeatures = new HashMap (); 324 private HashMap fInitProperties = new HashMap (); 325 private SAXParserImpl fSAXParser; 326 327 public JAXPSAXParser() { 328 super(); 329 } 330 331 JAXPSAXParser(SAXParserImpl saxParser) { 332 super(); 333 fSAXParser = saxParser; 334 } 335 336 341 public synchronized void setFeature(String name, boolean value) 342 throws SAXNotRecognizedException , SAXNotSupportedException { 343 if (name == null) { 344 throw new NullPointerException (); 346 } 347 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 348 try { 349 setProperty(SECURITY_MANAGER, value ? new SecurityManager () : null); 350 } 351 catch (SAXNotRecognizedException exc) { 352 if (value) { 355 throw exc; 356 } 357 } 358 catch (SAXNotSupportedException exc) { 359 if (value) { 362 throw exc; 363 } 364 } 365 return; 366 } 367 if (!fInitFeatures.containsKey(name)) { 368 boolean current = super.getFeature(name); 369 fInitFeatures.put(name, current ? Boolean.TRUE : Boolean.FALSE); 370 } 371 372 if (fSAXParser != null && fSAXParser.fSchemaValidator != null) { 373 setSchemaValidatorFeature(name, value); 374 } 375 super.setFeature(name, value); 376 } 377 378 public synchronized boolean getFeature(String name) 379 throws SAXNotRecognizedException , SAXNotSupportedException { 380 if (name == null) { 381 throw new NullPointerException (); 383 } 384 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 385 try { 386 return (super.getProperty(SECURITY_MANAGER) != null); 387 } 388 catch (SAXException exc) { 390 return false; 391 } 392 } 393 return super.getFeature(name); 394 } 395 396 401 public synchronized void setProperty(String name, Object value) 402 throws SAXNotRecognizedException , SAXNotSupportedException { 403 if (name == null) { 404 throw new NullPointerException (); 406 } 407 if (fSAXParser != null) { 408 if (JAXP_SCHEMA_LANGUAGE.equals(name)) { 410 if (fSAXParser.grammar != null) { 413 throw new SAXNotSupportedException ( 414 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "schema-already-specified", new Object [] {name})); 415 } 416 if ( W3C_XML_SCHEMA.equals(value) ) { 417 if( fSAXParser.isValidating() ) { 419 fSAXParser.schemaLanguage = W3C_XML_SCHEMA; 420 setFeature(XMLSCHEMA_VALIDATION_FEATURE, true); 421 if (!fInitProperties.containsKey(JAXP_SCHEMA_LANGUAGE)) { 424 fInitProperties.put(JAXP_SCHEMA_LANGUAGE, super.getProperty(JAXP_SCHEMA_LANGUAGE)); 425 } 426 super.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); 427 } 428 429 } 430 else if (value == null) { 431 fSAXParser.schemaLanguage = null; 432 setFeature(XMLSCHEMA_VALIDATION_FEATURE, false); 433 } 434 else { 435 throw new SAXNotSupportedException ( 439 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "schema-not-supported", null)); 440 } 441 return; 442 } 443 else if (JAXP_SCHEMA_SOURCE.equals(name)) { 444 if (fSAXParser.grammar != null) { 447 throw new SAXNotSupportedException ( 448 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "schema-already-specified", new Object [] {name})); 449 } 450 String val = (String )getProperty(JAXP_SCHEMA_LANGUAGE); 451 if ( val != null && W3C_XML_SCHEMA.equals(val) ) { 452 if (!fInitProperties.containsKey(JAXP_SCHEMA_SOURCE)) { 453 fInitProperties.put(JAXP_SCHEMA_SOURCE, super.getProperty(JAXP_SCHEMA_SOURCE)); 454 } 455 super.setProperty(name, value); 456 } 457 else { 458 throw new SAXNotSupportedException ( 459 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 460 "jaxp-order-not-supported", 461 new Object [] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE})); 462 } 463 return; 464 } 465 } 466 if (!fInitProperties.containsKey(name)) { 467 fInitProperties.put(name, super.getProperty(name)); 468 } 469 470 if (fSAXParser != null && fSAXParser.fSchemaValidator != null) { 471 setSchemaValidatorProperty(name, value); 472 } 473 super.setProperty(name, value); 474 } 475 476 public synchronized Object getProperty(String name) 477 throws SAXNotRecognizedException , SAXNotSupportedException { 478 if (name == null) { 479 throw new NullPointerException (); 481 } 482 if (fSAXParser != null && JAXP_SCHEMA_LANGUAGE.equals(name)) { 483 return fSAXParser.schemaLanguage; 485 } 486 return super.getProperty(name); 487 } 488 489 synchronized void restoreInitState() 490 throws SAXNotRecognizedException , SAXNotSupportedException { 491 Iterator iter; 492 if (!fInitFeatures.isEmpty()) { 493 iter = fInitFeatures.entrySet().iterator(); 494 while (iter.hasNext()) { 495 Map.Entry entry = (Map.Entry ) iter.next(); 496 String name = (String ) entry.getKey(); 497 boolean value = ((Boolean ) entry.getValue()).booleanValue(); 498 super.setFeature(name, value); 499 } 500 fInitFeatures.clear(); 501 } 502 if (!fInitProperties.isEmpty()) { 503 iter = fInitProperties.entrySet().iterator(); 504 while (iter.hasNext()) { 505 Map.Entry entry = (Map.Entry ) iter.next(); 506 String name = (String ) entry.getKey(); 507 Object value = entry.getValue(); 508 super.setProperty(name, value); 509 } 510 fInitProperties.clear(); 511 } 512 } 513 514 public void parse(InputSource inputSource) 515 throws SAXException , IOException { 516 if (fSAXParser != null && fSAXParser.fSchemaValidator != null) { 517 if (fSAXParser.fSchemaValidationManager != null) { 518 fSAXParser.fSchemaValidationManager.reset(); 519 } 520 resetSchemaValidator(); 521 } 522 super.parse(inputSource); 523 } 524 525 public void parse(String systemId) 526 throws SAXException , IOException { 527 if (fSAXParser != null && fSAXParser.fSchemaValidator != null) { 528 if (fSAXParser.fSchemaValidationManager != null) { 529 fSAXParser.fSchemaValidationManager.reset(); 530 } 531 resetSchemaValidator(); 532 } 533 super.parse(systemId); 534 } 535 536 XMLParserConfiguration getXMLParserConfiguration() { 537 return fConfiguration; 538 } 539 540 void setFeature0(String name, boolean value) 541 throws SAXNotRecognizedException , SAXNotSupportedException { 542 super.setFeature(name, value); 543 } 544 545 boolean getFeature0(String name) 546 throws SAXNotRecognizedException , SAXNotSupportedException { 547 return super.getFeature(name); 548 } 549 550 void setProperty0(String name, Object value) 551 throws SAXNotRecognizedException , SAXNotSupportedException { 552 super.setProperty(name, value); 553 } 554 555 Object getProperty0(String name) 556 throws SAXNotRecognizedException , SAXNotSupportedException { 557 return super.getProperty(name); 558 } 559 560 private void setSchemaValidatorFeature(String name, boolean value) 561 throws SAXNotRecognizedException , SAXNotSupportedException { 562 try { 563 fSAXParser.fSchemaValidator.setFeature(name, value); 564 } 565 catch (XMLConfigurationException e) { 567 String identifier = e.getIdentifier(); 568 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 569 throw new SAXNotRecognizedException ( 570 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 571 "feature-not-recognized", new Object [] {identifier})); 572 } 573 else { 574 throw new SAXNotSupportedException ( 575 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 576 "feature-not-supported", new Object [] {identifier})); 577 } 578 } 579 } 580 581 private void setSchemaValidatorProperty(String name, Object value) 582 throws SAXNotRecognizedException , SAXNotSupportedException { 583 try { 584 fSAXParser.fSchemaValidator.setProperty(name, value); 585 } 586 catch (XMLConfigurationException e) { 588 String identifier = e.getIdentifier(); 589 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 590 throw new SAXNotRecognizedException ( 591 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 592 "property-not-recognized", new Object [] {identifier})); 593 } 594 else { 595 throw new SAXNotSupportedException ( 596 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 597 "property-not-supported", new Object [] {identifier})); 598 } 599 } 600 } 601 602 private void resetSchemaValidator() throws SAXException { 603 try { 604 fSAXParser.fSchemaValidator.reset(fSAXParser.fSchemaValidatorComponentManager); 605 } 606 catch (XMLConfigurationException e) { 608 throw new SAXException (e); 609 } 610 } 611 } 612 } 613 | Popular Tags |