1 16 17 package org.apache.xerces.impl.xs.traversers; 18 19 import java.util.Vector ; 20 21 import org.apache.xerces.impl.dv.InvalidDatatypeFacetException; 22 import org.apache.xerces.impl.dv.SchemaDVFactory; 23 import org.apache.xerces.impl.dv.XSSimpleType; 24 import org.apache.xerces.impl.dv.xs.SchemaDVFactoryImpl; 25 import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl; 26 import org.apache.xerces.impl.xs.SchemaGrammar; 27 import org.apache.xerces.impl.xs.SchemaSymbols; 28 import org.apache.xerces.impl.xs.XSAnnotationImpl; 29 import org.apache.xerces.impl.xs.util.XInt; 30 import org.apache.xerces.impl.xs.util.XSObjectListImpl; 31 import org.apache.xerces.util.DOMUtil; 32 import org.apache.xerces.xni.QName; 33 import org.apache.xerces.xs.XSConstants; 34 import org.apache.xerces.xs.XSObjectList; 35 import org.apache.xerces.xs.XSTypeDefinition; 36 import org.w3c.dom.Element ; 37 38 78 class XSDSimpleTypeTraverser extends XSDAbstractTraverser { 79 80 private final SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance(); 82 83 private boolean fIsBuiltIn = false; 85 86 XSDSimpleTypeTraverser (XSDHandler handler, 87 XSAttributeChecker gAttrCheck) { 88 super(handler, gAttrCheck); 89 if (schemaFactory instanceof SchemaDVFactoryImpl) { 90 ((SchemaDVFactoryImpl)schemaFactory).setDeclPool(handler.fDeclPool); 91 } 92 } 93 94 XSSimpleType traverseGlobal(Element elmNode, 96 XSDocumentInfo schemaDoc, 97 SchemaGrammar grammar) { 98 99 Object [] attrValues = fAttrChecker.checkAttributes(elmNode, true, schemaDoc); 101 String nameAtt = (String )attrValues[XSAttributeChecker.ATTIDX_NAME]; 102 XSSimpleType type = traverseSimpleTypeDecl(elmNode, attrValues, schemaDoc, grammar); 103 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 104 105 if (nameAtt == null) { 107 reportSchemaError("s4s-att-must-appear", new Object []{SchemaSymbols.ELT_SIMPLETYPE, SchemaSymbols.ATT_NAME}, elmNode); 108 type = null; 109 } 110 111 if (type != null) { 113 grammar.addGlobalTypeDecl(type); 114 } 115 116 return type; 117 } 118 119 XSSimpleType traverseLocal(Element elmNode, 120 XSDocumentInfo schemaDoc, 121 SchemaGrammar grammar) { 122 123 Object [] attrValues = fAttrChecker.checkAttributes(elmNode, false, schemaDoc); 125 String name = genAnonTypeName(elmNode); 126 XSSimpleType type = getSimpleType (name, elmNode, attrValues, schemaDoc, grammar); 127 if (type instanceof XSSimpleTypeDecl) { 128 ((XSSimpleTypeDecl)type).setAnonymous(true); 129 } 130 fAttrChecker.returnAttrArray(attrValues, schemaDoc); 131 132 return type; 133 } 134 135 private XSSimpleType traverseSimpleTypeDecl(Element simpleTypeDecl, 136 Object [] attrValues, 137 XSDocumentInfo schemaDoc, 138 SchemaGrammar grammar) { 139 140 String name = (String )attrValues[XSAttributeChecker.ATTIDX_NAME]; 142 return getSimpleType(name, simpleTypeDecl, attrValues, schemaDoc, grammar); 143 } 144 145 148 private String genAnonTypeName(Element simpleTypeDecl) { 149 150 StringBuffer typeName = new StringBuffer ("#AnonType_"); 155 Element node = DOMUtil.getParent(simpleTypeDecl); 156 while (node != null && (node != DOMUtil.getRoot(DOMUtil.getDocument(node)))) { 157 typeName.append(node.getAttribute(SchemaSymbols.ATT_NAME)); 158 node = DOMUtil.getParent(node); 159 } 160 return typeName.toString(); 161 } 162 163 171 private XSSimpleType getSimpleType(String name, Element simpleTypeDecl, Object [] attrValues, XSDocumentInfo schemaDoc, SchemaGrammar grammar) { 172 XInt finalAttr = (XInt)attrValues[XSAttributeChecker.ATTIDX_FINAL]; 173 int finalProperty = finalAttr == null ? schemaDoc.fFinalDefault : finalAttr.intValue(); 174 Element child = DOMUtil.getFirstChildElement(simpleTypeDecl); 176 XSAnnotationImpl [] annotations = null; 177 if (child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) { 178 XSAnnotationImpl annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc); 179 if (annotation != null) 180 annotations = new XSAnnotationImpl [] {annotation}; 181 child = DOMUtil.getNextSiblingElement(child); 182 } 183 else { 184 String text = DOMUtil.getSyntheticAnnotation(simpleTypeDecl); 185 if (text != null) { 186 XSAnnotationImpl annotation = traverseSyntheticAnnotation(simpleTypeDecl, text, attrValues, false, schemaDoc); 187 annotations = new XSAnnotationImpl[] {annotation}; 188 } 189 } 190 if (child == null) { 192 reportSchemaError("s4s-elt-must-match.2", new Object []{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))"}, simpleTypeDecl); 193 return errorType(name, schemaDoc.fTargetNamespace, XSConstants.DERIVATION_RESTRICTION); 194 } 195 String varietyProperty = DOMUtil.getLocalName(child); 197 short refType = XSConstants.DERIVATION_RESTRICTION; 198 boolean restriction = false, list = false, union = false; 199 if (varietyProperty.equals(SchemaSymbols.ELT_RESTRICTION)) { 200 refType = XSConstants.DERIVATION_RESTRICTION; 201 restriction = true; 202 } 203 else if (varietyProperty.equals(SchemaSymbols.ELT_LIST)) { 204 refType = XSConstants.DERIVATION_LIST; 205 list = true; 206 } 207 else if (varietyProperty.equals(SchemaSymbols.ELT_UNION)) { 208 refType = XSConstants.DERIVATION_UNION; 209 union = true; 210 } 211 else { 212 reportSchemaError("s4s-elt-must-match.1", new Object []{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))", varietyProperty}, simpleTypeDecl); 213 return errorType(name, schemaDoc.fTargetNamespace, XSConstants.DERIVATION_RESTRICTION); 214 } 215 Element nextChild = DOMUtil.getNextSiblingElement(child); 217 if (nextChild != null) { 218 reportSchemaError("s4s-elt-must-match.1", new Object []{SchemaSymbols.ELT_SIMPLETYPE, "(annotation?, (restriction | list | union))", DOMUtil.getLocalName(nextChild)}, nextChild); 219 } 220 Object [] contentAttrs = fAttrChecker.checkAttributes(child, false, schemaDoc); 222 QName baseTypeName = (QName)contentAttrs[restriction ? 223 XSAttributeChecker.ATTIDX_BASE : 224 XSAttributeChecker.ATTIDX_ITEMTYPE]; 225 Vector memberTypes = (Vector )contentAttrs[XSAttributeChecker.ATTIDX_MEMBERTYPES]; 226 Element content = DOMUtil.getFirstChildElement(child); 228 if (content != null && DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_ANNOTATION)) { 230 XSAnnotationImpl annotation = traverseAnnotationDecl(content, contentAttrs, false, schemaDoc); 231 if (annotation != null ) { 232 if(annotations == null) { 233 annotations = new XSAnnotationImpl [] {annotation}; 234 } 235 else { 236 XSAnnotationImpl [] tempArray = new XSAnnotationImpl[2]; 237 tempArray[0] = annotations[0]; 238 annotations = tempArray; 239 annotations[1] = annotation; 240 } 241 } 242 content = DOMUtil.getNextSiblingElement(content); 243 } 244 else { 245 String text = DOMUtil.getSyntheticAnnotation(child); 246 if (text != null) { 247 XSAnnotationImpl annotation = traverseSyntheticAnnotation(child, text, contentAttrs, false, schemaDoc); 248 if (annotations == null) { 249 annotations = new XSAnnotationImpl [] {annotation}; 250 } 251 else { 252 XSAnnotationImpl [] tempArray = new XSAnnotationImpl[2]; 253 tempArray[0] = annotations[0]; 254 annotations = tempArray; 255 annotations[1] = annotation; 256 } 257 } 258 } 259 XSSimpleType baseValidator = null; 261 if ((restriction || list) && baseTypeName != null) { 262 baseValidator = findDTValidator(child, name, baseTypeName, refType, schemaDoc); 263 if (baseValidator == null && fIsBuiltIn) { 265 fIsBuiltIn = false; 266 return null; 267 } 268 } 269 Vector dTValidators = null; 271 XSSimpleType dv = null; 272 XSObjectList dvs; 273 if (union && memberTypes != null && memberTypes.size() > 0) { 274 int size = memberTypes.size(); 275 dTValidators = new Vector (size, 2); 276 for (int i = 0; i < size; i++) { 278 dv = findDTValidator(child, name, (QName)memberTypes.elementAt(i), 280 XSConstants.DERIVATION_UNION, schemaDoc); 281 if (dv != null) { 282 if (dv.getVariety() == XSSimpleType.VARIETY_UNION) { 284 dvs = dv.getMemberTypes(); 285 for (int j = 0; j < dvs.getLength(); j++) 286 dTValidators.addElement(dvs.item(j)); 287 } else { 288 dTValidators.addElement(dv); 289 } 290 } 291 } 292 } 293 boolean skipFacets = false; 297 if (content != null && DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_SIMPLETYPE)) { 299 if (restriction || list) { 300 if (baseTypeName != null) { 302 reportSchemaError(list ? "src-simple-type.3.a" : "src-simple-type.2.a", null, content); 303 } 304 else { 305 baseValidator = traverseLocal(content, schemaDoc, grammar); 307 } 308 content = DOMUtil.getNextSiblingElement(content); 310 } 311 else if (union) { 312 if (dTValidators == null) { 313 dTValidators = new Vector (2, 2); 314 } 315 do { 316 dv = traverseLocal(content, schemaDoc, grammar); 318 if (dv != null) { 319 if (dv.getVariety() == XSSimpleType.VARIETY_UNION) { 321 dvs = dv.getMemberTypes(); 322 for (int j = 0; j < dvs.getLength(); j++) 323 dTValidators.addElement(dvs.item(j)); 324 } else { 325 dTValidators.addElement(dv); 326 } 327 } 328 content = DOMUtil.getNextSiblingElement(content); 330 } while (content != null && DOMUtil.getLocalName(content).equals(SchemaSymbols.ELT_SIMPLETYPE)); 331 } 332 } 333 else if ((restriction || list) && baseTypeName == null) { 334 reportSchemaError(list ? "src-simple-type.3.b" : "src-simple-type.2.b", null, child); 336 skipFacets = true; 338 baseValidator = SchemaGrammar.fAnySimpleType; 339 } 340 else if (union && (memberTypes == null || memberTypes.size() == 0)) { 341 reportSchemaError("src-union-memberTypes-or-simpleTypes", null, child); 343 dTValidators = new Vector (1); 344 dTValidators.addElement(SchemaGrammar.fAnySimpleType); 345 } 346 if ((restriction || list) && baseValidator == null) { 349 baseValidator = SchemaGrammar.fAnySimpleType; 350 } 351 if (union && (dTValidators == null || dTValidators.size() == 0)) { 354 dTValidators = new Vector (1); 355 dTValidators.addElement(SchemaGrammar.fAnySimpleType); 356 } 357 if (list && isListDatatype(baseValidator)) { 359 reportSchemaError("cos-st-restricts.2.1", new Object []{name, baseValidator.getName()}, child); 360 } 361 XSSimpleType newDecl = null; 363 if (restriction) { 364 newDecl = schemaFactory.createTypeRestriction(name, schemaDoc.fTargetNamespace, (short)finalProperty, baseValidator, 365 annotations == null? null : new XSObjectListImpl(annotations, annotations.length)); 366 } 367 else if (list) { 368 newDecl = schemaFactory.createTypeList(name, schemaDoc.fTargetNamespace, (short)finalProperty, baseValidator, 369 annotations == null? null : new XSObjectListImpl(annotations, annotations.length)); 370 } 371 else if (union) { 372 XSSimpleType[] memberDecls = new XSSimpleType[dTValidators.size()]; 373 for (int i = 0; i < dTValidators.size(); i++) 374 memberDecls[i] = (XSSimpleType)dTValidators.elementAt(i); 375 newDecl = schemaFactory.createTypeUnion(name, schemaDoc.fTargetNamespace, (short)finalProperty, memberDecls, 376 annotations == null? null : new XSObjectListImpl(annotations, annotations.length)); 377 } 378 if (restriction && content != null) { 380 FacetInfo fi = traverseFacets(content, baseValidator, schemaDoc); 381 content = fi.nodeAfterFacets; 382 383 if (!skipFacets) { 384 try { 385 fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport); 386 newDecl.applyFacets(fi.facetdata, fi.fPresentFacets, fi.fFixedFacets, fValidationState); 387 } catch (InvalidDatatypeFacetException ex) { 388 reportSchemaError(ex.getKey(), ex.getArgs(), child); 389 } 390 } 391 } 392 if (content != null) { 394 if (restriction) { 395 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); 396 } 397 else if (list) { 398 reportSchemaError("s4s-elt-must-match.1", new Object []{SchemaSymbols.ELT_LIST, "(annotation?, (simpleType?))", DOMUtil.getLocalName(content)}, content); 399 } 400 else if (union) { 401 reportSchemaError("s4s-elt-must-match.1", new Object []{SchemaSymbols.ELT_UNION, "(annotation?, (simpleType*))", DOMUtil.getLocalName(content)}, content); 402 } 403 } 404 fAttrChecker.returnAttrArray(contentAttrs, schemaDoc); 405 return newDecl; 407 } 408 409 private XSSimpleType findDTValidator(Element elm, String refName, 416 QName baseTypeStr, short baseRefContext, 417 XSDocumentInfo schemaDoc) { 418 if (baseTypeStr == null) 419 return null; 420 421 XSTypeDefinition baseType = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, baseTypeStr, elm); 422 if (baseType != null) { 423 if (baseType.getTypeCategory() != XSTypeDefinition.SIMPLE_TYPE || 425 baseType == SchemaGrammar.fAnySimpleType && 426 baseRefContext == XSConstants.DERIVATION_RESTRICTION) { 427 if (baseType == SchemaGrammar.fAnySimpleType && 430 checkBuiltIn(refName, schemaDoc.fTargetNamespace)) { 431 return null; 432 } 433 reportSchemaError("cos-st-restricts.1.1", new Object []{baseTypeStr.rawname, refName}, elm); 434 return SchemaGrammar.fAnySimpleType; 435 } 436 if ((baseType.getFinal() & baseRefContext) != 0) { 437 if (baseRefContext == XSConstants.DERIVATION_RESTRICTION) { 438 reportSchemaError("st-props-correct.3", new Object []{refName, baseTypeStr.rawname}, elm); 439 } 440 else if (baseRefContext == XSConstants.DERIVATION_LIST) { 441 reportSchemaError("cos-st-restricts.2.3.1.1", new Object []{baseTypeStr.rawname, refName}, elm); 442 } 443 else if (baseRefContext == XSConstants.DERIVATION_UNION) { 444 reportSchemaError("cos-st-restricts.3.3.1.1", new Object []{baseTypeStr.rawname, refName}, elm); 445 } 446 } 447 } 448 449 return (XSSimpleType)baseType; 450 } 451 452 private final boolean checkBuiltIn(String name, String namespace) { 455 if (namespace != SchemaSymbols.URI_SCHEMAFORSCHEMA) 456 return false; 457 if (SchemaGrammar.SG_SchemaNS.getGlobalTypeDecl(name) != null) 458 fIsBuiltIn = true; 459 return fIsBuiltIn; 460 } 461 462 private boolean isListDatatype(XSSimpleType validator) { 464 if (validator.getVariety() == XSSimpleType.VARIETY_LIST) 465 return true; 466 467 if (validator.getVariety() == XSSimpleType.VARIETY_UNION) { 468 XSObjectList temp = validator.getMemberTypes(); 469 for (int i = 0; i < temp.getLength(); i++) { 470 if (((XSSimpleType)temp.item(i)).getVariety() == XSSimpleType.VARIETY_LIST) { 471 return true; 472 } 473 } 474 } 475 476 return false; 477 } 479 private XSSimpleType errorType(String name, String namespace, short refType) { 480 switch (refType) { 481 case XSConstants.DERIVATION_RESTRICTION: 482 return schemaFactory.createTypeRestriction(name, namespace, (short)0, 483 SchemaGrammar.fAnySimpleType, null); 484 case XSConstants.DERIVATION_LIST: 485 return schemaFactory.createTypeList(name, namespace, (short)0, 486 SchemaGrammar.fAnySimpleType, null); 487 case XSConstants.DERIVATION_UNION: 488 return schemaFactory.createTypeUnion(name, namespace, (short)0, 489 new XSSimpleType[]{SchemaGrammar.fAnySimpleType}, null); 490 } 491 492 return null; 493 } 494 495 } | Popular Tags |