2002-12-24 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[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                 // 2. MinOccurs & MaxOccurs of the Particle must be absent\r
48                 [MonoTODO]\r
49                 internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)\r
50                 {\r
51                         if(Name == null)\r
52                                 error(h,"Required attribute name must be present");\r
53                         else if(!XmlSchemaUtil.CheckNCName(this.name)) \r
54                                 error(h,"attribute name must be NCName");\r
55                         else\r
56                                 qualifiedName = new XmlQualifiedName(Name,info.TargetNamespace);\r
57 \r
58                         if(Particle == null)\r
59                         {\r
60                                 error(h,"Particle is required");\r
61                         }\r
62                         else \r
63                         {\r
64                                 if(Particle.MaxOccursString != null)\r
65                                         Particle.error(h,"MaxOccurs must not be present when the Particle is a child of Group");\r
66                                 if(Particle.MinOccursString != null)\r
67                                         Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");\r
68                         \r
69                                 if(Particle is XmlSchemaChoice)\r
70                                 {\r
71                                         errorCount += ((XmlSchemaChoice)Particle).Compile(h,info);\r
72                                 }\r
73                                 else if(Particle is XmlSchemaSequence)\r
74                                 {\r
75                                         errorCount += ((XmlSchemaSequence)Particle).Compile(h,info);\r
76                                 }\r
77                                 else if(Particle is XmlSchemaAll)\r
78                                 {\r
79                                         errorCount += ((XmlSchemaAll)Particle).Compile(h,info);\r
80                                 }\r
81                                 else\r
82                                 {\r
83                                         error(h,"only all,choice or sequence are allowed");\r
84                                 }\r
85                         }\r
86                         \r
87                         XmlSchemaUtil.CompileID(Id,this,info.IDCollection,h);\r
88 \r
89                         return errorCount;\r
90                 }\r
91                 \r
92                 [MonoTODO]\r
93                 internal int Validate(ValidationEventHandler h)\r
94                 {\r
95                         return errorCount;\r
96                 }\r
97 \r
98                 //From the Errata\r
99                 //<group \r
100                 //  id = ID\r
101                 //  name = NCName\r
102                 //  {any attributes with non-schema namespace . . .}>\r
103                 //  Content: (annotation?, (all | choice | sequence)?)\r
104                 //</group>\r
105                 internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)\r
106                 {\r
107                         XmlSchemaGroup group = new XmlSchemaGroup();\r
108                         reader.MoveToElement();\r
109 \r
110                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
111                         {\r
112                                 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);\r
113                                 reader.Skip();\r
114                                 return null;\r
115                         }\r
116 \r
117                         group.LineNumber = reader.LineNumber;\r
118                         group.LinePosition = reader.LinePosition;\r
119                         group.SourceUri = reader.BaseURI;\r
120 \r
121                         while(reader.MoveToNextAttribute())\r
122                         {\r
123                                 if(reader.Name == "id")\r
124                                 {\r
125                                         group.Id = reader.Value;\r
126                                 }\r
127                                 else if(reader.Name == "name")\r
128                                 {\r
129                                         group.name = reader.Value;\r
130                                 }\r
131                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
132                                 {\r
133                                         error(h,reader.Name + " is not a valid attribute for group",null);\r
134                                 }\r
135                                 else\r
136                                 {\r
137                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,group);\r
138                                 }\r
139                         }\r
140                         \r
141                         reader.MoveToElement();\r
142                         if(reader.IsEmptyElement)\r
143                                 return group;\r
144 \r
145 //                       Content: (annotation?, (all | choice | sequence)?)\r
146                         int level = 1;\r
147                         while(reader.ReadNextElement())\r
148                         {\r
149                                 if(reader.NodeType == XmlNodeType.EndElement)\r
150                                 {\r
151                                         if(reader.LocalName != xmlname)\r
152                                                 error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);\r
153                                         break;\r
154                                 }\r
155                                 if(level <= 1 && reader.LocalName == "annotation")\r
156                                 {\r
157                                         level = 2; //Only one annotation\r
158                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
159                                         if(annotation != null)\r
160                                                 group.Annotation = annotation;\r
161                                         continue;\r
162                                 }\r
163                                 if(level <= 2)\r
164                                 {\r
165                                         if(reader.LocalName == "all")\r
166                                         {\r
167                                                 level = 3;\r
168                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);\r
169                                                 if(all != null)\r
170                                                         group.Particle = all;\r
171                                                 continue;\r
172                                         }\r
173                                         if(reader.LocalName == "choice")\r
174                                         {\r
175                                                 level = 3;\r
176                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);\r
177                                                 if(choice != null)\r
178                                                         group.Particle = choice;\r
179                                                 continue;\r
180                                         }\r
181                                         if(reader.LocalName == "sequence")\r
182                                         {\r
183                                                 level = 3;\r
184                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);\r
185                                                 if(sequence != null)\r
186                                                         group.Particle = sequence;\r
187                                                 continue;\r
188                                         }\r
189                                 }\r
190                                 reader.RaiseInvalidElementError();\r
191                         }\r
192                         return group;\r
193                 }\r
194         }\r
195 }\r