1 57 58 package com.sun.org.apache.xerces.internal.impl.xs.traversers; 59 60 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException; 61 import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo; 62 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType; 63 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar; 64 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols; 65 import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeDecl; 66 import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl; 67 import com.sun.org.apache.xerces.internal.impl.xs.XSAttributeUseImpl; 68 import com.sun.org.apache.xerces.internal.impl.xs.XSComplexTypeDecl; 69 import com.sun.org.apache.xerces.internal.xs.XSConstants; 70 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 71 import com.sun.org.apache.xerces.internal.impl.xs.util.XInt; 72 import com.sun.org.apache.xerces.internal.util.DOMUtil; 73 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 74 import com.sun.org.apache.xerces.internal.xni.QName; 75 import org.w3c.dom.Element ; 76 77 97 class XSDAttributeTraverser extends XSDAbstractTraverser { 98 99 public XSDAttributeTraverser (XSDHandler handler, 100 XSAttributeChecker gAttrCheck) { 101 super(handler, gAttrCheck); 102 } 103 104 protected XSAttributeUseImpl traverseLocal(Element attrDecl, 105 XSDocumentInfo schemaDoc, 106 SchemaGrammar grammar, 107 XSComplexTypeDecl enclosingCT) { 108 109 Object [] attrValues = fAttrChecker.checkAttributes(attrDecl, false, schemaDoc); 111 112 String defaultAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_DEFAULT]; 113 String fixedAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_FIXED]; 114 String nameAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_NAME]; 115 QName refAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_REF]; 116 XInt useAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_USE]; 117 118 XSAttributeDecl attribute = null; 120 if (attrDecl.getAttributeNode(SchemaSymbols.ATT_REF) != null) { 121 if (refAtt != null) { 122 attribute = (XSAttributeDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ATTRIBUTE_TYPE, refAtt, attrDecl); 123 124 Element child = DOMUtil.getFirstChildElement(attrDecl); 125 if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { 126 traverseAnnotationDecl(child, attrValues, false, schemaDoc); 128 child = DOMUtil.getNextSiblingElement(child); 129 } 130 131 if (child != null) { 132 reportSchemaError("src-attribute.3.2", new Object []{refAtt.rawname}, child); 133 } 134 nameAtt = refAtt.localpart; 136 } else { 137 attribute = null; 138 } 139 } else { 140 attribute = traverseNamedAttr(attrDecl, attrValues, schemaDoc, grammar, false, enclosingCT); 141 } 142 143 short consType = XSConstants.VC_NONE; 145 if (defaultAtt != null) { 146 consType = XSConstants.VC_DEFAULT; 147 } else if (fixedAtt != null) { 148 consType = XSConstants.VC_FIXED; 149 defaultAtt = fixedAtt; 150 fixedAtt = null; 151 } 152 153 XSAttributeUseImpl attrUse = null; 154 if (attribute != null) { 155 if (fSchemaHandler.fDeclPool !=null) { 156 attrUse = fSchemaHandler.fDeclPool.getAttributeUse(); 157 } else { 158 attrUse = new XSAttributeUseImpl(); 159 } 160 attrUse.fAttrDecl = attribute; 161 attrUse.fUse = useAtt.shortValue(); 162 attrUse.fConstraintType = consType; 163 if (defaultAtt != null) { 164 attrUse.fDefault = new ValidatedInfo(); 165 attrUse.fDefault.normalizedValue = defaultAtt; 166 } 167 } 168 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 169 170 172 if (defaultAtt != null && fixedAtt != null) { 174 reportSchemaError("src-attribute.1", new Object []{nameAtt}, attrDecl); 175 } 176 177 if (consType == XSConstants.VC_DEFAULT && 179 useAtt != null && useAtt.intValue() != SchemaSymbols.USE_OPTIONAL) { 180 reportSchemaError("src-attribute.2", new Object []{nameAtt}, attrDecl); 181 } 182 183 185 if (defaultAtt != null && attrUse != null) { 186 fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport); 188 try { 189 checkDefaultValid(attrUse); 190 } 191 catch (InvalidDatatypeValueException ide) { 192 reportSchemaError (ide.getKey(), ide.getArgs(), attrDecl); 193 reportSchemaError ("a-props-correct.2", new Object []{nameAtt, defaultAtt}, attrDecl); 194 } 195 196 if (((XSSimpleType)attribute.getTypeDefinition()).isIDType() ) { 198 reportSchemaError ("a-props-correct.3", new Object []{nameAtt}, attrDecl); 199 } 200 201 if (attrUse.fAttrDecl.getConstraintType() == XSConstants.VC_FIXED && 205 attrUse.fConstraintType != XSConstants.VC_NONE) { 206 if (attrUse.fConstraintType != XSConstants.VC_FIXED || 207 !attrUse.fAttrDecl.getValInfo().actualValue.equals(attrUse.fDefault.actualValue)) { 208 reportSchemaError ("au-props-correct.2", new Object []{nameAtt, attrUse.fAttrDecl.getValInfo().stringValue()}, attrDecl); 209 } 210 } 211 } 212 213 return attrUse; 214 } 215 216 protected XSAttributeDecl traverseGlobal(Element attrDecl, 217 XSDocumentInfo schemaDoc, 218 SchemaGrammar grammar) { 219 220 Object [] attrValues = fAttrChecker.checkAttributes(attrDecl, true, schemaDoc); 222 XSAttributeDecl attribute = traverseNamedAttr(attrDecl, attrValues, schemaDoc, grammar, true, null); 223 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 224 return attribute; 225 226 } 227 228 238 XSAttributeDecl traverseNamedAttr(Element attrDecl, 239 Object [] attrValues, 240 XSDocumentInfo schemaDoc, 241 SchemaGrammar grammar, 242 boolean isGlobal, 243 XSComplexTypeDecl enclosingCT) { 244 245 String defaultAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_DEFAULT]; 246 String fixedAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_FIXED]; 247 XInt formAtt = (XInt) attrValues[XSAttributeChecker.ATTIDX_FORM]; 248 String nameAtt = (String ) attrValues[XSAttributeChecker.ATTIDX_NAME]; 249 QName typeAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_TYPE]; 250 251 XSAttributeDecl attribute = null; 253 if (fSchemaHandler.fDeclPool !=null) { 254 attribute = fSchemaHandler.fDeclPool.getAttributeDecl(); 255 } else { 256 attribute = new XSAttributeDecl(); 257 } 258 259 if (nameAtt != null) 261 nameAtt = fSymbolTable.addSymbol(nameAtt); 262 263 String tnsAtt = null; 265 XSComplexTypeDecl enclCT = null; 266 short scope = XSAttributeDecl.SCOPE_ABSENT; 267 if (isGlobal) { 268 tnsAtt = schemaDoc.fTargetNamespace; 269 scope = XSAttributeDecl.SCOPE_GLOBAL; 270 } 271 else { 272 if (enclosingCT != null) { 273 enclCT = enclosingCT; 274 scope = XSAttributeDecl.SCOPE_LOCAL; 275 } 276 if (formAtt != null) { 277 if (formAtt.intValue() == SchemaSymbols.FORM_QUALIFIED) 278 tnsAtt = schemaDoc.fTargetNamespace; 279 } else if (schemaDoc.fAreLocalAttributesQualified) { 280 tnsAtt = schemaDoc.fTargetNamespace; 281 } 282 } 283 ValidatedInfo attDefault = null; 286 short constraintType = XSConstants.VC_NONE; 287 if (isGlobal) { 288 if (fixedAtt != null) { 289 attDefault = new ValidatedInfo(); 290 attDefault.normalizedValue = fixedAtt; 291 constraintType = XSConstants.VC_FIXED; 292 } else if (defaultAtt != null) { 293 attDefault = new ValidatedInfo(); 294 attDefault.normalizedValue = defaultAtt; 295 constraintType = XSConstants.VC_DEFAULT; 296 } 297 } 298 299 Element child = DOMUtil.getFirstChildElement(attrDecl); 301 XSAnnotationImpl annotation = null; 302 if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { 303 annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc); 304 child = DOMUtil.getNextSiblingElement(child); 305 } 306 307 XSSimpleType attrType = null; 309 boolean haveAnonType = false; 310 311 if (child != null) { 313 String childName = DOMUtil.getLocalName(child); 314 315 if (childName.equals(SchemaSymbols.ELT_SIMPLETYPE)) { 316 attrType = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(child, schemaDoc, grammar); 317 haveAnonType = true; 318 child = DOMUtil.getNextSiblingElement(child); 319 } 320 } 321 322 if (attrType == null && typeAtt != null) { 324 XSTypeDefinition type = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, typeAtt, attrDecl); 325 if (type != null && type.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) 326 attrType = (XSSimpleType)type; 327 else 328 reportSchemaError("src-resolve", new Object []{typeAtt.rawname, "simpleType definition"}, attrDecl); 329 } 330 331 if (attrType == null) { 332 attrType = SchemaGrammar.fAnySimpleType; 333 } 334 335 attribute.setValues(nameAtt, tnsAtt, attrType, 336 constraintType, scope, attDefault, enclCT, annotation); 337 338 if (isGlobal && nameAtt != null) 340 grammar.addGlobalAttributeDecl(attribute); 341 342 344 if (nameAtt == null) { 346 if (isGlobal) 347 reportSchemaError("s4s-att-must-appear", new Object []{SchemaSymbols.ELT_ATTRIBUTE, SchemaSymbols.ATT_NAME}, attrDecl); 348 else 349 reportSchemaError("src-attribute.3.1", null, attrDecl); 350 nameAtt = NO_NAME; 351 } 352 353 if (child != null) { 355 reportSchemaError("s4s-elt-must-match.1", new Object []{nameAtt, "(annotation?, (simpleType?))", DOMUtil.getLocalName(child)}, child); 356 } 357 358 360 362 if (defaultAtt != null && fixedAtt != null) { 364 reportSchemaError("src-attribute.1", new Object []{nameAtt}, attrDecl); 365 } 366 367 370 374 377 if (haveAnonType && (typeAtt != null)) { 379 reportSchemaError( "src-attribute.4", new Object []{nameAtt}, attrDecl); 380 } 381 382 checkNotationType(nameAtt, attrType, attrDecl); 385 386 388 if (attDefault != null) { 390 fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport); 391 try { 392 checkDefaultValid(attribute); 393 } 394 catch (InvalidDatatypeValueException ide) { 395 reportSchemaError (ide.getKey(), ide.getArgs(), attrDecl); 396 reportSchemaError ("a-props-correct.2", new Object []{nameAtt, attDefault.normalizedValue}, attrDecl); 397 } 398 } 399 400 if (attDefault != null) { 402 if (attrType.isIDType() ) { 403 reportSchemaError ("a-props-correct.3", new Object []{nameAtt}, attrDecl); 404 } 405 } 406 407 409 if (nameAtt != null && nameAtt.equals(XMLSymbols.PREFIX_XMLNS)) { 411 reportSchemaError("no-xmlns", null, attrDecl); 412 } 413 414 416 if (tnsAtt != null && tnsAtt.equals(SchemaSymbols.URI_XSI)) { 418 reportSchemaError("no-xsi", new Object []{SchemaSymbols.URI_XSI}, attrDecl); 419 } 420 421 if (attribute.getName() == null) 423 return null; 424 425 return attribute; 426 } 427 428 void checkDefaultValid(XSAttributeDecl attribute) throws InvalidDatatypeValueException { 430 ((XSSimpleType)attribute.getTypeDefinition()).validate(attribute.getValInfo().normalizedValue, fValidationState, attribute.getValInfo()); 432 ((XSSimpleType)attribute.getTypeDefinition()).validate(attribute.getValInfo().stringValue(), fValidationState, attribute.getValInfo()); 434 } 435 436 void checkDefaultValid(XSAttributeUseImpl attrUse) throws InvalidDatatypeValueException { 438 ((XSSimpleType)attrUse.fAttrDecl.getTypeDefinition()).validate(attrUse.fDefault.normalizedValue, fValidationState, attrUse.fDefault); 440 ((XSSimpleType)attrUse.fAttrDecl.getTypeDefinition()).validate(attrUse.fDefault.stringValue(), fValidationState, attrUse.fDefault); 442 } 443 444 } 445 | Popular Tags |