**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaGroup.cs
blob63b5656f8dc32702b32864be5ef88349e5eda398
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 //
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.
29 using System;
30 using System.Collections;
31 using System.Xml.Serialization;
32 using System.Xml;
34 namespace System.Xml.Schema
36 /// <summary>
37 /// refers to the named group
38 /// </summary>
39 public class XmlSchemaGroup : XmlSchemaAnnotated
41 private string name;
42 private XmlSchemaGroupBase particle;
43 private XmlQualifiedName qualifiedName;
44 private bool isCircularDefinition;
46 const string xmlname = "group";
48 public XmlSchemaGroup()
50 qualifiedName = XmlQualifiedName.Empty;
53 [System.Xml.Serialization.XmlAttribute("name")]
54 public string Name
56 get{ return name; }
57 set{ name = value; }
60 [XmlElement("all",typeof(XmlSchemaAll),Namespace=XmlSchema.Namespace)]
61 [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]
62 [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]
63 public XmlSchemaGroupBase Particle
65 get{ return particle; }
66 set{ particle = value; }
69 [XmlIgnore]
70 #if NET_2_0
71 public XmlQualifiedName QualifiedName
72 #else
73 internal XmlQualifiedName QualifiedName
74 #endif
76 get{ return qualifiedName;}
79 internal bool IsCircularDefinition
81 get { return isCircularDefinition; }
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)
88 // If this is already compiled this time, simply skip.
89 if (this.IsComplied (schema.CompilationId))
90 return 0;
92 if(Name == null)
93 error(h,"Required attribute name must be present");
94 else if(!XmlSchemaUtil.CheckNCName(this.name))
95 error(h,"attribute name must be NCName");
96 else
97 qualifiedName = new XmlQualifiedName(Name,schema.TargetNamespace);
99 if(Particle == null)
101 error(h,"Particle is required");
103 else
105 if(Particle.MaxOccursString != null)
106 Particle.error(h,"MaxOccurs must not be present when the Particle is a child of Group");
107 if(Particle.MinOccursString != null)
108 Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");
110 Particle.Compile (h, schema);
113 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
115 this.CompilationId = schema.CompilationId;
116 return errorCount;
119 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
121 if (this.IsValidated (schema.ValidationId))
122 return errorCount;
124 // 3.8.6 Model Group Correct :: 2. Circular group disallowed.
125 if (Particle != null) { // in case of invalid schema.
126 Particle.parentIsGroupDefinition = true;
128 try {
129 Particle.CheckRecursion (0, h, schema);
130 } catch (XmlSchemaException ex) {
131 error (h, ex.Message, ex);
132 this.isCircularDefinition = true;
133 return errorCount;
135 errorCount += Particle.Validate (h,schema);
137 Particle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),
138 new ArrayList (), h, schema);
139 Particle.ValidateUniqueTypeAttribution (
140 new XmlSchemaObjectTable (), h, schema);
143 this.ValidationId = schema.ValidationId;
144 return errorCount;
147 //From the Errata
148 //<group
149 // id = ID
150 // name = NCName
151 // {any attributes with non-schema namespace . . .}>
152 // Content: (annotation?, (all | choice | sequence)?)
153 //</group>
154 internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
156 XmlSchemaGroup group = new XmlSchemaGroup();
157 reader.MoveToElement();
159 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
161 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
162 reader.Skip();
163 return null;
166 group.LineNumber = reader.LineNumber;
167 group.LinePosition = reader.LinePosition;
168 group.SourceUri = reader.BaseURI;
170 while(reader.MoveToNextAttribute())
172 if(reader.Name == "id")
174 group.Id = reader.Value;
176 else if(reader.Name == "name")
178 group.name = reader.Value;
180 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
182 error(h,reader.Name + " is not a valid attribute for group",null);
184 else
186 XmlSchemaUtil.ReadUnhandledAttribute(reader,group);
190 reader.MoveToElement();
191 if(reader.IsEmptyElement)
192 return group;
194 // Content: (annotation?, (all | choice | sequence)?)
195 int level = 1;
196 while(reader.ReadNextElement())
198 if(reader.NodeType == XmlNodeType.EndElement)
200 if(reader.LocalName != xmlname)
201 error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);
202 break;
204 if(level <= 1 && reader.LocalName == "annotation")
206 level = 2; //Only one annotation
207 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
208 if(annotation != null)
209 group.Annotation = annotation;
210 continue;
212 if(level <= 2)
214 if(reader.LocalName == "all")
216 level = 3;
217 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
218 if(all != null)
219 group.Particle = all;
220 continue;
222 if(reader.LocalName == "choice")
224 level = 3;
225 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
226 if(choice != null)
227 group.Particle = choice;
228 continue;
230 if(reader.LocalName == "sequence")
232 level = 3;
233 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
234 if(sequence != null)
235 group.Particle = sequence;
236 continue;
239 reader.RaiseInvalidElementError();
241 return group;