2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaGroup.cs
index 5e2d7a16a35a267ef0a38d65deebfa77f5d62480..d308b3f8668b50f35bc85bd68177808d1ac2ff93 100755 (executable)
-// Author: Dwivedi, Ajay kumar\r
-//            Adwiv@Yahoo.com\r
+//\r
+// System.Xml.Schema.XmlSchemaGroup.cs\r
+//\r
+// Author:\r
+//     Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
+//     Atsushi Enomoto  ginga@kit.hi-ho.ne.jp\r
+//\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 using System;\r
+using System.Collections;\r
 using System.Xml.Serialization;\r
+using System.Xml;\r
 \r
 namespace System.Xml.Schema\r
 {\r
        /// <summary>\r
-       /// Summary description for XmlSchemaGroup.\r
+       /// refers to the named group\r
        /// </summary>\r
        public class XmlSchemaGroup : XmlSchemaAnnotated\r
        {\r
                private string name;\r
                private XmlSchemaGroupBase particle;\r
+               private XmlQualifiedName qualifiedName;\r
+               private bool isCircularDefinition;\r
+               \r
+               const string xmlname = "group";\r
 \r
                public XmlSchemaGroup()\r
                {\r
-                       name = string.Empty;\r
+                       qualifiedName = XmlQualifiedName.Empty;\r
                }\r
-               [XmlAttribute]\r
+\r
+               [System.Xml.Serialization.XmlAttribute("name")]\r
                public string Name \r
                {\r
                        get{ return  name; } \r
                        set{ name = value; }\r
                }\r
-               [XmlElement]\r
+\r
+               [XmlElement("all",typeof(XmlSchemaAll),Namespace=XmlSchema.Namespace)]\r
+               [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]\r
+               [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]\r
                public XmlSchemaGroupBase Particle\r
                {\r
                        get{ return  particle; }\r
                        set{ particle = value; }\r
                }\r
+\r
+               [XmlIgnore]\r
+#if NET_2_0\r
+               public XmlQualifiedName QualifiedName \r
+#else\r
+               internal XmlQualifiedName QualifiedName \r
+#endif\r
+               {\r
+                       get{ return qualifiedName;}\r
+               }\r
+\r
+               internal bool IsCircularDefinition\r
+               {\r
+                       get { return isCircularDefinition; }\r
+               }\r
+\r
+               // 1. name must be present\r
+               // 2. MinOccurs & MaxOccurs of the Particle must be absent\r
+               internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
+               {\r
+                       // If this is already compiled this time, simply skip.\r
+                       if (this.IsComplied (schema.CompilationId))\r
+                               return 0;\r
+\r
+#if NET_2_0\r
+                       if (Particle != null)\r
+                               Particle.Parent = this;\r
+#endif\r
+\r
+                       if(Name == null)\r
+                               error(h,"Required attribute name must be present");\r
+                       else if(!XmlSchemaUtil.CheckNCName(this.name)) \r
+                               error(h,"attribute name must be NCName");\r
+                       else\r
+                               qualifiedName = new XmlQualifiedName(Name,schema.TargetNamespace);\r
+\r
+                       if(Particle == null)\r
+                       {\r
+                               error(h,"Particle is required");\r
+                       }\r
+                       else \r
+                       {\r
+                               if(Particle.MaxOccursString != null)\r
+                                       Particle.error(h,"MaxOccurs must not be present when the Particle is a child of Group");\r
+                               if(Particle.MinOccursString != null)\r
+                                       Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");\r
+                       \r
+                               Particle.Compile (h, schema);\r
+                       }\r
+                       \r
+                       XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
+\r
+                       this.CompilationId = schema.CompilationId;\r
+                       return errorCount;\r
+               }\r
+               \r
+               internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
+               {\r
+                       if (this.IsValidated (schema.ValidationId))\r
+                               return errorCount;\r
+\r
+                       // 3.8.6 Model Group Correct :: 2. Circular group disallowed.\r
+                       if (Particle != null) { // in case of invalid schema.\r
+                               Particle.parentIsGroupDefinition = true;\r
+\r
+                               try {\r
+                                       Particle.CheckRecursion (0, h, schema);\r
+                               } catch (XmlSchemaException ex) {\r
+                                       error (h, ex.Message, ex);\r
+                                       this.isCircularDefinition = true;\r
+                                       return errorCount;\r
+                               }\r
+                               errorCount += Particle.Validate (h,schema);\r
+\r
+                               Particle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),\r
+                                       new ArrayList (), h, schema);\r
+                               Particle.ValidateUniqueTypeAttribution (\r
+                                       new XmlSchemaObjectTable (), h, schema);\r
+                       }\r
+\r
+                       this.ValidationId = schema.ValidationId;\r
+                       return errorCount;\r
+               }\r
+\r
+               //From the Errata\r
+               //<group \r
+               //  id = ID\r
+               //  name = NCName\r
+               //  {any attributes with non-schema namespace . . .}>\r
+               //  Content: (annotation?, (all | choice | sequence)?)\r
+               //</group>\r
+               internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)\r
+               {\r
+                       XmlSchemaGroup group = new XmlSchemaGroup();\r
+                       reader.MoveToElement();\r
+\r
+                       if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
+                       {\r
+                               error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);\r
+                               reader.Skip();\r
+                               return null;\r
+                       }\r
+\r
+                       group.LineNumber = reader.LineNumber;\r
+                       group.LinePosition = reader.LinePosition;\r
+                       group.SourceUri = reader.BaseURI;\r
+\r
+                       while(reader.MoveToNextAttribute())\r
+                       {\r
+                               if(reader.Name == "id")\r
+                               {\r
+                                       group.Id = reader.Value;\r
+                               }\r
+                               else if(reader.Name == "name")\r
+                               {\r
+                                       group.name = reader.Value;\r
+                               }\r
+                               else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
+                               {\r
+                                       error(h,reader.Name + " is not a valid attribute for group",null);\r
+                               }\r
+                               else\r
+                               {\r
+                                       XmlSchemaUtil.ReadUnhandledAttribute(reader,group);\r
+                               }\r
+                       }\r
+                       \r
+                       reader.MoveToElement();\r
+                       if(reader.IsEmptyElement)\r
+                               return group;\r
+\r
+//                      Content: (annotation?, (all | choice | sequence)?)\r
+                       int level = 1;\r
+                       while(reader.ReadNextElement())\r
+                       {\r
+                               if(reader.NodeType == XmlNodeType.EndElement)\r
+                               {\r
+                                       if(reader.LocalName != xmlname)\r
+                                               error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);\r
+                                       break;\r
+                               }\r
+                               if(level <= 1 && reader.LocalName == "annotation")\r
+                               {\r
+                                       level = 2; //Only one annotation\r
+                                       XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
+                                       if(annotation != null)\r
+                                               group.Annotation = annotation;\r
+                                       continue;\r
+                               }\r
+                               if(level <= 2)\r
+                               {\r
+                                       if(reader.LocalName == "all")\r
+                                       {\r
+                                               level = 3;\r
+                                               XmlSchemaAll all = XmlSchemaAll.Read(reader,h);\r
+                                               if(all != null)\r
+                                                       group.Particle = all;\r
+                                               continue;\r
+                                       }\r
+                                       if(reader.LocalName == "choice")\r
+                                       {\r
+                                               level = 3;\r
+                                               XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);\r
+                                               if(choice != null)\r
+                                                       group.Particle = choice;\r
+                                               continue;\r
+                                       }\r
+                                       if(reader.LocalName == "sequence")\r
+                                       {\r
+                                               level = 3;\r
+                                               XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);\r
+                                               if(sequence != null)\r
+                                                       group.Particle = sequence;\r
+                                               continue;\r
+                                       }\r
+                               }\r
+                               reader.RaiseInvalidElementError();\r
+                       }\r
+                       return group;\r
+               }\r
        }\r
 }\r