2006-11-22 Atsushi Enomoto <atsushi@ximian.com>
[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                 // 1. name must be present
85                 // 2. MinOccurs & MaxOccurs of the Particle must be absent
86                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
87                 {
88                         // If this is already compiled this time, simply skip.
89                         if (CompilationId == schema.CompilationId)
90                                 return 0;
91
92 #if NET_2_0
93                         if (Particle != null)
94                                 Particle.Parent = this;
95 #endif
96
97                         if(Name == null)
98                                 error(h,"Required attribute name must be present");
99                         else if(!XmlSchemaUtil.CheckNCName(this.name)) 
100                                 error(h,"attribute name must be NCName");
101                         else
102                                 qualifiedName = new XmlQualifiedName(Name,schema.TargetNamespace);
103
104                         if(Particle == null)
105                         {
106                                 error(h,"Particle is required");
107                         }
108                         else 
109                         {
110                                 if(Particle.MaxOccursString != null)
111                                         Particle.error(h,"MaxOccurs must not be present when the Particle is a child of Group");
112                                 if(Particle.MinOccursString != null)
113                                         Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");
114                         
115                                 Particle.Compile (h, schema);
116                         }
117                         
118                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
119
120                         this.CompilationId = schema.CompilationId;
121                         return errorCount;
122                 }
123                 
124                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
125                 {
126                         if (this.IsValidated (schema.ValidationId))
127                                 return errorCount;
128
129                         // 3.8.6 Model Group Correct :: 2. Circular group disallowed.
130                         if (Particle != null) { // in case of invalid schema.
131                                 Particle.parentIsGroupDefinition = true;
132
133                                 try {
134                                         Particle.CheckRecursion (0, h, schema);
135                                 } catch (XmlSchemaException ex) {
136                                         error (h, ex.Message, ex);
137                                         this.isCircularDefinition = true;
138                                         return errorCount;
139                                 }
140                                 errorCount += Particle.Validate (h,schema);
141
142                                 Particle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),
143                                         new ArrayList (), h, schema);
144                                 Particle.ValidateUniqueTypeAttribution (
145                                         new XmlSchemaObjectTable (), h, schema);
146                         }
147
148                         this.ValidationId = schema.ValidationId;
149                         return errorCount;
150                 }
151
152                 //From the Errata
153                 //<group 
154                 //  id = ID
155                 //  name = NCName
156                 //  {any attributes with non-schema namespace . . .}>
157                 //  Content: (annotation?, (all | choice | sequence)?)
158                 //</group>
159                 internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
160                 {
161                         XmlSchemaGroup group = new XmlSchemaGroup();
162                         reader.MoveToElement();
163
164                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
165                         {
166                                 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
167                                 reader.Skip();
168                                 return null;
169                         }
170
171                         group.LineNumber = reader.LineNumber;
172                         group.LinePosition = reader.LinePosition;
173                         group.SourceUri = reader.BaseURI;
174
175                         while(reader.MoveToNextAttribute())
176                         {
177                                 if(reader.Name == "id")
178                                 {
179                                         group.Id = reader.Value;
180                                 }
181                                 else if(reader.Name == "name")
182                                 {
183                                         group.name = reader.Value;
184                                 }
185                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
186                                 {
187                                         error(h,reader.Name + " is not a valid attribute for group",null);
188                                 }
189                                 else
190                                 {
191                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,group);
192                                 }
193                         }
194                         
195                         reader.MoveToElement();
196                         if(reader.IsEmptyElement)
197                                 return group;
198
199 //                       Content: (annotation?, (all | choice | sequence)?)
200                         int level = 1;
201                         while(reader.ReadNextElement())
202                         {
203                                 if(reader.NodeType == XmlNodeType.EndElement)
204                                 {
205                                         if(reader.LocalName != xmlname)
206                                                 error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);
207                                         break;
208                                 }
209                                 if(level <= 1 && reader.LocalName == "annotation")
210                                 {
211                                         level = 2; //Only one annotation
212                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
213                                         if(annotation != null)
214                                                 group.Annotation = annotation;
215                                         continue;
216                                 }
217                                 if(level <= 2)
218                                 {
219                                         if(reader.LocalName == "all")
220                                         {
221                                                 level = 3;
222                                                 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
223                                                 if(all != null)
224                                                         group.Particle = all;
225                                                 continue;
226                                         }
227                                         if(reader.LocalName == "choice")
228                                         {
229                                                 level = 3;
230                                                 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
231                                                 if(choice != null)
232                                                         group.Particle = choice;
233                                                 continue;
234                                         }
235                                         if(reader.LocalName == "sequence")
236                                         {
237                                                 level = 3;
238                                                 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
239                                                 if(sequence != null)
240                                                         group.Particle = sequence;
241                                                 continue;
242                                         }
243                                 }
244                                 reader.RaiseInvalidElementError();
245                         }
246                         return group;
247                 }
248         }
249 }