2002-06-10 Dwivedi, Ajay kumar <adwiv@yahoo.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaGroup.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3 using System;\r
4 using System.Xml.Serialization;\r
5 using System.Xml;\r
6 \r
7 namespace System.Xml.Schema\r
8 {\r
9         /// <summary>\r
10         /// refers to the named group\r
11         /// </summary>\r
12         public class XmlSchemaGroup : XmlSchemaAnnotated\r
13         {\r
14                 private string name;\r
15                 private XmlSchemaGroupBase particle;\r
16                 private XmlQualifiedName qualifiedName;\r
17                 \r
18                 private static string xmlname = "group";\r
19 \r
20                 public XmlSchemaGroup()\r
21                 {\r
22                 }\r
23 \r
24                 [System.Xml.Serialization.XmlAttribute("name")]\r
25                 public string Name \r
26                 {\r
27                         get{ return  name; } \r
28                         set{ name = value; }\r
29                 }\r
30 \r
31                 [XmlElement("all",typeof(XmlSchemaAll),Namespace="http://www.w3.org/2001/XMLSchema")]\r
32                 [XmlElement("choice",typeof(XmlSchemaChoice),Namespace="http://www.w3.org/2001/XMLSchema")]\r
33                 [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace="http://www.w3.org/2001/XMLSchema")]\r
34                 public XmlSchemaGroupBase Particle\r
35                 {\r
36                         get{ return  particle; }\r
37                         set{ particle = value; }\r
38                 }\r
39 \r
40                 [XmlIgnore]\r
41                 internal XmlQualifiedName QualifiedName \r
42                 {\r
43                         get{ return qualifiedName;}\r
44                 }\r
45 \r
46                 // 1. name must be present\r
47                 [MonoTODO]\r
48                 internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)\r
49                 {\r
50                         if(Name == null)\r
51                                 error(h,"Required attribute name must be present");\r
52                         else if(!XmlSchemaUtil.CheckNCName(this.name)) \r
53                                 error(h,"attribute name must be NCName");\r
54                         else\r
55                                 qualifiedName = new XmlQualifiedName(Name,info.targetNS);\r
56                         \r
57                         if(Particle == null)\r
58                         {\r
59                                 error(h,"Particle is required");\r
60                         }\r
61                         else if(Particle is XmlSchemaChoice)\r
62                         {\r
63                                 errorCount += ((XmlSchemaChoice)Particle).Compile(h,info);\r
64                         }\r
65                         else if(Particle is XmlSchemaSequence)\r
66                         {\r
67                                 errorCount += ((XmlSchemaSequence)Particle).Compile(h,info);\r
68                         }\r
69                         else if(Particle is XmlSchemaAll)\r
70                         {\r
71                                 errorCount += ((XmlSchemaAll)Particle).Compile(h,info);\r
72                         }\r
73                         else\r
74                         {\r
75                                 error(h,"only all,choice or sequence are allowed");\r
76                         }\r
77 \r
78                         if(this.Id != null && !XmlSchemaUtil.CheckID(Id))\r
79                                 error(h, "id must be a valid ID");\r
80 \r
81                         return errorCount;\r
82                 }\r
83                 \r
84                 [MonoTODO]\r
85                 internal int Validate(ValidationEventHandler h)\r
86                 {\r
87                         return errorCount;\r
88                 }\r
89 \r
90                 //From the Errata\r
91                 //<group \r
92                 //  id = ID\r
93                 //  name = NCName\r
94                 //  {any attributes with non-schema namespace . . .}>\r
95                 //  Content: (annotation?, (all | choice | sequence)?)\r
96                 //</group>\r
97                 internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)\r
98                 {\r
99                         XmlSchemaGroup group = new XmlSchemaGroup();\r
100                         reader.MoveToElement();\r
101 \r
102                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
103                         {\r
104                                 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);\r
105                                 reader.Skip();\r
106                                 return null;\r
107                         }\r
108 \r
109                         group.LineNumber = reader.LineNumber;\r
110                         group.LinePosition = reader.LinePosition;\r
111                         group.SourceUri = reader.BaseURI;\r
112 \r
113                         while(reader.MoveToNextAttribute())\r
114                         {\r
115                                 if(reader.Name == "id")\r
116                                 {\r
117                                         group.Id = reader.Value;\r
118                                 }\r
119                                 else if(reader.Name == "name")\r
120                                 {\r
121                                         group.name = reader.Value;\r
122                                 }\r
123                                 else if(reader.NamespaceURI == "" || reader.NamespaceURI == XmlSchema.Namespace)\r
124                                 {\r
125                                         error(h,reader.Name + " is not a valid attribute for group",null);\r
126                                 }\r
127                                 else\r
128                                 {\r
129                                         //TODO: Add to Unhandled attributes\r
130                                 }\r
131                         }\r
132                         \r
133                         reader.MoveToElement();\r
134                         if(reader.IsEmptyElement)\r
135                                 return group;\r
136 \r
137 //                       Content: (annotation?, (all | choice | sequence)?)\r
138                         int level = 1;\r
139                         while(reader.ReadNextElement())\r
140                         {\r
141                                 if(reader.NodeType == XmlNodeType.EndElement)\r
142                                 {\r
143                                         if(reader.LocalName != xmlname)\r
144                                                 error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);\r
145                                         break;\r
146                                 }\r
147                                 if(level <= 1 && reader.LocalName == "annotation")\r
148                                 {\r
149                                         level = 2; //Only one annotation\r
150                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
151                                         if(annotation != null)\r
152                                                 group.Annotation = annotation;\r
153                                         continue;\r
154                                 }\r
155                                 if(level <= 2)\r
156                                 {\r
157                                         if(reader.LocalName == "all")\r
158                                         {\r
159                                                 level = 3;\r
160                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);\r
161                                                 if(all != null)\r
162                                                         group.Particle = all;\r
163                                                 continue;\r
164                                         }\r
165                                         if(reader.LocalName == "choice")\r
166                                         {\r
167                                                 level = 3;\r
168                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);\r
169                                                 if(choice != null)\r
170                                                         group.Particle = choice;\r
171                                                 continue;\r
172                                         }\r
173                                         if(reader.LocalName == "sequence")\r
174                                         {\r
175                                                 level = 3;\r
176                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);\r
177                                                 if(sequence != null)\r
178                                                         group.Particle = sequence;\r
179                                                 continue;\r
180                                         }\r
181                                 }\r
182                                 reader.RaiseInvalidElementError();\r
183                         }\r
184                         return group;\r
185                 }\r
186         }\r
187 }\r