1 57 58 package com.sun.org.apache.xerces.internal.impl.xs.traversers; 59 60 import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo; 61 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType; 62 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar; 63 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols; 64 import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl; 65 import com.sun.org.apache.xerces.internal.impl.xs.XSComplexTypeDecl; 66 import com.sun.org.apache.xerces.internal.impl.xs.XSConstraints; 67 import com.sun.org.apache.xerces.internal.impl.xs.XSElementDecl; 68 import com.sun.org.apache.xerces.internal.impl.xs.XSParticleDecl; 69 import com.sun.org.apache.xerces.internal.xs.XSConstants; 70 import com.sun.org.apache.xerces.internal.xs.XSObject; 71 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 72 import com.sun.org.apache.xerces.internal.impl.xs.util.XInt; 73 import com.sun.org.apache.xerces.internal.util.DOMUtil; 74 import com.sun.org.apache.xerces.internal.util.SymbolTable; 75 import com.sun.org.apache.xerces.internal.xni.QName; 76 import org.w3c.dom.Element ; 77 import org.w3c.dom.Attr ; 78 79 104 class XSDElementTraverser extends XSDAbstractTraverser { 105 106 protected final XSElementDecl fTempElementDecl = new XSElementDecl(); 107 108 boolean fDeferTraversingLocalElements; 111 112 XSDElementTraverser (XSDHandler handler, 113 XSAttributeChecker gAttrCheck) { 114 super(handler, gAttrCheck); 115 } 116 117 130 XSParticleDecl traverseLocal(Element elmDecl, 131 XSDocumentInfo schemaDoc, 132 SchemaGrammar grammar, 133 int allContextFlags, 134 XSObject parent) { 135 136 XSParticleDecl particle = null; 137 if (fSchemaHandler.fDeclPool !=null) { 138 particle = fSchemaHandler.fDeclPool.getParticleDecl(); 139 } else { 140 particle = new XSParticleDecl(); 141 } 142 if(fDeferTraversingLocalElements) { 143 particle.fType = XSParticleDecl.PARTICLE_ELEMENT; 147 Attr attr = elmDecl.getAttributeNode(SchemaSymbols.ATT_MINOCCURS); 148 if (attr != null) { 149 String min = attr.getValue(); 150 try { 151 int m = Integer.parseInt(min.trim()); 152 if (m >= 0) 153 particle.fMinOccurs = m; 154 } 155 catch (NumberFormatException ex) { 156 } 157 } 158 fSchemaHandler.fillInLocalElemInfo(elmDecl, schemaDoc, allContextFlags, parent, particle); 159 } else { 160 traverseLocal(particle, elmDecl, schemaDoc, grammar, allContextFlags, parent); 161 if (particle.fType == XSParticleDecl.PARTICLE_EMPTY) 163 particle = null; 164 } 165 166 return particle; 167 } 168 169 177 protected void traverseLocal(XSParticleDecl particle, 178 Element elmDecl, 179 XSDocumentInfo schemaDoc, 180 SchemaGrammar grammar, 181 int allContextFlags, 182 XSObject parent) { 183 184 Object [] attrValues = fAttrChecker.checkAttributes(elmDecl, false, schemaDoc); 186 187 QName refAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_REF]; 188 XInt minAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_MINOCCURS]; 189 XInt maxAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS]; 190 191 XSElementDecl element = null; 192 if (elmDecl.getAttributeNode(SchemaSymbols.ATT_REF) != null) { 193 if (refAtt != null) { 194 element = (XSElementDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ELEMENT_TYPE, refAtt, elmDecl); 195 196 Element child = DOMUtil.getFirstChildElement(elmDecl); 197 if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { 198 traverseAnnotationDecl(child, attrValues, false, schemaDoc); 200 child = DOMUtil.getNextSiblingElement(child); 201 } 202 203 if (child != null) { 208 reportSchemaError("src-element.2.2", new Object []{refAtt.rawname, DOMUtil.getLocalName(child)}, child); 209 } 210 } else { 211 element = null; 212 } 213 } else { 214 element = traverseNamedElement(elmDecl, attrValues, schemaDoc, grammar, false, parent); 215 } 216 217 particle.fMinOccurs = minAtt.intValue(); 218 particle.fMaxOccurs = maxAtt.intValue(); 219 if (element != null) { 220 particle.fType = XSParticleDecl.PARTICLE_ELEMENT; 221 particle.fValue = element; 222 } 223 else { 224 particle.fType = XSParticleDecl.PARTICLE_EMPTY; 225 } 226 Long defaultVals = (Long )attrValues[XSAttributeChecker.ATTIDX_FROMDEFAULT]; 227 checkOccurrences(particle, SchemaSymbols.ELT_ELEMENT, 228 (Element )elmDecl.getParentNode(), allContextFlags, 229 defaultVals.longValue()); 230 231 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 232 } 233 234 242 XSElementDecl traverseGlobal(Element elmDecl, 243 XSDocumentInfo schemaDoc, 244 SchemaGrammar grammar) { 245 246 Object [] attrValues = fAttrChecker.checkAttributes(elmDecl, true, schemaDoc); 248 XSElementDecl element = traverseNamedElement(elmDecl, attrValues, schemaDoc, grammar, true, null); 249 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 250 return element; 251 252 } 253 254 264 XSElementDecl traverseNamedElement(Element elmDecl, 265 Object [] attrValues, 266 XSDocumentInfo schemaDoc, 267 SchemaGrammar grammar, 268 boolean isGlobal, 269 XSObject parent) { 270 271 Boolean abstractAtt = (Boolean ) attrValues[XSAttributeChecker.ATTIDX_ABSTRACT]; 272 XInt blockAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_BLOCK]; 273 String defaultAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_DEFAULT]; 274 XInt finalAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FINAL]; 275 String fixedAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_FIXED]; 276 XInt formAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FORM]; 277 String nameAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_NAME]; 278 Boolean nillableAtt = (Boolean ) attrValues[XSAttributeChecker.ATTIDX_NILLABLE]; 279 QName subGroupAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_SUBSGROUP]; 280 QName typeAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_TYPE]; 281 282 284 XSElementDecl element = null; 285 if (fSchemaHandler.fDeclPool !=null) { 286 element = fSchemaHandler.fDeclPool.getElementDecl(); 287 } else { 288 element = new XSElementDecl(); 289 } 290 if (nameAtt != null) 292 element.fName = fSymbolTable.addSymbol(nameAtt); 293 294 if (isGlobal) { 296 element.fTargetNamespace = schemaDoc.fTargetNamespace; 297 element.setIsGlobal(); 298 } 299 else { 300 if (parent instanceof XSComplexTypeDecl) 301 element.setIsLocal((XSComplexTypeDecl)parent); 302 303 if (formAtt != null) { 304 if (formAtt.intValue() == SchemaSymbols.FORM_QUALIFIED) 305 element.fTargetNamespace = schemaDoc.fTargetNamespace; 306 else 307 element.fTargetNamespace = null; 308 } else if (schemaDoc.fAreLocalElementsQualified) { 309 element.fTargetNamespace = schemaDoc.fTargetNamespace; 310 } else { 311 element.fTargetNamespace = null; 312 } 313 } 314 315 element.fBlock = blockAtt == null ? schemaDoc.fBlockDefault : blockAtt.shortValue(); 317 element.fFinal = finalAtt == null ? schemaDoc.fFinalDefault : finalAtt.shortValue(); 318 element.fBlock &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION | XSConstants.DERIVATION_SUBSTITUTION); 320 element.fFinal &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION); 321 322 if (nillableAtt.booleanValue()) 323 element.setIsNillable(); 324 if (abstractAtt != null && abstractAtt.booleanValue()) 325 element.setIsAbstract(); 326 327 if (fixedAtt != null) { 329 element.fDefault = new ValidatedInfo(); 330 element.fDefault.normalizedValue = fixedAtt; 331 element.setConstraintType(XSConstants.VC_FIXED); 332 } else if (defaultAtt != null) { 333 element.fDefault = new ValidatedInfo(); 334 element.fDefault.normalizedValue = defaultAtt; 335 element.setConstraintType(XSConstants.VC_DEFAULT); 336 } else { 337 element.setConstraintType(XSConstants.VC_NONE); 338 } 339 340 if (subGroupAtt != null) { 342 element.fSubGroup = (XSElementDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ELEMENT_TYPE, subGroupAtt, elmDecl); 343 } 344 345 Element child = DOMUtil.getFirstChildElement(elmDecl); 347 XSAnnotationImpl annotation = null; 348 if(child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { 349 annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc); 350 child = DOMUtil.getNextSiblingElement(child); 351 } 352 element.fAnnotation = annotation; 353 354 XSTypeDefinition elementType = null; 356 boolean haveAnonType = false; 357 358 if (child != null) { 360 String childName = DOMUtil.getLocalName(child); 361 362 if (childName.equals(SchemaSymbols.ELT_COMPLEXTYPE)) { 363 elementType = fSchemaHandler.fComplexTypeTraverser.traverseLocal(child, schemaDoc, grammar); 364 haveAnonType = true; 365 child = DOMUtil.getNextSiblingElement(child); 366 } 367 else if (childName.equals(SchemaSymbols.ELT_SIMPLETYPE)) { 368 elementType = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(child, schemaDoc, grammar); 369 haveAnonType = true; 370 child = DOMUtil.getNextSiblingElement(child); 371 } 372 } 373 374 if (elementType == null && typeAtt != null) { 376 elementType = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, typeAtt, elmDecl); 377 } 378 379 if (elementType == null && element.fSubGroup != null) { 381 elementType = element.fSubGroup.fType; 382 } 383 384 if (elementType == null) { 385 elementType = SchemaGrammar.fAnyType; 386 } 387 388 element.fType = elementType; 389 390 392 if (child != null) { 394 String childName = DOMUtil.getLocalName(child); 395 while (child != null && 396 (childName.equals(SchemaSymbols.ELT_KEY) || 397 childName.equals(SchemaSymbols.ELT_KEYREF) || 398 childName.equals(SchemaSymbols.ELT_UNIQUE))) { 399 400 if (childName.equals(SchemaSymbols.ELT_KEY) || 401 childName.equals(SchemaSymbols.ELT_UNIQUE)) { 402 DOMUtil.setHidden(child); 405 fSchemaHandler.fUniqueOrKeyTraverser.traverse(child, element, schemaDoc, grammar); 406 if(DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME).length() != 0 ) { 407 fSchemaHandler.checkForDuplicateNames( 408 (schemaDoc.fTargetNamespace == null) ? ","+DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME) 409 : schemaDoc.fTargetNamespace+","+ DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME), 410 fSchemaHandler.getIDRegistry(), 411 child, schemaDoc); 412 } 413 } else if (childName.equals(SchemaSymbols.ELT_KEYREF)) { 414 fSchemaHandler.storeKeyRef(child, schemaDoc, element); 415 } 416 child = DOMUtil.getNextSiblingElement(child); 417 if (child != null) { 418 childName = DOMUtil.getLocalName(child); 419 } 420 } 421 } 422 423 if (isGlobal && nameAtt != null) 425 grammar.addGlobalElementDecl(element); 426 427 429 if (nameAtt == null) { 431 if (isGlobal) 432 reportSchemaError("s4s-att-must-appear", new Object []{SchemaSymbols.ELT_ELEMENT, SchemaSymbols.ATT_NAME}, elmDecl); 433 else 434 reportSchemaError("src-element.2.1", null, elmDecl); 435 nameAtt = NO_NAME; 436 } 437 438 if (child != null) { 440 reportSchemaError("s4s-elt-must-match.1", new Object []{nameAtt, "(annotation?, (simpleType | complexType)?, (unique | key | keyref)*))", DOMUtil.getLocalName(child)}, child); 441 } 442 443 445 447 if (defaultAtt != null && fixedAtt != null) { 449 reportSchemaError("src-element.1", new Object []{nameAtt}, elmDecl); 450 } 451 452 456 459 if (haveAnonType && (typeAtt != null)) { 461 reportSchemaError("src-element.3", new Object []{nameAtt}, elmDecl); 462 } 463 464 checkNotationType(nameAtt, elementType, elmDecl); 467 468 470 if (element.fDefault != null) { 472 fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport); 473 if (XSConstraints.ElementDefaultValidImmediate(element.fType, element.fDefault.normalizedValue, fValidationState, element.fDefault) == null) { 474 reportSchemaError ("e-props-correct.2", new Object []{nameAtt, element.fDefault.normalizedValue}, elmDecl); 475 element.setConstraintType(XSConstants.VC_NONE); 476 } 477 } 478 479 if (element.fSubGroup != null) { 481 if (!XSConstraints.checkTypeDerivationOk(element.fType, element.fSubGroup.fType, element.fSubGroup.fFinal)) { 482 reportSchemaError ("e-props-correct.4", new Object []{nameAtt, subGroupAtt.prefix+":"+subGroupAtt.localpart}, elmDecl); 483 } 484 } 485 486 if (element.fDefault != null) { 488 if ((elementType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE && 489 ((XSSimpleType)elementType).isIDType()) || 490 (elementType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE && 491 ((XSComplexTypeDecl)elementType).containsTypeID())) { 492 reportSchemaError ("e-props-correct.5", new Object []{element.fName}, elmDecl); 493 } 494 } 495 496 if (element.fName == null) 498 return null; 499 500 return element; 501 } 502 503 void reset(SymbolTable symbolTable) { 504 super.reset(symbolTable); 505 fDeferTraversingLocalElements = true; 506 } 508 } 509 | Popular Tags |