1 57 58 package com.sun.org.apache.xerces.internal.impl.xs.traversers; 59 60 import java.util.Vector ; 61 62 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeFacetException; 63 import com.sun.org.apache.xerces.internal.impl.dv.SchemaDVFactory; 64 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType; 65 import com.sun.org.apache.xerces.internal.impl.dv.xs.SchemaDVFactoryImpl; 66 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar; 67 import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols; 68 import com.sun.org.apache.xerces.internal.impl.xs.XSAnnotationImpl; 69 import com.sun.org.apache.xerces.internal.xs.XSConstants; 70 import com.sun.org.apache.xerces.internal.xs.XSObjectList; 71 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 72 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl; 73 import com.sun.org.apache.xerces.internal.impl.xs.util.XInt; 74 import com.sun.org.apache.xerces.internal.util.DOMUtil; 75 import com.sun.org.apache.xerces.internal.xni.QName; 76 import org.w3c.dom.Element ; 77 78 116 class XSDSimpleTypeTraverser extends XSDAbstractTraverser { 117 118 private final SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance(); 120 121 private boolean fIsBuiltIn = false; 123 124 XSDSimpleTypeTraverser (XSDHandler handler, 125 XSAttributeChecker gAttrCheck) { 126 super(handler, gAttrCheck); 127 if (schemaFactory instanceof SchemaDVFactoryImpl) { 128 ((SchemaDVFactoryImpl)schemaFactory).setDeclPool(handler.fDeclPool); 129 } 130 } 131 132 XSSimpleType traverseGlobal(Element elmNode, 134 XSDocumentInfo schemaDoc, 135 SchemaGrammar grammar) { 136 137 Object [] attrValues = fAttrChecker.checkAttributes(elmNode, true, schemaDoc); 139 String nameAtt = (String )attrValues[XSAttributeChecker.ATTIDX_NAME]; 140 XSSimpleType type = traverseSimpleTypeDecl(elmNode, attrValues, schemaDoc, grammar); 141 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 142 143 if (nameAtt == null) { 145 reportSchemaError("s4s-att-must-appear", new Object []{SchemaSymbols.ELT_SIMPLETYPE, SchemaSymbols.ATT_NAME}, elmNode); 146 type = null; 147 } 148 149 if (type != null) { 151 grammar.addGlobalTypeDecl(type); 152 } 153 154 return type; 155 } 156 157 XSSimpleType traverseLocal(Element elmNode, 158 XSDocumentInfo schemaDoc, 159 SchemaGrammar grammar) { 160 161 Object [] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc); 163 XSSimpleType type = traverseSimpleTypeDecl (elmNode, attrValues, schemaDoc, grammar); 164 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 165 166 return type; 167 } 168 169 private XSSimpleType traverseSimpleTypeDecl(Element simpleTypeDecl, 170 Object [] attrValues, 171 XSDocumentInfo schemaDoc, 172 SchemaGrammar grammar) { 173 174 String name = (String )attrValues[XSAttributeChecker.ATTIDX_NAME]; 176 XInt finalAttr = (XInt)attrValues[XSAttributeChecker.ATTIDX_FINAL]; 177 int finalProperty = finalAttr == null ? schemaDoc.fFinalDefault : finalAttr.intValue(); 178 179 Element child = DOMUtil.getFirstChildElement(simpleTypeDecl); 181 XSAnnotationImpl [] annotations = null; 182 if (child != null) { 183 if (DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { 185 XSAnnotationImpl annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc); 186 if (annotation != null) 187 annotations = new XSAnnotationImpl [] {annotation}; 188 child = DOMUtil.getNextSiblingElement(child); 189 } 190 } 191 192 if (child == null) { 194 reportSchemaError("s4s-elt-must-match.2", new Object []{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))"}, simpleTypeDecl); 195 return errorType(name, schemaDoc.fTargetNamespace, XSConstants.DERIVATION_RESTRICTION); 196 } 197 198 String varietyProperty = DOMUtil.getLocalName(child); 200 short refType = XSConstants.DERIVATION_RESTRICTION; 201 boolean restriction = false, list = false, union = false; 202 if (varietyProperty.equals(SchemaSymbols.ELT_RESTRICTION)) { 203 refType = XSConstants.DERIVATION_RESTRICTION; 204 restriction = true; 205 } 206 else if (varietyProperty.equals(SchemaSymbols.ELT_LIST)) { 207 refType = XSConstants.DERIVATION_LIST; 208 list = true; 209 } 210 else if (varietyProperty.equals(SchemaSymbols.ELT_UNION)) { 211 refType = XSConstants.DERIVATION_UNION; 212 union = true; 213 } 214 else { 215 reportSchemaError("s4s-elt-must-match.1", new Object []{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))", varietyProperty}, simpleTypeDecl); 216 return errorType(name, schemaDoc.fTargetNamespace, XSConstants.DERIVATION_RESTRICTION); 217 } 218 219 Element nextChild = DOMUtil.getNextSiblingElement(child); 221 if (nextChild != null) { 222 reportSchemaError("s4s-elt-must-match.1", new Object []{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))", DOMUtil.getLocalName(nextChild)}, nextChild); 223 } 224 225 Object [] contentAttrs = fAttrChecker.checkAttributes(child, false, schemaDoc); 227 QName baseTypeName = (QName)contentAttrs[restriction ? 228 XSAttributeChecker.ATTIDX_BASE : 229 XSAttributeChecker.ATTIDX_ITEMTYPE]; 230 Vector memberTypes = (Vector )contentAttrs[XSAttributeChecker.ATTIDX_MEMBERTYPES]; 231 232 Element content = DOMUtil.getFirstChildElement(child); 234 235 if (content != null) { 237 if (DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_ANNOTATION)) { 239 XSAnnotationImpl annotation = traverseAnnotationDecl(content, contentAttrs, false, schemaDoc); 240 if(annotation != null ) { 241 if(annotations == null) 242 annotations = new XSAnnotationImpl [] {annotation}; 243 else { 244 XSAnnotationImpl [] tempArray = new XSAnnotationImpl[2]; 245 tempArray[0] = annotations[0]; 246 annotations = tempArray; 247 annotations[1] = annotation; 248 } 249 } 250 content = DOMUtil.getNextSiblingElement(content); 251 } 252 } 253 254 XSSimpleType baseValidator = null; 256 if ((restriction || list) && baseTypeName != null) { 257 baseValidator = findDTValidator(child, name, baseTypeName, refType, schemaDoc); 258 if (baseValidator == null && fIsBuiltIn) { 260 fIsBuiltIn = false; 261 return null; 262 } 263 } 264 265 Vector dTValidators = null; 267 XSSimpleType dv = null; 268 XSObjectList dvs; 269 if (union && memberTypes != null && memberTypes.size() > 0) { 270 int size = memberTypes.size(); 271 dTValidators = new Vector (size, 2); 272 for (int i = 0; i < size; i++) { 274 dv = findDTValidator(child, name, (QName)memberTypes.elementAt(i), 276 XSConstants.DERIVATION_UNION, schemaDoc); 277 if (dv != null) { 278 if (dv.getVariety() == XSSimpleType.VARIETY_UNION) { 280 dvs = dv.getMemberTypes(); 281 for (int j = 0; j < dvs.getLength(); j++) 282 dTValidators.addElement(dvs.item(j)); 283 } else { 284 dTValidators.addElement(dv); 285 } 286 } 287 } 288 } 289 290 boolean skipFacets = false; 294 295 if (content != null && DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_SIMPLETYPE)) { 297 if (restriction || list) { 298 if (baseTypeName != null) { 300 reportSchemaError(list ? "src-simple-type.3.a" : "src-simple-type.2.a", null, content); 301 } 302 else { 303 baseValidator = traverseLocal(content, schemaDoc, grammar); 305 } 306 content = DOMUtil.getNextSiblingElement(content); 308 } 309 else if (union) { 310 if (dTValidators == null) { 311 dTValidators = new Vector (2, 2); 312 } 313 do { 314 dv = traverseLocal(content, schemaDoc, grammar); 316 if (dv != null) { 317 if (dv.getVariety() == XSSimpleType.VARIETY_UNION) { 319 dvs = dv.getMemberTypes(); 320 for (int j = 0; j < dvs.getLength(); j++) 321 dTValidators.addElement(dvs.item(j)); 322 } else { 323 dTValidators.addElement(dv); 324 } 325 } 326 content = DOMUtil.getNextSiblingElement(content); 328 } while (content != null && DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_SIMPLETYPE)); 329 } 330 } 331 else if ((restriction || list) && baseTypeName == null) { 332 reportSchemaError(list ? "src-simple-type.3.b" : "src-simple-type.2.b", null, child); 334 skipFacets = true; 336 baseValidator = SchemaGrammar.fAnySimpleType; 337 } 338 else if (union && (memberTypes == null || memberTypes.size() == 0)) { 339 reportSchemaError("src-union-memberTypes-or-simpleTypes", null, child); 341 dTValidators = new Vector (1); 342 dTValidators.addElement(SchemaGrammar.fAnySimpleType); 343 } 344 345 if ((restriction || list) && baseValidator == null) { 348 baseValidator = SchemaGrammar.fAnySimpleType; 349 } 350 if (union && (dTValidators == null || dTValidators.size() == 0)) { 353 dTValidators = new Vector (1); 354 dTValidators.addElement(SchemaGrammar.fAnySimpleType); 355 } 356 357 if (list && isListDatatype(baseValidator)) { 359 reportSchemaError("cos-st-restricts.2.1", new Object []{name, baseValidator.getName()}, child); 360 } 361 362 XSSimpleType newDecl = null; 364 if (restriction) { 365 newDecl = schemaFactory.createTypeRestriction(name, schemaDoc.fTargetNamespace, (short)finalProperty, baseValidator, 366 annotations == null? null : new XSObjectListImpl(annotations, annotations.length)); 367 } 368 else if (list) { 369 newDecl = schemaFactory.createTypeList(name, schemaDoc.fTargetNamespace, (short)finalProperty, baseValidator, 370 annotations == null? null : new XSObjectListImpl(annotations, annotations.length)); 371 } 372 else if (union) { 373 XSSimpleType[] memberDecls = new XSSimpleType[dTValidators.size()]; 374 for (int i = 0; i < dTValidators.size(); i++) 375 memberDecls[i] = (XSSimpleType)dTValidators.elementAt(i); 376 newDecl = schemaFactory.createTypeUnion(name, schemaDoc.fTargetNamespace, (short)finalProperty, memberDecls, 377 annotations == null? null : new XSObjectListImpl(annotations, annotations.length)); 378 } 379 380 if (restriction && content != null) { 382 FacetInfo fi = traverseFacets(content, baseValidator, schemaDoc); 383 content = fi.nodeAfterFacets; 384 385 if (!skipFacets) { 386 try { 387 fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport); 388 newDecl.applyFacets(fi.facetdata, fi.fPresentFacets, fi.fFixedFacets, fValidationState); 389 } catch (InvalidDatatypeFacetException ex) { 390 reportSchemaError(ex.getKey(), ex.getArgs(), child); 391 } 392 } 393 } 394 395 if (content != null) { 397 if (restriction) { 398 reportSchemaError("s4s-elt-must-match.1", new Object []{SchemaSymbols.ELT_RESTRICTION, "(annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*))", DOMUtil.getLocalName(content)}, content); 399 } 400 else if (list) { 401 reportSchemaError("s4s-elt-must-match.1", new Object []{SchemaSymbols.ELT_LIST, "(annotation?, (simpleType?))", DOMUtil.getLocalName(content)}, content); 402 } 403 else if (union) { 404 reportSchemaError("s4s-elt-must-match.1", new Object []{SchemaSymbols.ELT_UNION, "(annotation?, (simpleType*))", DOMUtil.getLocalName(content)}, content); 405 } 406 } 407 408 fAttrChecker.returnAttrArray(contentAttrs, schemaDoc); 409 410 return newDecl; 412 } 413 414 private XSSimpleType findDTValidator(Element elm, String refName, 421 QName baseTypeStr, short baseRefContext, 422 XSDocumentInfo schemaDoc) { 423 if (baseTypeStr == null) 424 return null; 425 426 XSTypeDefinition baseType = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, baseTypeStr, elm); 427 if (baseType != null) { 428 if (baseType.getTypeCategory() != XSTypeDefinition.SIMPLE_TYPE || 430 (baseType == SchemaGrammar.fAnySimpleType && 431 baseRefContext == XSConstants.DERIVATION_RESTRICTION)) { 432 if (baseType == SchemaGrammar.fAnySimpleType && 435 checkBuiltIn(refName, schemaDoc.fTargetNamespace)) { 436 return null; 437 } 438 reportSchemaError("cos-st-restricts.1.1", new Object []{baseTypeStr.rawname, refName}, elm); 439 return SchemaGrammar.fAnySimpleType; 440 } 441 if ((baseType.getFinal() & baseRefContext) != 0) { 442 if (baseRefContext == XSConstants.DERIVATION_RESTRICTION) { 443 reportSchemaError("st-props-correct.3", new Object []{refName, baseTypeStr.rawname}, elm); 444 } 445 else if (baseRefContext == XSConstants.DERIVATION_LIST) { 446 reportSchemaError("cos-st-restricts.2.3.1.1", new Object []{baseTypeStr.rawname, refName}, elm); 447 } 448 else if (baseRefContext == XSConstants.DERIVATION_UNION) { 449 reportSchemaError("cos-st-restricts.3.3.1.1", new Object []{baseTypeStr.rawname, refName}, elm); 450 } 451 } 452 } 453 454 return (XSSimpleType)baseType; 455 } 456 457 private final boolean checkBuiltIn(String name, String namespace) { 460 if (namespace != SchemaSymbols.URI_SCHEMAFORSCHEMA) 461 return false; 462 if (SchemaGrammar.SG_SchemaNS.getGlobalTypeDecl(name) != null) 463 fIsBuiltIn = true; 464 return fIsBuiltIn; 465 } 466 467 private boolean isListDatatype(XSSimpleType validator) { 469 if (validator.getVariety() == XSSimpleType.VARIETY_LIST) 470 return true; 471 472 if (validator.getVariety() == XSSimpleType.VARIETY_UNION) { 473 XSObjectList temp = validator.getMemberTypes(); 474 for (int i = 0; i < temp.getLength(); i++) { 475 if (((XSSimpleType)temp.item(i)).getVariety() == XSSimpleType.VARIETY_LIST) { 476 return true; 477 } 478 } 479 } 480 481 return false; 482 } 484 private XSSimpleType errorType(String name, String namespace, short refType) { 485 switch (refType) { 486 case XSConstants.DERIVATION_RESTRICTION: 487 return schemaFactory.createTypeRestriction(name, namespace, (short)0, 488 SchemaGrammar.fAnySimpleType, null); 489 case XSConstants.DERIVATION_LIST: 490 return schemaFactory.createTypeList(name, namespace, (short)0, 491 SchemaGrammar.fAnySimpleType, null); 492 case XSConstants.DERIVATION_UNION: 493 return schemaFactory.createTypeUnion(name, namespace, (short)0, 494 new XSSimpleType[]{SchemaGrammar.fAnySimpleType}, null); 495 } 496 497 return null; 498 } 499 500 } | Popular Tags |