1 16 17 package org.apache.xerces.impl.xs.traversers; 18 19 import java.io.IOException ; 20 import java.io.StringReader ; 21 import java.util.ArrayList ; 22 import java.util.Hashtable ; 23 import java.util.Stack ; 24 import java.util.Vector ; 25 26 import org.apache.xerces.impl.Constants; 27 import org.apache.xerces.impl.XMLEntityManager; 28 import org.apache.xerces.impl.XMLErrorReporter; 29 import org.apache.xerces.impl.xs.SchemaGrammar; 30 import org.apache.xerces.impl.xs.SchemaNamespaceSupport; 31 import org.apache.xerces.impl.xs.SchemaSymbols; 32 import org.apache.xerces.impl.xs.XMLSchemaException; 33 import org.apache.xerces.impl.xs.XMLSchemaLoader; 34 import org.apache.xerces.impl.xs.XSComplexTypeDecl; 35 import org.apache.xerces.impl.xs.XSDDescription; 36 import org.apache.xerces.impl.xs.XSDeclarationPool; 37 import org.apache.xerces.impl.xs.XSElementDecl; 38 import org.apache.xerces.impl.xs.XSGrammarBucket; 39 import org.apache.xerces.impl.xs.XSGroupDecl; 40 import org.apache.xerces.impl.xs.XSMessageFormatter; 41 import org.apache.xerces.impl.xs.XSModelGroupImpl; 42 import org.apache.xerces.impl.xs.XSParticleDecl; 43 import org.apache.xerces.impl.xs.opti.ElementImpl; 44 import org.apache.xerces.impl.xs.opti.SchemaDOMParser; 45 import org.apache.xerces.impl.xs.opti.SchemaParsingConfig; 46 import org.apache.xerces.impl.xs.util.SimpleLocator; 47 48 import org.apache.xerces.util.DOMUtil; 49 import org.apache.xerces.parsers.SAXParser; 50 import org.apache.xerces.parsers.XML11Configuration; 51 import org.apache.xerces.util.DOMInputSource; 52 import org.apache.xerces.util.DefaultErrorHandler; 53 import org.apache.xerces.util.SAXInputSource; 54 import org.apache.xerces.util.SymbolTable; 55 import org.apache.xerces.util.XMLSymbols; 56 import org.apache.xerces.util.URI.MalformedURIException; 57 import org.apache.xerces.xni.QName; 58 import org.apache.xerces.xni.grammars.Grammar; 59 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 60 import org.apache.xerces.xni.grammars.XMLGrammarPool; 61 import org.apache.xerces.xni.grammars.XMLSchemaDescription; 62 import org.apache.xerces.xni.parser.XMLComponentManager; 63 import org.apache.xerces.xni.parser.XMLConfigurationException; 64 import org.apache.xerces.xni.parser.XMLEntityResolver; 65 import org.apache.xerces.xni.parser.XMLErrorHandler; 66 import org.apache.xerces.xni.parser.XMLInputSource; 67 import org.apache.xerces.xs.XSObject; 68 import org.apache.xerces.xs.XSParticle; 69 import org.w3c.dom.Document ; 70 import org.w3c.dom.Element ; 71 import org.w3c.dom.Node ; 72 import org.xml.sax.InputSource ; 73 import org.xml.sax.SAXException ; 74 import org.xml.sax.XMLReader ; 75 import org.xml.sax.helpers.XMLReaderFactory ; 76 77 78 93 public class XSDHandler { 94 95 96 protected static final String VALIDATION = 97 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 98 99 100 protected static final String XMLSCHEMA_VALIDATION = 101 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 102 103 104 protected static final String ALLOW_JAVA_ENCODINGS = 105 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 106 107 108 protected static final String CONTINUE_AFTER_FATAL_ERROR = 109 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 110 111 112 protected static final String STANDARD_URI_CONFORMANT_FEATURE = 113 Constants.XERCES_FEATURE_PREFIX + Constants.STANDARD_URI_CONFORMANT_FEATURE; 114 115 116 protected static final String DISALLOW_DOCTYPE = 117 Constants.XERCES_FEATURE_PREFIX + Constants.DISALLOW_DOCTYPE_DECL_FEATURE; 118 119 120 protected static final String GENERATE_SYNTHETIC_ANNOTATIONS = 121 Constants.XERCES_FEATURE_PREFIX + Constants.GENERATE_SYNTHETIC_ANNOTATIONS_FEATURE; 122 123 124 protected static final String VALIDATE_ANNOTATIONS = 125 Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATE_ANNOTATIONS_FEATURE; 126 127 128 protected static final String HONOUR_ALL_SCHEMALOCATIONS = 129 Constants.XERCES_FEATURE_PREFIX + Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE; 130 131 132 private static final String NAMESPACE_PREFIXES = 133 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE; 134 135 136 protected static final String STRING_INTERNING = 137 Constants.SAX_FEATURE_PREFIX + Constants.STRING_INTERNING_FEATURE; 138 139 140 protected static final String ERROR_HANDLER = 141 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 142 143 144 protected static final String JAXP_SCHEMA_SOURCE = 145 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE; 146 147 148 public static final String ENTITY_RESOLVER = 149 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 150 151 protected static final String ENTITY_MANAGER = 152 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 153 154 155 public static final String ERROR_REPORTER = 156 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 157 158 159 public static final String XMLGRAMMAR_POOL = 160 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 161 162 163 public static final String SYMBOL_TABLE = 164 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 165 166 167 protected static final String SECURITY_MANAGER = 168 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 169 170 protected static final boolean DEBUG_NODE_POOL = false; 171 172 174 final static int ATTRIBUTE_TYPE = 1; 177 final static int ATTRIBUTEGROUP_TYPE = 2; 178 final static int ELEMENT_TYPE = 3; 179 final static int GROUP_TYPE = 4; 180 final static int IDENTITYCONSTRAINT_TYPE = 5; 181 final static int NOTATION_TYPE = 6; 182 final static int TYPEDECL_TYPE = 7; 183 184 public final static String REDEF_IDENTIFIER = "_fn3dktizrknc9pi"; 187 188 protected Hashtable fNotationRegistry = new Hashtable (); 192 193 protected XSDeclarationPool fDeclPool = null; 194 195 196 private Hashtable fUnparsedAttributeRegistry = new Hashtable (); 204 private Hashtable fUnparsedAttributeGroupRegistry = new Hashtable (); 205 private Hashtable fUnparsedElementRegistry = new Hashtable (); 206 private Hashtable fUnparsedGroupRegistry = new Hashtable (); 207 private Hashtable fUnparsedIdentityConstraintRegistry = new Hashtable (); 208 private Hashtable fUnparsedNotationRegistry = new Hashtable (); 209 private Hashtable fUnparsedTypeRegistry = new Hashtable (); 210 private Hashtable fUnparsedAttributeRegistrySub = new Hashtable (); 214 private Hashtable fUnparsedAttributeGroupRegistrySub = new Hashtable (); 215 private Hashtable fUnparsedElementRegistrySub = new Hashtable (); 216 private Hashtable fUnparsedGroupRegistrySub = new Hashtable (); 217 private Hashtable fUnparsedIdentityConstraintRegistrySub = new Hashtable (); 218 private Hashtable fUnparsedNotationRegistrySub = new Hashtable (); 219 private Hashtable fUnparsedTypeRegistrySub = new Hashtable (); 220 221 private Hashtable fXSDocumentInfoRegistry = new Hashtable (); 228 229 private Hashtable fDependencyMap = new Hashtable (); 233 234 private Hashtable fImportMap = new Hashtable (); 239 private Vector fAllTNSs = new Vector (); 243 private Hashtable fLocationPairs = null; 245 246 private Hashtable fHiddenNodes = null; 249 250 private String null2EmptyString(String ns) { 252 return ns == null ? XMLSymbols.EMPTY_STRING : ns; 253 } 254 private String emptyString2Null(String ns) { 255 return ns == XMLSymbols.EMPTY_STRING ? null : ns; 256 } 257 private String doc2SystemId(Element ele) { 259 String documentURI = null; 260 263 if(ele.getOwnerDocument() instanceof org.apache.xerces.impl.xs.opti.SchemaDOM){ 264 documentURI = ((org.apache.xerces.impl.xs.opti.SchemaDOM) ele.getOwnerDocument()).getDocumentURI(); 265 } 266 return documentURI != null ? documentURI : (String ) fDoc2SystemId.get(ele); 267 } 268 269 private Hashtable fTraversed = new Hashtable (); 275 276 private Hashtable fDoc2SystemId = new Hashtable (); 279 280 private XSDocumentInfo fRoot = null; 282 283 private Hashtable fDoc2XSDocumentMap = new Hashtable (); 286 287 private Hashtable fRedefine2XSDMap = new Hashtable (); 290 291 private Hashtable fRedefine2NSSupport = new Hashtable (); 293 294 private Hashtable fRedefinedRestrictedAttributeGroupRegistry = new Hashtable (); 300 private Hashtable fRedefinedRestrictedGroupRegistry = new Hashtable (); 301 302 private boolean fLastSchemaWasDuplicate; 305 306 private boolean fValidateAnnotations = false; 308 309 private boolean fHonourAllSchemaLocations = false; 311 312 private XMLErrorReporter fErrorReporter; 314 private XMLEntityResolver fEntityResolver; 315 316 private XSAttributeChecker fAttributeChecker; 318 319 private SymbolTable fSymbolTable; 321 322 private XSGrammarBucket fGrammarBucket; 324 325 private XSDDescription fSchemaGrammarDescription; 327 328 private XMLGrammarPool fGrammarPool; 330 331 XSDAttributeGroupTraverser fAttributeGroupTraverser; 333 XSDAttributeTraverser fAttributeTraverser; 334 XSDComplexTypeTraverser fComplexTypeTraverser; 335 XSDElementTraverser fElementTraverser; 336 XSDGroupTraverser fGroupTraverser; 337 XSDKeyrefTraverser fKeyrefTraverser; 338 XSDNotationTraverser fNotationTraverser; 339 XSDSimpleTypeTraverser fSimpleTypeTraverser; 340 XSDUniqueOrKeyTraverser fUniqueOrKeyTraverser; 341 XSDWildcardTraverser fWildCardTraverser; 342 343 SchemaDOMParser fSchemaParser; 344 SchemaContentHandler fXSContentHandler; 345 XML11Configuration fAnnotationValidator; 346 XSAnnotationGrammarPool fGrammarBucketAdapter; 347 348 351 private static final int INIT_STACK_SIZE = 30; 353 private static final int INC_STACK_SIZE = 10; 355 private int fLocalElemStackPos = 0; 357 358 private XSParticleDecl[] fParticle = new XSParticleDecl[INIT_STACK_SIZE]; 359 private Element [] fLocalElementDecl = new Element [INIT_STACK_SIZE]; 360 private XSDocumentInfo[] fLocalElementDecl_schema = new XSDocumentInfo[INIT_STACK_SIZE]; private int[] fAllContext = new int[INIT_STACK_SIZE]; 362 private XSObject[] fParent = new XSObject[INIT_STACK_SIZE]; 363 private String [][] fLocalElemNamespaceContext = new String [INIT_STACK_SIZE][1]; 364 365 368 private static final int INIT_KEYREF_STACK = 2; 370 private static final int INC_KEYREF_STACK_AMOUNT = 2; 372 private int fKeyrefStackPos = 0; 374 375 private Element [] fKeyrefs = new Element [INIT_KEYREF_STACK]; 376 private XSDocumentInfo [] fKeyrefsMapXSDocumentInfo = new XSDocumentInfo[INIT_KEYREF_STACK]; 377 private XSElementDecl [] fKeyrefElems = new XSElementDecl [INIT_KEYREF_STACK]; 378 private String [][] fKeyrefNamespaceContext = new String [INIT_KEYREF_STACK][1]; 379 380 public XSDHandler(){ 382 fHiddenNodes = new Hashtable (); 383 fSchemaParser = new SchemaDOMParser(new SchemaParsingConfig()); 384 } 385 386 public XSDHandler (XSGrammarBucket gBucket) { 390 this(); 391 fGrammarBucket = gBucket; 392 393 fSchemaGrammarDescription = new XSDDescription(); 397 } 399 410 public SchemaGrammar parseSchema(XMLInputSource is, XSDDescription desc, 411 Hashtable locationPairs) 412 throws IOException { 413 fLocationPairs = locationPairs; 414 fSchemaParser.resetNodePool(); 415 SchemaGrammar grammar = null; 416 String schemaNamespace = null; 417 short referType = desc.getContextType(); 418 if (referType != XSDDescription.CONTEXT_PREPARSE){ 425 if(fHonourAllSchemaLocations && referType == XSDDescription.CONTEXT_IMPORT && isExistingGrammar(desc)) { 427 grammar = fGrammarBucket.getGrammar(desc.getTargetNamespace()); 428 } 429 else { 430 grammar = findGrammar(desc); 431 } 432 if (grammar != null) 433 return grammar; 434 schemaNamespace = desc.getTargetNamespace(); 435 if (schemaNamespace != null) { 437 schemaNamespace = fSymbolTable.addSymbol(schemaNamespace); 438 } 439 } 440 441 prepareForParse(); 444 445 Document schemaRootDoc = null; 446 Element schemaRoot = null; 447 if (is instanceof DOMInputSource) { 449 fHiddenNodes.clear(); 451 Node domNode = ((DOMInputSource)is).getNode(); 452 453 if (domNode instanceof Document ) { 454 schemaRootDoc = (Document )domNode; 455 schemaRoot = DOMUtil.getRoot(schemaRootDoc); 456 } 457 else if (domNode instanceof Element ) { 458 schemaRoot = (Element )domNode; 459 } 460 else { 461 return null; 462 } 463 } else if (is instanceof SAXInputSource) { 465 XMLReader parser = ((SAXInputSource)is).getXMLReader(); 466 InputSource inputSource = ((SAXInputSource)is).getInputSource(); 467 boolean namespacePrefixes = false; 468 if (parser != null) { 469 try { 470 namespacePrefixes = parser.getFeature(NAMESPACE_PREFIXES); 471 } 472 catch (SAXException se) {} 473 } 474 else { 475 try { 476 parser = XMLReaderFactory.createXMLReader(); 477 } 478 catch (SAXException se) { 481 parser = new SAXParser(); 482 } 483 try { 484 parser.setFeature(NAMESPACE_PREFIXES, true); 485 namespacePrefixes = true; 486 } 487 catch (SAXException se) {} 488 } 489 boolean stringsInternalized = false; 492 try { 493 stringsInternalized = parser.getFeature(STRING_INTERNING); 494 } 495 catch (SAXException exc) { 496 } 499 if (fXSContentHandler == null) { 500 fXSContentHandler = new SchemaContentHandler(); 501 } 502 fXSContentHandler.reset(fSchemaParser, fSymbolTable, 503 namespacePrefixes, stringsInternalized); 504 parser.setContentHandler(fXSContentHandler); 505 parser.setErrorHandler(fErrorReporter.getSAXErrorHandler()); 506 try { 507 parser.parse(inputSource); 508 } 509 catch (SAXException se) { 510 return null; 511 } 512 schemaRootDoc = fXSContentHandler.getDocument(); 513 if (schemaRootDoc == null) { 514 return null; 516 } 517 schemaRoot = DOMUtil.getRoot(schemaRootDoc); 518 } 519 else { 520 schemaRoot = getSchemaDocument(schemaNamespace, is, 521 referType == XSDDescription.CONTEXT_PREPARSE, 522 referType, null); 523 524 } 526 if(schemaRoot == null){ 527 return null; 529 } 530 531 if ( referType == XSDDescription.CONTEXT_PREPARSE) { 532 Element schemaElem = schemaRoot; 533 schemaNamespace = DOMUtil.getAttrValue(schemaElem, SchemaSymbols.ATT_TARGETNAMESPACE); 534 if(schemaNamespace != null && schemaNamespace.length() > 0) { 535 schemaNamespace = fSymbolTable.addSymbol(schemaNamespace); 538 desc.setTargetNamespace(schemaNamespace); 539 } 540 else { 541 schemaNamespace = null; 542 } 543 grammar = findGrammar(desc); 544 if (grammar != null) 545 return grammar; 546 String schemaId = XMLEntityManager.expandSystemId(is.getSystemId(), is.getBaseSystemId(), false); 547 XSDKey key = new XSDKey(schemaId, referType, schemaNamespace); 548 fTraversed.put(key, schemaRoot); 549 if (schemaId != null) { 550 fDoc2SystemId.put(schemaRoot, schemaId); 551 } 552 } 553 554 prepareForTraverse(); 557 558 fRoot = constructTrees(schemaRoot, is.getSystemId(), desc); 559 if (fRoot == null) { 560 return null; 561 } 562 563 buildGlobalNameRegistries(); 565 566 ArrayList annotationInfo = fValidateAnnotations ? new ArrayList () : null; 568 traverseSchemas(annotationInfo); 569 570 traverseLocalElements(); 572 573 resolveKeyRefs(); 575 576 580 for (int i = fAllTNSs.size() - 1; i >= 0; i--) { 583 String tns = (String )fAllTNSs.elementAt(i); 585 Vector ins = (Vector )fImportMap.get(tns); 587 SchemaGrammar sg = fGrammarBucket.getGrammar(emptyString2Null(tns)); 589 if (sg == null) 590 continue; 591 SchemaGrammar isg; 592 int count = 0; 594 for (int j = 0; j < ins.size(); j++) { 595 isg = fGrammarBucket.getGrammar((String )ins.elementAt(j)); 597 if (isg != null) 599 ins.setElementAt(isg, count++); 600 } 601 ins.setSize(count); 602 sg.setImportedGrammars(ins); 604 } 605 606 607 if (fValidateAnnotations && annotationInfo.size() > 0) { 608 validateAnnotations(annotationInfo); 609 } 610 611 return fGrammarBucket.getGrammar(fRoot.fTargetNamespace); 613 } 615 private void validateAnnotations(ArrayList annotationInfo) { 616 if (fAnnotationValidator == null) { 617 createAnnotationValidator(); 618 } 619 final int size = annotationInfo.size(); 620 final XMLInputSource src = new XMLInputSource(null, null, null); 621 fGrammarBucketAdapter.refreshGrammars(fGrammarBucket); 622 for (int i = 0; i < size; i += 2) { 623 src.setSystemId((String ) annotationInfo.get(i)); 624 XSAnnotationInfo annotation = (XSAnnotationInfo) annotationInfo.get(i+1); 625 while (annotation != null) { 626 src.setCharacterStream(new StringReader (annotation.fAnnotation)); 627 try { 628 fAnnotationValidator.parse(src); 629 } 630 catch (IOException exc) {} 631 annotation = annotation.next; 632 } 633 } 634 } 635 636 private void createAnnotationValidator() { 637 fAnnotationValidator = new XML11Configuration(); 638 fGrammarBucketAdapter = new XSAnnotationGrammarPool(); 639 fAnnotationValidator.setFeature(VALIDATION, true); 640 fAnnotationValidator.setFeature(XMLSCHEMA_VALIDATION, true); 641 fAnnotationValidator.setProperty(XMLGRAMMAR_POOL, fGrammarBucketAdapter); 642 643 XMLErrorHandler errorHandler = fErrorReporter.getErrorHandler(); 644 fAnnotationValidator.setProperty(ERROR_HANDLER, (errorHandler != null) ? errorHandler : new DefaultErrorHandler()); 645 } 646 647 651 SchemaGrammar getGrammar(String tns) { 652 return fGrammarBucket.getGrammar(tns); 653 } 654 655 660 protected SchemaGrammar findGrammar(XSDDescription desc) { 661 SchemaGrammar sg = fGrammarBucket.getGrammar(desc.getTargetNamespace()); 662 if (sg == null) { 663 if (fGrammarPool != null) { 664 sg = (SchemaGrammar)fGrammarPool.retrieveGrammar(desc); 665 if (sg != null) { 666 if (!fGrammarBucket.putGrammar(sg, true)) { 669 reportSchemaWarning("GrammarConflict", null, null); 672 sg = null; 673 } 674 } 675 } 676 } 677 return sg; 678 } 679 680 683 private static final String [][] NS_ERROR_CODES = { 684 {"src-include.2.1", "src-include.2.1"}, 685 {"src-redefine.3.1", "src-redefine.3.1"}, 686 {"src-import.3.1", "src-import.3.2"}, 687 null, 688 {"TargetNamespace.1", "TargetNamespace.2"}, 689 {"TargetNamespace.1", "TargetNamespace.2"}, 690 {"TargetNamespace.1", "TargetNamespace.2"}, 691 {"TargetNamespace.1", "TargetNamespace.2"} 692 }; 693 694 private static final String [] ELE_ERROR_CODES = { 695 "src-include.1", "src-redefine.2", "src-import.2", "schema_reference.4", 696 "schema_reference.4", "schema_reference.4", "schema_reference.4", "schema_reference.4" 697 }; 698 699 protected XSDocumentInfo constructTrees(Element schemaRoot, String locationHint, XSDDescription desc) { 710 if (schemaRoot == null) return null; 711 String callerTNS = desc.getTargetNamespace(); 712 short referType = desc.getContextType(); 713 714 XSDocumentInfo currSchemaInfo = null; 715 try { 716 currSchemaInfo = new XSDocumentInfo(schemaRoot, fAttributeChecker, fSymbolTable); 718 } catch (XMLSchemaException se) { 719 reportSchemaError(ELE_ERROR_CODES[referType], 720 new Object []{locationHint}, 721 schemaRoot); 722 return null; 723 } 724 if (currSchemaInfo.fTargetNamespace != null && 726 currSchemaInfo.fTargetNamespace.length() == 0) { 727 reportSchemaWarning("EmptyTargetNamespace", 728 new Object []{locationHint}, 729 schemaRoot); 730 currSchemaInfo.fTargetNamespace = null; 731 } 732 733 if (callerTNS != null) { 734 int secondIdx = 0; 737 if (referType == XSDDescription.CONTEXT_INCLUDE || 739 referType == XSDDescription.CONTEXT_REDEFINE) { 740 if (currSchemaInfo.fTargetNamespace == null) { 743 currSchemaInfo.fTargetNamespace = callerTNS; 744 currSchemaInfo.fIsChameleonSchema = true; 745 } 746 else if (callerTNS != currSchemaInfo.fTargetNamespace) { 749 reportSchemaError(NS_ERROR_CODES[referType][secondIdx], 750 new Object [] {callerTNS, currSchemaInfo.fTargetNamespace}, 751 schemaRoot); 752 return null; 753 } 754 } 755 else if (referType != XSDDescription.CONTEXT_PREPARSE && callerTNS != currSchemaInfo.fTargetNamespace) { 757 reportSchemaError(NS_ERROR_CODES[referType][secondIdx], 758 new Object [] {callerTNS, currSchemaInfo.fTargetNamespace}, 759 schemaRoot); 760 return null; 761 } 762 } 763 else if (currSchemaInfo.fTargetNamespace != null) { 766 if (referType == XSDDescription.CONTEXT_PREPARSE) { 768 desc.setTargetNamespace(currSchemaInfo.fTargetNamespace); 769 callerTNS = currSchemaInfo.fTargetNamespace; 770 } 771 else { 772 int secondIdx = 1; 775 reportSchemaError(NS_ERROR_CODES[referType][secondIdx], 776 new Object [] {callerTNS, currSchemaInfo.fTargetNamespace}, 777 schemaRoot); 778 return null; 779 } 780 } 781 784 currSchemaInfo.addAllowedNS(currSchemaInfo.fTargetNamespace); 786 787 SchemaGrammar sg = null; 788 789 if (referType == XSDDescription.CONTEXT_INCLUDE || 790 referType == XSDDescription.CONTEXT_REDEFINE) { 791 sg = fGrammarBucket.getGrammar(currSchemaInfo.fTargetNamespace); 792 } 793 else if(fHonourAllSchemaLocations && referType == XSDDescription.CONTEXT_IMPORT) { 794 sg = findGrammar(desc); 795 if(sg == null) { 796 sg = new SchemaGrammar(currSchemaInfo.fTargetNamespace, desc.makeClone(), fSymbolTable); 797 fGrammarBucket.putGrammar(sg); 798 } 799 } 800 else { 801 sg = new SchemaGrammar(currSchemaInfo.fTargetNamespace, desc.makeClone(), fSymbolTable); 802 fGrammarBucket.putGrammar(sg); 803 } 804 805 sg.addDocument(null, (String )fDoc2SystemId.get(currSchemaInfo.fSchemaElement)); 808 809 fDoc2XSDocumentMap.put(schemaRoot, currSchemaInfo); 810 Vector dependencies = new Vector (); 811 Element rootNode = schemaRoot; 812 813 Element newSchemaRoot = null; 814 for (Element child = DOMUtil.getFirstChildElement(rootNode); 815 child != null; 816 child = DOMUtil.getNextSiblingElement(child)) { 817 String schemaNamespace=null; 818 String schemaHint=null; 819 String localName = DOMUtil.getLocalName(child); 820 821 short refType = -1; 822 823 if (localName.equals(SchemaSymbols.ELT_ANNOTATION)) 824 continue; 825 else if (localName.equals(SchemaSymbols.ELT_IMPORT)) { 826 refType = XSDDescription.CONTEXT_IMPORT; 827 Object [] importAttrs = fAttributeChecker.checkAttributes(child, true, currSchemaInfo); 830 schemaHint = (String )importAttrs[XSAttributeChecker.ATTIDX_SCHEMALOCATION]; 831 schemaNamespace = (String )importAttrs[XSAttributeChecker.ATTIDX_NAMESPACE]; 832 if (schemaNamespace != null) 833 schemaNamespace = fSymbolTable.addSymbol(schemaNamespace); 834 if (schemaNamespace == currSchemaInfo.fTargetNamespace) { 836 reportSchemaError("src-import.1.1", new Object [] {schemaNamespace}, child); 837 } 838 839 Element importChild = DOMUtil.getFirstChildElement(child); 841 if(importChild != null ) { 842 String importComponentType = DOMUtil.getLocalName(importChild); 843 if (importComponentType.equals(SchemaSymbols.ELT_ANNOTATION)) { 844 sg.addAnnotation( 846 fElementTraverser.traverseAnnotationDecl(importChild, importAttrs, true, currSchemaInfo)); 847 } else { 848 reportSchemaError("s4s-elt-must-match.1", new Object [] {localName, "annotation?", importComponentType}, child); 849 } 850 if(DOMUtil.getNextSiblingElement(importChild) != null) { 851 reportSchemaError("s4s-elt-must-match.1", new Object [] {localName, "annotation?", DOMUtil.getLocalName(DOMUtil.getNextSiblingElement(importChild))}, child); 852 } 853 } 854 else { 855 String text = DOMUtil.getSyntheticAnnotation(child); 856 if (text != null) { 857 sg.addAnnotation(fElementTraverser.traverseSyntheticAnnotation(child, text, importAttrs, true, currSchemaInfo)); 858 } 859 } 860 fAttributeChecker.returnAttrArray(importAttrs, currSchemaInfo); 861 862 if(currSchemaInfo.isAllowedNS(schemaNamespace)) { 865 if(!fHonourAllSchemaLocations) 866 continue; 867 } 868 else { 869 currSchemaInfo.addAllowedNS(schemaNamespace); 870 } 871 String tns = null2EmptyString(currSchemaInfo.fTargetNamespace); 874 Vector ins = (Vector )fImportMap.get(tns); 876 if (ins == null) { 878 fAllTNSs.addElement(tns); 880 ins = new Vector (); 881 fImportMap.put(tns, ins); 882 ins.addElement(schemaNamespace); 883 } 884 else if (!ins.contains(schemaNamespace)){ 885 ins.addElement(schemaNamespace); 886 } 887 888 fSchemaGrammarDescription.reset(); 889 fSchemaGrammarDescription.setContextType(XSDDescription.CONTEXT_IMPORT); 890 fSchemaGrammarDescription.setBaseSystemId(doc2SystemId(schemaRoot)); 891 fSchemaGrammarDescription.setLocationHints(new String []{schemaHint}); 892 fSchemaGrammarDescription.setTargetNamespace(schemaNamespace); 893 894 if ((!fHonourAllSchemaLocations && findGrammar(fSchemaGrammarDescription) != null) || isExistingGrammar(fSchemaGrammarDescription)) 897 continue; 898 newSchemaRoot = resolveSchema(fSchemaGrammarDescription, false, child); 899 } 900 else if ((localName.equals(SchemaSymbols.ELT_INCLUDE)) || 901 (localName.equals(SchemaSymbols.ELT_REDEFINE))) { 902 Object [] includeAttrs = fAttributeChecker.checkAttributes(child, true, currSchemaInfo); 906 schemaHint = (String )includeAttrs[XSAttributeChecker.ATTIDX_SCHEMALOCATION]; 907 if (localName.equals(SchemaSymbols.ELT_REDEFINE)) { 909 fRedefine2NSSupport.put(child, new SchemaNamespaceSupport(currSchemaInfo.fNamespaceSupport)); 910 } 911 912 if(localName.equals(SchemaSymbols.ELT_INCLUDE)) { 915 Element includeChild = DOMUtil.getFirstChildElement(child); 916 if(includeChild != null ) { 917 String includeComponentType = DOMUtil.getLocalName(includeChild); 918 if (includeComponentType.equals(SchemaSymbols.ELT_ANNOTATION)) { 919 sg.addAnnotation( 921 fElementTraverser.traverseAnnotationDecl(includeChild, includeAttrs, true, currSchemaInfo)); 922 } else { 923 reportSchemaError("s4s-elt-must-match.1", new Object [] {localName, "annotation?", includeComponentType}, child); 924 } 925 if(DOMUtil.getNextSiblingElement(includeChild) != null) { 926 reportSchemaError("s4s-elt-must-match.1", new Object [] {localName, "annotation?", DOMUtil.getLocalName(DOMUtil.getNextSiblingElement(includeChild))}, child); 927 } 928 } 929 else { 930 String text = DOMUtil.getSyntheticAnnotation(child); 931 if (text != null) { 932 sg.addAnnotation(fElementTraverser.traverseSyntheticAnnotation(child, text, includeAttrs, true, currSchemaInfo)); 933 } 934 } 935 } 936 else { 937 for (Element redefinedChild = DOMUtil.getFirstChildElement(child); 938 redefinedChild != null; 939 redefinedChild = DOMUtil.getNextSiblingElement(redefinedChild)) { 940 String redefinedComponentType = DOMUtil.getLocalName(redefinedChild); 941 if (redefinedComponentType.equals(SchemaSymbols.ELT_ANNOTATION)) { 942 sg.addAnnotation( 944 fElementTraverser.traverseAnnotationDecl(redefinedChild, includeAttrs, true, currSchemaInfo)); 945 DOMUtil.setHidden(redefinedChild, fHiddenNodes); 946 } 947 else { 948 String text = DOMUtil.getSyntheticAnnotation(child); 949 if (text != null) { 950 sg.addAnnotation(fElementTraverser.traverseSyntheticAnnotation(child, text, includeAttrs, true, currSchemaInfo)); 951 } 952 } 953 } 955 } 956 fAttributeChecker.returnAttrArray(includeAttrs, currSchemaInfo); 957 if (schemaHint == null) { 959 reportSchemaError("s4s-att-must-appear", new Object [] { 960 "<include> or <redefine>", "schemaLocation"}, 961 child); 962 } 963 boolean mustResolve = false; 965 refType = XSDDescription.CONTEXT_INCLUDE; 966 if(localName.equals(SchemaSymbols.ELT_REDEFINE)) { 967 mustResolve = nonAnnotationContent(child); 968 refType = XSDDescription.CONTEXT_REDEFINE; 969 } 970 fSchemaGrammarDescription.reset(); 971 fSchemaGrammarDescription.setContextType(refType); 972 fSchemaGrammarDescription.setBaseSystemId(doc2SystemId(schemaRoot)); 973 fSchemaGrammarDescription.setLocationHints(new String []{schemaHint}); 974 fSchemaGrammarDescription.setTargetNamespace(callerTNS); 975 newSchemaRoot = resolveSchema(fSchemaGrammarDescription, mustResolve, child); 976 schemaNamespace = currSchemaInfo.fTargetNamespace; 977 } 978 else { 979 break; 982 } 983 984 XSDocumentInfo newSchemaInfo = null; 987 if (fLastSchemaWasDuplicate) { 988 newSchemaInfo = newSchemaRoot == null ? null : (XSDocumentInfo)fDoc2XSDocumentMap.get(newSchemaRoot); 989 } 990 else { 991 newSchemaInfo = constructTrees(newSchemaRoot, schemaHint, fSchemaGrammarDescription); 992 } 993 994 if (localName.equals(SchemaSymbols.ELT_REDEFINE) && 995 newSchemaInfo != null) { 996 fRedefine2XSDMap.put(child, newSchemaInfo); 999 } 1000 if (newSchemaRoot != null) { 1001 if (newSchemaInfo != null) 1002 dependencies.addElement(newSchemaInfo); 1003 newSchemaRoot = null; 1004 } 1005 } 1006 1007 fDependencyMap.put(currSchemaInfo, dependencies); 1008 return currSchemaInfo; 1009 } 1011 private boolean isExistingGrammar(XSDDescription desc) { 1012 SchemaGrammar sg = fGrammarBucket.getGrammar(desc.getTargetNamespace()); 1013 if(sg == null) { 1014 return findGrammar(desc) != null; 1015 } 1016 else { 1017 try { 1018 return sg.getDocumentLocations().contains(XMLEntityManager.expandSystemId(desc.getLiteralSystemId(), desc.getBaseSystemId(), false)); 1019 } catch (MalformedURIException e) { 1020 return false; 1021 } 1022 } 1023 } 1024 1025 protected void buildGlobalNameRegistries() { 1031 1032 1033 1043 Stack schemasToProcess = new Stack (); 1044 schemasToProcess.push(fRoot); 1045 1046 while (!schemasToProcess.empty()) { 1047 XSDocumentInfo currSchemaDoc = 1048 (XSDocumentInfo)schemasToProcess.pop(); 1049 Element currDoc = currSchemaDoc.fSchemaElement; 1050 if(DOMUtil.isHidden(currDoc, fHiddenNodes)){ 1051 continue; 1053 } 1054 1055 Element currRoot = currDoc; 1056 boolean dependenciesCanOccur = true; 1058 for (Element globalComp = 1059 DOMUtil.getFirstChildElement(currRoot); 1060 globalComp != null; 1061 globalComp = DOMUtil.getNextSiblingElement(globalComp)) { 1062 if (DOMUtil.getLocalName(globalComp).equals(SchemaSymbols.ELT_ANNOTATION)) { 1065 continue; 1067 } 1068 else if (DOMUtil.getLocalName(globalComp).equals(SchemaSymbols.ELT_INCLUDE) || 1069 DOMUtil.getLocalName(globalComp).equals(SchemaSymbols.ELT_IMPORT)) { 1070 if (!dependenciesCanOccur) { 1071 reportSchemaError("s4s-elt-invalid-content.3", new Object [] {DOMUtil.getLocalName(globalComp)}, globalComp); 1072 } 1073 DOMUtil.setHidden(globalComp, fHiddenNodes); 1074 } 1075 else if (DOMUtil.getLocalName(globalComp).equals(SchemaSymbols.ELT_REDEFINE)) { 1076 if (!dependenciesCanOccur) { 1077 reportSchemaError("s4s-elt-invalid-content.3", new Object [] {DOMUtil.getLocalName(globalComp)}, globalComp); 1078 } 1079 for (Element redefineComp = DOMUtil.getFirstChildElement(globalComp); 1080 redefineComp != null; 1081 redefineComp = DOMUtil.getNextSiblingElement(redefineComp)) { 1082 String lName = DOMUtil.getAttrValue(redefineComp, SchemaSymbols.ATT_NAME); 1083 if (lName.length() == 0) continue; 1085 String qName = currSchemaDoc.fTargetNamespace == null ? 1086 ","+lName: 1087 currSchemaDoc.fTargetNamespace +","+lName; 1088 String componentType = DOMUtil.getLocalName(redefineComp); 1089 if (componentType.equals(SchemaSymbols.ELT_ATTRIBUTEGROUP)) { 1090 checkForDuplicateNames(qName, fUnparsedAttributeGroupRegistry, fUnparsedAttributeGroupRegistrySub, redefineComp, currSchemaDoc); 1091 String targetLName = DOMUtil.getAttrValue(redefineComp, SchemaSymbols.ATT_NAME)+REDEF_IDENTIFIER; 1093 renameRedefiningComponents(currSchemaDoc, redefineComp, SchemaSymbols.ELT_ATTRIBUTEGROUP, 1095 lName, targetLName); 1096 } 1097 else if ((componentType.equals(SchemaSymbols.ELT_COMPLEXTYPE)) || 1098 (componentType.equals(SchemaSymbols.ELT_SIMPLETYPE))) { 1099 checkForDuplicateNames(qName, fUnparsedTypeRegistry, fUnparsedTypeRegistrySub, redefineComp, currSchemaDoc); 1100 String targetLName = DOMUtil.getAttrValue(redefineComp, SchemaSymbols.ATT_NAME) + REDEF_IDENTIFIER; 1102 if (componentType.equals(SchemaSymbols.ELT_COMPLEXTYPE)) { 1104 renameRedefiningComponents(currSchemaDoc, redefineComp, SchemaSymbols.ELT_COMPLEXTYPE, 1105 lName, targetLName); 1106 } 1107 else { renameRedefiningComponents(currSchemaDoc, redefineComp, SchemaSymbols.ELT_SIMPLETYPE, 1109 lName, targetLName); 1110 } 1111 } 1112 else if (componentType.equals(SchemaSymbols.ELT_GROUP)) { 1113 checkForDuplicateNames(qName, fUnparsedGroupRegistry, fUnparsedGroupRegistrySub, redefineComp, currSchemaDoc); 1114 String targetLName = DOMUtil.getAttrValue(redefineComp, SchemaSymbols.ATT_NAME)+REDEF_IDENTIFIER; 1116 renameRedefiningComponents(currSchemaDoc, redefineComp, SchemaSymbols.ELT_GROUP, 1118 lName, targetLName); 1119 } 1120 } } 1124 else { 1125 dependenciesCanOccur = false; 1126 String lName = DOMUtil.getAttrValue(globalComp, SchemaSymbols.ATT_NAME); 1127 if (lName.length() == 0) continue; 1129 String qName = currSchemaDoc.fTargetNamespace == null? 1130 ","+lName: 1131 currSchemaDoc.fTargetNamespace +","+lName; 1132 String componentType = DOMUtil.getLocalName(globalComp); 1133 if (componentType.equals(SchemaSymbols.ELT_ATTRIBUTE)) { 1134 checkForDuplicateNames(qName, fUnparsedAttributeRegistry, fUnparsedAttributeRegistrySub, globalComp, currSchemaDoc); 1135 } 1136 else if (componentType.equals(SchemaSymbols.ELT_ATTRIBUTEGROUP)) { 1137 checkForDuplicateNames(qName, fUnparsedAttributeGroupRegistry, fUnparsedAttributeGroupRegistrySub, globalComp, currSchemaDoc); 1138 } 1139 else if ((componentType.equals(SchemaSymbols.ELT_COMPLEXTYPE)) || 1140 (componentType.equals(SchemaSymbols.ELT_SIMPLETYPE))) { 1141 checkForDuplicateNames(qName, fUnparsedTypeRegistry, fUnparsedTypeRegistrySub, globalComp, currSchemaDoc); 1142 } 1143 else if (componentType.equals(SchemaSymbols.ELT_ELEMENT)) { 1144 checkForDuplicateNames(qName, fUnparsedElementRegistry, fUnparsedElementRegistrySub, globalComp, currSchemaDoc); 1145 } 1146 else if (componentType.equals(SchemaSymbols.ELT_GROUP)) { 1147 checkForDuplicateNames(qName, fUnparsedGroupRegistry, fUnparsedGroupRegistrySub, globalComp, currSchemaDoc); 1148 } 1149 else if (componentType.equals(SchemaSymbols.ELT_NOTATION)) { 1150 checkForDuplicateNames(qName, fUnparsedNotationRegistry, fUnparsedNotationRegistrySub, globalComp, currSchemaDoc); 1151 } 1152 } 1153 } 1155 DOMUtil.setHidden(currDoc, fHiddenNodes); 1157 Vector currSchemaDepends = (Vector )fDependencyMap.get(currSchemaDoc); 1159 for (int i = 0; i < currSchemaDepends.size(); i++) { 1160 schemasToProcess.push(currSchemaDepends.elementAt(i)); 1161 } 1162 } 1164 } 1166 protected void traverseSchemas(ArrayList annotationInfo) { 1178 setSchemasVisible(fRoot); 1183 Stack schemasToProcess = new Stack (); 1184 schemasToProcess.push(fRoot); 1185 while (!schemasToProcess.empty()) { 1186 XSDocumentInfo currSchemaDoc = 1187 (XSDocumentInfo)schemasToProcess.pop(); 1188 Element currDoc = currSchemaDoc.fSchemaElement; 1189 1190 SchemaGrammar currSG = fGrammarBucket.getGrammar(currSchemaDoc.fTargetNamespace); 1191 1192 if(DOMUtil.isHidden(currDoc, fHiddenNodes)) { 1193 continue; 1195 } 1196 Element currRoot = currDoc; 1197 boolean sawAnnotation = false; 1198 for (Element globalComp = 1200 DOMUtil.getFirstVisibleChildElement(currRoot, fHiddenNodes); 1201 globalComp != null; 1202 globalComp = DOMUtil.getNextVisibleSiblingElement(globalComp, fHiddenNodes)) { 1203 DOMUtil.setHidden(globalComp, fHiddenNodes); 1204 String componentType = DOMUtil.getLocalName(globalComp); 1205 if (DOMUtil.getLocalName(globalComp).equals(SchemaSymbols.ELT_REDEFINE)) { 1207 currSchemaDoc.backupNSSupport((SchemaNamespaceSupport)fRedefine2NSSupport.get(globalComp)); 1209 for (Element redefinedComp = DOMUtil.getFirstVisibleChildElement(globalComp, fHiddenNodes); 1210 redefinedComp != null; 1211 redefinedComp = DOMUtil.getNextVisibleSiblingElement(redefinedComp, fHiddenNodes)) { 1212 String redefinedComponentType = DOMUtil.getLocalName(redefinedComp); 1213 DOMUtil.setHidden(redefinedComp, fHiddenNodes); 1214 if (redefinedComponentType.equals(SchemaSymbols.ELT_ATTRIBUTEGROUP)) { 1215 fAttributeGroupTraverser.traverseGlobal(redefinedComp, currSchemaDoc, currSG); 1216 } 1217 else if (redefinedComponentType.equals(SchemaSymbols.ELT_COMPLEXTYPE)) { 1218 fComplexTypeTraverser.traverseGlobal(redefinedComp, currSchemaDoc, currSG); 1219 } 1220 else if (redefinedComponentType.equals(SchemaSymbols.ELT_GROUP)) { 1221 fGroupTraverser.traverseGlobal(redefinedComp, currSchemaDoc, currSG); 1222 } 1223 else if (redefinedComponentType.equals(SchemaSymbols.ELT_SIMPLETYPE)) { 1224 fSimpleTypeTraverser.traverseGlobal(redefinedComp, currSchemaDoc, currSG); 1225 } 1226 else { 1232 reportSchemaError("s4s-elt-must-match.1", new Object [] {DOMUtil.getLocalName(globalComp), "(annotation | (simpleType | complexType | group | attributeGroup))*", redefinedComponentType}, redefinedComp); 1233 } 1234 } currSchemaDoc.restoreNSSupport(); 1236 } 1237 else if (componentType.equals(SchemaSymbols.ELT_ATTRIBUTE)) { 1238 fAttributeTraverser.traverseGlobal(globalComp, currSchemaDoc, currSG); 1239 } 1240 else if (componentType.equals(SchemaSymbols.ELT_ATTRIBUTEGROUP)) { 1241 fAttributeGroupTraverser.traverseGlobal(globalComp, currSchemaDoc, currSG); 1242 } 1243 else if (componentType.equals(SchemaSymbols.ELT_COMPLEXTYPE)) { 1244 fComplexTypeTraverser.traverseGlobal(globalComp, currSchemaDoc, currSG); 1245 } 1246 else if (componentType.equals(SchemaSymbols.ELT_ELEMENT)) { 1247 fElementTraverser.traverseGlobal(globalComp, currSchemaDoc, currSG); 1248 } 1249 else if (componentType.equals(SchemaSymbols.ELT_GROUP)) { 1250 fGroupTraverser.traverseGlobal(globalComp, currSchemaDoc, currSG); 1251 } 1252 else if (componentType.equals(SchemaSymbols.ELT_NOTATION)) { 1253 fNotationTraverser.traverse(globalComp, currSchemaDoc, currSG); 1254 } 1255 else if (componentType.equals(SchemaSymbols.ELT_SIMPLETYPE)) { 1256 fSimpleTypeTraverser.traverseGlobal(globalComp, currSchemaDoc, currSG); 1257 } 1258 else if (componentType.equals(SchemaSymbols.ELT_ANNOTATION)) { 1259 currSG.addAnnotation(fElementTraverser.traverseAnnotationDecl(globalComp, currSchemaDoc.getSchemaAttrs(), true, currSchemaDoc)); 1260 sawAnnotation = true; 1261 } 1262 else { 1263 reportSchemaError("s4s-elt-invalid-content.1", new Object [] {SchemaSymbols.ELT_SCHEMA, DOMUtil.getLocalName(globalComp)}, globalComp); 1264 } 1265 } 1267 if (!sawAnnotation) { 1268 String text = DOMUtil.getSyntheticAnnotation(currRoot); 1269 if (text != null) { 1270 currSG.addAnnotation(fElementTraverser.traverseSyntheticAnnotation(currRoot, text, currSchemaDoc.getSchemaAttrs(), true, currSchemaDoc)); 1271 } 1272 } 1273 1274 1275 if (annotationInfo != null) { 1276 XSAnnotationInfo info = currSchemaDoc.getAnnotations(); 1277 1278 if (info != null) { 1279 annotationInfo.add(doc2SystemId(currDoc)); 1280 annotationInfo.add(info); 1281 } 1282 } 1283 currSchemaDoc.returnSchemaAttrs(); 1285 DOMUtil.setHidden(currDoc, fHiddenNodes); 1286 1287 Vector currSchemaDepends = (Vector )fDependencyMap.get(currSchemaDoc); 1289 for (int i = 0; i < currSchemaDepends.size(); i++) { 1290 schemasToProcess.push(currSchemaDepends.elementAt(i)); 1291 } 1292 } } 1295 private Vector fReportedTNS = null; 1298 private final boolean needReportTNSError(String uri) { 1302 if (fReportedTNS == null) 1303 fReportedTNS = new Vector (); 1304 else if (fReportedTNS.contains(uri)) 1305 return false; 1306 fReportedTNS.addElement(uri); 1307 return true; 1308 } 1309 1310 private static final String [] COMP_TYPE = { 1311 null, "attribute declaration", 1313 "attribute group", 1314 "element declaration", 1315 "group", 1316 "identity constraint", 1317 "notation", 1318 "type definition", 1319 }; 1320 1321 private static final String [] CIRCULAR_CODES = { 1322 "Internal-Error", 1323 "Internal-Error", 1324 "src-attribute_group.3", 1325 "e-props-correct.6", 1326 "mg-props-correct.2", 1327 "Internal-Error", 1328 "Internal-Error", 1329 "st-props-correct.2", }; 1331 1332 protected Object getGlobalDecl(XSDocumentInfo currSchema, 1350 int declType, 1351 QName declToTraverse, 1352 Element elmNode) { 1353 1354 if (DEBUG_NODE_POOL) { 1355 System.out.println("TRAVERSE_GL: "+declToTraverse.toString()); 1356 } 1357 if (declToTraverse.uri != null && 1363 declToTraverse.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA) { 1364 if (declType == TYPEDECL_TYPE) { 1365 Object retObj = SchemaGrammar.SG_SchemaNS.getGlobalTypeDecl(declToTraverse.localpart); 1366 if (retObj != null) 1367 return retObj; 1368 } 1369 } 1370 1371 if (!currSchema.isAllowedNS(declToTraverse.uri)) { 1373 if (currSchema.needReportTNSError(declToTraverse.uri)) { 1375 String code = declToTraverse.uri == null ? "src-resolve.4.1" : "src-resolve.4.2"; 1376 reportSchemaError(code, new Object []{fDoc2SystemId.get(currSchema.fSchemaElement), declToTraverse.uri, declToTraverse.rawname}, elmNode); 1377 } 1378 return null; 1379 } 1380 1381 SchemaGrammar sGrammar = fGrammarBucket.getGrammar(declToTraverse.uri); 1383 if (sGrammar == null) { 1384 if (needReportTNSError(declToTraverse.uri)) 1385 reportSchemaError("src-resolve", new Object []{declToTraverse.rawname, COMP_TYPE[declType]}, elmNode); 1386 return null; 1387 } 1388 1389 Object retObj = null; 1391 switch (declType) { 1392 case ATTRIBUTE_TYPE : 1393 retObj = sGrammar.getGlobalAttributeDecl(declToTraverse.localpart); 1394 break; 1395 case ATTRIBUTEGROUP_TYPE : 1396 retObj = sGrammar.getGlobalAttributeGroupDecl(declToTraverse.localpart); 1397 break; 1398 case ELEMENT_TYPE : 1399 retObj = sGrammar.getGlobalElementDecl(declToTraverse.localpart); 1400 break; 1401 case GROUP_TYPE : 1402 retObj = sGrammar.getGlobalGroupDecl(declToTraverse.localpart); 1403 break; 1404 case IDENTITYCONSTRAINT_TYPE : 1405 retObj = sGrammar.getIDConstraintDecl(declToTraverse.localpart); 1406 break; 1407 case NOTATION_TYPE : 1408 retObj = sGrammar.getGlobalNotationDecl(declToTraverse.localpart); 1409 break; 1410 case TYPEDECL_TYPE : 1411 retObj = sGrammar.getGlobalTypeDecl(declToTraverse.localpart); 1412 break; 1413 } 1414 1415 if (retObj != null) 1417 return retObj; 1418 1419 XSDocumentInfo schemaWithDecl = null; 1420 Element decl = null; 1421 XSDocumentInfo declDoc = null; 1422 1423 String declKey = declToTraverse.uri == null? ","+declToTraverse.localpart: 1425 declToTraverse.uri+","+declToTraverse.localpart; 1426 switch (declType) { 1427 case ATTRIBUTE_TYPE : 1428 decl = (Element )fUnparsedAttributeRegistry.get(declKey); 1429 declDoc = (XSDocumentInfo)fUnparsedAttributeRegistrySub.get(declKey); 1430 break; 1431 case ATTRIBUTEGROUP_TYPE : 1432 decl = (Element )fUnparsedAttributeGroupRegistry.get(declKey); 1433 declDoc = (XSDocumentInfo)fUnparsedAttributeGroupRegistrySub.get(declKey); 1434 break; 1435 case ELEMENT_TYPE : 1436 decl = (Element )fUnparsedElementRegistry.get(declKey); 1437 declDoc = (XSDocumentInfo)fUnparsedElementRegistrySub.get(declKey); 1438 break; 1439 case GROUP_TYPE : 1440 decl = (Element )fUnparsedGroupRegistry.get(declKey); 1441 declDoc = (XSDocumentInfo)fUnparsedGroupRegistrySub.get(declKey); 1442 break; 1443 case IDENTITYCONSTRAINT_TYPE : 1444 decl = (Element )fUnparsedIdentityConstraintRegistry.get(declKey); 1445 declDoc = (XSDocumentInfo)fUnparsedIdentityConstraintRegistrySub.get(declKey); 1446 break; 1447 case NOTATION_TYPE : 1448 decl = (Element )fUnparsedNotationRegistry.get(declKey); 1449 declDoc = (XSDocumentInfo)fUnparsedNotationRegistrySub.get(declKey); 1450 break; 1451 case TYPEDECL_TYPE : 1452 decl = (Element )fUnparsedTypeRegistry.get(declKey); 1453 declDoc = (XSDocumentInfo)fUnparsedTypeRegistrySub.get(declKey); 1454 break; 1455 default: 1456 reportSchemaError("Internal-Error", new Object [] {"XSDHandler asked to locate component of type " + declType + "; it does not recognize this type!"}, elmNode); 1457 } 1458 1459 if (decl == null) { 1461 reportSchemaError("src-resolve", new Object []{declToTraverse.rawname, COMP_TYPE[declType]}, elmNode); 1462 return null; 1463 } 1464 1465 schemaWithDecl = findXSDocumentForDecl(currSchema, decl, declDoc); 1469 if (schemaWithDecl == null) { 1470 String code = declToTraverse.uri == null ? "src-resolve.4.1" : "src-resolve.4.2"; 1472 reportSchemaError(code, new Object []{fDoc2SystemId.get(currSchema.fSchemaElement), declToTraverse.uri, declToTraverse.rawname}, elmNode); 1473 return null; 1474 } 1475 if (DOMUtil.isHidden(decl, fHiddenNodes)) { 1479 String code = CIRCULAR_CODES[declType]; 1480 if (declType == TYPEDECL_TYPE) { 1481 if (SchemaSymbols.ELT_COMPLEXTYPE.equals(DOMUtil.getLocalName(decl))) 1482 code = "ct-props-correct.3"; 1483 } 1484 reportSchemaError(code, new Object [] {declToTraverse.prefix+":"+declToTraverse.localpart}, elmNode); 1486 return null; 1487 } 1488 1489 DOMUtil.setHidden(decl, fHiddenNodes); 1490 SchemaNamespaceSupport nsSupport = null; 1491 Element parent = DOMUtil.getParent(decl); 1493 if (DOMUtil.getLocalName(parent).equals(SchemaSymbols.ELT_REDEFINE)) 1494 nsSupport = (SchemaNamespaceSupport)fRedefine2NSSupport.get(parent); 1495 schemaWithDecl.backupNSSupport(nsSupport); 1498 1499 switch (declType) { 1501 case ATTRIBUTE_TYPE : 1502 retObj = fAttributeTraverser.traverseGlobal(decl, schemaWithDecl, sGrammar); 1503 break; 1504 case ATTRIBUTEGROUP_TYPE : 1505 retObj = fAttributeGroupTraverser.traverseGlobal(decl, schemaWithDecl, sGrammar); 1506 break; 1507 case ELEMENT_TYPE : 1508 retObj = fElementTraverser.traverseGlobal(decl, schemaWithDecl, sGrammar); 1509 break; 1510 case GROUP_TYPE : 1511 retObj = fGroupTraverser.traverseGlobal(decl, schemaWithDecl, sGrammar); 1512 break; 1513 case IDENTITYCONSTRAINT_TYPE : 1514 retObj = null; 1517 break; 1518 case NOTATION_TYPE : 1519 retObj = fNotationTraverser.traverse(decl, schemaWithDecl, sGrammar); 1520 break; 1521 case TYPEDECL_TYPE : 1522 if (DOMUtil.getLocalName(decl).equals(SchemaSymbols.ELT_COMPLEXTYPE)) 1523 retObj = fComplexTypeTraverser.traverseGlobal(decl, schemaWithDecl, sGrammar); 1524 else 1525 retObj = fSimpleTypeTraverser.traverseGlobal(decl, schemaWithDecl, sGrammar); 1526 } 1527 1528 schemaWithDecl.restoreNSSupport(); 1531 1532 return retObj; 1533 } 1535 Object getGrpOrAttrGrpRedefinedByRestriction(int type, QName name, XSDocumentInfo currSchema, Element elmNode) { 1545 String realName = name.uri != null?name.uri+","+name.localpart: 1546 ","+name.localpart; 1547 String nameToFind = null; 1548 switch (type) { 1549 case ATTRIBUTEGROUP_TYPE: 1550 nameToFind = (String )fRedefinedRestrictedAttributeGroupRegistry.get(realName); 1551 break; 1552 case GROUP_TYPE: 1553 nameToFind = (String )fRedefinedRestrictedGroupRegistry.get(realName); 1554 break; 1555 default: 1556 return null; 1557 } 1558 if (nameToFind == null) return null; 1559 int commaPos = nameToFind.indexOf(","); 1560 QName qNameToFind = new QName(XMLSymbols.EMPTY_STRING, nameToFind.substring(commaPos+1), 1561 nameToFind.substring(commaPos), (commaPos == 0)? null : nameToFind.substring(0, commaPos)); 1562 Object retObj = getGlobalDecl(currSchema, type, qNameToFind, elmNode); 1563 if(retObj == null) { 1564 switch (type) { 1565 case ATTRIBUTEGROUP_TYPE: 1566 reportSchemaError("src-redefine.7.2.1", new Object []{name.localpart}, elmNode); 1567 break; 1568 case GROUP_TYPE: 1569 reportSchemaError("src-redefine.6.2.1", new Object []{name.localpart}, elmNode); 1570 break; 1571 } 1572 return null; 1573 } 1574 return retObj; 1575 } 1577 protected void resolveKeyRefs() { 1587 for (int i=0; i<fKeyrefStackPos; i++) { 1588 XSDocumentInfo keyrefSchemaDoc = fKeyrefsMapXSDocumentInfo[i]; 1589 keyrefSchemaDoc.fNamespaceSupport.makeGlobal(); 1590 keyrefSchemaDoc.fNamespaceSupport.setEffectiveContext( fKeyrefNamespaceContext[i] ); 1591 SchemaGrammar keyrefGrammar = fGrammarBucket.getGrammar(keyrefSchemaDoc.fTargetNamespace); 1592 DOMUtil.setHidden(fKeyrefs[i], fHiddenNodes); 1595 fKeyrefTraverser.traverse(fKeyrefs[i], fKeyrefElems[i], keyrefSchemaDoc, keyrefGrammar); 1596 } 1597 } 1599 protected Hashtable getIDRegistry() { 1602 return fUnparsedIdentityConstraintRegistry; 1603 } 1604 protected Hashtable getIDRegistry_sub() { 1606 return fUnparsedIdentityConstraintRegistrySub; 1607 } 1608 1609 1610 1611 protected void storeKeyRef (Element keyrefToStore, XSDocumentInfo schemaDoc, 1614 XSElementDecl currElemDecl) { 1615 String keyrefName = DOMUtil.getAttrValue(keyrefToStore, SchemaSymbols.ATT_NAME); 1616 if (keyrefName.length() != 0) { 1617 String keyrefQName = schemaDoc.fTargetNamespace == null? 1618 "," + keyrefName: schemaDoc.fTargetNamespace+","+keyrefName; 1619 checkForDuplicateNames(keyrefQName, fUnparsedIdentityConstraintRegistry, fUnparsedIdentityConstraintRegistrySub, keyrefToStore, schemaDoc); 1620 } 1621 1623 if (fKeyrefStackPos == fKeyrefs.length) { 1625 Element [] elemArray = new Element [fKeyrefStackPos + INC_KEYREF_STACK_AMOUNT]; 1626 System.arraycopy(fKeyrefs, 0, elemArray, 0, fKeyrefStackPos); 1627 fKeyrefs = elemArray; 1628 XSElementDecl [] declArray = new XSElementDecl [fKeyrefStackPos + INC_KEYREF_STACK_AMOUNT]; 1629 System.arraycopy(fKeyrefElems, 0, declArray, 0, fKeyrefStackPos); 1630 fKeyrefElems = declArray; 1631 String [][] stringArray = new String [fKeyrefStackPos + INC_KEYREF_STACK_AMOUNT][]; 1632 System.arraycopy(fKeyrefNamespaceContext, 0, stringArray, 0, fKeyrefStackPos); 1633 fKeyrefNamespaceContext = stringArray; 1634 1635 XSDocumentInfo [] xsDocumentInfo = new XSDocumentInfo [fKeyrefStackPos + INC_KEYREF_STACK_AMOUNT]; 1636 System.arraycopy(fKeyrefsMapXSDocumentInfo, 0, xsDocumentInfo, 0, fKeyrefStackPos); 1637 fKeyrefsMapXSDocumentInfo = xsDocumentInfo; 1638 1639 } 1640 fKeyrefs[fKeyrefStackPos] = keyrefToStore; 1641 fKeyrefElems[fKeyrefStackPos] = currElemDecl; 1642 fKeyrefNamespaceContext[fKeyrefStackPos] = schemaDoc.fNamespaceSupport.getEffectiveLocalContext(); 1643 1644 fKeyrefsMapXSDocumentInfo[fKeyrefStackPos++] = schemaDoc; 1645 } 1647 1648 1656 private Element resolveSchema(XSDDescription desc, 1657 boolean mustResolve, Element referElement) { 1658 XMLInputSource schemaSource = null; 1659 try { 1660 schemaSource = XMLSchemaLoader.resolveDocument(desc, fLocationPairs, fEntityResolver); 1661 } 1662 catch (IOException ex) { 1663 if (mustResolve) { 1664 reportSchemaError("schema_reference.4", 1665 new Object []{desc.getLocationHints()[0]}, 1666 referElement); 1667 } 1668 else { 1669 reportSchemaWarning("schema_reference.4", 1670 new Object []{desc.getLocationHints()[0]}, 1671 referElement); 1672 } 1673 } 1674 if (schemaSource instanceof DOMInputSource) { 1675 fHiddenNodes.clear(); 1676 Node node = ((DOMInputSource)schemaSource).getNode(); 1677 1678 if (node instanceof Document ) { 1679 return DOMUtil.getRoot((Document ) node); 1680 } 1681 else if (node instanceof Element ) { 1682 return (Element ) node; 1683 } 1684 else { 1685 return null; 1686 } 1687 } else if (schemaSource instanceof SAXInputSource) { 1689 XMLReader parser = ((SAXInputSource)schemaSource).getXMLReader(); 1690 InputSource inputSource = ((SAXInputSource)schemaSource).getInputSource(); 1691 boolean namespacePrefixes = false; 1692 if (parser != null) { 1693 try { 1694 namespacePrefixes = parser.getFeature(NAMESPACE_PREFIXES); 1695 } 1696 catch (SAXException se) {} 1697 } 1698 else { 1699 try { 1700 parser = XMLReaderFactory.createXMLReader(); 1701 } 1702 catch (SAXException se) { 1705 parser = new SAXParser(); 1706 } 1707 try { 1708 parser.setFeature(NAMESPACE_PREFIXES, true); 1709 namespacePrefixes = true; 1710 } 1711 catch (SAXException se) {} 1712 } 1713 boolean stringsInternalized = false; 1716 try { 1717 stringsInternalized = parser.getFeature(STRING_INTERNING); 1718 } 1719 catch (SAXException exc) { 1720 } 1723 if (fXSContentHandler == null) { 1724 fXSContentHandler = new SchemaContentHandler(); 1725 } 1726 fXSContentHandler.reset(fSchemaParser, fSymbolTable, 1727 namespacePrefixes, stringsInternalized); 1728 parser.setContentHandler(fXSContentHandler); 1729 parser.setErrorHandler(fErrorReporter.getSAXErrorHandler()); 1730 try { 1731 parser.parse(inputSource); 1732 } 1733 catch (SAXException se) { 1734 return null; 1735 } 1736 catch (IOException ioe) { 1737 return null; 1738 } 1739 Document root = fXSContentHandler.getDocument(); 1740 if (root == null) { 1741 return null; 1743 } 1744 return DOMUtil.getRoot(root); 1745 } 1746 return getSchemaDocument(desc.getTargetNamespace(), schemaSource, mustResolve, desc.getContextType(), referElement); 1747 } 1749 1758 private Element getSchemaDocument(String schemaNamespace, XMLInputSource schemaSource, 1759 boolean mustResolve, short referType, Element referElement) { 1760 1761 boolean hasInput = true; 1762 Element schemaElement = null; 1764 try { 1765 if (schemaSource != null && 1772 (schemaSource.getSystemId() != null || 1773 schemaSource.getByteStream() != null || 1774 schemaSource.getCharacterStream() != null)) { 1775 1776 XSDKey key = null; 1781 String schemaId = null; 1782 if (referType != XSDDescription.CONTEXT_PREPARSE){ 1783 schemaId = XMLEntityManager.expandSystemId(schemaSource.getSystemId(), schemaSource.getBaseSystemId(), false); 1784 key = new XSDKey(schemaId, referType, schemaNamespace); 1785 if((schemaElement = (Element )fTraversed.get(key)) != null) { 1786 fLastSchemaWasDuplicate = true; 1787 return schemaElement; 1788 } 1789 } 1790 1791 fSchemaParser.parse(schemaSource); 1792 schemaElement = fSchemaParser.getDocument2() == null ? null: DOMUtil.getRoot(fSchemaParser.getDocument2()); 1793 1794 if (key != null) 1797 fTraversed.put(key, schemaElement ); 1798 if (schemaId != null) 1799 fDoc2SystemId.put(schemaElement, schemaId ); 1800 fLastSchemaWasDuplicate = false; 1801 return schemaElement; 1802 } 1803 else { 1804 hasInput = false; 1805 } 1806 } 1807 catch (IOException ex) { 1808 } 1809 1810 if (mustResolve) { 1813 if (hasInput) { 1814 reportSchemaError("schema_reference.4", 1815 new Object []{schemaSource.getSystemId()}, 1816 referElement); 1817 } 1818 else { 1819 reportSchemaError("schema_reference.4", 1820 new Object []{schemaSource == null ? "" : schemaSource.getSystemId()}, 1821 referElement); 1822 } 1823 } 1824 else if (hasInput) { 1825 reportSchemaWarning("schema_reference.4", 1826 new Object []{schemaSource.getSystemId()}, 1827 referElement); 1828 } 1829 1830 fLastSchemaWasDuplicate = false; 1831 return null; 1832 } 1834 1838 private void createTraversers() { 1840 fAttributeChecker = new XSAttributeChecker(this); 1841 fAttributeGroupTraverser = new XSDAttributeGroupTraverser(this, fAttributeChecker); 1842 fAttributeTraverser = new XSDAttributeTraverser(this, fAttributeChecker); 1843 fComplexTypeTraverser = new XSDComplexTypeTraverser(this, fAttributeChecker); 1844 fElementTraverser = new XSDElementTraverser(this, fAttributeChecker); 1845 fGroupTraverser = new XSDGroupTraverser(this, fAttributeChecker); 1846 fKeyrefTraverser = new XSDKeyrefTraverser(this, fAttributeChecker); 1847 fNotationTraverser = new XSDNotationTraverser(this, fAttributeChecker); 1848 fSimpleTypeTraverser = new XSDSimpleTypeTraverser(this, fAttributeChecker); 1849 fUniqueOrKeyTraverser = new XSDUniqueOrKeyTraverser(this, fAttributeChecker); 1850 fWildCardTraverser = new XSDWildcardTraverser(this, fAttributeChecker); 1851 } 1853 void prepareForParse() { 1856 fTraversed.clear(); 1857 fDoc2SystemId.clear(); 1858 fHiddenNodes.clear(); 1859 fLastSchemaWasDuplicate = false; 1860 } 1861 1862 void prepareForTraverse() { 1865 fUnparsedAttributeRegistry.clear(); 1866 fUnparsedAttributeGroupRegistry.clear(); 1867 fUnparsedElementRegistry.clear(); 1868 fUnparsedGroupRegistry.clear(); 1869 fUnparsedIdentityConstraintRegistry.clear(); 1870 fUnparsedNotationRegistry.clear(); 1871 fUnparsedTypeRegistry.clear(); 1872 1873 fUnparsedAttributeRegistrySub.clear(); 1874 fUnparsedAttributeGroupRegistrySub.clear(); 1875 fUnparsedElementRegistrySub.clear(); 1876 fUnparsedGroupRegistrySub.clear(); 1877 fUnparsedIdentityConstraintRegistrySub.clear(); 1878 fUnparsedNotationRegistrySub.clear(); 1879 fUnparsedTypeRegistrySub.clear(); 1880 1881 fXSDocumentInfoRegistry.clear(); 1882 fDependencyMap.clear(); 1883 fDoc2XSDocumentMap.clear(); 1884 fRedefine2XSDMap.clear(); 1885 fRedefine2NSSupport.clear(); 1886 fAllTNSs.removeAllElements(); 1887 fImportMap.clear(); 1888 fRoot = null; 1889 1890 for (int i = 0; i < fLocalElemStackPos; i++) { 1892 fParticle[i] = null; 1893 fLocalElementDecl[i] = null; 1894 fLocalElementDecl_schema[i] = null; 1895 fLocalElemNamespaceContext[i] = null; 1896 } 1897 fLocalElemStackPos = 0; 1898 1899 for (int i = 0; i < fKeyrefStackPos; i++) { 1901 fKeyrefs[i] = null; 1902 fKeyrefElems[i] = null; 1903 fKeyrefNamespaceContext[i] = null; 1904 fKeyrefsMapXSDocumentInfo[i] = null; 1905 } 1906 fKeyrefStackPos = 0; 1907 1908 if (fAttributeChecker == null) { 1910 createTraversers(); 1911 } 1912 1913 fAttributeChecker.reset(fSymbolTable); 1915 fAttributeGroupTraverser.reset(fSymbolTable, fValidateAnnotations); 1916 fAttributeTraverser.reset(fSymbolTable, fValidateAnnotations); 1917 fComplexTypeTraverser.reset(fSymbolTable, fValidateAnnotations); 1918 fElementTraverser.reset(fSymbolTable, fValidateAnnotations); 1919 fGroupTraverser.reset(fSymbolTable, fValidateAnnotations); 1920 fKeyrefTraverser.reset(fSymbolTable, fValidateAnnotations); 1921 fNotationTraverser.reset(fSymbolTable, fValidateAnnotations); 1922 fSimpleTypeTraverser.reset(fSymbolTable, fValidateAnnotations); 1923 fUniqueOrKeyTraverser.reset(fSymbolTable, fValidateAnnotations); 1924 fWildCardTraverser.reset(fSymbolTable, fValidateAnnotations); 1925 1926 fRedefinedRestrictedAttributeGroupRegistry.clear(); 1927 fRedefinedRestrictedGroupRegistry.clear(); 1928 } 1929 public void setDeclPool (XSDeclarationPool declPool){ 1930 fDeclPool = declPool; 1931 } 1932 1933 public void reset(XMLComponentManager componentManager) { 1934 1935 fSymbolTable = (SymbolTable) componentManager.getProperty(SYMBOL_TABLE); 1937 1938 fEntityResolver = (XMLEntityResolver) componentManager.getProperty(ENTITY_MANAGER); 1940 XMLEntityResolver er = (XMLEntityResolver)componentManager.getProperty(ENTITY_RESOLVER); 1941 if (er != null) 1942 fSchemaParser.setEntityResolver(er); 1943 1944 fErrorReporter = 1946 (XMLErrorReporter) componentManager.getProperty(ERROR_REPORTER); 1947 try { 1948 XMLErrorHandler currErrorHandler = fErrorReporter.getErrorHandler(); 1949 if (currErrorHandler != fSchemaParser.getProperty(ERROR_HANDLER)) { 1953 fSchemaParser.setProperty(ERROR_HANDLER, (currErrorHandler != null) ? currErrorHandler : new DefaultErrorHandler()); 1954 if (fAnnotationValidator != null) { 1955 fAnnotationValidator.setProperty(ERROR_HANDLER, (currErrorHandler != null) ? currErrorHandler : new DefaultErrorHandler()); 1956 } 1957 } 1958 } catch (XMLConfigurationException e) { 1959 } 1960 1961 try { 1962 fValidateAnnotations = componentManager.getFeature(VALIDATE_ANNOTATIONS); 1963 } catch (XMLConfigurationException e) { 1964 fValidateAnnotations = false; 1965 } 1966 1967 try { 1968 fHonourAllSchemaLocations = componentManager.getFeature(HONOUR_ALL_SCHEMALOCATIONS); 1969 } catch (XMLConfigurationException e) { 1970 fHonourAllSchemaLocations = false; 1971 } 1972 1973 try { 1974 fSchemaParser.setFeature( 1975 CONTINUE_AFTER_FATAL_ERROR, 1976 fErrorReporter.getFeature(CONTINUE_AFTER_FATAL_ERROR)); 1977 } catch (XMLConfigurationException e) { 1978 } 1979 1980 try { 1981 fSchemaParser.setFeature( 1982 ALLOW_JAVA_ENCODINGS, 1983 componentManager.getFeature(ALLOW_JAVA_ENCODINGS)); 1984 } catch (XMLConfigurationException e) { 1985 } 1986 try { 1987 fSchemaParser.setFeature( 1988 STANDARD_URI_CONFORMANT_FEATURE, 1989 componentManager.getFeature(STANDARD_URI_CONFORMANT_FEATURE)); 1990 } catch (XMLConfigurationException e) { 1991 } 1992 1993 try { 1994 fGrammarPool = 1995 (XMLGrammarPool) componentManager.getProperty(XMLGRAMMAR_POOL); 1996 } catch (XMLConfigurationException e) { 1997 fGrammarPool = null; 1998 } 1999 try { 2001 fSchemaParser.setFeature( DISALLOW_DOCTYPE, 2002 componentManager.getFeature(DISALLOW_DOCTYPE)); 2003 } catch (XMLConfigurationException e) { 2004 } 2005 try { 2006 Object security = componentManager.getProperty(SECURITY_MANAGER); 2007 if (security != null){ 2008 fSchemaParser.setProperty(SECURITY_MANAGER, security); 2009 } 2010 } catch (XMLConfigurationException e) { 2011 } 2012 2013 } 2015 2016 2020 void traverseLocalElements() { 2021 fElementTraverser.fDeferTraversingLocalElements = false; 2022 2023 for (int i = 0; i < fLocalElemStackPos; i++) { 2024 Element currElem = fLocalElementDecl[i]; 2025 XSDocumentInfo currSchema = fLocalElementDecl_schema[i]; 2028 SchemaGrammar currGrammar = fGrammarBucket.getGrammar(currSchema.fTargetNamespace); 2029 fElementTraverser.traverseLocal (fParticle[i], currElem, currSchema, currGrammar, fAllContext[i], fParent[i], fLocalElemNamespaceContext[i]); 2030 if (fParticle[i].fType == XSParticleDecl.PARTICLE_EMPTY) { 2032 XSModelGroupImpl group = null; 2033 if (fParent[i] instanceof XSComplexTypeDecl) { 2034 XSParticle p = ((XSComplexTypeDecl)fParent[i]).getParticle(); 2035 if (p != null) 2036 group = (XSModelGroupImpl)p.getTerm(); 2037 } 2038 else { 2039 group = ((XSGroupDecl)fParent[i]).fModelGroup; 2040 } 2041 if (group != null) 2042 removeParticle(group, fParticle[i]); 2043 } 2044 } 2045 } 2046 2047 private boolean removeParticle(XSModelGroupImpl group, XSParticleDecl particle) { 2048 XSParticleDecl member; 2049 for (int i = 0; i < group.fParticleCount; i++) { 2050 member = group.fParticles[i]; 2051 if (member == particle) { 2052 for (int j = i; j < group.fParticleCount-1; j++) 2053 group.fParticles[j] = group.fParticles[j+1]; 2054 group.fParticleCount--; 2055 return true; 2056 } 2057 if (member.fType == XSParticleDecl.PARTICLE_MODELGROUP) { 2058 if (removeParticle((XSModelGroupImpl)member.fValue, particle)) 2059 return true; 2060 } 2061 } 2062 return false; 2063 } 2064 2065 void fillInLocalElemInfo(Element elmDecl, 2068 XSDocumentInfo schemaDoc, 2069 int allContextFlags, 2070 XSObject parent, 2071 XSParticleDecl particle) { 2072 2073 if (fParticle.length == fLocalElemStackPos) { 2075 XSParticleDecl[] newStackP = new XSParticleDecl[fLocalElemStackPos+INC_STACK_SIZE]; 2077 System.arraycopy(fParticle, 0, newStackP, 0, fLocalElemStackPos); 2078 fParticle = newStackP; 2079 Element [] newStackE = new Element [fLocalElemStackPos+INC_STACK_SIZE]; 2080 System.arraycopy(fLocalElementDecl, 0, newStackE, 0, fLocalElemStackPos); 2081 fLocalElementDecl = newStackE; 2082 XSDocumentInfo [] newStackE_schema = new XSDocumentInfo[fLocalElemStackPos+INC_STACK_SIZE]; 2083 System.arraycopy(fLocalElementDecl_schema, 0, newStackE_schema, 0, fLocalElemStackPos); 2084 fLocalElementDecl_schema = newStackE_schema; 2085 int[] newStackI = new int[fLocalElemStackPos+INC_STACK_SIZE]; 2086 System.arraycopy(fAllContext, 0, newStackI, 0, fLocalElemStackPos); 2087 fAllContext = newStackI; 2088 XSObject[] newStackC = new XSObject[fLocalElemStackPos+INC_STACK_SIZE]; 2089 System.arraycopy(fParent, 0, newStackC, 0, fLocalElemStackPos); 2090 fParent = newStackC; 2091 String [][] newStackN = new String [fLocalElemStackPos+INC_STACK_SIZE][]; 2092 System.arraycopy(fLocalElemNamespaceContext, 0, newStackN, 0, fLocalElemStackPos); 2093 fLocalElemNamespaceContext = newStackN; 2094 } 2095 2096 fParticle[fLocalElemStackPos] = particle; 2097 fLocalElementDecl[fLocalElemStackPos] = elmDecl; 2098 fLocalElementDecl_schema[fLocalElemStackPos] = schemaDoc; 2099 fAllContext[fLocalElemStackPos] = allContextFlags; 2100 fParent[fLocalElemStackPos] = parent; 2101 fLocalElemNamespaceContext[fLocalElemStackPos++] = schemaDoc.fNamespaceSupport.getEffectiveLocalContext(); 2102 } 2104 2112 void checkForDuplicateNames(String qName, 2113 Hashtable registry, Hashtable registry_sub, Element currComp, 2114 XSDocumentInfo currSchema) { 2115 Object objElem = null; 2116 if ((objElem = registry.get(qName)) == null) { 2119 registry.put(qName, currComp); 2121 registry_sub.put(qName, currSchema); 2122 2123 } 2124 else { 2125 Element collidingElem = (Element )objElem; 2126 XSDocumentInfo collidingElemSchema = (XSDocumentInfo)registry_sub.get(qName); 2127 if (collidingElem == currComp) return; 2128 Element elemParent = null; 2129 XSDocumentInfo redefinedSchema = null; 2130 boolean collidedWithRedefine = true; 2133 if ((DOMUtil.getLocalName((elemParent = DOMUtil.getParent(collidingElem))).equals(SchemaSymbols.ELT_REDEFINE))) { 2134 redefinedSchema = (XSDocumentInfo)(fRedefine2XSDMap.get(elemParent)); 2135 } 2137 else if ((DOMUtil.getLocalName(DOMUtil.getParent(currComp)).equals(SchemaSymbols.ELT_REDEFINE))) { 2138 redefinedSchema = collidingElemSchema; 2139 collidedWithRedefine = false; 2140 } 2141 if (redefinedSchema != null) { if(collidingElemSchema == currSchema){ 2145 reportSchemaError("sch-props-correct.2", new Object []{qName}, currComp); 2146 return; 2147 } 2148 2149 String newName = qName.substring(qName.lastIndexOf(',')+1)+REDEF_IDENTIFIER; 2150 if (redefinedSchema == currSchema) { currComp.setAttribute(SchemaSymbols.ATT_NAME, newName); 2153 if (currSchema.fTargetNamespace == null){ 2154 registry.put(","+newName, currComp); 2155 registry_sub.put(","+newName, currSchema); 2156 } 2157 else{ 2158 registry.put(currSchema.fTargetNamespace+","+newName, currComp); 2159 registry_sub.put(currSchema.fTargetNamespace+","+newName, currSchema); 2160 } 2161 if (currSchema.fTargetNamespace == null) 2163 checkForDuplicateNames(","+newName, registry, registry_sub, currComp, currSchema); 2164 else 2165 checkForDuplicateNames(currSchema.fTargetNamespace+","+newName, registry, registry_sub, currComp, currSchema); 2166 } 2167 else { if (collidedWithRedefine) { 2169 if (currSchema.fTargetNamespace == null) 2170 checkForDuplicateNames(","+newName, registry, registry_sub, currComp, currSchema); 2171 else 2172 checkForDuplicateNames(currSchema.fTargetNamespace+","+newName, registry, registry_sub, currComp, currSchema); 2173 } 2174 else { 2175 reportSchemaError("sch-props-correct.2", new Object [] {qName}, currComp); 2177 } 2178 } 2179 } 2180 else { 2181 reportSchemaError("sch-props-correct.2", new Object []{qName}, currComp); 2183 } 2184 } 2185 } 2187 private void renameRedefiningComponents(XSDocumentInfo currSchema, 2194 Element child, String componentType, 2195 String oldName, String newName) { 2196 if (componentType.equals(SchemaSymbols.ELT_SIMPLETYPE)) { 2197 Element grandKid = DOMUtil.getFirstChildElement(child); 2198 if (grandKid == null) { 2199 reportSchemaError("src-redefine.5.a.a", null, child); 2200 } 2201 else { 2202 String grandKidName = DOMUtil.getLocalName(grandKid); 2203 if (grandKidName.equals(SchemaSymbols.ELT_ANNOTATION)) { 2204 grandKid = DOMUtil.getNextSiblingElement(grandKid); 2205 } 2206 if (grandKid == null) { 2207 reportSchemaError("src-redefine.5.a.a", null, child); 2208 } 2209 else { 2210 grandKidName = DOMUtil.getLocalName(grandKid); 2211 if (!grandKidName.equals(SchemaSymbols.ELT_RESTRICTION)) { 2212 reportSchemaError("src-redefine.5.a.b", new Object []{grandKidName}, child); 2213 } 2214 else { 2215 Object [] attrs = fAttributeChecker.checkAttributes(grandKid, false, currSchema); 2216 QName derivedBase = (QName)attrs[XSAttributeChecker.ATTIDX_BASE]; 2217 if (derivedBase == null || 2218 derivedBase.uri != currSchema.fTargetNamespace || 2219 !derivedBase.localpart.equals(oldName)) { 2220 reportSchemaError("src-redefine.5.a.c", 2221 new Object []{grandKidName, 2222 (currSchema.fTargetNamespace==null?"":currSchema.fTargetNamespace) 2223 + "," + oldName}, 2224 child); 2225 } 2226 else { 2227 if (derivedBase.prefix != null && derivedBase.prefix.length() > 0) 2229 grandKid.setAttribute( SchemaSymbols.ATT_BASE, 2230 derivedBase.prefix + ":" + newName ); 2231 else 2232 grandKid.setAttribute( SchemaSymbols.ATT_BASE, newName ); 2233 } 2235 fAttributeChecker.returnAttrArray(attrs, currSchema); 2236 } 2237 } 2238 } 2239 } 2240 else if (componentType.equals(SchemaSymbols.ELT_COMPLEXTYPE)) { 2241 Element grandKid = DOMUtil.getFirstChildElement(child); 2242 if (grandKid == null) { 2243 reportSchemaError("src-redefine.5.b.a", null, child); 2244 } 2245 else { 2246 if (DOMUtil.getLocalName(grandKid).equals(SchemaSymbols.ELT_ANNOTATION)) { 2247 grandKid = DOMUtil.getNextSiblingElement(grandKid); 2248 } 2249 if (grandKid == null) { 2250 reportSchemaError("src-redefine.5.b.a", null, child); 2251 } 2252 else { 2253 Element greatGrandKid = DOMUtil.getFirstChildElement(grandKid); 2255 if (greatGrandKid == null) { 2256 reportSchemaError("src-redefine.5.b.b", null, grandKid); 2257 } 2258 else { 2259 String greatGrandKidName = DOMUtil.getLocalName(greatGrandKid); 2260 if (greatGrandKidName.equals(SchemaSymbols.ELT_ANNOTATION)) { 2261 greatGrandKid = DOMUtil.getNextSiblingElement(greatGrandKid); 2262 } 2263 if (greatGrandKid == null) { 2264 reportSchemaError("src-redefine.5.b.b", null, grandKid); 2265 } 2266 else { 2267 greatGrandKidName = DOMUtil.getLocalName(greatGrandKid); 2268 if (!greatGrandKidName.equals(SchemaSymbols.ELT_RESTRICTION) && 2269 !greatGrandKidName.equals(SchemaSymbols.ELT_EXTENSION)) { 2270 reportSchemaError("src-redefine.5.b.c", new Object []{greatGrandKidName}, greatGrandKid); 2271 } 2272 else { 2273 Object [] attrs = fAttributeChecker.checkAttributes(greatGrandKid, false, currSchema); 2274 QName derivedBase = (QName)attrs[XSAttributeChecker.ATTIDX_BASE]; 2275 if (derivedBase == null || 2276 derivedBase.uri != currSchema.fTargetNamespace || 2277 !derivedBase.localpart.equals(oldName)) { 2278 reportSchemaError("src-redefine.5.b.d", 2279 new Object []{greatGrandKidName, 2280 (currSchema.fTargetNamespace==null?"":currSchema.fTargetNamespace) 2281 + "," + oldName}, 2282 greatGrandKid); 2283 } 2284 else { 2285 if (derivedBase.prefix != null && derivedBase.prefix.length() > 0) 2287 greatGrandKid.setAttribute( SchemaSymbols.ATT_BASE, 2288 derivedBase.prefix + ":" + newName ); 2289 else 2290 greatGrandKid.setAttribute( SchemaSymbols.ATT_BASE, 2291 newName ); 2292 } 2294 } 2295 } 2296 } 2297 } 2298 } 2299 } 2300 else if (componentType.equals(SchemaSymbols.ELT_ATTRIBUTEGROUP)) { 2301 String processedBaseName = (currSchema.fTargetNamespace == null)? 2302 ","+oldName:currSchema.fTargetNamespace+","+oldName; 2303 int attGroupRefsCount = changeRedefineGroup(processedBaseName, componentType, newName, child, currSchema); 2304 if (attGroupRefsCount > 1) { 2305 reportSchemaError("src-redefine.7.1", new Object []{new Integer (attGroupRefsCount)}, child); 2306 } 2307 else if (attGroupRefsCount == 1) { 2308 } 2310 else 2311 if (currSchema.fTargetNamespace == null) 2312 fRedefinedRestrictedAttributeGroupRegistry.put(processedBaseName, ","+newName); 2313 else 2314 fRedefinedRestrictedAttributeGroupRegistry.put(processedBaseName, currSchema.fTargetNamespace+","+newName); 2315 } 2316 else if (componentType.equals(SchemaSymbols.ELT_GROUP)) { 2317 String processedBaseName = (currSchema.fTargetNamespace == null)? 2318 ","+oldName:currSchema.fTargetNamespace+","+oldName; 2319 int groupRefsCount = changeRedefineGroup(processedBaseName, componentType, newName, child, currSchema); 2320 if (groupRefsCount > 1) { 2321 reportSchemaError("src-redefine.6.1.1", new Object []{new Integer (groupRefsCount)}, child); 2322 } 2323 else if (groupRefsCount == 1) { 2324 } 2326 else { 2327 if (currSchema.fTargetNamespace == null) 2328 fRedefinedRestrictedGroupRegistry.put(processedBaseName, ","+newName); 2329 else 2330 fRedefinedRestrictedGroupRegistry.put(processedBaseName, currSchema.fTargetNamespace+","+newName); 2331 } 2332 } 2333 else { 2334 reportSchemaError("Internal-Error", new Object [] {"could not handle this particular <redefine>; please submit your schemas and instance document in a bug report!"}, child); 2335 } 2336 } 2340 private String findQName(String name, XSDocumentInfo schemaDoc) { 2352 SchemaNamespaceSupport currNSMap = schemaDoc.fNamespaceSupport; 2353 int colonPtr = name.indexOf(':'); 2354 String prefix = XMLSymbols.EMPTY_STRING; 2355 if (colonPtr > 0) 2356 prefix = name.substring(0, colonPtr); 2357 String uri = currNSMap.getURI(fSymbolTable.addSymbol(prefix)); 2358 String localpart = (colonPtr == 0)?name:name.substring(colonPtr+1); 2359 if (prefix == XMLSymbols.EMPTY_STRING && uri == null && schemaDoc.fIsChameleonSchema) 2360 uri = schemaDoc.fTargetNamespace; 2361 if (uri == null) 2362 return ","+localpart; 2363 return uri+","+localpart; 2364 } 2366 private int changeRedefineGroup(String originalQName, String elementSought, 2374 String newName, Element curr, XSDocumentInfo schemaDoc) { 2375 int result = 0; 2376 for (Element child = DOMUtil.getFirstChildElement(curr); 2377 child != null; child = DOMUtil.getNextSiblingElement(child)) { 2378 String name = DOMUtil.getLocalName(child); 2379 if (!name.equals(elementSought)) 2380 result += changeRedefineGroup(originalQName, elementSought, newName, child, schemaDoc); 2381 else { 2382 String ref = child.getAttribute( SchemaSymbols.ATT_REF ); 2383 if (ref.length() != 0) { 2384 String processedRef = findQName(ref, schemaDoc); 2385 if (originalQName.equals(processedRef)) { 2386 String prefix = XMLSymbols.EMPTY_STRING; 2387 int colonptr = ref.indexOf(":"); 2388 if (colonptr > 0) { 2389 prefix = ref.substring(0,colonptr); 2390 child.setAttribute(SchemaSymbols.ATT_REF, prefix + ":" + newName); 2391 } 2392 else 2393 child.setAttribute(SchemaSymbols.ATT_REF, newName); 2394 result++; 2395 if (elementSought.equals(SchemaSymbols.ELT_GROUP)) { 2396 String minOccurs = child.getAttribute( SchemaSymbols.ATT_MINOCCURS ); 2397 String maxOccurs = child.getAttribute( SchemaSymbols.ATT_MAXOCCURS ); 2398 if (!((maxOccurs.length() == 0 || maxOccurs.equals("1")) 2399 && (minOccurs.length() == 0 || minOccurs.equals("1")))) { 2400 reportSchemaError("src-redefine.6.1.2", new Object [] {ref}, child); 2401 } 2402 } 2403 } 2404 } } 2406 } 2407 return result; 2408 } 2410 private XSDocumentInfo findXSDocumentForDecl(XSDocumentInfo currSchema, 2419 Element decl, XSDocumentInfo decl_Doc) { 2420 2421 if (DEBUG_NODE_POOL) { 2422 System.out.println("DOCUMENT NS:"+ currSchema.fTargetNamespace+" hashcode:"+ ((Object )currSchema.fSchemaElement).hashCode()); 2423 } 2424 Object temp = decl_Doc; 2425 if (temp == null) { 2426 return null; 2428 } 2429 XSDocumentInfo declDocInfo = (XSDocumentInfo)temp; 2430 return declDocInfo; 2431 2444 } 2446 private boolean nonAnnotationContent(Element elem) { 2448 for(Element child = DOMUtil.getFirstChildElement(elem); child != null; child = DOMUtil.getNextSiblingElement(child)) { 2449 if(!(DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION))) return true; 2450 } 2451 return false; 2452 } 2454 private void setSchemasVisible(XSDocumentInfo startSchema) { 2455 if (DOMUtil.isHidden(startSchema.fSchemaElement, fHiddenNodes)) { 2456 DOMUtil.setVisible(startSchema.fSchemaElement, fHiddenNodes); 2458 Vector dependingSchemas = (Vector )fDependencyMap.get(startSchema); 2459 for (int i = 0; i < dependingSchemas.size(); i++) { 2460 setSchemasVisible((XSDocumentInfo)dependingSchemas.elementAt(i)); 2461 } 2462 } 2463 } 2466 private SimpleLocator xl = new SimpleLocator(); 2467 2468 2473 public SimpleLocator element2Locator(Element e) { 2474 if (!( e instanceof ElementImpl)) 2475 return null; 2476 2477 SimpleLocator l = new SimpleLocator(); 2478 return element2Locator(e, l) ? l : null; 2479 } 2480 2481 2486 public boolean element2Locator(Element e, SimpleLocator l) { 2487 if (l == null) 2488 return false; 2489 if (e instanceof ElementImpl) { 2490 ElementImpl ele = (ElementImpl)e; 2491 Document doc = ele.getOwnerDocument(); 2493 String sid = (String )fDoc2SystemId.get(DOMUtil.getRoot(doc)); 2494 int line = ele.getLineNumber(); 2496 int column = ele.getColumnNumber(); 2497 l.setValues(sid, sid, line, column, ele.getCharacterOffset()); 2498 return true; 2499 } 2500 return false; 2501 } 2502 2503 void reportSchemaError(String key, Object [] args, Element ele) { 2504 if (element2Locator(ele, xl)) { 2505 fErrorReporter.reportError(xl, XSMessageFormatter.SCHEMA_DOMAIN, 2506 key, args, XMLErrorReporter.SEVERITY_ERROR); 2507 } 2508 else { 2509 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 2510 key, args, XMLErrorReporter.SEVERITY_ERROR); 2511 } 2512 } 2513 2514 void reportSchemaWarning(String key, Object [] args, Element ele) { 2515 if (element2Locator(ele, xl)) { 2516 fErrorReporter.reportError(xl, XSMessageFormatter.SCHEMA_DOMAIN, 2517 key, args, XMLErrorReporter.SEVERITY_WARNING); 2518 } 2519 else { 2520 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN, 2521 key, args, XMLErrorReporter.SEVERITY_WARNING); 2522 } 2523 } 2524 2525 2531 private static class XSAnnotationGrammarPool implements XMLGrammarPool { 2532 2533 private XSGrammarBucket fGrammarBucket; 2534 private Grammar [] fInitialGrammarSet; 2535 2536 public Grammar[] retrieveInitialGrammarSet(String grammarType) { 2537 if (grammarType == XMLGrammarDescription.XML_SCHEMA) { 2538 if (fInitialGrammarSet == null) { 2539 if (fGrammarBucket == null) { 2540 fInitialGrammarSet = new Grammar [] {SchemaGrammar.SG_Schema4Annotations}; 2541 } 2542 else { 2543 SchemaGrammar [] schemaGrammars = fGrammarBucket.getGrammars(); 2544 2549 for (int i = 0; i < schemaGrammars.length; ++i) { 2550 if (SchemaSymbols.URI_SCHEMAFORSCHEMA.equals(schemaGrammars[i].getTargetNamespace())) { 2551 fInitialGrammarSet = schemaGrammars; 2552 return fInitialGrammarSet; 2553 } 2554 } 2555 Grammar [] grammars = new Grammar[schemaGrammars.length + 1]; 2556 System.arraycopy(schemaGrammars, 0, grammars, 0, schemaGrammars.length); 2557 grammars[grammars.length - 1] = SchemaGrammar.SG_Schema4Annotations; 2558 fInitialGrammarSet = grammars; 2559 } 2560 } 2561 return fInitialGrammarSet; 2562 } 2563 return new Grammar[0]; 2564 } 2565 2566 public void cacheGrammars(String grammarType, Grammar[] grammars) { 2567 2568 } 2569 2570 public Grammar retrieveGrammar(XMLGrammarDescription desc) { 2571 if (desc.getGrammarType() == XMLGrammarDescription.XML_SCHEMA) { 2572 final String tns = ((XMLSchemaDescription) desc).getTargetNamespace(); 2573 if (fGrammarBucket != null) { 2574 Grammar grammar = fGrammarBucket.getGrammar(tns); 2575 if (grammar != null) { 2576 return grammar; 2577 } 2578 } 2579 if (SchemaSymbols.URI_SCHEMAFORSCHEMA.equals(tns)) { 2580 return SchemaGrammar.SG_Schema4Annotations; 2581 } 2582 } 2583 return null; 2584 } 2585 2586 public void refreshGrammars(XSGrammarBucket gBucket) { 2587 fGrammarBucket = gBucket; 2588 fInitialGrammarSet = null; 2589 } 2590 2591 public void lockPool() {} 2592 2593 public void unlockPool() {} 2594 2595 public void clear() {} 2596 } 2597 2598 2635 private static class XSDKey { 2636 String systemId; 2637 short referType; 2638 String referNS; 2641 2642 XSDKey(String systemId, short referType, String referNS) { 2643 this.systemId = systemId; 2644 this.referType = referType; 2645 this.referNS = referNS; 2646 } 2647 2648 public int hashCode() { 2649 return referNS == null ? 0 : referNS.hashCode(); 2652 } 2653 2654 public boolean equals(Object obj) { 2655 if (!(obj instanceof XSDKey)) { 2656 return false; 2657 } 2658 XSDKey key = (XSDKey)obj; 2659 2660 if (referType == XSDDescription.CONTEXT_REDEFINE || 2662 key.referType == XSDDescription.CONTEXT_REDEFINE) { 2663 if (referType != key.referType) 2664 return false; 2665 } 2666 2667 if (referNS != key.referNS) 2669 return false; 2670 2671 if (systemId == null || !systemId.equals(key.systemId)) { 2673 return false; 2674 } 2675 2676 return true; 2677 } 2678 } 2679 2680 2683 public void setGenerateSyntheticAnnotations(boolean state) { 2684 fSchemaParser.setFeature(GENERATE_SYNTHETIC_ANNOTATIONS, state); 2685 } 2686 2687} | Popular Tags |