| 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
|