Handle more type conversion.
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaGroup.cs
1 //
2 // System.Xml.Schema.XmlSchemaGroup.cs
3 //
4 // Author:
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com
6 //      Atsushi Enomoto  ginga@kit.hi-ho.ne.jp
7 //
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.Collections;
31 using System.Xml.Serialization;
32 using System.Xml;
33
34 namespace System.Xml.Schema
35 {
36         /// <summary>
37         /// refers to the named group
38         /// </summary>
39         public class XmlSchemaGroup : XmlSchemaAnnotated
40         {
41                 private string name;
42                 private XmlSchemaGroupBase particle;
43                 private XmlQualifiedName qualifiedName;
44                 private bool isCircularDefinition;
45                 
46                 const string xmlname = "group";
47
48                 public XmlSchemaGroup()
49                 {
50                         qualifiedName = XmlQualifiedName.Empty;
51                 }
52
53                 [System.Xml.Serialization.XmlAttribute("name")]
54                 public string Name 
55                 {
56                         get{ return  name; } 
57                         set{ name = value; }
58                 }
59
60                 [XmlElement("all",typeof(XmlSchemaAll))]
61                 [XmlElement("choice",typeof(XmlSchemaChoice))]
62                 [XmlElement("sequence",typeof(XmlSchemaSequence))]
63                 public XmlSchemaGroupBase Particle
64                 {
65                         get{ return  particle; }
66                         set{ particle = value; }
67                 }
68
69                 [XmlIgnore]
70 #if NET_2_0
71                 public XmlQualifiedName QualifiedName 
72 #else
73                 internal XmlQualifiedName QualifiedName 
74 #endif
75                 {
76                         get{ return qualifiedName;}
77                 }
78
79                 internal bool IsCircularDefinition
80                 {
81                         get { return isCircularDefinition; }
82                 }
83
84                 internal override void SetParent (XmlSchemaObject parent)
85                 {
86                         base.SetParent (parent);
87                         if (Particle != null)
88                                 Particle.SetParent (this);
89                 }
90
91                 // 1. name must be present
92                 // 2. MinOccurs & MaxOccurs of the Particle must be absent
93                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
94                 {
95                         // If this is already compiled this time, simply skip.
96                         if (CompilationId == schema.CompilationId)
97                                 return 0;
98
99                         if(Name == null)
100                                 error(h,"Required attribute name must be present");
101                         else if(!XmlSchemaUtil.CheckNCName(this.name)) 
102                                 error(h,"attribute name must be NCName");
103                         else
104                                 qualifiedName = new XmlQualifiedName(Name, AncestorSchema.TargetNamespace);
105
106                         if(Particle == null)
107                         {
108                                 error(h,"Particle is required");
109                         }
110                         else 
111                         {
112                                 if(Particle.MaxOccursString != null)
113                                         Particle.error(h,"MaxOccurs must not be present when the Particle is a child of Group");
114                                 if(Particle.MinOccursString != null)
115                                         Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");
116                         
117                                 Particle.Compile (h, schema);
118                         }
119                         
120                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
121
122                         this.CompilationId = schema.CompilationId;
123                         return errorCount;
124                 }
125                 
126                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
127                 {
128                         if (this.IsValidated (schema.ValidationId))
129                                 return errorCount;
130
131                         // 3.8.6 Model Group Correct :: 2. Circular group disallowed.
132                         if (Particle != null) { // in case of invalid schema.
133                                 Particle.parentIsGroupDefinition = true;
134
135                                 try {
136                                         Particle.CheckRecursion (0, h, schema);
137                                 } catch (XmlSchemaException ex) {
138                                         error (h, ex.Message, ex);
139                                         this.isCircularDefinition = true;
140                                         return errorCount;
141                                 }
142                                 errorCount += Particle.Validate (h,schema);
143
144                                 Particle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),
145                                         new ArrayList (), h, schema);
146                                 Particle.ValidateUniqueTypeAttribution (
147                                         new XmlSchemaObjectTable (), h, schema);
148                         }
149
150                         this.ValidationId = schema.ValidationId;
151                         return errorCount;
152                 }
153
154                 //From the Errata
155                 //<group 
156                 //  id = ID
157                 //  name = NCName
158                 //  {any attributes with non-schema namespace . . .}>
159                 //  Content: (annotation?, (all | choice | sequence)?)
160                 //</group>
161                 internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
162                 {
163                         XmlSchemaGroup group = new XmlSchemaGroup();
164                         reader.MoveToElement();
165
166                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
167                         {
168                                 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
169                                 reader.Skip();
170                                 return null;
171                         }
172
173                         group.LineNumber = reader.LineNumber;
174                         group.LinePosition = reader.LinePosition;
175                         group.SourceUri = reader.BaseURI;
176
177                         while(reader.MoveToNextAttribute())
178                         {
179                                 if(reader.Name == "id")
180                                 {
181                                         group.Id = reader.Value;
182                                 }
183                                 else if(reader.Name == "name")
184                                 {
185                                         group.name = reader.Value;
186                                 }
187                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
188                                 {
189                                         error(h,reader.Name + " is not a valid attribute for group",null);
190                                 }
191                                 else
192                                 {
193                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,group);
194                                 }
195                         }
196                         
197                         reader.MoveToElement();
198                         if(reader.IsEmptyElement)
199                                 return group;
200
201 //                       Content: (annotation?, (all | choice | sequence)?)
202                         int level = 1;
203                         while(reader.ReadNextElement())
204                         {
205                                 if(reader.NodeType == XmlNodeType.EndElement)
206                                 {
207                                         if(reader.LocalName != xmlname)
208                                                 error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);
209                                         break;
210                                 }
211                                 if(level <= 1 && reader.LocalName == "annotation")
212                                 {
213                                         level = 2; //Only one annotation
214                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
215                                         if(annotation != null)
216                                                 group.Annotation = annotation;
217                                         continue;
218                                 }
219                                 if(level <= 2)
220                                 {
221                                         if(reader.LocalName == "all")
222                                         {
223                                                 level = 3;
224                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
225                                                 if(all != null)
226                                                         group.Particle = all;
227                                                 continue;
228                                         }
229                                         if(reader.LocalName == "choice")
230                                         {
231                                                 level = 3;
232                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
233                                                 if(choice != null)
234                                                         group.Particle = choice;
235                                                 continue;
236                                         }
237                                         if(reader.LocalName == "sequence")
238                                         {
239                                                 level = 3;
240                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
241                                                 if(sequence != null)
242                                                         group.Particle = sequence;
243                                                 continue;
244                                         }
245                                 }
246                                 reader.RaiseInvalidElementError();
247                         }
248                         return group;
249                 }
250         }
251 }