1 16 17 package org.apache.xerces.jaxp.validation; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.Reader ; 22 import java.io.StringReader ; 23 import java.util.HashMap ; 24 import java.util.Locale ; 25 26 import javax.xml.parsers.FactoryConfigurationError ; 27 import javax.xml.parsers.SAXParserFactory ; 28 import javax.xml.transform.Result ; 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.sax.SAXResult ; 31 import javax.xml.transform.sax.SAXSource ; 32 import javax.xml.validation.TypeInfoProvider ; 33 import javax.xml.validation.ValidatorHandler ; 34 35 import org.apache.xerces.impl.Constants; 36 import org.apache.xerces.impl.XMLEntityManager; 37 import org.apache.xerces.impl.XMLErrorReporter; 38 import org.apache.xerces.impl.dv.XSSimpleType; 39 import org.apache.xerces.impl.validation.EntityState; 40 import org.apache.xerces.impl.validation.ValidationManager; 41 import org.apache.xerces.impl.xs.XMLSchemaValidator; 42 import org.apache.xerces.util.AttributesProxy; 43 import org.apache.xerces.util.SAXLocatorWrapper; 44 import org.apache.xerces.util.SAXMessageFormatter; 45 import org.apache.xerces.util.SymbolTable; 46 import org.apache.xerces.util.URI; 47 import org.apache.xerces.util.XMLAttributesImpl; 48 import org.apache.xerces.util.XMLSymbols; 49 import org.apache.xerces.xni.Augmentations; 50 import org.apache.xerces.xni.NamespaceContext; 51 import org.apache.xerces.xni.QName; 52 import org.apache.xerces.xni.XMLAttributes; 53 import org.apache.xerces.xni.XMLDocumentHandler; 54 import org.apache.xerces.xni.XMLLocator; 55 import org.apache.xerces.xni.XMLResourceIdentifier; 56 import org.apache.xerces.xni.XMLString; 57 import org.apache.xerces.xni.XNIException; 58 import org.apache.xerces.xni.parser.XMLConfigurationException; 59 import org.apache.xerces.xni.parser.XMLDocumentSource; 60 import org.apache.xerces.xni.parser.XMLParseException; 61 import org.apache.xerces.xs.AttributePSVI; 62 import org.apache.xerces.xs.ElementPSVI; 63 import org.apache.xerces.xs.ItemPSVI; 64 import org.apache.xerces.xs.PSVIProvider; 65 import org.apache.xerces.xs.XSTypeDefinition; 66 import org.w3c.dom.TypeInfo ; 67 import org.w3c.dom.ls.LSInput ; 68 import org.w3c.dom.ls.LSResourceResolver ; 69 import org.xml.sax.Attributes ; 70 import org.xml.sax.ContentHandler ; 71 import org.xml.sax.DTDHandler ; 72 import org.xml.sax.ErrorHandler ; 73 import org.xml.sax.InputSource ; 74 import org.xml.sax.Locator ; 75 import org.xml.sax.SAXException ; 76 import org.xml.sax.SAXNotRecognizedException ; 77 import org.xml.sax.SAXNotSupportedException ; 78 import org.xml.sax.XMLReader ; 79 import org.xml.sax.ext.Attributes2 ; 80 import org.xml.sax.ext.EntityResolver2 ; 81 82 91 final class ValidatorHandlerImpl extends ValidatorHandler implements 92 DTDHandler , EntityState, PSVIProvider, ValidatorHelper, XMLDocumentHandler { 93 94 96 97 private static final String NAMESPACE_PREFIXES = 98 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE; 99 100 101 protected static final String STRING_INTERNING = 102 Constants.SAX_FEATURE_PREFIX + Constants.STRING_INTERNING_FEATURE; 103 104 106 107 private static final String ERROR_REPORTER = 108 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 109 110 111 private static final String NAMESPACE_CONTEXT = 112 Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_CONTEXT_PROPERTY; 113 114 115 private static final String SCHEMA_VALIDATOR = 116 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 117 118 119 private static final String SECURITY_MANAGER = 120 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 121 122 123 private static final String SYMBOL_TABLE = 124 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 125 126 127 private static final String VALIDATION_MANAGER = 128 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 129 130 134 135 private XMLErrorReporter fErrorReporter; 136 137 138 private NamespaceContext fNamespaceContext; 139 140 141 private XMLSchemaValidator fSchemaValidator; 142 143 144 private SymbolTable fSymbolTable; 145 146 147 private ValidationManager fValidationManager; 148 149 150 private XMLSchemaValidatorComponentManager fComponentManager; 151 152 153 private final SAXLocatorWrapper fSAXLocatorWrapper = new SAXLocatorWrapper(); 154 155 156 private boolean fNeedPushNSContext = true; 157 158 159 private HashMap fUnparsedEntities = null; 160 161 162 private boolean fStringsInternalized = false; 163 164 165 private final QName fElementQName = new QName(); 166 private final QName fAttributeQName = new QName(); 167 private final XMLAttributesImpl fAttributes = new XMLAttributesImpl(); 168 private final AttributesProxy fAttrAdapter = new AttributesProxy(fAttributes); 169 private final XMLString fTempString = new XMLString(); 170 171 175 private ContentHandler fContentHandler = null; 176 177 180 181 public ValidatorHandlerImpl(XSGrammarPoolContainer grammarContainer) { 182 this(new XMLSchemaValidatorComponentManager(grammarContainer)); 183 fComponentManager.addRecognizedFeatures(new String [] {NAMESPACE_PREFIXES}); 184 fComponentManager.setFeature(NAMESPACE_PREFIXES, false); 185 setErrorHandler(null); 186 setResourceResolver(null); 187 } 188 189 public ValidatorHandlerImpl(XMLSchemaValidatorComponentManager componentManager) { 190 fComponentManager = componentManager; 191 fErrorReporter = (XMLErrorReporter) fComponentManager.getProperty(ERROR_REPORTER); 192 fNamespaceContext = (NamespaceContext) fComponentManager.getProperty(NAMESPACE_CONTEXT); 193 fSchemaValidator = (XMLSchemaValidator) fComponentManager.getProperty(SCHEMA_VALIDATOR); 194 fSymbolTable = (SymbolTable) fComponentManager.getProperty(SYMBOL_TABLE); 195 fValidationManager = (ValidationManager) fComponentManager.getProperty(VALIDATION_MANAGER); 196 } 197 198 201 202 public void setContentHandler(ContentHandler receiver) { 203 fContentHandler = receiver; 204 } 205 206 public ContentHandler getContentHandler() { 207 return fContentHandler; 208 } 209 210 public void setErrorHandler(ErrorHandler errorHandler) { 211 fComponentManager.setErrorHandler(errorHandler); 212 } 213 214 public ErrorHandler getErrorHandler() { 215 return fComponentManager.getErrorHandler(); 216 } 217 218 public void setResourceResolver(LSResourceResolver resourceResolver) { 219 fComponentManager.setResourceResolver(resourceResolver); 220 } 221 222 public LSResourceResolver getResourceResolver() { 223 return fComponentManager.getResourceResolver(); 224 } 225 226 public TypeInfoProvider getTypeInfoProvider() { 227 return fTypeInfoProvider; 228 } 229 230 public boolean getFeature(String name) 231 throws SAXNotRecognizedException , SAXNotSupportedException { 232 if (name == null) { 233 throw new NullPointerException (); 234 } 235 try { 236 return fComponentManager.getFeature(name); 237 } 238 catch (XMLConfigurationException e) { 239 final String identifier = e.getIdentifier(); 240 final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? 241 "feature-not-recognized" : "feature-not-supported"; 242 throw new SAXNotRecognizedException ( 243 SAXMessageFormatter.formatMessage(Locale.getDefault(), 244 key, new Object [] {identifier})); 245 } 246 } 247 248 public void setFeature(String name, boolean value) 249 throws SAXNotRecognizedException , SAXNotSupportedException { 250 if (name == null) { 251 throw new NullPointerException (); 252 } 253 try { 254 fComponentManager.setFeature(name, value); 255 } 256 catch (XMLConfigurationException e) { 257 final String identifier = e.getIdentifier(); 258 final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? 259 "feature-not-recognized" : "feature-not-supported"; 260 throw new SAXNotRecognizedException ( 261 SAXMessageFormatter.formatMessage(Locale.getDefault(), 262 key, new Object [] {identifier})); 263 } 264 } 265 266 public Object getProperty(String name) 267 throws SAXNotRecognizedException , SAXNotSupportedException { 268 if (name == null) { 269 throw new NullPointerException (); 270 } 271 try { 272 return fComponentManager.getProperty(name); 273 } 274 catch (XMLConfigurationException e) { 275 final String identifier = e.getIdentifier(); 276 final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? 277 "property-not-recognized" : "property-not-supported"; 278 throw new SAXNotRecognizedException ( 279 SAXMessageFormatter.formatMessage(Locale.getDefault(), 280 key, new Object [] {identifier})); 281 } 282 } 283 284 public void setProperty(String name, Object object) 285 throws SAXNotRecognizedException , SAXNotSupportedException { 286 if (name == null) { 287 throw new NullPointerException (); 288 } 289 try { 290 fComponentManager.setProperty(name, object); 291 } 292 catch (XMLConfigurationException e) { 293 final String identifier = e.getIdentifier(); 294 final String key = e.getType() == XMLConfigurationException.NOT_RECOGNIZED ? 295 "property-not-recognized" : "property-not-supported"; 296 throw new SAXNotRecognizedException ( 297 SAXMessageFormatter.formatMessage(Locale.getDefault(), 298 key, new Object [] {identifier})); 299 } 300 } 301 302 305 306 public boolean isEntityDeclared(String name) { 307 return false; 308 } 309 310 public boolean isEntityUnparsed(String name) { 311 if (fUnparsedEntities != null) { 312 return fUnparsedEntities.containsKey(name); 313 } 314 return false; 315 } 316 317 320 321 public void startDocument(XMLLocator locator, String encoding, 322 NamespaceContext namespaceContext, Augmentations augs) 323 throws XNIException { 324 if (fContentHandler != null) { 325 try { 326 fContentHandler.startDocument(); 327 } 328 catch (SAXException e) { 329 throw new XNIException(e); 330 } 331 } 332 } 333 334 public void xmlDecl(String version, String encoding, String standalone, 335 Augmentations augs) throws XNIException {} 336 337 public void doctypeDecl(String rootElement, String publicId, 338 String systemId, Augmentations augs) throws XNIException {} 339 340 public void comment(XMLString text, Augmentations augs) throws XNIException {} 341 342 public void processingInstruction(String target, XMLString data, 343 Augmentations augs) throws XNIException { 344 if (fContentHandler != null) { 345 try { 346 fContentHandler.processingInstruction(target, data.toString()); 347 } 348 catch (SAXException e) { 349 throw new XNIException(e); 350 } 351 } 352 } 353 354 public void startElement(QName element, XMLAttributes attributes, 355 Augmentations augs) throws XNIException { 356 if (fContentHandler != null) { 357 try { 358 fTypeInfoProvider.beginStartElement(augs, attributes); 359 fContentHandler.startElement((element.uri != null) ? element.uri : XMLSymbols.EMPTY_STRING, 360 element.localpart, element.rawname, fAttrAdapter); 361 } 362 catch (SAXException e) { 363 throw new XNIException(e); 364 } 365 finally { 366 fTypeInfoProvider.finishStartElement(); 367 } 368 } 369 } 370 371 public void emptyElement(QName element, XMLAttributes attributes, 372 Augmentations augs) throws XNIException { 373 374 startElement(element, attributes, augs); 375 endElement(element, augs); 376 } 377 378 public void startGeneralEntity(String name, 379 XMLResourceIdentifier identifier, String encoding, 380 Augmentations augs) throws XNIException {} 381 382 public void textDecl(String version, String encoding, Augmentations augs) 383 throws XNIException {} 384 385 public void endGeneralEntity(String name, Augmentations augs) 386 throws XNIException {} 387 388 public void characters(XMLString text, Augmentations augs) 389 throws XNIException { 390 if (fContentHandler != null) { 391 if (text.length == 0) { 394 return; 395 } 396 try { 397 fContentHandler.characters(text.ch, text.offset, text.length); 398 } 399 catch (SAXException e) { 400 throw new XNIException(e); 401 } 402 } 403 } 404 405 public void ignorableWhitespace(XMLString text, Augmentations augs) 406 throws XNIException { 407 if (fContentHandler != null) { 408 try { 409 fContentHandler.ignorableWhitespace(text.ch, text.offset, text.length); 410 } 411 catch (SAXException e) { 412 throw new XNIException(e); 413 } 414 } 415 } 416 417 public void endElement(QName element, Augmentations augs) 418 throws XNIException { 419 if (fContentHandler != null) { 420 try { 421 fTypeInfoProvider.beginEndElement(augs); 422 fContentHandler.endElement((element.uri != null) ? element.uri : XMLSymbols.EMPTY_STRING, 423 element.localpart, element.rawname); 424 } 425 catch (SAXException e) { 426 throw new XNIException(e); 427 } 428 finally { 429 fTypeInfoProvider.finishEndElement(); 430 } 431 } 432 } 433 434 public void startCDATA(Augmentations augs) throws XNIException {} 435 436 public void endCDATA(Augmentations augs) throws XNIException {} 437 438 public void endDocument(Augmentations augs) throws XNIException { 439 if (fContentHandler != null) { 440 try { 441 fContentHandler.endDocument(); 442 } 443 catch (SAXException e) { 444 throw new XNIException(e); 445 } 446 } 447 } 448 449 public void setDocumentSource(XMLDocumentSource source) {} 451 452 public XMLDocumentSource getDocumentSource() { 453 return fSchemaValidator; 454 } 455 456 459 460 public void setDocumentLocator(Locator locator) { 461 fSAXLocatorWrapper.setLocator(locator); 462 if (fContentHandler != null) { 463 fContentHandler.setDocumentLocator(locator); 464 } 465 } 466 467 public void startDocument() throws SAXException { 468 fComponentManager.reset(); 469 fSchemaValidator.setDocumentHandler(this); 470 fValidationManager.setEntityState(this); 471 fTypeInfoProvider.finishStartElement(); fNeedPushNSContext = true; 473 if (fUnparsedEntities != null && !fUnparsedEntities.isEmpty()) { 474 fUnparsedEntities.clear(); 476 } 477 fErrorReporter.setDocumentLocator(fSAXLocatorWrapper); 478 try { 479 fSchemaValidator.startDocument(fSAXLocatorWrapper, fSAXLocatorWrapper.getEncoding(), fNamespaceContext, null); 480 } 481 catch (XMLParseException e) { 482 throw Util.toSAXParseException(e); 483 } 484 catch (XNIException e) { 485 throw Util.toSAXException(e); 486 } 487 } 488 489 public void endDocument() throws SAXException { 490 fSAXLocatorWrapper.setLocator(null); 491 try { 492 fSchemaValidator.endDocument(null); 493 } 494 catch (XMLParseException e) { 495 throw Util.toSAXParseException(e); 496 } 497 catch (XNIException e) { 498 throw Util.toSAXException(e); 499 } 500 } 501 502 public void startPrefixMapping(String prefix, String uri) 503 throws SAXException { 504 String prefixSymbol; 505 String uriSymbol; 506 if (!fStringsInternalized) { 507 prefixSymbol = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING; 508 uriSymbol = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null; 509 } 510 else { 511 prefixSymbol = (prefix != null) ? prefix : XMLSymbols.EMPTY_STRING; 512 uriSymbol = (uri != null && uri.length() > 0) ? uri : null; 513 } 514 if (fNeedPushNSContext) { 515 fNeedPushNSContext = false; 516 fNamespaceContext.pushContext(); 517 } 518 fNamespaceContext.declarePrefix(prefixSymbol, uriSymbol); 519 if (fContentHandler != null) { 520 fContentHandler.startPrefixMapping(prefix, uri); 521 } 522 } 523 524 public void endPrefixMapping(String prefix) throws SAXException { 525 if (fContentHandler != null) { 526 fContentHandler.endPrefixMapping(prefix); 527 } 528 } 529 530 public void startElement(String uri, String localName, String qName, 531 Attributes atts) throws SAXException { 532 if (fNeedPushNSContext) { 533 fNamespaceContext.pushContext(); 534 } 535 fNeedPushNSContext = true; 536 537 fillQName(fElementQName, uri, localName, qName); 539 540 if (atts instanceof Attributes2 ) { 542 fillXMLAttributes2((Attributes2 ) atts); 543 } 544 else { 545 fillXMLAttributes(atts); 546 } 547 548 try { 549 fSchemaValidator.startElement(fElementQName, fAttributes, null); 550 } 551 catch (XMLParseException e) { 552 throw Util.toSAXParseException(e); 553 } 554 catch (XNIException e) { 555 throw Util.toSAXException(e); 556 } 557 } 558 559 public void endElement(String uri, String localName, String qName) 560 throws SAXException { 561 fillQName(fElementQName, uri, localName, qName); 562 try { 563 fSchemaValidator.endElement(fElementQName, null); 564 } 565 catch (XMLParseException e) { 566 throw Util.toSAXParseException(e); 567 } 568 catch (XNIException e) { 569 throw Util.toSAXException(e); 570 } 571 finally { 572 fNamespaceContext.popContext(); 573 } 574 } 575 576 public void characters(char[] ch, int start, int length) 577 throws SAXException { 578 try { 579 fTempString.setValues(ch, start, length); 580 fSchemaValidator.characters(fTempString, null); 581 } 582 catch (XMLParseException e) { 583 throw Util.toSAXParseException(e); 584 } 585 catch (XNIException e) { 586 throw Util.toSAXException(e); 587 } 588 } 589 590 public void ignorableWhitespace(char[] ch, int start, int length) 591 throws SAXException { 592 try { 593 fTempString.setValues(ch, start, length); 594 fSchemaValidator.ignorableWhitespace(fTempString, null); 595 } 596 catch (XMLParseException e) { 597 throw Util.toSAXParseException(e); 598 } 599 catch (XNIException e) { 600 throw Util.toSAXException(e); 601 } 602 } 603 604 public void processingInstruction(String target, String data) 605 throws SAXException { 606 611 if (fContentHandler != null) { 612 fContentHandler.processingInstruction(target, data); 613 } 614 } 615 616 public void skippedEntity(String name) throws SAXException { 617 if (fContentHandler != null) { 620 fContentHandler.skippedEntity(name); 621 } 622 } 623 624 627 628 public void notationDecl(String name, String publicId, 629 String systemId) throws SAXException {} 630 631 public void unparsedEntityDecl(String name, String publicId, 632 String systemId, String notationName) throws SAXException { 633 if (fUnparsedEntities == null) { 634 fUnparsedEntities = new HashMap (); 635 } 636 fUnparsedEntities.put(name, name); 637 } 638 639 642 643 public void validate(Source source, Result result) 644 throws SAXException , IOException { 645 if (result instanceof SAXResult || result == null) { 646 final SAXSource saxSource = (SAXSource ) source; 647 final SAXResult saxResult = (SAXResult ) result; 648 649 if (result != null) { 650 setContentHandler(saxResult.getHandler()); 651 } 652 653 try { 654 XMLReader reader = saxSource.getXMLReader(); 655 if( reader==null ) { 656 SAXParserFactory spf = SAXParserFactory.newInstance(); 658 spf.setNamespaceAware(true); 659 try { 660 reader = spf.newSAXParser().getXMLReader(); 661 if (reader instanceof org.apache.xerces.parsers.SAXParser) { 663 SecurityManager securityManager = (SecurityManager ) fComponentManager.getProperty(SECURITY_MANAGER); 664 if (securityManager != null) { 665 try { 666 reader.setProperty(SECURITY_MANAGER, securityManager); 667 } 668 catch (SAXException exc) {} 670 } 671 } 672 } catch( Exception e ) { 673 throw new FactoryConfigurationError (e); 675 } 676 } 677 678 try { 681 fStringsInternalized = reader.getFeature(STRING_INTERNING); 682 } 683 catch (SAXException exc) { 684 fStringsInternalized = false; 687 } 688 689 ErrorHandler errorHandler = fComponentManager.getErrorHandler(); 690 reader.setErrorHandler(errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance()); 691 reader.setEntityResolver(fResolutionForwarder); 692 fResolutionForwarder.setEntityResolver(fComponentManager.getResourceResolver()); 693 reader.setContentHandler(this); 694 reader.setDTDHandler(this); 695 696 InputSource is = saxSource.getInputSource(); 697 reader.parse(is); 698 } 699 finally { 700 setContentHandler(null); 702 } 703 return; 704 } 705 throw new IllegalArgumentException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 706 "SourceResultMismatch", 707 new Object [] {source.getClass().getName(), result.getClass().getName()})); 708 } 709 710 713 714 public ElementPSVI getElementPSVI() { 715 return fTypeInfoProvider.getElementPSVI(); 716 } 717 718 public AttributePSVI getAttributePSVI(int index) { 719 return fTypeInfoProvider.getAttributePSVI(index); 720 } 721 722 public AttributePSVI getAttributePSVIByName(String uri, String localname) { 723 return fTypeInfoProvider.getAttributePSVIByName(uri, localname); 724 } 725 726 732 733 private void fillQName(QName toFill, String uri, String localpart, String raw) { 734 if (!fStringsInternalized) { 735 uri = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null; 736 localpart = (localpart != null) ? fSymbolTable.addSymbol(localpart) : XMLSymbols.EMPTY_STRING; 737 raw = (raw != null) ? fSymbolTable.addSymbol(raw) : XMLSymbols.EMPTY_STRING; 738 } 739 else { 740 if (uri != null && uri.length() == 0) { 741 uri = null; 742 } 743 if (localpart == null) { 744 localpart = XMLSymbols.EMPTY_STRING; 745 } 746 if (raw == null) { 747 raw = XMLSymbols.EMPTY_STRING; 748 } 749 } 750 String prefix = XMLSymbols.EMPTY_STRING; 751 int prefixIdx = raw.indexOf(':'); 752 if (prefixIdx != -1) { 753 prefix = fSymbolTable.addSymbol(raw.substring(0, prefixIdx)); 754 } 755 toFill.setValues(prefix, localpart, raw, uri); 756 } 757 758 759 private void fillXMLAttributes(Attributes att) { 760 fAttributes.removeAllAttributes(); 761 final int len = att.getLength(); 762 for (int i = 0; i < len; ++i) { 763 fillXMLAttribute(att, i); 764 fAttributes.setSpecified(i, true); 765 } 766 } 767 768 769 private void fillXMLAttributes2(Attributes2 att) { 770 fAttributes.removeAllAttributes(); 771 final int len = att.getLength(); 772 for (int i = 0; i < len; ++i) { 773 fillXMLAttribute(att, i); 774 fAttributes.setSpecified(i, att.isSpecified(i)); 775 if (att.isDeclared(i)) { 776 fAttributes.getAugmentations(i).putItem(Constants.ATTRIBUTE_DECLARED, Boolean.TRUE); 777 } 778 } 779 } 780 781 782 private void fillXMLAttribute(Attributes att, int index) { 783 fillQName(fAttributeQName, att.getURI(index), att.getLocalName(index), att.getQName(index)); 784 String type = att.getType(index); 785 fAttributes.addAttributeNS(fAttributeQName, (type != null) ? type : XMLSymbols.fCDATASymbol, att.getValue(index)); 786 } 787 788 793 private final XMLSchemaTypeInfoProvider fTypeInfoProvider = new XMLSchemaTypeInfoProvider(); 794 private static class XMLSchemaTypeInfoProvider extends TypeInfoProvider { 795 796 797 private Augmentations fElementAugs; 798 799 800 private XMLAttributes fAttributes; 801 802 803 private boolean fInStartElement = false; 804 805 806 void beginStartElement(Augmentations elementAugs, XMLAttributes attributes) { 807 fInStartElement = true; 808 fElementAugs = elementAugs; 809 fAttributes = attributes; 810 } 811 812 813 void finishStartElement() { 814 fInStartElement = false; 815 fElementAugs = null; 816 fAttributes = null; 817 } 818 819 820 void beginEndElement(Augmentations elementAugs) { 821 fElementAugs = elementAugs; 822 } 823 824 825 void finishEndElement() { 826 fElementAugs = null; 827 } 828 829 834 private void checkState() { 835 if( !fInStartElement ) { 836 throw new IllegalStateException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 837 "TypeInfoProviderIllegalState", null)); 838 } 839 } 840 841 public TypeInfo getAttributeTypeInfo(int index) { 842 checkState(); 843 return getAttributeType(index); 844 } 845 846 private TypeInfo getAttributeType( int index ) { 847 checkState(); 848 if( index<0 || fAttributes.getLength()<=index ) 849 throw new IndexOutOfBoundsException (Integer.toString(index)); 850 Augmentations augs = fAttributes.getAugmentations(index); 851 if (augs == null) return null; 852 AttributePSVI psvi = (AttributePSVI)augs.getItem(Constants.ATTRIBUTE_PSVI); 853 return getTypeInfoFromPSVI(psvi); 854 } 855 856 public TypeInfo getAttributeTypeInfo(String attributeUri, String attributeLocalName) { 857 checkState(); 858 return getAttributeTypeInfo(fAttributes.getIndex(attributeUri,attributeLocalName)); 859 } 860 861 public TypeInfo getAttributeTypeInfo(String attributeQName) { 862 checkState(); 863 return getAttributeTypeInfo(fAttributes.getIndex(attributeQName)); 864 } 865 866 public TypeInfo getElementTypeInfo() { 867 checkState(); 868 if (fElementAugs == null) return null; 869 ElementPSVI psvi = (ElementPSVI)fElementAugs.getItem(Constants.ELEMENT_PSVI); 870 return getTypeInfoFromPSVI(psvi); 871 } 872 873 private TypeInfo getTypeInfoFromPSVI( ItemPSVI psvi ) { 874 if(psvi==null) return null; 875 876 if( psvi.getValidity()== ElementPSVI.VALIDITY_VALID ) { 881 XSTypeDefinition t = psvi.getMemberTypeDefinition(); 882 if (t != null) { 883 return (t instanceof TypeInfo ) ? (TypeInfo ) t : null; 884 } 885 } 886 887 XSTypeDefinition t = psvi.getTypeDefinition(); 888 if (t != null) { 890 return (t instanceof TypeInfo ) ? (TypeInfo ) t : null; 891 } 892 return null; 893 } 894 895 public boolean isIdAttribute(int index) { 896 checkState(); 897 XSSimpleType type = (XSSimpleType)getAttributeType(index); 898 if(type==null) return false; 899 return type.isIDType(); 900 } 901 902 public boolean isSpecified(int index) { 903 checkState(); 904 return fAttributes.isSpecified(index); 905 } 906 907 910 911 ElementPSVI getElementPSVI() { 913 return (fElementAugs != null) ? (ElementPSVI) fElementAugs.getItem(Constants.ELEMENT_PSVI) : null; 914 } 915 916 AttributePSVI getAttributePSVI(int index) { 917 if (fAttributes != null) { 918 Augmentations augs = fAttributes.getAugmentations(index); 919 if (augs != null) { 920 return (AttributePSVI) augs.getItem(Constants.ATTRIBUTE_PSVI); 921 } 922 } 923 return null; 924 } 925 926 AttributePSVI getAttributePSVIByName(String uri, String localname) { 927 if (fAttributes != null) { 928 Augmentations augs = fAttributes.getAugmentations(uri, localname); 929 if (augs != null) { 930 return (AttributePSVI) augs.getItem(Constants.ATTRIBUTE_PSVI); 931 } 932 } 933 return null; 934 } 935 } 936 937 938 private final ResolutionForwarder fResolutionForwarder = new ResolutionForwarder(null); 939 static final class ResolutionForwarder 940 implements EntityResolver2 { 941 942 946 947 private static final String XML_TYPE = "http://www.w3.org/TR/REC-xml"; 948 949 950 protected LSResourceResolver fEntityResolver; 951 952 956 957 public ResolutionForwarder() {} 958 959 960 public ResolutionForwarder(LSResourceResolver entityResolver) { 961 setEntityResolver(entityResolver); 962 } 963 964 968 969 public void setEntityResolver(LSResourceResolver entityResolver) { 970 fEntityResolver = entityResolver; 971 } 973 974 public LSResourceResolver getEntityResolver() { 975 return fEntityResolver; 976 } 978 981 public InputSource getExternalSubset(String name, String baseURI) 982 throws SAXException , IOException { 983 return null; 984 } 985 986 990 public InputSource resolveEntity(String name, String publicId, 991 String baseURI, String systemId) throws SAXException , IOException { 992 if (fEntityResolver != null) { 993 LSInput lsInput = fEntityResolver.resolveResource(XML_TYPE, null, publicId, systemId, baseURI); 994 if (lsInput != null) { 995 final String pubId = lsInput.getPublicId(); 996 final String sysId = lsInput.getSystemId(); 997 final String baseSystemId = lsInput.getBaseURI(); 998 final Reader charStream = lsInput.getCharacterStream(); 999 final InputStream byteStream = lsInput.getByteStream(); 1000 final String data = lsInput.getStringData(); 1001 final String encoding = lsInput.getEncoding(); 1002 1003 1010 InputSource inputSource = new InputSource (); 1011 inputSource.setPublicId(pubId); 1012 inputSource.setSystemId((baseSystemId != null) ? resolveSystemId(systemId, baseSystemId) : systemId); 1013 1014 if (charStream != null) { 1015 inputSource.setCharacterStream(charStream); 1016 } 1017 else if (byteStream != null) { 1018 inputSource.setByteStream(byteStream); 1019 } 1020 else if (data != null && data.length() != 0) { 1021 inputSource.setCharacterStream(new StringReader (data)); 1022 } 1023 inputSource.setEncoding(encoding); 1024 return inputSource; 1025 } 1026 } 1027 return null; 1028 } 1029 1030 1031 public InputSource resolveEntity(String publicId, String systemId) 1032 throws SAXException , IOException { 1033 return resolveEntity(null, publicId, null, systemId); 1034 } 1035 1036 1037 private String resolveSystemId(String systemId, String baseURI) { 1038 try { 1039 return XMLEntityManager.expandSystemId(systemId, baseURI, false); 1040 } 1041 catch (URI.MalformedURIException ex) { 1045 return systemId; 1046 } 1047 } 1048 } 1049} 1050 | Popular Tags |