1 16 17 package org.apache.xerces.impl.xs; 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Hashtable ; 23 import java.util.Stack ; 24 import java.util.Vector ; 25 26 import org.apache.xerces.impl.Constants; 27 import org.apache.xerces.impl.RevalidationHandler; 28 import org.apache.xerces.impl.XMLErrorReporter; 29 import org.apache.xerces.impl.dv.DatatypeException; 30 import org.apache.xerces.impl.dv.InvalidDatatypeValueException; 31 import org.apache.xerces.impl.dv.ValidatedInfo; 32 import org.apache.xerces.impl.dv.XSSimpleType; 33 import org.apache.xerces.impl.validation.ValidationManager; 34 import org.apache.xerces.impl.validation.ValidationState; 35 import org.apache.xerces.impl.xs.identity.Field; 36 import org.apache.xerces.impl.xs.identity.FieldActivator; 37 import org.apache.xerces.impl.xs.identity.IdentityConstraint; 38 import org.apache.xerces.impl.xs.identity.KeyRef; 39 import org.apache.xerces.impl.xs.identity.Selector; 40 import org.apache.xerces.impl.xs.identity.UniqueOrKey; 41 import org.apache.xerces.impl.xs.identity.ValueStore; 42 import org.apache.xerces.impl.xs.identity.XPathMatcher; 43 import org.apache.xerces.impl.xs.models.CMBuilder; 44 import org.apache.xerces.impl.xs.models.CMNodeFactory; 45 import org.apache.xerces.impl.xs.models.XSCMValidator; 46 import org.apache.xerces.util.AugmentationsImpl; 47 import org.apache.xerces.util.IntStack; 48 import org.apache.xerces.util.SymbolTable; 49 import org.apache.xerces.util.XMLAttributesImpl; 50 import org.apache.xerces.util.XMLChar; 51 import org.apache.xerces.util.XMLSymbols; 52 import org.apache.xerces.xni.Augmentations; 53 import org.apache.xerces.xni.NamespaceContext; 54 import org.apache.xerces.xni.QName; 55 import org.apache.xerces.xni.XMLAttributes; 56 import org.apache.xerces.xni.XMLDocumentHandler; 57 import org.apache.xerces.xni.XMLLocator; 58 import org.apache.xerces.xni.XMLResourceIdentifier; 59 import org.apache.xerces.xni.XMLString; 60 import org.apache.xerces.xni.XNIException; 61 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 62 import org.apache.xerces.xni.grammars.XMLGrammarPool; 63 import org.apache.xerces.xni.parser.XMLComponent; 64 import org.apache.xerces.xni.parser.XMLComponentManager; 65 import org.apache.xerces.xni.parser.XMLConfigurationException; 66 import org.apache.xerces.xni.parser.XMLDocumentFilter; 67 import org.apache.xerces.xni.parser.XMLDocumentSource; 68 import org.apache.xerces.xni.parser.XMLEntityResolver; 69 import org.apache.xerces.xni.parser.XMLInputSource; 70 import org.apache.xerces.xs.AttributePSVI; 71 import org.apache.xerces.xs.ElementPSVI; 72 import org.apache.xerces.xs.ShortList; 73 import org.apache.xerces.xs.XSConstants; 74 import org.apache.xerces.xs.XSObjectList; 75 import org.apache.xerces.xs.XSTypeDefinition; 76 import org.xml.sax.SAXNotRecognizedException ; 77 import org.xml.sax.SAXNotSupportedException ; 78 79 103 public class XMLSchemaValidator 104 implements XMLComponent, XMLDocumentFilter, FieldActivator, RevalidationHandler { 105 106 private static final boolean DEBUG = false; 110 111 113 114 protected static final String VALIDATION = 115 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 116 117 118 protected static final String SCHEMA_VALIDATION = 119 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 120 121 122 protected static final String SCHEMA_FULL_CHECKING = 123 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING; 124 125 126 protected static final String DYNAMIC_VALIDATION = 127 Constants.XERCES_FEATURE_PREFIX + Constants.DYNAMIC_VALIDATION_FEATURE; 128 129 130 protected static final String NORMALIZE_DATA = 131 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; 132 133 134 protected static final String SCHEMA_ELEMENT_DEFAULT = 135 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT; 136 137 138 protected static final String SCHEMA_AUGMENT_PSVI = 139 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI; 140 141 142 protected static final String ALLOW_JAVA_ENCODINGS = 143 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 144 145 146 protected static final String STANDARD_URI_CONFORMANT_FEATURE = 147 Constants.XERCES_FEATURE_PREFIX + Constants.STANDARD_URI_CONFORMANT_FEATURE; 148 149 150 protected static final String GENERATE_SYNTHETIC_ANNOTATIONS = 151 Constants.XERCES_FEATURE_PREFIX + Constants.GENERATE_SYNTHETIC_ANNOTATIONS_FEATURE; 152 153 154 protected static final String VALIDATE_ANNOTATIONS = 155 Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATE_ANNOTATIONS_FEATURE; 156 157 158 protected static final String HONOUR_ALL_SCHEMALOCATIONS = 159 Constants.XERCES_FEATURE_PREFIX + Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE; 160 161 162 protected static final String USE_GRAMMAR_POOL_ONLY = 163 Constants.XERCES_FEATURE_PREFIX + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE; 164 165 166 protected static final String CONTINUE_AFTER_FATAL_ERROR = 167 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 168 169 protected static final String PARSER_SETTINGS = 170 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; 171 172 174 175 public static final String SYMBOL_TABLE = 176 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 177 178 179 public static final String ERROR_REPORTER = 180 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 181 182 183 public static final String ENTITY_RESOLVER = 184 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 185 186 187 public static final String XMLGRAMMAR_POOL = 188 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 189 190 protected static final String VALIDATION_MANAGER = 191 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 192 193 protected static final String ENTITY_MANAGER = 194 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 195 196 197 protected static final String SCHEMA_LOCATION = 198 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION; 199 200 201 protected static final String SCHEMA_NONS_LOCATION = 202 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION; 203 204 205 protected static final String JAXP_SCHEMA_SOURCE = 206 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE; 207 208 209 protected static final String JAXP_SCHEMA_LANGUAGE = 210 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE; 211 212 214 215 private static final String [] RECOGNIZED_FEATURES = 216 { 217 VALIDATION, 218 SCHEMA_VALIDATION, 219 DYNAMIC_VALIDATION, 220 SCHEMA_FULL_CHECKING, 221 ALLOW_JAVA_ENCODINGS, 222 CONTINUE_AFTER_FATAL_ERROR, 223 STANDARD_URI_CONFORMANT_FEATURE, 224 GENERATE_SYNTHETIC_ANNOTATIONS, 225 VALIDATE_ANNOTATIONS, 226 HONOUR_ALL_SCHEMALOCATIONS, 227 USE_GRAMMAR_POOL_ONLY}; 228 229 230 231 private static final Boolean [] FEATURE_DEFAULTS = { null, 232 null, null, null, null, null, null, 244 null, 245 null, 246 null, 247 null}; 248 249 250 private static final String [] RECOGNIZED_PROPERTIES = 251 { 252 SYMBOL_TABLE, 253 ERROR_REPORTER, 254 ENTITY_RESOLVER, 255 VALIDATION_MANAGER, 256 SCHEMA_LOCATION, 257 SCHEMA_NONS_LOCATION, 258 JAXP_SCHEMA_SOURCE, 259 JAXP_SCHEMA_LANGUAGE, 260 }; 261 262 263 private static final Object [] PROPERTY_DEFAULTS = 264 { null, null, null, null, null, null, null, null, }; 265 266 protected static final int ID_CONSTRAINT_NUM = 1; 270 271 275 276 protected ElementPSVImpl fCurrentPSVI = new ElementPSVImpl(); 277 278 protected final AugmentationsImpl fAugmentations = new AugmentationsImpl(); 284 285 289 protected final HashMap fMayMatchFieldMap = new HashMap (); 290 291 protected XMLString fDefaultValue; 293 294 protected boolean fDynamicValidation = false; 296 protected boolean fSchemaDynamicValidation = false; 297 protected boolean fDoValidation = false; 298 protected boolean fFullChecking = false; 299 protected boolean fNormalizeData = true; 300 protected boolean fSchemaElementDefault = true; 301 protected boolean fAugPSVI = true; 302 protected boolean fIdConstraint = false; 303 protected boolean fUseGrammarPoolOnly = false; 304 305 306 private String fSchemaType = null; 307 308 protected boolean fEntityRef = false; 310 protected boolean fInCDATA = false; 311 312 314 315 protected SymbolTable fSymbolTable; 316 317 320 private XMLLocator fLocator; 321 322 327 protected final class XSIErrorReporter { 328 329 XMLErrorReporter fErrorReporter; 331 332 Vector fErrors = new Vector (); 335 int[] fContext = new int[INITIAL_STACK_SIZE]; 336 int fContextCount; 337 338 public void reset(XMLErrorReporter errorReporter) { 340 fErrorReporter = errorReporter; 341 fErrors.removeAllElements(); 342 fContextCount = 0; 343 } 344 345 public void pushContext() { 348 if (!fAugPSVI) { 349 return; 350 } 351 if (fContextCount == fContext.length) { 353 int newSize = fContextCount + INC_STACK_SIZE; 354 int[] newArray = new int[newSize]; 355 System.arraycopy(fContext, 0, newArray, 0, fContextCount); 356 fContext = newArray; 357 } 358 359 fContext[fContextCount++] = fErrors.size(); 360 } 361 362 public String [] popContext() { 364 if (!fAugPSVI) { 365 return null; 366 } 367 int contextPos = fContext[--fContextCount]; 369 int size = fErrors.size() - contextPos; 371 if (size == 0) 373 return null; 374 String [] errors = new String [size]; 376 for (int i = 0; i < size; i++) { 377 errors[i] = (String ) fErrors.elementAt(contextPos + i); 378 } 379 fErrors.setSize(contextPos); 381 return errors; 382 } 383 384 public String [] mergeContext() { 388 if (!fAugPSVI) { 389 return null; 390 } 391 int contextPos = fContext[--fContextCount]; 393 int size = fErrors.size() - contextPos; 395 if (size == 0) 397 return null; 398 String [] errors = new String [size]; 400 for (int i = 0; i < size; i++) { 401 errors[i] = (String ) fErrors.elementAt(contextPos + i); 402 } 403 return errors; 406 } 407 408 public void reportError(String domain, String key, Object [] arguments, short severity) 409 throws XNIException { 410 fErrorReporter.reportError(domain, key, arguments, severity); 411 if (fAugPSVI) { 412 fErrors.addElement(key); 413 } 414 } 416 public void reportError( 417 XMLLocator location, 418 String domain, 419 String key, 420 Object [] arguments, 421 short severity) 422 throws XNIException { 423 fErrorReporter.reportError(location, domain, key, arguments, severity); 424 if (fAugPSVI) { 425 fErrors.addElement(key); 426 } 427 } } 429 430 431 protected final XSIErrorReporter fXSIErrorReporter = new XSIErrorReporter(); 432 433 434 protected XMLEntityResolver fEntityResolver; 435 436 protected ValidationManager fValidationManager = null; 438 protected ValidationState fValidationState = new ValidationState(); 439 protected XMLGrammarPool fGrammarPool; 440 441 protected String fExternalSchemas = null; 443 protected String fExternalNoNamespaceSchema = null; 444 445 protected Object fJaxpSchemaSource = null; 447 448 449 protected final XSDDescription fXSDDescription = new XSDDescription(); 450 protected final Hashtable fLocationPairs = new Hashtable (); 451 452 453 455 456 protected XMLDocumentHandler fDocumentHandler; 457 458 protected XMLDocumentSource fDocumentSource; 459 460 464 469 public String [] getRecognizedFeatures() { 470 return (String []) (RECOGNIZED_FEATURES.clone()); 471 } 473 488 public void setFeature(String featureId, boolean state) throws XMLConfigurationException { 489 } 491 496 public String [] getRecognizedProperties() { 497 return (String []) (RECOGNIZED_PROPERTIES.clone()); 498 } 500 515 public void setProperty(String propertyId, Object value) throws XMLConfigurationException { 516 } 518 527 public Boolean getFeatureDefault(String featureId) { 528 for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { 529 if (RECOGNIZED_FEATURES[i].equals(featureId)) { 530 return FEATURE_DEFAULTS[i]; 531 } 532 } 533 return null; 534 } 536 545 public Object getPropertyDefault(String propertyId) { 546 for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { 547 if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { 548 return PROPERTY_DEFAULTS[i]; 549 } 550 } 551 return null; 552 } 554 558 559 public void setDocumentHandler(XMLDocumentHandler documentHandler) { 560 fDocumentHandler = documentHandler; 561 } 563 564 public XMLDocumentHandler getDocumentHandler() { 565 return fDocumentHandler; 566 } 568 572 573 public void setDocumentSource(XMLDocumentSource source) { 574 fDocumentSource = source; 575 } 577 578 public XMLDocumentSource getDocumentSource() { 579 return fDocumentSource; 580 } 582 604 public void startDocument( 605 XMLLocator locator, 606 String encoding, 607 NamespaceContext namespaceContext, 608 Augmentations augs) 609 throws XNIException { 610 611 fValidationState.setNamespaceSupport(namespaceContext); 612 fState4XsiType.setNamespaceSupport(namespaceContext); 613 fState4ApplyDefault.setNamespaceSupport(namespaceContext); 614 fLocator = locator; 615 616 handleStartDocument(locator, encoding); 617 if (fDocumentHandler != null) { 619 fDocumentHandler.startDocument(locator, encoding, namespaceContext, augs); 620 } 621 622 } 624 637 public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) 638 throws XNIException { 639 640 if (fDocumentHandler != null) { 642 fDocumentHandler.xmlDecl(version, encoding, standalone, augs); 643 } 644 645 } 647 659 public void doctypeDecl( 660 String rootElement, 661 String publicId, 662 String systemId, 663 Augmentations augs) 664 throws XNIException { 665 666 if (fDocumentHandler != null) { 668 fDocumentHandler.doctypeDecl(rootElement, publicId, systemId, augs); 669 } 670 671 } 673 682 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) 683 throws XNIException { 684 685 Augmentations modifiedAugs = handleStartElement(element, attributes, augs); 686 if (fDocumentHandler != null) { 688 fDocumentHandler.startElement(element, attributes, modifiedAugs); 689 } 690 691 } 693 702 public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) 703 throws XNIException { 704 705 Augmentations modifiedAugs = handleStartElement(element, attributes, augs); 706 707 fDefaultValue = null; 711 if (fElementDepth != -2) 714 modifiedAugs = handleEndElement(element, modifiedAugs); 715 716 if (fDocumentHandler != null) { 718 if (!fSchemaElementDefault || fDefaultValue == null) { 719 fDocumentHandler.emptyElement(element, attributes, modifiedAugs); 720 } else { 721 fDocumentHandler.startElement(element, attributes, modifiedAugs); 722 fDocumentHandler.characters(fDefaultValue, null); 723 fDocumentHandler.endElement(element, modifiedAugs); 724 } 725 } 726 } 728 736 public void characters(XMLString text, Augmentations augs) throws XNIException { 737 738 text = handleCharacters(text); 739 if (fDocumentHandler != null) { 741 if (fNormalizeData && fUnionType) { 742 if (augs != null) 747 fDocumentHandler.characters(fEmptyXMLStr, augs); 748 } else { 749 fDocumentHandler.characters(text, augs); 750 } 751 } 752 753 } 755 768 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 769 770 handleIgnorableWhitespace(text); 771 if (fDocumentHandler != null) { 773 fDocumentHandler.ignorableWhitespace(text, augs); 774 } 775 776 } 778 786 public void endElement(QName element, Augmentations augs) throws XNIException { 787 788 fDefaultValue = null; 791 Augmentations modifiedAugs = handleEndElement(element, augs); 792 if (fDocumentHandler != null) { 794 if (!fSchemaElementDefault || fDefaultValue == null) { 795 fDocumentHandler.endElement(element, modifiedAugs); 796 } else { 797 fDocumentHandler.characters(fDefaultValue, null); 798 fDocumentHandler.endElement(element, modifiedAugs); 799 } 800 } 801 } 803 810 public void startCDATA(Augmentations augs) throws XNIException { 811 812 fInCDATA = true; 814 if (fDocumentHandler != null) { 816 fDocumentHandler.startCDATA(augs); 817 } 818 819 } 821 828 public void endCDATA(Augmentations augs) throws XNIException { 829 830 fInCDATA = false; 832 if (fDocumentHandler != null) { 833 fDocumentHandler.endCDATA(augs); 834 } 835 836 } 838 845 public void endDocument(Augmentations augs) throws XNIException { 846 847 handleEndDocument(); 848 849 if (fDocumentHandler != null) { 851 fDocumentHandler.endDocument(augs); 852 } 853 fLocator = null; 854 855 } 857 861 862 863 864 865 public boolean characterData(String data, Augmentations augs) { 866 867 fSawText = fSawText || data.length() > 0; 868 869 872 if (fNormalizeData && fWhiteSpace != -1 && fWhiteSpace != XSSimpleType.WS_PRESERVE) { 875 normalizeWhitespace(data, fWhiteSpace == XSSimpleType.WS_COLLAPSE); 877 fBuffer.append(fNormalizedStr.ch, fNormalizedStr.offset, fNormalizedStr.length); 878 } else { 879 if (fAppendBuffer) 880 fBuffer.append(data); 881 } 882 883 boolean allWhiteSpace = true; 886 if (fCurrentType != null 887 && fCurrentType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { 888 XSComplexTypeDecl ctype = (XSComplexTypeDecl) fCurrentType; 889 if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_ELEMENT) { 890 for (int i = 0; i < data.length(); i++) { 892 if (!XMLChar.isSpace(data.charAt(i))) { 893 allWhiteSpace = false; 894 fSawCharacters = true; 895 break; 896 } 897 } 898 } 899 } 900 901 return allWhiteSpace; 902 } 903 904 public void elementDefault(String data) { 905 } 907 908 912 929 public void startGeneralEntity( 930 String name, 931 XMLResourceIdentifier identifier, 932 String encoding, 933 Augmentations augs) 934 throws XNIException { 935 936 fEntityRef = true; 938 if (fDocumentHandler != null) { 940 fDocumentHandler.startGeneralEntity(name, identifier, encoding, augs); 941 } 942 943 } 945 962 public void textDecl(String version, String encoding, Augmentations augs) throws XNIException { 963 964 if (fDocumentHandler != null) { 966 fDocumentHandler.textDecl(version, encoding, augs); 967 } 968 969 } 971 979 public void comment(XMLString text, Augmentations augs) throws XNIException { 980 981 if (fDocumentHandler != null) { 983 fDocumentHandler.comment(text, augs); 984 } 985 986 } 988 1005 public void processingInstruction(String target, XMLString data, Augmentations augs) 1006 throws XNIException { 1007 1008 if (fDocumentHandler != null) { 1010 fDocumentHandler.processingInstruction(target, data, augs); 1011 } 1012 1013 } 1015 1027 public void endGeneralEntity(String name, Augmentations augs) throws XNIException { 1028 1029 fEntityRef = false; 1031 if (fDocumentHandler != null) { 1032 fDocumentHandler.endGeneralEntity(name, augs); 1033 } 1034 1035 } 1037 1039 static final int INITIAL_STACK_SIZE = 8; 1040 static final int INC_STACK_SIZE = 8; 1041 1042 1046 1048 private static final boolean DEBUG_NORMALIZATION = false; 1049 private final XMLString fEmptyXMLStr = new XMLString(null, 0, -1); 1051 private static final int BUFFER_SIZE = 20; 1053 private final XMLString fNormalizedStr = new XMLString(); 1054 private boolean fFirstChunk = true; 1055 private boolean fTrailing = false; private short fWhiteSpace = -1; private boolean fUnionType = false; 1059 1060 1061 private final XSGrammarBucket fGrammarBucket = new XSGrammarBucket(); 1062 private final SubstitutionGroupHandler fSubGroupHandler = new SubstitutionGroupHandler(fGrammarBucket); 1063 1064 1065 private final XSSimpleType fQNameDV = 1068 (XSSimpleType) SchemaGrammar.SG_SchemaNS.getGlobalTypeDecl(SchemaSymbols.ATTVAL_QNAME); 1069 1070 private final CMNodeFactory nodeFactory = new CMNodeFactory(); 1071 1072 private final CMBuilder fCMBuilder = new CMBuilder(nodeFactory); 1074 1075 private final XMLSchemaLoader fSchemaLoader = 1077 new XMLSchemaLoader( 1078 fXSIErrorReporter.fErrorReporter, 1079 fGrammarBucket, 1080 fSubGroupHandler, 1081 fCMBuilder); 1082 1083 1085 1086 private String fValidationRoot; 1088 1089 1090 private int fSkipValidationDepth; 1091 1092 1093 private int fNFullValidationDepth; 1094 1095 1096 private int fNNoneValidationDepth; 1097 1098 1099 private int fElementDepth; 1100 1101 1102 private boolean fSubElement; 1103 1104 1105 private boolean[] fSubElementStack = new boolean[INITIAL_STACK_SIZE]; 1106 1107 1108 private XSElementDecl fCurrentElemDecl; 1109 1110 1111 private XSElementDecl[] fElemDeclStack = new XSElementDecl[INITIAL_STACK_SIZE]; 1112 1113 1114 private boolean fNil; 1115 1116 1117 private boolean[] fNilStack = new boolean[INITIAL_STACK_SIZE]; 1118 1119 1120 private XSNotationDecl fNotation; 1121 1122 1123 private XSNotationDecl[] fNotationStack = new XSNotationDecl[INITIAL_STACK_SIZE]; 1124 1125 1126 private XSTypeDefinition fCurrentType; 1127 1128 1129 private XSTypeDefinition[] fTypeStack = new XSTypeDefinition[INITIAL_STACK_SIZE]; 1130 1131 1132 private XSCMValidator fCurrentCM; 1133 1134 1135 private XSCMValidator[] fCMStack = new XSCMValidator[INITIAL_STACK_SIZE]; 1136 1137 1138 private int[] fCurrCMState; 1139 1140 1141 private int[][] fCMStateStack = new int[INITIAL_STACK_SIZE][]; 1142 1143 1144 private boolean fStrictAssess = true; 1145 1146 1147 private boolean[] fStrictAssessStack = new boolean[INITIAL_STACK_SIZE]; 1148 1149 1150 private final StringBuffer fBuffer = new StringBuffer (); 1151 1152 1153 private boolean fAppendBuffer = true; 1154 1155 1156 private boolean fSawText = false; 1157 1158 1159 private boolean[] fSawTextStack = new boolean[INITIAL_STACK_SIZE]; 1160 1161 1162 private boolean fSawCharacters = false; 1163 1164 1165 private boolean[] fStringContent = new boolean[INITIAL_STACK_SIZE]; 1166 1167 1168 private final QName fTempQName = new QName(); 1169 1170 1171 private ValidatedInfo fValidatedInfo = new ValidatedInfo(); 1172 1173 private ValidationState fState4XsiType = new ValidationState(); 1176 1177 private ValidationState fState4ApplyDefault = new ValidationState(); 1180 1181 1183 1197 protected XPathMatcherStack fMatcherStack = new XPathMatcherStack(); 1198 1199 1200 protected ValueStoreCache fValueStoreCache = new ValueStoreCache(); 1201 1202 1206 1207 public XMLSchemaValidator() { 1208 fState4XsiType.setExtraChecking(false); 1209 fState4ApplyDefault.setFacetChecking(false); 1210 1211 } 1213 1227 public void reset(XMLComponentManager componentManager) throws XMLConfigurationException { 1228 1229 1230 fIdConstraint = false; 1231 fLocationPairs.clear(); 1233 1234 fValidationState.resetIDTables(); 1236 1237 nodeFactory.reset(componentManager); 1239 1240 fSchemaLoader.reset(componentManager); 1242 1243 fCurrentElemDecl = null; 1245 fCurrentCM = null; 1246 fCurrCMState = null; 1247 fSkipValidationDepth = -1; 1248 fNFullValidationDepth = -1; 1249 fNNoneValidationDepth = -1; 1250 fElementDepth = -1; 1251 fSubElement = false; 1252 fSchemaDynamicValidation = false; 1253 1254 fEntityRef = false; 1256 fInCDATA = false; 1257 1258 fMatcherStack.clear(); 1259 1260 if (!fMayMatchFieldMap.isEmpty()) { 1261 fMayMatchFieldMap.clear(); 1263 } 1264 1265 fXSIErrorReporter.reset((XMLErrorReporter) componentManager.getProperty(ERROR_REPORTER)); 1267 1268 boolean parser_settings; 1269 try { 1270 parser_settings = componentManager.getFeature(PARSER_SETTINGS); 1271 } 1272 catch (XMLConfigurationException e){ 1273 parser_settings = true; 1274 } 1275 1276 if (!parser_settings){ 1277 fValidationManager.addValidationState(fValidationState); 1279 XMLSchemaLoader.processExternalHints( 1281 fExternalSchemas, 1282 fExternalNoNamespaceSchema, 1283 fLocationPairs, 1284 fXSIErrorReporter.fErrorReporter); 1285 return; 1286 } 1287 1288 1289 SymbolTable symbolTable = (SymbolTable) componentManager.getProperty(SYMBOL_TABLE); 1291 if (symbolTable != fSymbolTable) { 1292 fSymbolTable = symbolTable; 1293 } 1294 1295 try { 1296 fDynamicValidation = componentManager.getFeature(DYNAMIC_VALIDATION); 1297 } catch (XMLConfigurationException e) { 1298 fDynamicValidation = false; 1299 } 1300 1301 if (fDynamicValidation) { 1302 fDoValidation = true; 1303 } else { 1304 try { 1305 fDoValidation = componentManager.getFeature(VALIDATION); 1306 } catch (XMLConfigurationException e) { 1307 fDoValidation = false; 1308 } 1309 } 1310 1311 if (fDoValidation) { 1312 try { 1313 fDoValidation = componentManager.getFeature(XMLSchemaValidator.SCHEMA_VALIDATION); 1314 } catch (XMLConfigurationException e) { 1315 } 1316 } 1317 1318 try { 1319 fFullChecking = componentManager.getFeature(SCHEMA_FULL_CHECKING); 1320 } catch (XMLConfigurationException e) { 1321 fFullChecking = false; 1322 } 1323 1324 try { 1325 fNormalizeData = componentManager.getFeature(NORMALIZE_DATA); 1326 } catch (XMLConfigurationException e) { 1327 fNormalizeData = false; 1328 } 1329 1330 try { 1331 fSchemaElementDefault = componentManager.getFeature(SCHEMA_ELEMENT_DEFAULT); 1332 } catch (XMLConfigurationException e) { 1333 fSchemaElementDefault = false; 1334 } 1335 1336 try { 1337 fAugPSVI = componentManager.getFeature(SCHEMA_AUGMENT_PSVI); 1338 } catch (XMLConfigurationException e) { 1339 fAugPSVI = true; 1340 } 1341 try { 1342 fSchemaType = 1343 (String ) componentManager.getProperty( 1344 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE); 1345 } catch (XMLConfigurationException e) { 1346 fSchemaType = null; 1347 } 1348 1349 try { 1350 fUseGrammarPoolOnly = componentManager.getFeature(USE_GRAMMAR_POOL_ONLY); 1351 } 1352 catch (XMLConfigurationException e) { 1353 fUseGrammarPoolOnly = false; 1354 } 1355 1356 fEntityResolver = (XMLEntityResolver) componentManager.getProperty(ENTITY_MANAGER); 1357 1358 fValidationManager = (ValidationManager) componentManager.getProperty(VALIDATION_MANAGER); 1359 fValidationManager.addValidationState(fValidationState); 1360 fValidationState.setSymbolTable(fSymbolTable); 1361 1362 1363 try { 1365 fExternalSchemas = (String ) componentManager.getProperty(SCHEMA_LOCATION); 1366 fExternalNoNamespaceSchema = 1367 (String ) componentManager.getProperty(SCHEMA_NONS_LOCATION); 1368 } catch (XMLConfigurationException e) { 1369 fExternalSchemas = null; 1370 fExternalNoNamespaceSchema = null; 1371 } 1372 1373 XMLSchemaLoader.processExternalHints( 1378 fExternalSchemas, 1379 fExternalNoNamespaceSchema, 1380 fLocationPairs, 1381 fXSIErrorReporter.fErrorReporter); 1382 1383 try { 1384 fJaxpSchemaSource = componentManager.getProperty(JAXP_SCHEMA_SOURCE); 1385 } catch (XMLConfigurationException e) { 1386 fJaxpSchemaSource = null; 1387 1388 } 1389 1390 try { 1392 fGrammarPool = (XMLGrammarPool) componentManager.getProperty(XMLGRAMMAR_POOL); 1393 } catch (XMLConfigurationException e) { 1394 fGrammarPool = null; 1395 } 1396 1397 fState4XsiType.setSymbolTable(symbolTable); 1398 fState4ApplyDefault.setSymbolTable(symbolTable); 1399 1400 } 1402 1406 1413 public void startValueScopeFor(IdentityConstraint identityConstraint, int initialDepth) { 1414 1415 ValueStoreBase valueStore = 1416 fValueStoreCache.getValueStoreFor(identityConstraint, initialDepth); 1417 valueStore.startValueScope(); 1418 1419 } 1421 1427 public XPathMatcher activateField(Field field, int initialDepth) { 1428 ValueStore valueStore = 1429 fValueStoreCache.getValueStoreFor(field.getIdentityConstraint(), initialDepth); 1430 setMayMatch(field, Boolean.TRUE); 1431 XPathMatcher matcher = field.createMatcher(this, valueStore); 1432 fMatcherStack.addMatcher(matcher); 1433 matcher.startDocumentFragment(); 1434 return matcher; 1435 } 1437 1442 public void endValueScopeFor(IdentityConstraint identityConstraint, int initialDepth) { 1443 1444 ValueStoreBase valueStore = 1445 fValueStoreCache.getValueStoreFor(identityConstraint, initialDepth); 1446 valueStore.endValueScope(); 1447 1448 } 1450 1458 public void setMayMatch(Field field, Boolean state) { 1459 fMayMatchFieldMap.put(field, state); 1460 } 1462 1468 public Boolean mayMatch(Field field) { 1469 return (Boolean ) fMayMatchFieldMap.get(field); 1470 } 1472 private void activateSelectorFor(IdentityConstraint ic) { 1474 Selector selector = ic.getSelector(); 1475 FieldActivator activator = this; 1476 if (selector == null) 1477 return; 1478 XPathMatcher matcher = selector.createMatcher(activator, fElementDepth); 1479 fMatcherStack.addMatcher(matcher); 1480 matcher.startDocumentFragment(); 1481 } 1482 1483 1487 1488 void ensureStackCapacity() { 1489 1490 if (fElementDepth == fElemDeclStack.length) { 1491 int newSize = fElementDepth + INC_STACK_SIZE; 1492 boolean[] newArrayB = new boolean[newSize]; 1493 System.arraycopy(fSubElementStack, 0, newArrayB, 0, fElementDepth); 1494 fSubElementStack = newArrayB; 1495 1496 XSElementDecl[] newArrayE = new XSElementDecl[newSize]; 1497 System.arraycopy(fElemDeclStack, 0, newArrayE, 0, fElementDepth); 1498 fElemDeclStack = newArrayE; 1499 1500 newArrayB = new boolean[newSize]; 1501 System.arraycopy(fNilStack, 0, newArrayB, 0, fElementDepth); 1502 fNilStack = newArrayB; 1503 1504 XSNotationDecl[] newArrayN = new XSNotationDecl[newSize]; 1505 System.arraycopy(fNotationStack, 0, newArrayN, 0, fElementDepth); 1506 fNotationStack = newArrayN; 1507 1508 XSTypeDefinition[] newArrayT = new XSTypeDefinition[newSize]; 1509 System.arraycopy(fTypeStack, 0, newArrayT, 0, fElementDepth); 1510 fTypeStack = newArrayT; 1511 1512 XSCMValidator[] newArrayC = new XSCMValidator[newSize]; 1513 System.arraycopy(fCMStack, 0, newArrayC, 0, fElementDepth); 1514 fCMStack = newArrayC; 1515 1516 newArrayB = new boolean[newSize]; 1517 System.arraycopy(fSawTextStack, 0, newArrayB, 0, fElementDepth); 1518 fSawTextStack = newArrayB; 1519 1520 newArrayB = new boolean[newSize]; 1521 System.arraycopy(fStringContent, 0, newArrayB, 0, fElementDepth); 1522 fStringContent = newArrayB; 1523 1524 newArrayB = new boolean[newSize]; 1525 System.arraycopy(fStrictAssessStack, 0, newArrayB, 0, fElementDepth); 1526 fStrictAssessStack = newArrayB; 1527 1528 int[][] newArrayIA = new int[newSize][]; 1529 System.arraycopy(fCMStateStack, 0, newArrayIA, 0, fElementDepth); 1530 fCMStateStack = newArrayIA; 1531 } 1532 1533 } 1535 void handleStartDocument(XMLLocator locator, String encoding) { 1537 fValueStoreCache.startDocument(); 1538 if (fAugPSVI) { 1539 fCurrentPSVI.fGrammars = null; 1540 fCurrentPSVI.fSchemaInformation = null; 1541 } 1542 } 1544 void handleEndDocument() { 1545 fValueStoreCache.endDocument(); 1546 } 1548 XMLString handleCharacters(XMLString text) { 1551 1552 if (fSkipValidationDepth >= 0) 1553 return text; 1554 1555 fSawText = fSawText || text.length > 0; 1556 1557 if (fNormalizeData && fWhiteSpace != -1 && fWhiteSpace != XSSimpleType.WS_PRESERVE) { 1561 normalizeWhitespace(text, fWhiteSpace == XSSimpleType.WS_COLLAPSE); 1563 text = fNormalizedStr; 1564 } 1565 if (fAppendBuffer) 1566 fBuffer.append(text.ch, text.offset, text.length); 1567 1568 if (fCurrentType != null 1571 && fCurrentType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { 1572 XSComplexTypeDecl ctype = (XSComplexTypeDecl) fCurrentType; 1573 if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_ELEMENT) { 1574 for (int i = text.offset; i < text.offset + text.length; i++) { 1576 if (!XMLChar.isSpace(text.ch[i])) { 1577 fSawCharacters = true; 1578 break; 1579 } 1580 } 1581 } 1582 } 1583 1584 return text; 1585 } 1587 1593 private void normalizeWhitespace(XMLString value, boolean collapse) { 1594 boolean skipSpace = collapse; 1595 boolean sawNonWS = false; 1596 boolean leading = false; 1597 boolean trailing = false; 1598 char c; 1599 int size = value.offset + value.length; 1600 1601 if (fNormalizedStr.ch == null || fNormalizedStr.ch.length < value.length + 1) { 1603 fNormalizedStr.ch = new char[value.length + 1]; 1604 } 1605 fNormalizedStr.offset = 1; 1607 fNormalizedStr.length = 1; 1608 1609 for (int i = value.offset; i < size; i++) { 1610 c = value.ch[i]; 1611 if (XMLChar.isSpace(c)) { 1612 if (!skipSpace) { 1613 fNormalizedStr.ch[fNormalizedStr.length++] = ' '; 1615 skipSpace = collapse; 1616 } 1617 if (!sawNonWS) { 1618 leading = true; 1620 } 1621 } else { 1622 fNormalizedStr.ch[fNormalizedStr.length++] = c; 1623 skipSpace = false; 1624 sawNonWS = true; 1625 } 1626 } 1627 if (skipSpace) { 1628 if (fNormalizedStr.length > 1) { 1629 fNormalizedStr.length--; 1631 trailing = true; 1632 } else if (leading && !fFirstChunk) { 1633 trailing = true; 1636 } 1637 } 1638 1639 if (fNormalizedStr.length > 1) { 1640 if (!fFirstChunk && (fWhiteSpace == XSSimpleType.WS_COLLAPSE)) { 1641 if (fTrailing) { 1642 fNormalizedStr.offset = 0; 1645 fNormalizedStr.ch[0] = ' '; 1646 } else if (leading) { 1647 fNormalizedStr.offset = 0; 1650 fNormalizedStr.ch[0] = ' '; 1651 } 1652 } 1653 } 1654 1655 fNormalizedStr.length -= fNormalizedStr.offset; 1657 1658 fTrailing = trailing; 1659 1660 if (trailing || sawNonWS) 1661 fFirstChunk = false; 1662 } 1663 1664 private void normalizeWhitespace(String value, boolean collapse) { 1665 boolean skipSpace = collapse; 1666 char c; 1667 int size = value.length(); 1668 1669 if (fNormalizedStr.ch == null || fNormalizedStr.ch.length < size) { 1671 fNormalizedStr.ch = new char[size]; 1672 } 1673 fNormalizedStr.offset = 0; 1674 fNormalizedStr.length = 0; 1675 1676 for (int i = 0; i < size; i++) { 1677 c = value.charAt(i); 1678 if (XMLChar.isSpace(c)) { 1679 if (!skipSpace) { 1680 fNormalizedStr.ch[fNormalizedStr.length++] = ' '; 1682 skipSpace = collapse; 1683 } 1684 } else { 1685 fNormalizedStr.ch[fNormalizedStr.length++] = c; 1686 skipSpace = false; 1687 } 1688 } 1689 if (skipSpace) { 1690 if (fNormalizedStr.length != 0) 1691 fNormalizedStr.length--; 1693 } 1694 } 1695 1696 void handleIgnorableWhitespace(XMLString text) { 1698 1699 if (fSkipValidationDepth >= 0) 1700 return; 1701 1702 1705 } 1707 1708 Augmentations handleStartElement(QName element, XMLAttributes attributes, Augmentations augs) { 1709 1710 if (DEBUG) { 1711 System.out.println("==>handleStartElement: " + element); 1712 } 1713 1714 if (fElementDepth == -1 && fValidationManager.isGrammarFound()) { 1716 if (fSchemaType == null) { 1717 fSchemaDynamicValidation = true; 1722 } else { 1723 } 1728 1729 } 1730 1731 1734 String sLocation = 1735 attributes.getValue(SchemaSymbols.URI_XSI, SchemaSymbols.XSI_SCHEMALOCATION); 1736 String nsLocation = 1737 attributes.getValue(SchemaSymbols.URI_XSI, SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION); 1738 storeLocations(sLocation, nsLocation); 1742 1743 if (fSkipValidationDepth >= 0) { 1746 fElementDepth++; 1747 if (fAugPSVI) 1748 augs = getEmptyAugs(augs); 1749 return augs; 1750 } 1751 1752 SchemaGrammar sGrammar = 1754 findSchemaGrammar( 1755 XSDDescription.CONTEXT_ELEMENT, 1756 element.uri, 1757 null, 1758 element, 1759 attributes); 1760 1761 Object decl = null; 1767 if (fCurrentCM != null) { 1768 decl = fCurrentCM.oneTransition(element, fCurrCMState, fSubGroupHandler); 1769 if (fCurrCMState[0] == XSCMValidator.FIRST_ERROR) { 1771 XSComplexTypeDecl ctype = (XSComplexTypeDecl) fCurrentType; 1772 Vector next; 1774 if (ctype.fParticle != null 1775 && (next = fCurrentCM.whatCanGoHere(fCurrCMState)).size() > 0) { 1776 String expected = expectedStr(next); 1777 reportSchemaError( 1778 "cvc-complex-type.2.4.a", 1779 new Object [] { element.rawname, expected }); 1780 } else { 1781 reportSchemaError("cvc-complex-type.2.4.d", new Object [] { element.rawname }); 1782 } 1783 } 1784 } 1785 1786 if (fElementDepth != -1) { 1788 ensureStackCapacity(); 1789 fSubElementStack[fElementDepth] = true; 1790 fSubElement = false; 1791 fElemDeclStack[fElementDepth] = fCurrentElemDecl; 1792 fNilStack[fElementDepth] = fNil; 1793 fNotationStack[fElementDepth] = fNotation; 1794 fTypeStack[fElementDepth] = fCurrentType; 1795 fStrictAssessStack[fElementDepth] = fStrictAssess; 1796 fCMStack[fElementDepth] = fCurrentCM; 1797 fCMStateStack[fElementDepth] = fCurrCMState; 1798 fSawTextStack[fElementDepth] = fSawText; 1799 fStringContent[fElementDepth] = fSawCharacters; 1800 } 1801 1802 fElementDepth++; 1805 fCurrentElemDecl = null; 1806 XSWildcardDecl wildcard = null; 1807 fCurrentType = null; 1808 fStrictAssess = true; 1809 fNil = false; 1810 fNotation = null; 1811 1812 fBuffer.setLength(0); 1814 fSawText = false; 1815 fSawCharacters = false; 1816 1817 if (decl != null) { 1820 if (decl instanceof XSElementDecl) { 1821 fCurrentElemDecl = (XSElementDecl) decl; 1822 } else { 1823 wildcard = (XSWildcardDecl) decl; 1824 } 1825 } 1826 1827 if (wildcard != null && wildcard.fProcessContents == XSWildcardDecl.PC_SKIP) { 1829 fSkipValidationDepth = fElementDepth; 1830 if (fAugPSVI) 1831 augs = getEmptyAugs(augs); 1832 return augs; 1833 } 1834 1835 if (fCurrentElemDecl == null) { 1839 if (sGrammar != null) { 1840 fCurrentElemDecl = sGrammar.getGlobalElementDecl(element.localpart); 1841 } 1842 } 1843 1844 if (fCurrentElemDecl != null) { 1845 fCurrentType = fCurrentElemDecl.fType; 1847 } 1848 1849 String xsiType = attributes.getValue(SchemaSymbols.URI_XSI, SchemaSymbols.XSI_TYPE); 1851 1852 if (fCurrentType == null && xsiType == null) { 1854 if (fElementDepth == 0) { 1858 if (fDynamicValidation || fSchemaDynamicValidation) { 1861 if (fDocumentSource != null) { 1868 fDocumentSource.setDocumentHandler(fDocumentHandler); 1869 if (fDocumentHandler != null) 1870 fDocumentHandler.setDocumentSource(fDocumentSource); 1871 fElementDepth = -2; 1873 return augs; 1874 } 1875 1876 fSkipValidationDepth = fElementDepth; 1877 if (fAugPSVI) 1878 augs = getEmptyAugs(augs); 1879 return augs; 1880 } 1881 fXSIErrorReporter.fErrorReporter.reportError( 1888 XSMessageFormatter.SCHEMA_DOMAIN, 1889 "cvc-elt.1", 1890 new Object [] { element.rawname }, 1891 XMLErrorReporter.SEVERITY_ERROR); 1892 } 1893 else if (wildcard != null && wildcard.fProcessContents == XSWildcardDecl.PC_STRICT) { 1897 reportSchemaError("cvc-complex-type.2.4.c", new Object [] { element.rawname }); 1899 } 1900 fCurrentType = SchemaGrammar.fAnyType; 1904 fStrictAssess = false; 1905 fNFullValidationDepth = fElementDepth; 1906 fAppendBuffer = false; 1908 1909 fXSIErrorReporter.pushContext(); 1913 } else { 1914 fXSIErrorReporter.pushContext(); 1918 1919 if (xsiType != null) { 1921 XSTypeDefinition oldType = fCurrentType; 1922 fCurrentType = getAndCheckXsiType(element, xsiType, attributes); 1923 if (fCurrentType == null) { 1925 if (oldType == null) 1926 fCurrentType = SchemaGrammar.fAnyType; 1927 else 1928 fCurrentType = oldType; 1929 } 1930 } 1931 1932 fNNoneValidationDepth = fElementDepth; 1933 if (fCurrentElemDecl != null 1935 && fCurrentElemDecl.getConstraintType() == XSConstants.VC_FIXED) { 1936 fAppendBuffer = true; 1937 } 1938 else if (fCurrentType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) { 1940 fAppendBuffer = true; 1941 } else { 1942 XSComplexTypeDecl ctype = (XSComplexTypeDecl) fCurrentType; 1944 fAppendBuffer = (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_SIMPLE); 1945 } 1946 } 1947 1948 if (fCurrentElemDecl != null && fCurrentElemDecl.getAbstract()) 1951 reportSchemaError("cvc-elt.2", new Object [] { element.rawname }); 1952 1953 if (fElementDepth == 0) { 1955 fValidationRoot = element.rawname; 1956 } 1957 1958 if (fNormalizeData) { 1960 fFirstChunk = true; 1962 fTrailing = false; 1963 fUnionType = false; 1964 fWhiteSpace = -1; 1965 } 1966 1967 if (fCurrentType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { 1970 XSComplexTypeDecl ctype = (XSComplexTypeDecl) fCurrentType; 1971 if (ctype.getAbstract()) { 1972 reportSchemaError("cvc-type.2", new Object [] { element.rawname }); 1973 } 1974 if (fNormalizeData) { 1975 if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_SIMPLE) { 1978 if (ctype.fXSSimpleType.getVariety() == XSSimpleType.VARIETY_UNION) { 1979 fUnionType = true; 1980 } else { 1981 try { 1982 fWhiteSpace = ctype.fXSSimpleType.getWhitespace(); 1983 } catch (DatatypeException e) { 1984 } 1986 } 1987 } 1988 } 1989 } 1990 else if (fNormalizeData) { 1992 XSSimpleType dv = (XSSimpleType) fCurrentType; 1994 if (dv.getVariety() == XSSimpleType.VARIETY_UNION) { 1995 fUnionType = true; 1996 } else { 1997 try { 1998 fWhiteSpace = dv.getWhitespace(); 1999 } catch (DatatypeException e) { 2000 } 2002 } 2003 } 2004 2005 fCurrentCM = null; 2007 if (fCurrentType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { 2008 fCurrentCM = ((XSComplexTypeDecl) fCurrentType).getContentModel(fCMBuilder); 2009 } 2010 2011 fCurrCMState = null; 2013 if (fCurrentCM != null) 2014 fCurrCMState = fCurrentCM.startContentModel(); 2015 2016 String xsiNil = attributes.getValue(SchemaSymbols.URI_XSI, SchemaSymbols.XSI_NIL); 2018 if (xsiNil != null && fCurrentElemDecl != null) 2020 fNil = getXsiNil(element, xsiNil); 2021 2022 XSAttributeGroupDecl attrGrp = null; 2025 if (fCurrentType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { 2026 XSComplexTypeDecl ctype = (XSComplexTypeDecl) fCurrentType; 2027 attrGrp = ctype.getAttrGrp(); 2028 } 2029 fValueStoreCache.startElement(); 2031 fMatcherStack.pushContext(); 2032 if (fCurrentElemDecl != null && fCurrentElemDecl.fIDCPos > 0) { 2033 fIdConstraint = true; 2034 fValueStoreCache.initValueStoresFor(fCurrentElemDecl, this); 2036 } 2037 processAttributes(element, attributes, attrGrp); 2038 2039 if (attrGrp != null) { 2041 addDefaultAttributes(element, attributes, attrGrp); 2042 } 2043 2044 int count = fMatcherStack.getMatcherCount(); 2046 for (int i = 0; i < count; i++) { 2047 XPathMatcher matcher = fMatcherStack.getMatcherAt(i); 2048 matcher.startElement( element, attributes); 2049 } 2050 2051 if (fAugPSVI) { 2052 augs = getEmptyAugs(augs); 2053 2054 fCurrentPSVI.fValidationContext = fValidationRoot; 2056 fCurrentPSVI.fDeclaration = fCurrentElemDecl; 2058 fCurrentPSVI.fTypeDecl = fCurrentType; 2060 fCurrentPSVI.fNotation = fNotation; 2062 } 2063 2064 return augs; 2065 2066 } 2068 2073 Augmentations handleEndElement(QName element, Augmentations augs) { 2074 2075 if (DEBUG) { 2076 System.out.println("==>handleEndElement:" + element); 2077 } 2078 if (fSkipValidationDepth >= 0) { 2080 if (fSkipValidationDepth == fElementDepth && fSkipValidationDepth > 0) { 2083 fNFullValidationDepth = fSkipValidationDepth - 1; 2085 fSkipValidationDepth = -1; 2086 fElementDepth--; 2087 fSubElement = fSubElementStack[fElementDepth]; 2088 fCurrentElemDecl = fElemDeclStack[fElementDepth]; 2089 fNil = fNilStack[fElementDepth]; 2090 fNotation = fNotationStack[fElementDepth]; 2091 fCurrentType = fTypeStack[fElementDepth]; 2092 fCurrentCM = fCMStack[fElementDepth]; 2093 fStrictAssess = fStrictAssessStack[fElementDepth]; 2094 fCurrCMState = fCMStateStack[fElementDepth]; 2095 fSawText = fSawTextStack[fElementDepth]; 2096 fSawCharacters = fStringContent[fElementDepth]; 2097 } 2098 else { 2099 fElementDepth--; 2100 } 2101 2102 2106 if (fElementDepth == -1 && fFullChecking) { 2108 XSConstraints.fullSchemaChecking( 2109 fGrammarBucket, 2110 fSubGroupHandler, 2111 fCMBuilder, 2112 fXSIErrorReporter.fErrorReporter); 2113 } 2114 2115 if (fAugPSVI) 2116 augs = getEmptyAugs(augs); 2117 return augs; 2118 } 2119 2120 processElementContent(element); 2122 2123 2126 int oldCount = fMatcherStack.getMatcherCount(); 2128 for (int i = oldCount - 1; i >= 0; i--) { 2129 XPathMatcher matcher = fMatcherStack.getMatcherAt(i); 2130 if (fCurrentElemDecl == null) 2131 matcher.endElement(element, null, false, fValidatedInfo.actualValue, fValidatedInfo.actualValueType, fValidatedInfo.itemValueTypes); 2132 2133 else 2134 matcher.endElement( 2135 element, 2136 fCurrentType, 2137 fCurrentElemDecl.getNillable(), 2138 fDefaultValue == null 2139 ? fValidatedInfo.actualValue 2140 : fCurrentElemDecl.fDefault.actualValue, 2141 fDefaultValue == null 2142 ? fValidatedInfo.actualValueType 2143 : fCurrentElemDecl.fDefault.actualValueType, 2144 fDefaultValue == null 2145 ? fValidatedInfo.itemValueTypes 2146 : fCurrentElemDecl.fDefault.itemValueTypes); 2147 } 2148 2149 if (fMatcherStack.size() > 0) { 2150 fMatcherStack.popContext(); 2151 } 2152 2153 int newCount = fMatcherStack.getMatcherCount(); 2154 for (int i = oldCount - 1; i >= newCount; i--) { 2156 XPathMatcher matcher = fMatcherStack.getMatcherAt(i); 2157 if (matcher instanceof Selector.Matcher) { 2158 Selector.Matcher selMatcher = (Selector.Matcher) matcher; 2159 IdentityConstraint id; 2160 if ((id = selMatcher.getIdentityConstraint()) != null 2161 && id.getCategory() != IdentityConstraint.IC_KEYREF) { 2162 fValueStoreCache.transplant(id, selMatcher.getInitialDepth()); 2163 } 2164 } 2165 } 2166 2167 for (int i = oldCount - 1; i >= newCount; i--) { 2169 XPathMatcher matcher = fMatcherStack.getMatcherAt(i); 2170 if (matcher instanceof Selector.Matcher) { 2171 Selector.Matcher selMatcher = (Selector.Matcher) matcher; 2172 IdentityConstraint id; 2173 if ((id = selMatcher.getIdentityConstraint()) != null 2174 && id.getCategory() == IdentityConstraint.IC_KEYREF) { 2175 ValueStoreBase values = 2176 fValueStoreCache.getValueStoreFor(id, selMatcher.getInitialDepth()); 2177 if (values != null) values.endDocumentFragment(); 2179 } 2180 } 2181 } 2182 fValueStoreCache.endElement(); 2183 2184 SchemaGrammar[] grammars = null; 2185 if (fElementDepth == 0) { 2187 String invIdRef = fValidationState.checkIDRefID(); 2189 fValidationState.resetIDTables(); 2190 if (invIdRef != null) { 2191 reportSchemaError("cvc-id.1", new Object [] { invIdRef }); 2192 } 2193 if (fFullChecking) { 2195 XSConstraints.fullSchemaChecking( 2196 fGrammarBucket, 2197 fSubGroupHandler, 2198 fCMBuilder, 2199 fXSIErrorReporter.fErrorReporter); 2200 } 2201 2202 grammars = fGrammarBucket.getGrammars(); 2203 if (fGrammarPool != null) { 2205 fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, grammars); 2206 } 2207 augs = endElementPSVI(true, grammars, augs); 2208 } else { 2209 augs = endElementPSVI(false, grammars, augs); 2210 2211 fElementDepth--; 2213 2214 fSubElement = fSubElementStack[fElementDepth]; 2216 fCurrentElemDecl = fElemDeclStack[fElementDepth]; 2217 fNil = fNilStack[fElementDepth]; 2218 fNotation = fNotationStack[fElementDepth]; 2219 fCurrentType = fTypeStack[fElementDepth]; 2220 fCurrentCM = fCMStack[fElementDepth]; 2221 fStrictAssess = fStrictAssessStack[fElementDepth]; 2222 fCurrCMState = fCMStateStack[fElementDepth]; 2223 fSawText = fSawTextStack[fElementDepth]; 2224 fSawCharacters = fStringContent[fElementDepth]; 2225 2226 fWhiteSpace = -1; 2232 fAppendBuffer = false; 2235 fUnionType = false; 2237 } 2238 2239 return augs; 2240 } 2242 final Augmentations endElementPSVI( 2243 boolean root, 2244 SchemaGrammar[] grammars, 2245 Augmentations augs) { 2246 2247 if (fAugPSVI) { 2248 augs = getEmptyAugs(augs); 2249 2250 fCurrentPSVI.fDeclaration = this.fCurrentElemDecl; 2252 fCurrentPSVI.fTypeDecl = this.fCurrentType; 2253 fCurrentPSVI.fNotation = this.fNotation; 2254 fCurrentPSVI.fValidationContext = this.fValidationRoot; 2255 if (fElementDepth > fNFullValidationDepth) { 2260 fCurrentPSVI.fValidationAttempted = ElementPSVI.VALIDATION_FULL; 2261 } 2262 else if (fElementDepth > fNNoneValidationDepth) { 2266 fCurrentPSVI.fValidationAttempted = ElementPSVI.VALIDATION_NONE; 2267 } 2268 else { 2270 fCurrentPSVI.fValidationAttempted = ElementPSVI.VALIDATION_PARTIAL; 2271 fNFullValidationDepth = fNNoneValidationDepth = fElementDepth - 1; 2272 } 2273 2274 if (fDefaultValue != null) 2275 fCurrentPSVI.fSpecified = true; 2276 fCurrentPSVI.fNil = fNil; 2277 fCurrentPSVI.fMemberType = fValidatedInfo.memberType; 2278 fCurrentPSVI.fNormalizedValue = fValidatedInfo.normalizedValue; 2279 fCurrentPSVI.fActualValue = fValidatedInfo.actualValue; 2280 fCurrentPSVI.fActualValueType = fValidatedInfo.actualValueType; 2281 fCurrentPSVI.fItemValueTypes = fValidatedInfo.itemValueTypes; 2282 2283 if (fStrictAssess) { 2284 String [] errors = fXSIErrorReporter.mergeContext(); 2289 2290 fCurrentPSVI.fErrorCodes = errors; 2292 fCurrentPSVI.fValidity = 2294 (errors == null) ? ElementPSVI.VALIDITY_VALID : ElementPSVI.VALIDITY_INVALID; 2295 } else { 2296 fCurrentPSVI.fValidity = ElementPSVI.VALIDITY_NOTKNOWN; 2298 fXSIErrorReporter.popContext(); 2302 } 2303 2304 if (root) { 2305 fCurrentPSVI.fGrammars = grammars; 2307 fCurrentPSVI.fSchemaInformation = null; 2308 } 2309 } 2310 2311 return augs; 2312 2313 } 2314 2315 Augmentations getEmptyAugs(Augmentations augs) { 2316 if (augs == null) { 2317 augs = fAugmentations; 2318 augs.removeAllItems(); 2319 } 2320 augs.putItem(Constants.ELEMENT_PSVI, fCurrentPSVI); 2321 fCurrentPSVI.reset(); 2322 2323 return augs; 2324 } 2325 2326 void storeLocations(String sLocation, String nsLocation) { 2327 if (sLocation != null) { 2328 if (!XMLSchemaLoader.tokenizeSchemaLocationStr(sLocation, fLocationPairs)) { 2329 fXSIErrorReporter.reportError( 2331 XSMessageFormatter.SCHEMA_DOMAIN, 2332 "SchemaLocation", 2333 new Object [] { sLocation }, 2334 XMLErrorReporter.SEVERITY_WARNING); 2335 } 2336 } 2337 if (nsLocation != null) { 2338 XMLSchemaLoader.LocationArray la = 2339 ((XMLSchemaLoader.LocationArray) fLocationPairs.get(XMLSymbols.EMPTY_STRING)); 2340 if (la == null) { 2341 la = new XMLSchemaLoader.LocationArray(); 2342 fLocationPairs.put(XMLSymbols.EMPTY_STRING, la); 2343 } 2344 la.addLocation(nsLocation); 2345 } 2346 2347 } 2349 SchemaGrammar findSchemaGrammar( 2353 short contextType, 2354 String namespace, 2355 QName enclosingElement, 2356 QName triggeringComponet, 2357 XMLAttributes attributes) { 2358 SchemaGrammar grammar = null; 2359 grammar = fGrammarBucket.getGrammar(namespace); 2361 if (grammar == null) { 2362 fXSDDescription.reset(); 2363 fXSDDescription.fContextType = contextType; 2364 fXSDDescription.setNamespace(namespace); 2365 fXSDDescription.fEnclosedElementName = enclosingElement; 2366 fXSDDescription.fTriggeringComponent = triggeringComponet; 2367 fXSDDescription.fAttributes = attributes; 2368 if (fLocator != null) { 2369 fXSDDescription.setBaseSystemId(fLocator.getExpandedSystemId()); 2370 } 2371 2372 String [] temp = null; 2373 Object locationArray = 2374 fLocationPairs.get(namespace == null ? XMLSymbols.EMPTY_STRING : namespace); 2375 if (locationArray != null) 2376 temp = ((XMLSchemaLoader.LocationArray) locationArray).getLocationArray(); 2377 if (temp != null && temp.length != 0) { 2378 fXSDDescription.fLocationHints = new String [temp.length]; 2379 System.arraycopy(temp, 0, fXSDDescription.fLocationHints, 0, temp.length); 2380 } 2381 2382 if (fGrammarPool != null) { 2384 grammar = (SchemaGrammar) fGrammarPool.retrieveGrammar(fXSDDescription); 2385 if (grammar != null) { 2386 if (!fGrammarBucket.putGrammar(grammar, true)) { 2389 fXSIErrorReporter.fErrorReporter.reportError( 2392 XSMessageFormatter.SCHEMA_DOMAIN, 2393 "GrammarConflict", 2394 null, 2395 XMLErrorReporter.SEVERITY_WARNING); 2396 grammar = null; 2397 } 2398 } 2399 } 2400 if (grammar == null && !fUseGrammarPoolOnly) { 2401 try { 2403 XMLInputSource xis = 2404 XMLSchemaLoader.resolveDocument( 2405 fXSDDescription, 2406 fLocationPairs, 2407 fEntityResolver); 2408 grammar = fSchemaLoader.loadSchema(fXSDDescription, xis, fLocationPairs); 2409 } catch (IOException ex) { 2410 final String [] locationHints = fXSDDescription.getLocationHints(); 2411 fXSIErrorReporter.fErrorReporter.reportError( 2412 XSMessageFormatter.SCHEMA_DOMAIN, 2413 "schema_reference.4", 2414 new Object [] { locationHints != null ? locationHints[0] : XMLSymbols.EMPTY_STRING }, 2415 XMLErrorReporter.SEVERITY_WARNING); 2416 } 2417 } 2418 } 2419 2420 return grammar; 2421 2422 } 2424 XSTypeDefinition getAndCheckXsiType(QName element, String xsiType, XMLAttributes attributes) { 2425 2428 QName typeName = null; 2432 try { 2433 typeName = (QName) fQNameDV.validate(xsiType, fValidationState, null); 2434 } catch (InvalidDatatypeValueException e) { 2435 reportSchemaError(e.getKey(), e.getArgs()); 2436 reportSchemaError( 2437 "cvc-elt.4.1", 2438 new Object [] { 2439 element.rawname, 2440 SchemaSymbols.URI_XSI + "," + SchemaSymbols.XSI_TYPE, 2441 xsiType }); 2442 return null; 2443 } 2444 2445 XSTypeDefinition type = null; 2447 if (typeName.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA) { 2449 type = SchemaGrammar.SG_SchemaNS.getGlobalTypeDecl(typeName.localpart); 2450 } 2451 if (type == null) { 2453 SchemaGrammar grammar = 2455 findSchemaGrammar( 2456 XSDDescription.CONTEXT_XSITYPE, 2457 typeName.uri, 2458 element, 2459 typeName, 2460 attributes); 2461 2462 if (grammar != null) 2463 type = grammar.getGlobalTypeDecl(typeName.localpart); 2464 } 2465 if (type == null) { 2467 reportSchemaError("cvc-elt.4.2", new Object [] { element.rawname, xsiType }); 2468 return null; 2469 } 2470 2471 if (fCurrentType != null) { 2474 short block = fCurrentElemDecl.fBlock; 2476 if (fCurrentType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) 2477 block |= ((XSComplexTypeDecl) fCurrentType).fBlock; 2478 if (!XSConstraints.checkTypeDerivationOk(type, fCurrentType, block)) 2479 reportSchemaError( 2480 "cvc-elt.4.3", 2481 new Object [] { element.rawname, xsiType, fCurrentType.getName()}); 2482 } 2483 2484 return type; 2485 } 2487 boolean getXsiNil(QName element, String xsiNil) { 2488 if (fCurrentElemDecl != null && !fCurrentElemDecl.getNillable()) { 2492 reportSchemaError( 2493 "cvc-elt.3.1", 2494 new Object [] { 2495 element.rawname, 2496 SchemaSymbols.URI_XSI + "," + SchemaSymbols.XSI_NIL }); 2497 } 2498 else { 2501 String value = xsiNil.trim(); 2502 if (value.equals(SchemaSymbols.ATTVAL_TRUE) 2503 || value.equals(SchemaSymbols.ATTVAL_TRUE_1)) { 2504 if (fCurrentElemDecl != null 2505 && fCurrentElemDecl.getConstraintType() == XSConstants.VC_FIXED) { 2506 reportSchemaError( 2507 "cvc-elt.3.2.2", 2508 new Object [] { 2509 element.rawname, 2510 SchemaSymbols.URI_XSI + "," + SchemaSymbols.XSI_NIL }); 2511 } 2512 return true; 2513 } 2514 } 2515 return false; 2516 } 2517 2518 void processAttributes(QName element, XMLAttributes attributes, XSAttributeGroupDecl attrGrp) { 2519 2520 if (DEBUG) { 2521 System.out.println("==>processAttributes: " + attributes.getLength()); 2522 } 2523 2524 String wildcardIDName = null; 2526 2527 int attCount = attributes.getLength(); 2529 2530 Augmentations augs = null; 2531 AttributePSVImpl attrPSVI = null; 2532 2533 boolean isSimple = 2534 fCurrentType == null || fCurrentType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE; 2535 2536 XSObjectList attrUses = null; 2537 int useCount = 0; 2538 XSWildcardDecl attrWildcard = null; 2539 if (!isSimple) { 2540 attrUses = attrGrp.getAttributeUses(); 2541 useCount = attrUses.getLength(); 2542 attrWildcard = attrGrp.fAttributeWC; 2543 } 2544 2545 for (int index = 0; index < attCount; index++) { 2549 2550 attributes.getName(index, fTempQName); 2551 2552 if (DEBUG) { 2553 System.out.println("==>process attribute: " + fTempQName); 2554 } 2555 2556 if (fAugPSVI || fIdConstraint) { 2557 augs = attributes.getAugmentations(index); 2558 attrPSVI = (AttributePSVImpl) augs.getItem(Constants.ATTRIBUTE_PSVI); 2559 if (attrPSVI != null) { 2560 attrPSVI.reset(); 2561 } else { 2562 attrPSVI = new AttributePSVImpl(); 2563 augs.putItem(Constants.ATTRIBUTE_PSVI, attrPSVI); 2564 } 2565 attrPSVI.fValidationContext = fValidationRoot; 2567 } 2568 2569 2574 if (fTempQName.uri == SchemaSymbols.URI_XSI) { 2576 XSAttributeDecl attrDecl = null; 2577 if (fTempQName.localpart == SchemaSymbols.XSI_SCHEMALOCATION) 2578 attrDecl = 2579 SchemaGrammar.SG_XSI.getGlobalAttributeDecl( 2580 SchemaSymbols.XSI_SCHEMALOCATION); 2581 else if (fTempQName.localpart == SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION) 2582 attrDecl = 2583 SchemaGrammar.SG_XSI.getGlobalAttributeDecl( 2584 SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION); 2585 else if (fTempQName.localpart == SchemaSymbols.XSI_NIL) 2586 attrDecl = SchemaGrammar.SG_XSI.getGlobalAttributeDecl(SchemaSymbols.XSI_NIL); 2587 else if (fTempQName.localpart == SchemaSymbols.XSI_TYPE) 2588 attrDecl = SchemaGrammar.SG_XSI.getGlobalAttributeDecl(SchemaSymbols.XSI_TYPE); 2589 if (attrDecl != null) { 2590 processOneAttribute(element, attributes, index, attrDecl, null, attrPSVI); 2591 continue; 2592 } 2593 } 2594 2595 if (fTempQName.rawname == XMLSymbols.PREFIX_XMLNS 2597 || fTempQName.rawname.startsWith("xmlns:")) { 2598 continue; 2599 } 2600 2601 if (isSimple) { 2603 reportSchemaError( 2604 "cvc-type.3.1.1", 2605 new Object [] { element.rawname, fTempQName.rawname }); 2606 continue; 2607 } 2608 2609 XSAttributeUseImpl currUse = null, oneUse; 2611 for (int i = 0; i < useCount; i++) { 2612 oneUse = (XSAttributeUseImpl) attrUses.item(i); 2613 if (oneUse.fAttrDecl.fName == fTempQName.localpart 2614 && oneUse.fAttrDecl.fTargetNamespace == fTempQName.uri) { 2615 currUse = oneUse; 2616 break; 2617 } 2618 } 2619 2620 2624 if (currUse == null) { 2626 if (attrWildcard == null || !attrWildcard.allowNamespace(fTempQName.uri)) { 2629 reportSchemaError( 2631 "cvc-complex-type.3.2.2", 2632 new Object [] { element.rawname, fTempQName.rawname }); 2633 continue; 2634 } 2635 } 2636 2637 XSAttributeDecl currDecl = null; 2638 if (currUse != null) { 2639 currDecl = currUse.fAttrDecl; 2640 } else { 2641 if (attrWildcard.fProcessContents == XSWildcardDecl.PC_SKIP) 2644 continue; 2645 2646 SchemaGrammar grammar = 2648 findSchemaGrammar( 2649 XSDDescription.CONTEXT_ATTRIBUTE, 2650 fTempQName.uri, 2651 element, 2652 fTempQName, 2653 attributes); 2654 2655 if (grammar != null) { 2656 currDecl = grammar.getGlobalAttributeDecl(fTempQName.localpart); 2657 } 2658 2659 if (currDecl == null) { 2661 if (attrWildcard.fProcessContents == XSWildcardDecl.PC_STRICT) { 2663 reportSchemaError( 2664 "cvc-complex-type.3.2.2", 2665 new Object [] { element.rawname, fTempQName.rawname }); 2666 } 2667 2668 continue; 2670 } else { 2671 if (currDecl.fType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE 2674 && ((XSSimpleType) currDecl.fType).isIDType()) { 2675 if (wildcardIDName != null) { 2676 reportSchemaError( 2677 "cvc-complex-type.5.1", 2678 new Object [] { element.rawname, currDecl.fName, wildcardIDName }); 2679 } else 2680 wildcardIDName = currDecl.fName; 2681 } 2682 } 2683 } 2684 2685 processOneAttribute(element, attributes, index, currDecl, currUse, attrPSVI); 2686 } 2688 if (!isSimple && attrGrp.fIDAttrName != null && wildcardIDName != null) { 2690 reportSchemaError( 2691 "cvc-complex-type.5.2", 2692 new Object [] { element.rawname, wildcardIDName, attrGrp.fIDAttrName }); 2693 } 2694 2695 } 2697 void processOneAttribute( 2698 QName element, 2699 XMLAttributes attributes, 2700 int index, 2701 XSAttributeDecl currDecl, 2702 XSAttributeUseImpl currUse, 2703 AttributePSVImpl attrPSVI) { 2704 2705 String attrValue = attributes.getValue(index); 2706 fXSIErrorReporter.pushContext(); 2707 2708 XSSimpleType attDV = currDecl.fType; 2715 2716 Object actualValue = null; 2717 try { 2718 actualValue = attDV.validate(attrValue, fValidationState, fValidatedInfo); 2719 if (fNormalizeData) 2721 attributes.setValue(index, fValidatedInfo.normalizedValue); 2722 if (attributes instanceof XMLAttributesImpl) { 2723 XMLAttributesImpl attrs = (XMLAttributesImpl) attributes; 2724 boolean schemaId = 2725 fValidatedInfo.memberType != null 2726 ? fValidatedInfo.memberType.isIDType() 2727 : attDV.isIDType(); 2728 attrs.setSchemaId(index, schemaId); 2729 } 2730 2731 if (attDV.getVariety() == XSSimpleType.VARIETY_ATOMIC 2733 && attDV.getPrimitiveKind() == XSSimpleType.PRIMITIVE_NOTATION) { 2734 QName qName = (QName) actualValue; 2735 SchemaGrammar grammar = fGrammarBucket.getGrammar(qName.uri); 2736 2737 2743 if (grammar != null) { 2744 fNotation = grammar.getGlobalNotationDecl(qName.localpart); 2745 } 2746 } 2747 } catch (InvalidDatatypeValueException idve) { 2748 reportSchemaError(idve.getKey(), idve.getArgs()); 2749 reportSchemaError( 2750 "cvc-attribute.3", 2751 new Object [] { element.rawname, fTempQName.rawname, attrValue, attDV.getName()}); 2752 } 2753 2754 if (actualValue != null && currDecl.getConstraintType() == XSConstants.VC_FIXED) { 2757 if (!isComparable(fValidatedInfo, currDecl.fDefault) || !actualValue.equals(currDecl.fDefault.actualValue)) { 2758 reportSchemaError( 2759 "cvc-attribute.4", 2760 new Object [] { 2761 element.rawname, 2762 fTempQName.rawname, 2763 attrValue, 2764 currDecl.fDefault.stringValue()}); 2765 } 2766 } 2767 2768 if (actualValue != null 2770 && currUse != null 2771 && currUse.fConstraintType == XSConstants.VC_FIXED) { 2772 if (!isComparable(fValidatedInfo, currUse.fDefault) || !actualValue.equals(currUse.fDefault.actualValue)) { 2773 reportSchemaError( 2774 "cvc-complex-type.3.1", 2775 new Object [] { 2776 element.rawname, 2777 fTempQName.rawname, 2778 attrValue, 2779 currUse.fDefault.stringValue()}); 2780 } 2781 } 2782 if (fIdConstraint) { 2783 attrPSVI.fActualValue = actualValue; 2784 } 2785 2786 if (fAugPSVI) { 2787 attrPSVI.fDeclaration = currDecl; 2789 attrPSVI.fTypeDecl = attDV; 2791 2792 attrPSVI.fMemberType = fValidatedInfo.memberType; 2794 attrPSVI.fNormalizedValue = fValidatedInfo.normalizedValue; 2799 attrPSVI.fActualValue = fValidatedInfo.actualValue; 2800 attrPSVI.fActualValueType = fValidatedInfo.actualValueType; 2801 attrPSVI.fItemValueTypes = fValidatedInfo.itemValueTypes; 2802 2803 2804 2805 attrPSVI.fValidationAttempted = AttributePSVI.VALIDATION_FULL; 2807 2808 String [] errors = fXSIErrorReporter.mergeContext(); 2809 attrPSVI.fErrorCodes = errors; 2811 attrPSVI.fValidity = 2813 (errors == null) ? AttributePSVI.VALIDITY_VALID : AttributePSVI.VALIDITY_INVALID; 2814 } 2815 } 2816 2817 void addDefaultAttributes( 2818 QName element, 2819 XMLAttributes attributes, 2820 XSAttributeGroupDecl attrGrp) { 2821 if (DEBUG) { 2828 System.out.println("==>addDefaultAttributes: " + element); 2829 } 2830 XSObjectList attrUses = attrGrp.getAttributeUses(); 2831 int useCount = attrUses.getLength(); 2832 XSAttributeUseImpl currUse; 2833 XSAttributeDecl currDecl; 2834 short constType; 2835 ValidatedInfo defaultValue; 2836 boolean isSpecified; 2837 QName attName; 2838 for (int i = 0; i < useCount; i++) { 2840 2841 currUse = (XSAttributeUseImpl) attrUses.item(i); 2842 currDecl = currUse.fAttrDecl; 2843 constType = currUse.fConstraintType; 2845 defaultValue = currUse.fDefault; 2846 if (constType == XSConstants.VC_NONE) { 2847 constType = currDecl.getConstraintType(); 2848 defaultValue = currDecl.fDefault; 2849 } 2850 isSpecified = attributes.getValue(currDecl.fTargetNamespace, currDecl.fName) != null; 2852 2853 if (currUse.fUse == SchemaSymbols.USE_REQUIRED) { 2858 if (!isSpecified) 2859 reportSchemaError( 2860 "cvc-complex-type.4", 2861 new Object [] { element.rawname, currDecl.fName }); 2862 } 2863 if (!isSpecified && constType != XSConstants.VC_NONE) { 2865 attName = 2866 new QName(null, currDecl.fName, currDecl.fName, currDecl.fTargetNamespace); 2867 String normalized = (defaultValue != null) ? defaultValue.stringValue() : ""; 2868 int attrIndex = attributes.addAttribute(attName, "CDATA", normalized); 2869 if (attributes instanceof XMLAttributesImpl) { 2870 XMLAttributesImpl attrs = (XMLAttributesImpl) attributes; 2871 boolean schemaId = 2872 defaultValue != null 2873 && defaultValue.memberType != null 2874 ? defaultValue.memberType.isIDType() 2875 : currDecl.fType.isIDType(); 2876 attrs.setSchemaId(attrIndex, schemaId); 2877 } 2878 2879 if (fAugPSVI) { 2880 2881 Augmentations augs = attributes.getAugmentations(attrIndex); 2883 AttributePSVImpl attrPSVI = new AttributePSVImpl(); 2884 augs.putItem(Constants.ATTRIBUTE_PSVI, attrPSVI); 2885 2886 attrPSVI.fDeclaration = currDecl; 2887 attrPSVI.fTypeDecl = currDecl.fType; 2888 attrPSVI.fMemberType = defaultValue.memberType; 2889 attrPSVI.fNormalizedValue = normalized; 2890 attrPSVI.fActualValue = defaultValue.actualValue; 2891 attrPSVI.fActualValueType = defaultValue.actualValueType; 2892 attrPSVI.fItemValueTypes = defaultValue.itemValueTypes; 2893 attrPSVI.fValidationContext = fValidationRoot; 2894 attrPSVI.fValidity = AttributePSVI.VALIDITY_VALID; 2895 attrPSVI.fValidationAttempted = AttributePSVI.VALIDATION_FULL; 2896 attrPSVI.fSpecified = true; 2897 } 2898 } 2899 2900 } } 2903 2908 void processElementContent(QName element) { 2909 if (fCurrentElemDecl != null 2911 && fCurrentElemDecl.fDefault != null 2912 && !fSawText 2913 && !fSubElement 2914 && !fNil) { 2915 2916 String strv = fCurrentElemDecl.fDefault.stringValue(); 2917 int bufLen = strv.length(); 2918 if (fNormalizedStr.ch == null || fNormalizedStr.ch.length < bufLen) { 2919 fNormalizedStr.ch = new char[bufLen]; 2920 } 2921 strv.getChars(0, bufLen, fNormalizedStr.ch, 0); 2922 fNormalizedStr.offset = 0; 2923 fNormalizedStr.length = bufLen; 2924 fDefaultValue = fNormalizedStr; 2925 } 2926 2928 fValidatedInfo.normalizedValue = null; 2929 2930 if (fNil) { 2933 if (fSubElement || fSawText) { 2934 reportSchemaError( 2935 "cvc-elt.3.2.1", 2936 new Object [] { 2937 element.rawname, 2938 SchemaSymbols.URI_XSI + "," + SchemaSymbols.XSI_NIL }); 2939 } 2940 } 2941 2942 this.fValidatedInfo.reset(); 2943 2944 if (fCurrentElemDecl != null 2947 && fCurrentElemDecl.getConstraintType() != XSConstants.VC_NONE 2948 && !fSubElement 2949 && !fSawText 2950 && !fNil) { 2951 if (fCurrentType != fCurrentElemDecl.fType) { 2953 if (XSConstraints 2955 .ElementDefaultValidImmediate( 2956 fCurrentType, 2957 fCurrentElemDecl.fDefault.stringValue(), 2958 fState4XsiType, 2959 null) 2960 == null) 2961 reportSchemaError( 2962 "cvc-elt.5.1.1", 2963 new Object [] { 2964 element.rawname, 2965 fCurrentType.getName(), 2966 fCurrentElemDecl.fDefault.stringValue()}); 2967 } 2968 elementLocallyValidType(element, fCurrentElemDecl.fDefault.stringValue()); 2972 } else { 2973 2976 Object actualValue = elementLocallyValidType(element, fBuffer); 2979 if (fCurrentElemDecl != null 2981 && fCurrentElemDecl.getConstraintType() == XSConstants.VC_FIXED 2982 && !fNil) { 2983 String content = fBuffer.toString(); 2984 if (fSubElement) 2986 reportSchemaError("cvc-elt.5.2.2.1", new Object [] { element.rawname }); 2987 if (fCurrentType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { 2989 XSComplexTypeDecl ctype = (XSComplexTypeDecl) fCurrentType; 2990 if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_MIXED) { 2992 if (!fCurrentElemDecl.fDefault.normalizedValue.equals(content)) 2994 reportSchemaError( 2995 "cvc-elt.5.2.2.2.1", 2996 new Object [] { 2997 element.rawname, 2998 content, 2999 fCurrentElemDecl.fDefault.normalizedValue }); 3000 } 3001 else if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_SIMPLE) { 3003 if (actualValue != null && (!isComparable(fValidatedInfo, fCurrentElemDecl.fDefault) 3004 || !actualValue.equals(fCurrentElemDecl.fDefault.actualValue))) { 3005 reportSchemaError( 3006 "cvc-elt.5.2.2.2.2", 3007 new Object [] { 3008 element.rawname, 3009 content, 3010 fCurrentElemDecl.fDefault.stringValue()}); 3011 } 3012 } 3013 } else if (fCurrentType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) { 3014 if (actualValue != null && (!isComparable(fValidatedInfo, fCurrentElemDecl.fDefault) 3015 || !actualValue.equals(fCurrentElemDecl.fDefault.actualValue))) { 3016 reportSchemaError( 3019 "cvc-elt.5.2.2.2.2", 3020 new Object [] { 3021 element.rawname, 3022 content, 3023 fCurrentElemDecl.fDefault.stringValue()}); 3024 } 3025 } 3026 } 3027 } 3028 3029 if (fDefaultValue == null && fNormalizeData && fDocumentHandler != null && fUnionType) { 3030 String content = fValidatedInfo.normalizedValue; 3033 if (content == null) 3034 content = fBuffer.toString(); 3035 3036 int bufLen = content.length(); 3037 if (fNormalizedStr.ch == null || fNormalizedStr.ch.length < bufLen) { 3038 fNormalizedStr.ch = new char[bufLen]; 3039 } 3040 content.getChars(0, bufLen, fNormalizedStr.ch, 0); 3041 fNormalizedStr.offset = 0; 3042 fNormalizedStr.length = bufLen; 3043 fDocumentHandler.characters(fNormalizedStr, null); 3044 } 3045 } 3047 Object elementLocallyValidType(QName element, Object textContent) { 3048 if (fCurrentType == null) 3049 return null; 3050 3051 Object retValue = null; 3052 if (fCurrentType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) { 3056 if (fSubElement) 3058 reportSchemaError("cvc-type.3.1.2", new Object [] { element.rawname }); 3059 if (!fNil) { 3061 XSSimpleType dv = (XSSimpleType) fCurrentType; 3062 try { 3063 if (!fNormalizeData || fUnionType) { 3064 fValidationState.setNormalizationRequired(true); 3065 } 3066 retValue = dv.validate(textContent, fValidationState, fValidatedInfo); 3067 } catch (InvalidDatatypeValueException e) { 3068 reportSchemaError(e.getKey(), e.getArgs()); 3069 reportSchemaError( 3070 "cvc-type.3.1.3", 3071 new Object [] { element.rawname, textContent }); 3072 } 3073 } 3074 } else { 3075 retValue = elementLocallyValidComplexType(element, textContent); 3077 } 3078 3079 return retValue; 3080 } 3082 Object elementLocallyValidComplexType(QName element, Object textContent) { 3083 Object actualValue = null; 3084 XSComplexTypeDecl ctype = (XSComplexTypeDecl) fCurrentType; 3085 3086 if (!fNil) { 3091 if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_EMPTY 3093 && (fSubElement || fSawText)) { 3094 reportSchemaError("cvc-complex-type.2.1", new Object [] { element.rawname }); 3095 } 3096 else if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_SIMPLE) { 3098 if (fSubElement) 3099 reportSchemaError("cvc-complex-type.2.2", new Object [] { element.rawname }); 3100 XSSimpleType dv = ctype.fXSSimpleType; 3101 try { 3102 if (!fNormalizeData || fUnionType) { 3103 fValidationState.setNormalizationRequired(true); 3104 } 3105 actualValue = dv.validate(textContent, fValidationState, fValidatedInfo); 3106 } catch (InvalidDatatypeValueException e) { 3107 reportSchemaError(e.getKey(), e.getArgs()); 3108 reportSchemaError("cvc-complex-type.2.2", new Object [] { element.rawname }); 3109 } 3110 } 3113 else if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_ELEMENT) { 3115 if (fSawCharacters) { 3116 reportSchemaError("cvc-complex-type.2.3", new Object [] { element.rawname }); 3117 } 3118 } 3119 if (ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_ELEMENT 3121 || ctype.fContentType == XSComplexTypeDecl.CONTENTTYPE_MIXED) { 3122 if (DEBUG) { 3125 System.out.println(fCurrCMState); 3126 } 3127 if (fCurrCMState[0] >= 0 && !fCurrentCM.endContentModel(fCurrCMState)) { 3128 String expected = expectedStr(fCurrentCM.whatCanGoHere(fCurrCMState)); 3129 reportSchemaError( 3130 "cvc-complex-type.2.4.b", 3131 new Object [] { element.rawname, expected }); 3132 } 3133 } 3134 } 3135 return actualValue; 3136 } 3138 void reportSchemaError(String key, Object [] arguments) { 3139 if (fDoValidation) 3140 fXSIErrorReporter.reportError( 3141 XSMessageFormatter.SCHEMA_DOMAIN, 3142 key, 3143 arguments, 3144 XMLErrorReporter.SEVERITY_ERROR); 3145 } 3146 3147 3148 private boolean isComparable(ValidatedInfo info1, ValidatedInfo info2) { 3149 final short primitiveType1 = convertToPrimitiveKind(info1.actualValueType); 3150 final short primitiveType2 = convertToPrimitiveKind(info2.actualValueType); 3151 if (primitiveType1 != primitiveType2) { 3152 return (primitiveType1 == XSConstants.ANYSIMPLETYPE_DT && primitiveType2 == XSConstants.STRING_DT || 3153 primitiveType1 == XSConstants.STRING_DT && primitiveType2 == XSConstants.ANYSIMPLETYPE_DT); 3154 } 3155 else if (primitiveType1 == XSConstants.LIST_DT || primitiveType1 == XSConstants.LISTOFUNION_DT) { 3156 final ShortList typeList1 = info1.itemValueTypes; 3157 final ShortList typeList2 = info2.itemValueTypes; 3158 final int typeList1Length = typeList1 != null ? typeList1.getLength() : 0; 3159 final int typeList2Length = typeList2 != null ? typeList2.getLength() : 0; 3160 if (typeList1Length != typeList2Length) { 3161 return false; 3162 } 3163 for (int i = 0; i < typeList1Length; ++i) { 3164 final short primitiveItem1 = convertToPrimitiveKind(typeList1.item(i)); 3165 final short primitiveItem2 = convertToPrimitiveKind(typeList2.item(i)); 3166 if (primitiveItem1 != primitiveItem2) { 3167 if (primitiveItem1 == XSConstants.ANYSIMPLETYPE_DT && primitiveItem2 == XSConstants.STRING_DT || 3168 primitiveItem1 == XSConstants.STRING_DT && primitiveItem2 == XSConstants.ANYSIMPLETYPE_DT) { 3169 continue; 3170 } 3171 return false; 3172 } 3173 } 3174 } 3175 return true; 3176 } 3177 3178 private short convertToPrimitiveKind(short valueType) { 3179 3180 if (valueType <= XSConstants.NOTATION_DT) { 3181 return valueType; 3182 } 3183 3184 if (valueType <= XSConstants.ENTITY_DT) { 3185 return XSConstants.STRING_DT; 3186 } 3187 3188 if (valueType <= XSConstants.POSITIVEINTEGER_DT) { 3189 return XSConstants.DECIMAL_DT; 3190 } 3191 3192 return valueType; 3193 } 3194 3195 private String expectedStr(Vector expected) { 3196 StringBuffer ret = new StringBuffer ("{"); 3197 int size = expected.size(); 3198 for (int i = 0; i < size; i++) { 3199 if (i > 0) 3200 ret.append(", "); 3201 ret.append(expected.elementAt(i).toString()); 3202 } 3203 ret.append('}'); 3204 return ret.toString(); 3205 } 3206 3207 3208 3209 3211 3216 protected static class XPathMatcherStack { 3217 3218 3222 3223 protected XPathMatcher[] fMatchers = new XPathMatcher[4]; 3224 3225 3226 protected int fMatchersCount; 3227 3228 3229 protected IntStack fContextStack = new IntStack(); 3230 3231 3235 public XPathMatcherStack() { 3236 } 3238 3242 3243 public void clear() { 3244 for (int i = 0; i < fMatchersCount; i++) { 3245 fMatchers[i] = null; 3246 } 3247 fMatchersCount = 0; 3248 fContextStack.clear(); 3249 } 3251 3252 public int size() { 3253 return fContextStack.size(); 3254 } 3256 3257 public int getMatcherCount() { 3258 return fMatchersCount; 3259 } 3261 3262 public void addMatcher(XPathMatcher matcher) { 3263 ensureMatcherCapacity(); 3264 fMatchers[fMatchersCount++] = matcher; 3265 } 3267 3268 public XPathMatcher getMatcherAt(int index) { 3269 return fMatchers[index]; 3270 } 3272 3273 public void pushContext() { 3274 fContextStack.push(fMatchersCount); 3275 } 3277 3278 public void popContext() { 3279 fMatchersCount = fContextStack.pop(); 3280 } 3282 3286 3287 private void ensureMatcherCapacity() { 3288 if (fMatchersCount == fMatchers.length) { 3289 XPathMatcher[] array = new XPathMatcher[fMatchers.length * 2]; 3290 System.arraycopy(fMatchers, 0, array, 0, fMatchers.length); 3291 fMatchers = array; 3292 } 3293 } 3295 } 3297 3299 3305 protected abstract class ValueStoreBase implements ValueStore { 3306 3307 3311 3312 protected IdentityConstraint fIdentityConstraint; 3313 protected int fFieldCount = 0; 3314 protected Field[] fFields = null; 3315 3316 protected Object [] fLocalValues = null; 3317 protected short[] fLocalValueTypes = null; 3318 protected ShortList[] fLocalItemValueTypes = null; 3319 3320 3321 protected int fValuesCount; 3322 3323 3324 public final Vector fValues = new Vector (); 3325 public ShortVector fValueTypes = null; 3326 public Vector fItemValueTypes = null; 3327 3328 private boolean fUseValueTypeVector = false; 3329 private int fValueTypesLength = 0; 3330 private short fValueType = 0; 3331 3332 private boolean fUseItemValueTypeVector = false; 3333 private int fItemValueTypesLength = 0; 3334 private ShortList fItemValueType = null; 3335 3336 3337 final StringBuffer fTempBuffer = new StringBuffer (); 3338 3339 3343 3344 protected ValueStoreBase(IdentityConstraint identityConstraint) { 3345 fIdentityConstraint = identityConstraint; 3346 fFieldCount = fIdentityConstraint.getFieldCount(); 3347 fFields = new Field[fFieldCount]; 3348 fLocalValues = new Object [fFieldCount]; 3349 fLocalValueTypes = new short[fFieldCount]; 3350 fLocalItemValueTypes = new ShortList[fFieldCount]; 3351 for (int i = 0; i < fFieldCount; i++) { 3352 fFields[i] = fIdentityConstraint.getFieldAt(i); 3353 } 3354 } 3356 3360 public void clear() { 3363 fValuesCount = 0; 3364 fUseValueTypeVector = false; 3365 fValueTypesLength = 0; 3366 fValueType = 0; 3367 fUseItemValueTypeVector = false; 3368 fItemValueTypesLength = 0; 3369 fItemValueType = null; 3370 fValues.setSize(0); 3371 if (fValueTypes != null) { 3372 fValueTypes.clear(); 3373 } 3374 if (fItemValueTypes != null) { 3375 fItemValueTypes.setSize(0); 3376 } 3377 } 3379 public void append(ValueStoreBase newVal) { 3381 for (int i = 0; i < newVal.fValues.size(); i++) { 3382 fValues.addElement(newVal.fValues.elementAt(i)); 3383 } 3384 } 3386 3387 public void startValueScope() { 3388 fValuesCount = 0; 3389 for (int i = 0; i < fFieldCount; i++) { 3390 fLocalValues[i] = null; 3391 fLocalValueTypes[i] = 0; 3392 fLocalItemValueTypes[i] = null; 3393 } 3394 } 3396 3397 public void endValueScope() { 3398 3399 if (fValuesCount == 0) { 3400 if (fIdentityConstraint.getCategory() == IdentityConstraint.IC_KEY) { 3401 String code = "AbsentKeyValue"; 3402 String eName = fIdentityConstraint.getElementName(); 3403 reportSchemaError(code, new Object [] { eName }); 3404 } 3405 return; 3406 } 3407 3408 if (fValuesCount != fFieldCount) { 3410 switch (fIdentityConstraint.getCategory()) { 3411 case IdentityConstraint.IC_UNIQUE : 3412 { 3413 String code = "UniqueNotEnoughValues"; 3414 String ename = fIdentityConstraint.getElementName(); 3415 reportSchemaError(code, new Object [] { ename }); 3416 break; 3417 } 3418 case IdentityConstraint.IC_KEY : 3419 { 3420 String code = "KeyNotEnoughValues"; 3421 UniqueOrKey key = (UniqueOrKey) fIdentityConstraint; 3422 String ename = fIdentityConstraint.getElementName(); 3423 String kname = key.getIdentityConstraintName(); 3424 reportSchemaError(code, new Object [] { ename, kname }); 3425 break; 3426 } 3427 case IdentityConstraint.IC_KEYREF : 3428 { 3429 String code = "KeyRefNotEnoughValues"; 3430 KeyRef keyref = (KeyRef) fIdentityConstraint; 3431 String ename = fIdentityConstraint.getElementName(); 3432 String kname = (keyref.getKey()).getIdentityConstraintName(); 3433 reportSchemaError(code, new Object [] { ename, kname }); 3434 break; 3435 } 3436 } 3437 return; 3438 } 3439 3440 } 3442 public void endDocumentFragment() { 3448 } 3450 3455 public void endDocument() { 3456 } 3458 3462 3465 3466 public void reportError(String key, Object [] args) { 3467 reportSchemaError(key, args); 3468 } 3470 3478 public void addValue(Field field, Object actualValue, short valueType, ShortList itemValueType) { 3479 int i; 3480 for (i = fFieldCount - 1; i > -1; i--) { 3481 if (fFields[i] == field) { 3482 break; 3483 } 3484 } 3485 if (i == -1) { 3487 String code = "UnknownField"; 3488 reportSchemaError(code, new Object [] { field.toString()}); 3489 return; 3490 } 3491 if (Boolean.TRUE != mayMatch(field)) { 3492 String code = "FieldMultipleMatch"; 3493 reportSchemaError(code, new Object [] { field.toString()}); 3494 } else { 3495 fValuesCount++; 3496 } 3497 fLocalValues[i] = actualValue; 3498 fLocalValueTypes[i] = valueType; 3499 fLocalItemValueTypes[i] = itemValueType; 3500 if (fValuesCount == fFieldCount) { 3501 checkDuplicateValues(); 3502 for (i = 0; i < fFieldCount; i++) { 3504 fValues.addElement(fLocalValues[i]); 3505 addValueType(fLocalValueTypes[i]); 3506 addItemValueType(fLocalItemValueTypes[i]); 3507 } 3508 } 3509 } 3511 3514 public boolean contains() { 3515 int next = 0; 3518 final int size = fValues.size(); 3519 LOOP : for (int i = 0; i < size; i = next) { 3520 next = i + fFieldCount; 3521 for (int j = 0; j < fFieldCount; j++) { 3522 Object value1 = fLocalValues[j]; 3523 Object value2 = fValues.elementAt(i); 3524 short valueType1 = fLocalValueTypes[j]; 3525 short valueType2 = getValueTypeAt(i); 3526 if (value1 == null || value2 == null || valueType1 != valueType2 || !(value1.equals(value2))) { 3527 continue LOOP; 3528 } 3529 else if(valueType1 == XSConstants.LIST_DT || valueType1 == XSConstants.LISTOFUNION_DT) { 3530 ShortList list1 = fLocalItemValueTypes[j]; 3531 ShortList list2 = getItemValueTypeAt(i); 3532 if(list1 == null || list2 == null || !list1.equals(list2)) 3533 continue LOOP; 3534 } 3535 i++; 3536 } 3537 return true; 3539 } 3540 return false; 3542 } 3544 3549 public int contains(ValueStoreBase vsb) { 3550 3551 final Vector values = vsb.fValues; 3552 final int size1 = values.size(); 3553 if (fFieldCount <= 1) { 3554 for (int i = 0; i < size1; ++i) { 3555 short val = vsb.getValueTypeAt(i); 3556 if (!valueTypeContains(val) || !fValues.contains(values.elementAt(i))) { 3557 return i; 3558 } 3559 else if(val == XSConstants.LIST_DT || val == XSConstants.LISTOFUNION_DT) { 3560 ShortList list1 = vsb.getItemValueTypeAt(i); 3561 if (!itemValueTypeContains(list1)) { 3562 return i; 3563 } 3564 } 3565 } 3566 } 3567 3568 else { 3569 final int size2 = fValues.size(); 3570 3571 OUTER: for (int i = 0; i < size1; i += fFieldCount) { 3572 3573 INNER: for (int j = 0; j < size2; j += fFieldCount) { 3574 for (int k = 0; k < fFieldCount; ++k) { 3575 final Object value1 = values.elementAt(i+k); 3576 final Object value2 = fValues.elementAt(j+k); 3577 final short valueType1 = vsb.getValueTypeAt(i+k); 3578 final short valueType2 = getValueTypeAt(j+k); 3579 if (value1 != value2 && (valueType1 != valueType2 || value1 == null || !value1.equals(value2))) { 3580 continue INNER; 3581 } 3582 else if(valueType1 == XSConstants.LIST_DT || valueType1 == XSConstants.LISTOFUNION_DT) { 3583 ShortList list1 = vsb.getItemValueTypeAt(i+k); 3584 ShortList list2 = getItemValueTypeAt(j+k); 3585 if (list1 == null || list2 == null || !list1.equals(list2)) { 3586 continue INNER; 3587 } 3588 } 3589 } 3590 continue OUTER; 3591 } 3592 return i; 3593 } 3594 } 3595 return -1; 3596 3597 } 3599 3603 protected void checkDuplicateValues() { 3604 } 3607 3608 protected String toString(Object [] values) { 3609 3610 int size = values.length; 3612 if (size == 0) { 3613 return ""; 3614 } 3615 3616 fTempBuffer.setLength(0); 3617 3618 for (int i = 0; i < size; i++) { 3620 if (i > 0) { 3621 fTempBuffer.append(','); 3622 } 3623 fTempBuffer.append(values[i]); 3624 } 3625 return fTempBuffer.toString(); 3626 3627 } 3629 3630 protected String toString(Vector values, int start, int length) { 3631 3632 if (length == 0) { 3634 return ""; 3635 } 3636 3637 if (length == 1) { 3639 return String.valueOf(values.elementAt(start)); 3640 } 3641 3642 StringBuffer str = new StringBuffer (); 3644 for (int i = 0; i < length; i++) { 3645 if (i > 0) { 3646 str.append(','); 3647 } 3648 str.append(values.elementAt(start + i)); 3649 } 3650 return str.toString(); 3651 3652 } 3654 3658 3659 public String toString() { 3660 String s = super.toString(); 3661 int index1 = s.lastIndexOf('$'); 3662 if (index1 != -1) { 3663 s = s.substring(index1 + 1); 3664 } 3665 int index2 = s.lastIndexOf('.'); 3666 if (index2 != -1) { 3667 s = s.substring(index2 + 1); 3668 } 3669 return s + '[' + fIdentityConstraint + ']'; 3670 } 3672 3676 private void addValueType(short type) { 3677 if (fUseValueTypeVector) { 3678 fValueTypes.add(type); 3679 } 3680 else if (fValueTypesLength++ == 0) { 3681 fValueType = type; 3682 } 3683 else if (fValueType != type) { 3684 fUseValueTypeVector = true; 3685 if (fValueTypes == null) { 3686 fValueTypes = new ShortVector(fValueTypesLength * 2); 3687 } 3688 for (int i = 1; i < fValueTypesLength; ++i) { 3689 fValueTypes.add(fValueType); 3690 } 3691 fValueTypes.add(type); 3692 } 3693 } 3694 3695 private short getValueTypeAt(int index) { 3696 if (fUseValueTypeVector) { 3697 return fValueTypes.valueAt(index); 3698 } 3699 return fValueType; 3700 } 3701 3702 private boolean valueTypeContains(short value) { 3703 if (fUseValueTypeVector) { 3704 return fValueTypes.contains(value); 3705 } 3706 return fValueType == value; 3707 } 3708 3709 private void addItemValueType(ShortList itemValueType) { 3710 if (fUseItemValueTypeVector) { 3711 fItemValueTypes.add(itemValueType); 3712 } 3713 else if (fItemValueTypesLength++ == 0) { 3714 fItemValueType = itemValueType; 3715 } 3716 else if (!(fItemValueType == itemValueType || 3717 (fItemValueType != null && fItemValueType.equals(itemValueType)))) { 3718 fUseItemValueTypeVector = true; 3719 if (fItemValueTypes == null) { 3720 fItemValueTypes = new Vector (fItemValueTypesLength * 2); 3721 } 3722 for (int i = 1; i < fItemValueTypesLength; ++i) { 3723 fItemValueTypes.add(fItemValueType); 3724 } 3725 fItemValueTypes.add(itemValueType); 3726 } 3727 } 3728 3729 private ShortList getItemValueTypeAt(int index) { 3730 if (fUseItemValueTypeVector) { 3731 return (ShortList) fItemValueTypes.elementAt(index); 3732 } 3733 return fItemValueType; 3734 } 3735 3736 private boolean itemValueTypeContains(ShortList value) { 3737 if (fUseItemValueTypeVector) { 3738 return fItemValueTypes.contains(value); 3739 } 3740 return fItemValueType == value || 3741 (fItemValueType != null && fItemValueType.equals(value)); 3742 } 3743 3744 } 3746 3751 protected class UniqueValueStore extends ValueStoreBase { 3752 3753 3757 3758 public UniqueValueStore(UniqueOrKey unique) { 3759 super(unique); 3760 } 3762 3766 3769 protected void checkDuplicateValues() { 3770 if (contains()) { 3772 String code = "DuplicateUnique"; 3773 String value = toString(fLocalValues); 3774 String ename = fIdentityConstraint.getElementName(); 3775 reportSchemaError(code, new Object [] { value, ename }); 3776 } 3777 } 3779 } 3781 3786 protected class KeyValueStore extends ValueStoreBase { 3787 3788 3790 3794 3795 public KeyValueStore(UniqueOrKey key) { 3796 super(key); 3797 } 3799 3803 3806 protected void checkDuplicateValues() { 3807 if (contains()) { 3808 String code = "DuplicateKey"; 3809 String value = toString(fLocalValues); 3810 String ename = fIdentityConstraint.getElementName(); 3811 reportSchemaError(code, new Object [] { value, ename }); 3812 } 3813 } 3815 } 3817 3822 protected class KeyRefValueStore extends ValueStoreBase { 3823 3824 3828 3829 protected ValueStoreBase fKeyValueStore; 3830 3831 3835 3836 public KeyRefValueStore(KeyRef keyRef, KeyValueStore keyValueStore) { 3837 super(keyRef); 3838 fKeyValueStore = keyValueStore; 3839 } 3841 3845 public void endDocumentFragment() { 3848 3849 super.endDocumentFragment(); 3851 3852 fKeyValueStore = 3855 (ValueStoreBase) fValueStoreCache.fGlobalIDConstraintMap.get( 3856 ((KeyRef) fIdentityConstraint).getKey()); 3857 3858 if (fKeyValueStore == null) { 3859 String code = "KeyRefOutOfScope"; 3861 String value = fIdentityConstraint.toString(); 3862 reportSchemaError(code, new Object [] { value }); 3863 return; 3864 } 3865 int errorIndex = fKeyValueStore.contains(this); 3866 if (errorIndex != -1) { 3867 String code = "KeyNotFound"; 3868 String values = toString(fValues, errorIndex, fFieldCount); 3869 String element = fIdentityConstraint.getElementName(); 3870 String name = fIdentityConstraint.getName(); 3871 reportSchemaError(code, new Object [] { name, values, element }); 3872 } 3873 3874 } 3876 3877 public void endDocument() { 3878 super.endDocument(); 3879 3880 } 3882 } 3884 3886 3892 protected class ValueStoreCache { 3893 3894 final LocalIDKey fLocalId = new LocalIDKey(); 3898 3900 3901 protected final Vector fValueStores = new Vector (); 3902 3903 3912 protected final Hashtable fIdentityConstraint2ValueStoreMap = new Hashtable (); 3913 3914 protected final Stack fGlobalMapStack = new Stack (); 3934 protected final Hashtable fGlobalIDConstraintMap = new Hashtable (); 3935 3936 3940 3941 public ValueStoreCache() { 3942 } 3944 3948 3949 public void startDocument() { 3950 fValueStores.removeAllElements(); 3951 fIdentityConstraint2ValueStoreMap.clear(); 3952 fGlobalIDConstraintMap.clear(); 3953 fGlobalMapStack.removeAllElements(); 3954 } 3956 public void startElement() { 3959 if (fGlobalIDConstraintMap.size() > 0) 3961 fGlobalMapStack.push(fGlobalIDConstraintMap.clone()); 3962 else 3963 fGlobalMapStack.push(null); 3964 fGlobalIDConstraintMap.clear(); 3965 } 3967 3970 public void endElement() { 3971 if (fGlobalMapStack.isEmpty()) 3972 return; Hashtable oldMap = (Hashtable ) fGlobalMapStack.pop(); 3974 if (oldMap == null) 3976 return; 3977 3978 Enumeration keys = oldMap.keys(); 3979 while (keys.hasMoreElements()) { 3980 IdentityConstraint id = (IdentityConstraint) keys.nextElement(); 3981 ValueStoreBase oldVal = (ValueStoreBase) oldMap.get(id); 3982 if (oldVal != null) { 3983 ValueStoreBase currVal = (ValueStoreBase) fGlobalIDConstraintMap.get(id); 3984 if (currVal == null) 3985 fGlobalIDConstraintMap.put(id, oldVal); 3986 else if (currVal != oldVal) { 3987 currVal.append(oldVal); 3988 } 3989 } 3990 } 3991 } 3993 3997 public void initValueStoresFor(XSElementDecl eDecl, FieldActivator activator) { 3998 IdentityConstraint[] icArray = eDecl.fIDConstraints; 4000 int icCount = eDecl.fIDCPos; 4001 for (int i = 0; i < icCount; i++) { 4002 switch (icArray[i].getCategory()) { 4003 case (IdentityConstraint.IC_UNIQUE) : 4004 UniqueOrKey unique = (UniqueOrKey) icArray[i]; 4006 LocalIDKey toHash = new LocalIDKey(unique, fElementDepth); 4007 UniqueValueStore uniqueValueStore = 4008 (UniqueValueStore) fIdentityConstraint2ValueStoreMap.get(toHash); 4009 if (uniqueValueStore == null) { 4010 uniqueValueStore = new UniqueValueStore(unique); 4011 fIdentityConstraint2ValueStoreMap.put(toHash, uniqueValueStore); 4012 } else { 4013 uniqueValueStore.clear(); 4014 } 4015 fValueStores.addElement(uniqueValueStore); 4016 activateSelectorFor(icArray[i]); 4017 break; 4018 case (IdentityConstraint.IC_KEY) : 4019 UniqueOrKey key = (UniqueOrKey) icArray[i]; 4021 toHash = new LocalIDKey(key, fElementDepth); 4022 KeyValueStore keyValueStore = 4023 (KeyValueStore) fIdentityConstraint2ValueStoreMap.get(toHash); 4024 if (keyValueStore == null) { 4025 keyValueStore = new KeyValueStore(key); 4026 fIdentityConstraint2ValueStoreMap.put(toHash, keyValueStore); 4027 } else { 4028 keyValueStore.clear(); 4029 } 4030 fValueStores.addElement(keyValueStore); 4031 activateSelectorFor(icArray[i]); 4032 break; 4033 case (IdentityConstraint.IC_KEYREF) : 4034 KeyRef keyRef = (KeyRef) icArray[i]; 4036 toHash = new LocalIDKey(keyRef, fElementDepth); 4037 KeyRefValueStore keyRefValueStore = 4038 (KeyRefValueStore) fIdentityConstraint2ValueStoreMap.get(toHash); 4039 if (keyRefValueStore == null) { 4040 keyRefValueStore = new KeyRefValueStore(keyRef, null); 4041 fIdentityConstraint2ValueStoreMap.put(toHash, keyRefValueStore); 4042 } else { 4043 keyRefValueStore.clear(); 4044 } 4045 fValueStores.addElement(keyRefValueStore); 4046 activateSelectorFor(icArray[i]); 4047 break; 4048 } 4049 } 4050 } 4052 4053 public ValueStoreBase getValueStoreFor(IdentityConstraint id, int initialDepth) { 4054 fLocalId.fDepth = initialDepth; 4055 fLocalId.fId = id; 4056 return (ValueStoreBase) fIdentityConstraint2ValueStoreMap.get(fLocalId); 4057 } 4059 4060 public ValueStoreBase getGlobalValueStoreFor(IdentityConstraint id) { 4061 return (ValueStoreBase) fGlobalIDConstraintMap.get(id); 4062 } 4064 public void transplant(IdentityConstraint id, int initialDepth) { 4069 fLocalId.fDepth = initialDepth; 4070 fLocalId.fId = id; 4071 ValueStoreBase newVals = 4072 (ValueStoreBase) fIdentityConstraint2ValueStoreMap.get(fLocalId); 4073 if (id.getCategory() == IdentityConstraint.IC_KEYREF) 4074 return; 4075 ValueStoreBase currVals = (ValueStoreBase) fGlobalIDConstraintMap.get(id); 4076 if (currVals != null) { 4077 currVals.append(newVals); 4078 fGlobalIDConstraintMap.put(id, currVals); 4079 } else 4080 fGlobalIDConstraintMap.put(id, newVals); 4081 4082 } 4084 4085 public void endDocument() { 4086 4087 int count = fValueStores.size(); 4088 for (int i = 0; i < count; i++) { 4089 ValueStoreBase valueStore = (ValueStoreBase) fValueStores.elementAt(i); 4090 valueStore.endDocument(); 4091 } 4092 4093 } 4095 4099 4100 public String toString() { 4101 String s = super.toString(); 4102 int index1 = s.lastIndexOf('$'); 4103 if (index1 != -1) { 4104 return s.substring(index1 + 1); 4105 } 4106 int index2 = s.lastIndexOf('.'); 4107 if (index2 != -1) { 4108 return s.substring(index2 + 1); 4109 } 4110 return s; 4111 } 4113 } 4115 protected class LocalIDKey { 4118 4119 public IdentityConstraint fId; 4120 public int fDepth; 4121 4122 public LocalIDKey() { 4123 } 4124 4125 public LocalIDKey(IdentityConstraint id, int depth) { 4126 fId = id; 4127 fDepth = depth; 4128 } 4130 public int hashCode() { 4132 return fId.hashCode() + fDepth; 4133 } 4134 4135 public boolean equals(Object localIDKey) { 4136 if (localIDKey instanceof LocalIDKey) { 4137 LocalIDKey lIDKey = (LocalIDKey) localIDKey; 4138 return (lIDKey.fId == fId && lIDKey.fDepth == fDepth); 4139 } 4140 return false; 4141 } 4142 } 4144 4147 protected static final class ShortVector { 4148 4149 4153 4154 private int fLength; 4155 4156 4157 private short[] fData; 4158 4159 4163 public ShortVector() {} 4164 4165 public ShortVector(int initialCapacity) { 4166 fData = new short[initialCapacity]; 4167 } 4168 4169 4173 4174 public int length() { 4175 return fLength; 4176 } 4177 4178 4179 public void add(short value) { 4180 ensureCapacity(fLength + 1); 4181 fData[fLength++] = value; 4182 } 4183 4184 4185 public short valueAt(int position) { 4186 return fData[position]; 4187 } 4188 4189 4190 public void clear() { 4191 fLength = 0; 4192 } 4193 4194 4195 public boolean contains(short value) { 4196 for (int i = 0; i < fLength; ++i) { 4197 if (fData[i] == value) { 4198 return true; 4199 } 4200 } 4201 return false; 4202 } 4203 4204 4208 4209 private void ensureCapacity(int size) { 4210 if (fData == null) { 4211 fData = new short[8]; 4212 } 4213 else if (fData.length <= size) { 4214 short[] newdata = new short[fData.length * 2]; 4215 System.arraycopy(fData, 0, newdata, 0, fData.length); 4216 fData = newdata; 4217 } 4218 } 4219 } 4220 4221} | Popular Tags |