String targetNsAtt = (String) attrValues[XSAttributeChecker.ATTIDX_TARGETNAMESPACE];
QName typeAtt = (QName) attrValues[XSAttributeChecker.ATTIDX_TYPE];
// Step 1: get declaration information
XSElementDecl element = null;
if (fSchemaHandler.fDeclPool !=null) {
element = fSchemaHandler.fDeclPool.getElementDecl();
} else {
element = new XSElementDecl();
}
// get 'name'
if (nameAtt != null)
element.fName = fSymbolTable.addSymbol(nameAtt);
// get 'target namespace'
if (isGlobal) {
element.fTargetNamespace = schemaDoc.fTargetNamespace;
element.setIsGlobal();
}
else {
// Sanity check, parent should not be null
if (parent != null) {
if (parent instanceof XSComplexTypeDecl || fSchemaHandler.fSchemaVersion == Constants.SCHEMA_VERSION_1_1) {
element.setIsLocal(parent);
}
}
if (targetNsAtt!=null) {
// XML Schema 1.1, set the target namespace to be the value of the targetNamespace attribute if one is defined
targetNsAtt = fSymbolTable.addSymbol(targetNsAtt);
element.fTargetNamespace = targetNsAtt;
} else if (formAtt != null) {
if (formAtt.intValue() == SchemaSymbols.FORM_QUALIFIED)
element.fTargetNamespace = schemaDoc.fTargetNamespace;
else
element.fTargetNamespace = null;
} else if (schemaDoc.fAreLocalElementsQualified) {
element.fTargetNamespace = schemaDoc.fTargetNamespace;
} else {
element.fTargetNamespace = null;
}
}
// get 'block', 'final', 'nillable', 'abstract'
element.fBlock = blockAtt == null ? schemaDoc.fBlockDefault : blockAtt.shortValue();
element.fFinal = finalAtt == null ? schemaDoc.fFinalDefault : finalAtt.shortValue();
// discard valid Block/Final 'Default' values that are invalid for Block/Final
element.fBlock &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION | XSConstants.DERIVATION_SUBSTITUTION);
element.fFinal &= (XSConstants.DERIVATION_EXTENSION | XSConstants.DERIVATION_RESTRICTION);
if (nillableAtt.booleanValue())
element.setIsNillable();
if (abstractAtt != null && abstractAtt.booleanValue())
element.setIsAbstract();
// get 'value constraint'
if (fixedAtt != null) {
element.fDefault = new ValidatedInfo();
element.fDefault.normalizedValue = fixedAtt;
element.setConstraintType(XSConstants.VC_FIXED);
} else if (defaultAtt != null) {
element.fDefault = new ValidatedInfo();
element.fDefault.normalizedValue = defaultAtt;
element.setConstraintType(XSConstants.VC_DEFAULT);
} else {
element.setConstraintType(XSConstants.VC_NONE);
}
// get 'substitutionGroup affiliation'
if (subGroupAtt != null && !subGroupAtt.isEmpty()) {
Vector elemDecl = new Vector();
for (int i=0; i<subGroupAtt.size(); i++) {
QName subGroup = (QName)subGroupAtt.get(i);
// returns null if element is already parsed
XSElementDecl globalDecl = (XSElementDecl)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.ELEMENT_TYPE, subGroup, elmDecl);
if (globalDecl != null) {
elemDecl.add(globalDecl);
}
}
int validSubgroupElemDeclSize = elemDecl.size();
if (validSubgroupElemDeclSize > 0) {
element.fSubGroup = (XSElementDecl[])elemDecl.toArray(new XSElementDecl[validSubgroupElemDeclSize]);
}
}
// get 'annotation'
Element child = DOMUtil.getFirstChildElement(elmDecl);
XSAnnotationImpl annotation = null;
if(child != null && DOMUtil.getLocalName(child).equals(SchemaSymbols.ELT_ANNOTATION)) {
annotation = traverseAnnotationDecl(child, attrValues, false, schemaDoc);
child = DOMUtil.getNextSiblingElement(child);
}
else {
String text = DOMUtil.getSyntheticAnnotation(elmDecl);
if (text != null) {
annotation = traverseSyntheticAnnotation(elmDecl, text, attrValues, false, schemaDoc);
}
}
XSObjectList annotations;
if (annotation != null) {
annotations = new XSObjectListImpl();
((XSObjectListImpl)annotations).addXSObject (annotation);
} else {
annotations = XSObjectListImpl.EMPTY_LIST;
}
element.fAnnotations = annotations;
// get 'type definition'
XSTypeDefinition elementType = null;
boolean haveAnonType = false;
// Handle Anonymous type if there is one
if (child != null) {
String childName = DOMUtil.getLocalName(child);
if (childName.equals(SchemaSymbols.ELT_COMPLEXTYPE)) {
elementType = fSchemaHandler.fComplexTypeTraverser.traverseLocal(child, schemaDoc, grammar, element);
haveAnonType = true;
child = DOMUtil.getNextSiblingElement(child);
}
else if (childName.equals(SchemaSymbols.ELT_SIMPLETYPE)) {
elementType = fSchemaHandler.fSimpleTypeTraverser.traverseLocal(child, schemaDoc, grammar, element);
haveAnonType = true;
child = DOMUtil.getNextSiblingElement(child);
}
}
// Handler type attribute
if (elementType == null && typeAtt != null) {
elementType = (XSTypeDefinition)fSchemaHandler.getGlobalDecl(schemaDoc, XSDHandler.TYPEDECL_TYPE, typeAtt, elmDecl);
if (elementType == null) {
element.fUnresolvedTypeName = typeAtt;
}
}
// Get it from the substitutionGroup declaration
if (elementType == null && element.fSubGroup != null) {
elementType = element.fSubGroup[0].fType;
}
if (elementType == null) {
elementType = SchemaGrammar.getXSAnyType(fSchemaHandler.fSchemaVersion);
}
element.fType = elementType;
// get 'identity constraint'
// see if there's something here; it had better be key, keyref or unique.
if (child != null) {
String childName = DOMUtil.getLocalName(child);
// if XML Schema 1.1, check for alternative types first
if (fSchemaHandler.fSchemaVersion == Constants.SCHEMA_VERSION_1_1) {
while (childName.equals(SchemaSymbols.ELT_ALTERNATIVE)) {
fSchemaHandler.fTypeAlternativeTraverser.traverse(child, element, schemaDoc, grammar);
child = DOMUtil.getNextSiblingElement(child);
if (child != null) {
childName = DOMUtil.getLocalName(child);
}
else {
if (!element.isTypeTableOK()) {
reportSchemaError("src-element.5", new Object[]{nameAtt}, elmDecl);
}
element.setDefaultTypeDefinition();
break;
}
}
}
while (child != null &&
(childName.equals(SchemaSymbols.ELT_KEY) ||
childName.equals(SchemaSymbols.ELT_KEYREF) ||
childName.equals(SchemaSymbols.ELT_UNIQUE))) {
// if the identity constraint contains the ref attribute,
// defer traversal until all the named identity constraints have been traversed
if (DOMUtil.getAttr(child, SchemaSymbols.ATT_REF) != null) {
fSchemaHandler.storeIdentityConstraintReferral(child, schemaDoc, element);
}
else if (childName.equals(SchemaSymbols.ELT_KEY) ||
childName.equals(SchemaSymbols.ELT_UNIQUE)) {
// need to set <key>/<unique> to hidden before traversing it,
// because it has global scope
DOMUtil.setHidden(child, fSchemaHandler.fHiddenNodes);
fSchemaHandler.fUniqueOrKeyTraverser.traverse(child, element, schemaDoc, grammar);
if(DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME).length() != 0 ) {
fSchemaHandler.checkForDuplicateNames(
(schemaDoc.fTargetNamespace == null) ? ","+DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME)
: schemaDoc.fTargetNamespace+","+ DOMUtil.getAttrValue(child, SchemaSymbols.ATT_NAME),
fSchemaHandler.ATTRIBUTE_TYPE, fSchemaHandler.getIDRegistry(), fSchemaHandler.getIDRegistry_sub(),
child, schemaDoc);
}
} else if (childName.equals(SchemaSymbols.ELT_KEYREF)) {
fSchemaHandler.storeKeyRef(child, schemaDoc, element);
}
child = DOMUtil.getNextSiblingElement(child);
if (child != null) {
childName = DOMUtil.getLocalName(child);
}
}
}
// Step 3: check against schema for schemas
// required attributes
if (nameAtt == null) {
if (isGlobal)
reportSchemaError("s4s-att-must-appear", new Object[]{SchemaSymbols.ELT_ELEMENT, SchemaSymbols.ATT_NAME}, elmDecl);
else
reportSchemaError("src-element.2.1", null, elmDecl);
nameAtt = NO_NAME;
}
// element
if (child != null) {
if (fSchemaHandler.fSchemaVersion == Constants.SCHEMA_VERSION_1_1) {
reportSchemaError("s4s-elt-must-match.1", new Object[]{nameAtt, "(annotation?, (simpleType | complexType)?, alternative*, (unique | key | keyref)*))", DOMUtil.getLocalName(child)}, child);
}
else {
reportSchemaError("s4s-elt-must-match.1", new Object[]{nameAtt, "(annotation?, (simpleType | complexType)?, (unique | key | keyref)*))", DOMUtil.getLocalName(child)}, child);
}
}
// Step 4: check 3.3.3 constraints
// src-element
// 1 default and fixed must not both be present.
if (defaultAtt != null && fixedAtt != null) {
reportSchemaError("src-element.1", new Object[]{nameAtt}, elmDecl);
}
// 2 If the item's parent is not <schema>, then all of the following must be true:
// 2.1 One of ref or name must be present, but not both.
// This is checked in XSAttributeChecker
// 2.2 If ref is present, then all of <complexType>, <simpleType>, <key>, <keyref>, <unique>, nillable, default, fixed, form, block and type must be absent, i.e. only minOccurs, maxOccurs, id are allowed in addition to ref, along with <annotation>.
// Attributes are checked in XSAttributeChecker, elements are checked in "traverse" method
// 3 type and either <simpleType> or <complexType> are mutually exclusive.
if (haveAnonType && (typeAtt != null)) {
reportSchemaError("src-element.3", new Object[]{nameAtt}, elmDecl);
}
// 4 If the targetNamespace attribute is present, then all of the following are true:
if (targetNsAtt != null) {
// 4.2 The form attribute must not be present.
if (formAtt != null) {
reportSchemaError ("src-element.4.2", new Object[] {nameAtt}, elmDecl);
}
// 4.3 If the ancestor <schema> does not have a targetNamespace [attribute] or its 'actual value' is different from the 'actual value' of targetNamespace of <attribute>:
String schemaTns = schemaDoc.fTargetNamespace;
if (schemaTns==null || targetNsAtt!=schemaTns) {
// 4.3.1 <element> must have <complexType> as an ancestor
if (parent==null || !(parent instanceof XSComplexTypeDecl)) {
reportSchemaError ("src-element.4.3.1", new Object[] {nameAtt}, elmDecl);
}
// 4.3.2 There must be a <restriction> ancestor between the <attribute> and the nearest <complexType> ancestor, and the 'actual value' of the base [attribute] of <restriction> does not 'match' the name of 'xs:anyType'.
else if ((((XSComplexTypeDecl)parent).getDerivationMethod() != XSConstants.DERIVATION_RESTRICTION) || (((XSComplexTypeDecl)parent).getBaseType() == SchemaGrammar.getXSAnyType(fSchemaHandler.fSchemaVersion))) {
reportSchemaError ("src-element.4.3.2", new Object[] {nameAtt}, elmDecl);
}
}
}
// Step 5: check 3.3.6 constraints
// check for NOTATION type
checkNotationType(nameAtt, elementType, elmDecl);
// e-props-correct
// 2 If there is a {value constraint}, the canonical lexical representation of its value must be valid with respect to the {type definition} as defined in Element Default Valid (Immediate) (3.3.6).
if (element.fDefault != null) {
fValidationState.setNamespaceSupport(schemaDoc.fNamespaceSupport);
if (fSchemaHandler.fXSConstraints.ElementDefaultValidImmediate(element.fType, element.fDefault.normalizedValue, fValidationState, element.fDefault) == null) {
reportSchemaError ("e-props-correct.2", new Object[]{nameAtt, element.fDefault.normalizedValue}, elmDecl);
element.fDefault = null;
element.setConstraintType(XSConstants.VC_NONE);
}
}
// 4 If there is an {substitution group affiliation}, the {type definition} of the element declaration must be validly derived from the {type definition} of the {substitution group affiliation}, given the value of the {substitution group exclusions} of the {substitution group affiliation}, as defined in Type Derivation OK (Complex) (3.4.6) (if the {type definition} is complex) or as defined in Type Derivation OK (Simple) (3.14.6) (if the {type definition} is simple).
if (element.fSubGroup != null) {
for (int i=0; i< element.fSubGroup.length; i++) {
if (!fSchemaHandler.fXSConstraints.checkTypeDerivationOk(element.fType, element.fSubGroup[i].fType, element.fSubGroup[i].fFinal)) {
reportSchemaError ("e-props-correct.4", new Object[]{nameAtt, ((QName)subGroupAtt.get(i)).prefix +":"+((QName)subGroupAtt.get(i)).localpart}, elmDecl);
//TODO: remove the subGroup from the list?????
}
}
}
// Only applies to XML Schema 1.0
//
// 5 If the {type definition} or {type definition}'s {content type} is or is derived from ID then there must not be a {value constraint}.
if (fSchemaHandler.fSchemaVersion < Constants.SCHEMA_VERSION_1_1 && element.fDefault != null) {
if ((elementType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE &&
((XSSimpleType)elementType).isIDType()) ||
(elementType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE &&
((XSComplexTypeDecl)elementType).containsTypeID())) {
reportSchemaError ("e-props-correct.5", new Object[]{element.fName}, elmDecl);
element.fDefault = null;
element.setConstraintType(XSConstants.VC_NONE);
}
}
// Element without a name. Return null.
if (element.fName == null)
return null;
// Step 5: register the element decl to the grammar
if (isGlobal) {
grammar.addGlobalElementDeclAll(element);
if (grammar.getGlobalElementDecl(element.fName) == null) {
grammar.addGlobalElementDecl(element);
}
// we also add the element to the tolerate duplicates list as well
final String loc = fSchemaHandler.schemaDocument2SystemId(schemaDoc);
final XSElementDecl element2 = grammar.getGlobalElementDecl(element.fName, loc);
if (element2 == null) {
grammar.addGlobalElementDecl(element, loc);
}
// if we are tolerating duplicates, and we found a duplicate declaration