Merge pull request #185 from QuickJack/master
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaComplexType.cs
index 4a5ce6337d2353101ca90701b2e954890475b667..d53928f8df6b52828c0ae3f3503f4a82ecd13d28 100644 (file)
@@ -54,7 +54,9 @@ namespace System.Xml.Schema
                private XmlSchemaContentType resolvedContentType;
 
                internal bool ValidatedIsAbstract;
-               internal bool ParentIsSchema = false;
+               internal bool ParentIsSchema {
+                       get { return Parent is XmlSchema; }
+               }
 
                const string xmlname = "complexType";
 
@@ -64,13 +66,13 @@ namespace System.Xml.Schema
                        get {
                                if (anyType == null) {
                                        anyType = new XmlSchemaComplexType ();
-                                       anyType.Name = "";      // In MS.NET, it is not "anyType"
-                                       anyType.QNameInternal = XmlQualifiedName.Empty; // Not xs:anyType as well.
-#if BUGGY_MS_COMPLIANT
-                                       anyType.validatableParticle = XmlSchemaParticle.Empty; // This code makes validator handles these schemas incorrectly: particlesIb001, mgM013, mgH014, ctE004, ctD004
-#else
-                                       anyType.validatableParticle = XmlSchemaAny.AnyTypeContent;
-#endif
+                                       anyType.Name = "anyType";
+                                       anyType.QNameInternal = new XmlQualifiedName ("anyType", XmlSchema.Namespace);
+                                       if (XmlSchemaUtil.StrictMsCompliant)
+                                               anyType.validatableParticle = XmlSchemaParticle.Empty; // This code makes validator handles these schemas incorrectly: particlesIb001, mgM013, mgH014, ctE004, ctD004
+                                       else
+                                               anyType.validatableParticle = XmlSchemaAny.AnyTypeContent;
+
                                        anyType.contentTypeParticle = anyType.validatableParticle;
                                        anyType.DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;
                                        anyType.isMixed = true;
@@ -187,6 +189,19 @@ namespace System.Xml.Schema
                        get{ return contentTypeParticle; }
                }
 
+               internal override void SetParent (XmlSchemaObject parent)
+               {
+                       base.SetParent (parent);
+                       if (ContentModel != null)
+                               ContentModel.SetParent (this);
+                       if (Particle != null)
+                               Particle.SetParent (this);
+                       if (AnyAttribute != null)
+                               AnyAttribute.SetParent (this);
+                       foreach (XmlSchemaObject obj in Attributes)
+                               obj.SetParent (this);
+               }
+
                /// <remarks>
                /// 1. If ContentModel is present, neither particle nor Attributes nor AnyAttribute can be present.
                /// 2. If particle is present, 
@@ -207,18 +222,8 @@ namespace System.Xml.Schema
                        if (CompilationId == schema.CompilationId)
                                return errorCount;
 
-#if NET_2_0
-                       if (ContentModel != null)
-                               ContentModel.Parent = this;
-                       if (Particle != null)
-                               Particle.Parent = this;
-                       if (AnyAttribute != null)
-                               AnyAttribute.Parent = this;
-                       foreach (XmlSchemaObject obj in Attributes)
-                               obj.Parent = this;
-#endif
-
                        ValidatedIsAbstract = isAbstract;
+                       attributeUses.Clear();
 
                        if (isRedefinedComponent) {
                                if (Annotation != null)
@@ -241,7 +246,7 @@ namespace System.Xml.Schema
                                else if(!XmlSchemaUtil.CheckNCName(Name))
                                        error(h,"name must be a NCName");
                                else
-                                       this.QNameInternal = new XmlQualifiedName(Name, schema.TargetNamespace);
+                                       this.QNameInternal = new XmlQualifiedName(Name, AncestorSchema.TargetNamespace);
                                
                                if(Block != XmlSchemaDerivationMethod.None)
                                {
@@ -405,18 +410,27 @@ namespace System.Xml.Schema
                        }
                        else
                                resolvedDerivedBy = XmlSchemaDerivationMethod.Empty;
+               }
 
+               void FillContentTypeParticle (ValidationEventHandler h, XmlSchema schema)
+               {
+                       if (CollectProcessId == schema.CompilationId)
+                               return;
+                       CollectProcessId = schema.CompilationId;
+
+                       var ct = BaseXmlSchemaType as XmlSchemaComplexType;
+                       if (ct != null)
+                               ct.FillContentTypeParticle (h, schema);
 
                        // {content type} => ContentType and ContentTypeParticle (later)
                        if (ContentModel != null) {
                                CollectContentTypeFromContentModel (h, schema);
                        } else
                                CollectContentTypeFromImmediateContent ();
+
                        contentTypeParticle = validatableParticle.GetOptimizedParticle (true);
                        if (contentTypeParticle == XmlSchemaParticle.Empty && resolvedContentType == XmlSchemaContentType.ElementOnly)
                                resolvedContentType = XmlSchemaContentType.Empty;
-
-                       CollectProcessId = schema.CompilationId;
                }
 
                #region {content type}
@@ -472,7 +486,7 @@ namespace System.Xml.Schema
                        if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)
                                baseComplexType = XmlSchemaComplexType.AnyType;
 
-                       // On error case, it simple reject any contents
+                       // On error case, it simply rejects any contents
                        if (baseComplexType == null) {
                                validatableParticle = XmlSchemaParticle.Empty;
                                resolvedContentType = XmlSchemaContentType.Empty;
@@ -480,6 +494,7 @@ namespace System.Xml.Schema
                        }
 
                        // 3.4.2 complex content {content type}
+                       // FIXME: this part is looking different than the spec. sections.
                        if (cce.Particle == null || cce.Particle == XmlSchemaParticle.Empty) {
                                // - 2.1
                                if (baseComplexType == null) {
@@ -489,6 +504,9 @@ namespace System.Xml.Schema
                                } else {
                                        validatableParticle = baseComplexType.ValidatableParticle;
                                        resolvedContentType = baseComplexType.resolvedContentType;
+                                       // Bug #501814
+                                       if (resolvedContentType == XmlSchemaContentType.Empty)
+                                               resolvedContentType = GetComplexContentType (contentModel);
                                }
                        } else if (baseComplexType.validatableParticle == XmlSchemaParticle.Empty
                                || baseComplexType == XmlSchemaComplexType.AnyType) {
@@ -578,6 +596,12 @@ namespace System.Xml.Schema
 
                        CollectSchemaComponent (h, schema);
 
+                       ValidateBaseXmlSchemaType (h, schema);
+                       
+                       ValidateParticle (h, schema);
+                       
+                       FillContentTypeParticle (h, schema);
+
                        // 3.4.6: Properties Correct
                        // Term. 1 => 3.4.1 already done by CollectSchemaComponent()
                        //            except for {attribute uses} and {attribute wildcard}
@@ -586,11 +610,8 @@ namespace System.Xml.Schema
                        //
                        if (ContentModel != null)
                                ValidateContentModel (h, schema);
-                       else {
-                               if (Particle != null)
-                                       ValidateImmediateParticle (h, schema);
+                       else
                                ValidateImmediateAttributes (h, schema);
-                       }
 
                        // Additional support for 3.8.6 All Group Limited
                        if (ContentTypeParticle != null) {
@@ -630,19 +651,6 @@ namespace System.Xml.Schema
                        return errorCount;
                }
 
-               private void ValidateImmediateParticle (ValidationEventHandler h, XmlSchema schema)
-               {
-                       errorCount += particle.Validate (h, schema);
-                       XmlSchemaGroupRef pgrp = Particle as XmlSchemaGroupRef;
-                       if (pgrp != null) {
-                               if (pgrp.TargetGroup != null)
-                                       errorCount += pgrp.TargetGroup.Validate (h,schema);
-                               // otherwise, it might be missing sub components.
-                               else if (!schema.IsNamespaceAbsent (pgrp.RefName.Namespace))
-                                       error (h, "Referenced group " + pgrp.RefName + " was not found in the corresponding schema.");
-                       }
-               }
-
                private void ValidateImmediateAttributes (ValidationEventHandler h, XmlSchema schema)
                {
                        // {attribute uses}
@@ -651,13 +659,35 @@ namespace System.Xml.Schema
                        XmlSchemaUtil.ValidateAttributesResolved (attributeUses,
                                h, schema, attributes, anyAttribute, ref attributeWildcard, null, false);
                }
+               
+               private void ValidateBaseXmlSchemaType (ValidationEventHandler h, XmlSchema schema)
+               {
+                       if (ContentModel != null && BaseXmlSchemaTypeInternal != null)
+                               errorCount += BaseXmlSchemaTypeInternal.Validate (h, schema);
+               }
+
+               private void ValidateParticle (ValidationEventHandler h, XmlSchema schema)
+               {       
+                       if (ContentModel == null && Particle != null) {
+                               errorCount += particle.Validate (h, schema);
+                               XmlSchemaGroupRef pgrp = Particle as XmlSchemaGroupRef;
+                               if (pgrp != null) {
+                                       if (pgrp.TargetGroup != null)
+                                               errorCount += pgrp.TargetGroup.Validate (h,schema);
+                                       // otherwise, it might be missing sub components.
+                                       else if (!schema.IsNamespaceAbsent (pgrp.RefName.Namespace))
+                                               error (h, "Referenced group " + pgrp.RefName + " was not found in the corresponding schema.");
+                               }
+                       }
+               }
 
                private void ValidateContentModel (ValidationEventHandler h, XmlSchema schema)
                {
+                       errorCount += contentModel.Validate (h, schema);
+                       
                        XmlSchemaType baseType = BaseXmlSchemaTypeInternal;
 
                        // Here we check 3.4.6 Properties Correct :: 2. and 3.
-                       errorCount += contentModel.Validate (h, schema);
                        XmlSchemaComplexContentExtension cce = contentModel.Content as XmlSchemaComplexContentExtension;
                        XmlSchemaComplexContentRestriction ccr = contentModel.Content as XmlSchemaComplexContentRestriction;
                        XmlSchemaSimpleContentExtension sce = contentModel.Content as XmlSchemaSimpleContentExtension;
@@ -670,7 +700,6 @@ namespace System.Xml.Schema
                        if (ValidateRecursionCheck ())
                                error (h, "Circular definition of schema types was found.");
                        if (baseType != null) {
-                               baseType.Validate (h, schema);
                                // Fill "Datatype" property.
                                this.DatatypeInternal = baseType.Datatype;
                        } else if (BaseSchemaTypeName == XmlSchemaComplexType.AnyTypeName)
@@ -772,8 +801,6 @@ namespace System.Xml.Schema
                                // For ValidationEventHandler.
                                if (baseComplexType == null)
                                        baseComplexType = XmlSchemaComplexType.AnyType;
-                               if (ccr.Particle != null)
-                                       ccr.Particle.Validate (h, schema);
 
                                // attributes
                                localAnyAttribute = ccr.AnyAttribute;
@@ -830,11 +857,12 @@ namespace System.Xml.Schema
                        }
                        // complexType/simpleContent/restriction
                        if (scr != null) {
-                               // Attributes
-                               baseAnyAttribute = baseComplexType != null ? baseComplexType.AttributeWildcard : null;
-
+                               // attributes
                                localAnyAttribute = scr.AnyAttribute;
-                               if (localAnyAttribute != null && baseAnyAttribute != null)
+                               this.attributeWildcard = localAnyAttribute;
+                               if (baseComplexType != null)
+                                       baseAnyAttribute = baseComplexType.AttributeWildcard;
+                               if (baseAnyAttribute != null && localAnyAttribute != null)
                                        // 1.3 attribute wildcard subset. (=> 3.10.6)
                                        localAnyAttribute.ValidateWildcardSubset (baseAnyAttribute, h, schema);
                                // 3.4.6 :: 5.1. Beware that There is an errata for 5.1!!
@@ -845,6 +873,11 @@ namespace System.Xml.Schema
                                errorCount += XmlSchemaUtil.ValidateAttributesResolved (
                                        this.attributeUses, h, schema, scr.Attributes, 
                                        scr.AnyAttribute, ref attributeWildcard, null, false);
+                               foreach (DictionaryEntry entry in baseComplexType.AttributeUses) {
+                                       XmlSchemaAttribute attr = (XmlSchemaAttribute) entry.Value;
+                                       if (attributeUses [attr.QualifiedName] == null)
+                                               XmlSchemaUtil.AddToTable (attributeUses, attr, attr.QualifiedName, h);
+                               }
                        }
 
                        // Common process of AttributeWildcard.