1 16 17 package org.apache.xerces.impl.xs.traversers; 18 19 import org.apache.xerces.impl.dv.ValidatedInfo; 20 import org.apache.xerces.impl.dv.XSSimpleType; 21 import org.apache.xerces.impl.xs.SchemaGrammar; 22 import org.apache.xerces.impl.xs.SchemaSymbols; 23 import org.apache.xerces.impl.xs.XSAnnotationImpl; 24 import org.apache.xerces.impl.xs.XSComplexTypeDecl; 25 import org.apache.xerces.impl.xs.XSConstraints; 26 import org.apache.xerces.impl.xs.XSElementDecl; 27 import org.apache.xerces.impl.xs.XSParticleDecl; 28 import org.apache.xerces.xs.XSConstants; 29 import org.apache.xerces.xs.XSObject; 30 import org.apache.xerces.xs.XSTypeDefinition; 31 import org.apache.xerces.impl.xs.util.XInt; 32 import org.apache.xerces.util.DOMUtil; 33 import org.apache.xerces.util.SymbolTable; 34 import org.apache.xerces.xni.QName; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Attr ; 37 38 65 class XSDElementTraverser extends XSDAbstractTraverser { 66 67 protected final XSElementDecl fTempElementDecl = new XSElementDecl(); 68 69 boolean fDeferTraversingLocalElements; 72 73 XSDElementTraverser (XSDHandler handler, 74 XSAttributeChecker gAttrCheck) { 75 super(handler, gAttrCheck); 76 } 77 78 91 XSParticleDecl traverseLocal(Element elmDecl, 92 XSDocumentInfo schemaDoc, 93 SchemaGrammar grammar, 94 int allContextFlags, 95 XSObject parent) { 96 97 XSParticleDecl particle = null; 98 if (fSchemaHandler.fDeclPool !=null) { 99 particle = fSchemaHandler.fDeclPool.getParticleDecl(); 100 } else { 101 particle = new XSParticleDecl(); 102 } 103 if(fDeferTraversingLocalElements) { 104 particle.fType = XSParticleDecl.PARTICLE_ELEMENT; 108 Attr attr = elmDecl.getAttributeNode(SchemaSymbols.ATT_MINOCCURS); 109 if (attr != null) { 110 String min = attr.getValue(); 111 try { 112 int m = Integer.parseInt(min.trim()); 113 if (m >= 0) 114 particle.fMinOccurs = m; 115 } 116 catch (NumberFormatException ex) { 117 } 118 } 119 fSchemaHandler.fillInLocalElemInfo(elmDecl, schemaDoc, allContextFlags, parent, particle); 120 } else { 121 traverseLocal(particle, elmDecl, schemaDoc, grammar, allContextFlags, parent, null); 122 if (particle.fType == XSParticleDecl.PARTICLE_EMPTY) 124 particle = null; 125 } 126 127 return particle; 128 } 129 130 138 protected void traverseLocal(XSParticleDecl particle, 139 Element elmDecl, 140 XSDocumentInfo schemaDoc, 141 SchemaGrammar grammar, 142 int allContextFlags, 143 XSObject parent, 144 String [] localNSDecls) { 145 146 if (localNSDecls != null) { 147 schemaDoc.fNamespaceSupport.setEffectiveContext(localNSDecls); 148 } 149 150 Object [] attrValues = fAttrChecker.checkAttributes(elmDecl, false, schemaDoc); 152 153 QName refAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_REF]; 154 XInt minAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_MINOCCURS]; 155 XInt maxAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_MAXOCCURS]; 156 157 XSElementDecl element = null; 158 if (elmDecl.getAttributeNode(SchemaSymbols.ATT_REF) != null) { 159 if (refAtt != null) { 160 element = (XSElementDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ELEMENT_TYPE, refAtt, elmDecl); 161 162 Element child = DOMUtil.getFirstChildElement(elmDecl); 163 if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { 164 traverseAnnotationDecl(child, attrValues, false, schemaDoc); 166 child = DOMUtil.getNextSiblingElement(child); 167 } 168 if (child != null) { 173 reportSchemaError("src-element.2.2", new Object []{refAtt.rawname, DOMUtil.getLocalName(child)}, child); 174 } 175 } else { 176 element = null; 177 } 178 } else { 179 element = traverseNamedElement(elmDecl, attrValues, schemaDoc, grammar, false, parent); 180 } 181 182 particle.fMinOccurs = minAtt.intValue(); 183 particle.fMaxOccurs = maxAtt.intValue(); 184 if (element != null) { 185 particle.fType = XSParticleDecl.PARTICLE_ELEMENT; 186 particle.fValue = element; 187 } 188 else { 189 particle.fType = XSParticleDecl.PARTICLE_EMPTY; 190 } 191 Long defaultVals = (Long )attrValues[XSAttributeChecker.ATTIDX_FROMDEFAULT]; 192 checkOccurrences(particle, SchemaSymbols.ELT_ELEMENT, 193 (Element )elmDecl.getParentNode(), allContextFlags, 194 defaultVals.longValue()); 195 196 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 197 } 198 199 207 XSElementDecl traverseGlobal(Element elmDecl, 208 XSDocumentInfo schemaDoc, 209 SchemaGrammar grammar) { 210 211 213 Object [] attrValues = fAttrChecker.checkAttributes(elmDecl, true, schemaDoc); 214 XSElementDecl element = traverseNamedElement(elmDecl, attrValues, schemaDoc, grammar, true, null); 215 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 216 return element; 217 218 } 219 220 230 XSElementDecl traverseNamedElement(Element elmDecl, 231 Object [] attrValues, 232 XSDocumentInfo schemaDoc, 233 SchemaGrammar grammar, 234 boolean isGlobal, 235 XSObject parent) { 236 237 Boolean abstractAtt = (Boolean ) attrValues[XSAttributeChecker.ATTIDX_ABSTRACT]; 238 XInt blockAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_BLOCK]; 239 String defaultAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_DEFAULT]; 240 XInt finalAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FINAL]; 241 String fixedAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_FIXED]; 242 XInt formAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FORM]; 243 String nameAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_NAME]; 244 Boolean nillableAtt = (Boolean ) attrValues[XSAttributeChecker.ATTIDX_NILLABLE]; 245 QName subGroupAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_SUBSGROUP]; 246 QName typeAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_TYPE]; 247 248 250 XSElementDecl element = null; 251 if (fSchemaHandler.fDeclPool !=null) { 252 element = fSchemaHandler.fDeclPool.getElementDecl(); 253 } else { 254 element = new XSElementDecl(); 255 } 256 if (nameAtt != null) 258 element.fName = fSymbolTable.addSymbol(nameAtt); 259 260 if (isGlobal) { 262 element.fTargetNamespace = schemaDoc.fTargetNamespace; 263 element.setIsGlobal(); 264 } 265 else { 266 if (parent instanceof XSComplexTypeDecl) 267 element.setIsLocal((XSComplexTypeDecl)parent); 268 269 if (formAtt != null) { 270 if (formAtt.intValue() == SchemaSymbols.FORM_QUALIFIED) 271 element.fTargetNamespace = schemaDoc.fTargetNamespace; 272 else 273 element.fTargetNamespace = null; 274 } else if (schemaDoc.fAreLocalElementsQualified) { 275 element.fTargetNamespace = schemaDoc.fTargetNamespace; 276 } else { 277 element.fTargetNamespace = null; 278 } 279 } 280 281 element.fBlock = blockAtt == null ? schemaDoc.fBlockDefault : blockAtt.shortValue(); 283 element.fFinal = finalAtt == null ? schemaDoc.fFinalDefault : finalAtt.shortValue(); 284 element.fBlock &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION | XSConstants.DERIVATION_SUBSTITUTION); 286 element.fFinal &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION); 287 288 if (nillableAtt.booleanValue()) 289 element.setIsNillable(); 290 if (abstractAtt != null && abstractAtt.booleanValue()) 291 element.setIsAbstract(); 292 293 if (fixedAtt != null) { 295 element.fDefault = new ValidatedInfo(); 296 element.fDefault.normalizedValue = fixedAtt; 297 element.setConstraintType(XSConstants.VC_FIXED); 298 } else if (defaultAtt != null) { 299 element.fDefault = new ValidatedInfo(); 300 element.fDefault.normalizedValue = defaultAtt; 301 element.setConstraintType(XSConstants.VC_DEFAULT); 302 } else { 303 element.setConstraintType(XSConstants.VC_NONE); 304 } 305 306 if (subGroupAtt != null) { 308 element.fSubGroup = (XSElementDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ELEMENT_TYPE, subGroupAtt, elmDecl); 309 } 310 311 Element child = DOMUtil.getFirstChildElement(elmDecl); 313 XSAnnotationImpl annotation = null; 314 if(child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { 315 annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc); 316 child = DOMUtil.getNextSiblingElement(child); 317 } 318 else { 319 String text = DOMUtil.getSyntheticAnnotation(elmDecl); 320 if (text != null) { 321 annotation = traverseSyntheticAnnotation(elmDecl, text, attrValues, false, schemaDoc); 322 } 323 } 324 element.fAnnotation = annotation; 325 326 XSTypeDefinition elementType = null; 328 boolean haveAnonType = false; 329 330 if (child != null) { 332 String childName = DOMUtil.getLocalName(child); 333 334 if (childName.equals(SchemaSymbols.ELT_COMPLEXTYPE)) { 335 elementType = fSchemaHandler.fComplexTypeTraverser.traverseLocal(child, schemaDoc, grammar); 336 haveAnonType = true; 337 child = DOMUtil.getNextSiblingElement(child); 338 } 339 else if (childName.equals(SchemaSymbols.ELT_SIMPLETYPE)) { 340 elementType = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(child, schemaDoc, grammar); 341 haveAnonType = true; 342 child = DOMUtil.getNextSiblingElement(child); 343 } 344 } 345 346 if (elementType == null && typeAtt != null) { 348 elementType = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, typeAtt, elmDecl); 349 } 350 351 if (elementType == null && element.fSubGroup != null) { 353 elementType = element.fSubGroup.fType; 354 } 355 356 if (elementType == null) { 357 elementType = SchemaGrammar.fAnyType; 358 } 359 360 element.fType = elementType; 361 362 364 if (child != null) { 366 String childName = DOMUtil.getLocalName(child); 367 while (child != null && 368 (childName.equals(SchemaSymbols.ELT_KEY) || 369 childName.equals(SchemaSymbols.ELT_KEYREF) || 370 childName.equals(SchemaSymbols.ELT_UNIQUE))) { 371 372 if (childName.equals(SchemaSymbols.ELT_KEY) || 373 childName.equals(SchemaSymbols.ELT_UNIQUE)) { 374 DOMUtil.setHidden(child); 377 fSchemaHandler.fUniqueOrKeyTraverser.traverse(child, element, schemaDoc, grammar); 378 if(DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME).length() != 0 ) { 379 fSchemaHandler.checkForDuplicateNames( 380 (schemaDoc.fTargetNamespace == null) ? ","+DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME) 381 : schemaDoc.fTargetNamespace+","+ DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME), 382 fSchemaHandler.getIDRegistry(), fSchemaHandler.getIDRegistry_sub(), 383 child, schemaDoc); 384 } 385 } else if (childName.equals(SchemaSymbols.ELT_KEYREF)) { 386 fSchemaHandler.storeKeyRef(child, schemaDoc, element); 387 } 388 child = DOMUtil.getNextSiblingElement(child); 389 if (child != null) { 390 childName = DOMUtil.getLocalName(child); 391 } 392 } 393 } 394 395 if (isGlobal && nameAtt != null) 397 grammar.addGlobalElementDecl(element); 398 399 401 if (nameAtt == null) { 403 if (isGlobal) 404 reportSchemaError("s4s-att-must-appear", new Object []{SchemaSymbols.ELT_ELEMENT, SchemaSymbols.ATT_NAME}, elmDecl); 405 else 406 reportSchemaError("src-element.2.1", null, elmDecl); 407 nameAtt = NO_NAME; 408 } 409 410 if (child != null) { 412 reportSchemaError("s4s-elt-must-match.1", new Object []{nameAtt, "(annotation?, (simpleType | complexType)?, (unique | key | keyref)*))", DOMUtil.getLocalName(child)}, child); 413 } 414 415 417 419 if (defaultAtt != null && fixedAtt != null) { 421 reportSchemaError("src-element.1", new Object []{nameAtt}, elmDecl); 422 } 423 424 428 431 if (haveAnonType && (typeAtt != null)) { 433 reportSchemaError("src-element.3", new Object []{nameAtt}, elmDecl); 434 } 435 436 checkNotationType(nameAtt, elementType, elmDecl); 439 440 442 if (element.fDefault != null) { 444 fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport); 445 if (XSConstraints.ElementDefaultValidImmediate(element.fType, element.fDefault.normalizedValue, fValidationState, element.fDefault) == null) { 446 reportSchemaError ("e-props-correct.2", new Object []{nameAtt, element.fDefault.normalizedValue}, elmDecl); 447 element.setConstraintType(XSConstants.VC_NONE); 448 } 449 } 450 451 if (element.fSubGroup != null) { 453 if (!XSConstraints.checkTypeDerivationOk(element.fType, element.fSubGroup.fType, element.fSubGroup.fFinal)) { 454 reportSchemaError ("e-props-correct.4", new Object []{nameAtt, subGroupAtt.prefix+":"+subGroupAtt.localpart}, elmDecl); 455 } 456 } 457 458 if (element.fDefault != null) { 460 if ((elementType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE && 461 ((XSSimpleType)elementType).isIDType()) || 462 (elementType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE && 463 ((XSComplexTypeDecl)elementType).containsTypeID())) { 464 reportSchemaError ("e-props-correct.5", new Object []{element.fName}, elmDecl); 465 } 466 } 467 468 if (element.fName == null) 470 return null; 471 472 return element; 473 } 474 475 void reset(SymbolTable symbolTable, boolean validateAnnotations) { 476 super.reset(symbolTable, validateAnnotations); 477 fDeferTraversingLocalElements = true; 478 } 480 } 481 | Popular Tags |