2 // System.Xml.Schema.XmlSchemaGroup.cs
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.Xml.Serialization;
34 namespace System.Xml.Schema
37 /// refers to the named group
39 public class XmlSchemaGroup : XmlSchemaAnnotated
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")]
60 [XmlElement("all",typeof(XmlSchemaAll))]
61 [XmlElement("choice",typeof(XmlSchemaChoice))]
62 [XmlElement("sequence",typeof(XmlSchemaSequence))]
63 public XmlSchemaGroupBase Particle
65 get{ return particle; }
66 set{ particle = value; }
71 public XmlQualifiedName QualifiedName
73 internal XmlQualifiedName QualifiedName
76 get{ return qualifiedName;}
79 internal bool IsCircularDefinition
81 get { return isCircularDefinition; }
84 internal override void SetParent (XmlSchemaObject parent)
86 base.SetParent (parent);
88 Particle.SetParent (this);
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)
95 // If this is already compiled this time, simply skip.
96 if (CompilationId == schema.CompilationId)
100 error(h,"Required attribute name must be present");
101 else if(!XmlSchemaUtil.CheckNCName(this.name))
102 error(h,"attribute name must be NCName");
104 qualifiedName = new XmlQualifiedName(Name, AncestorSchema.TargetNamespace);
108 error(h,"Particle is required");
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");
117 Particle.Compile (h, schema);
120 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
122 this.CompilationId = schema.CompilationId;
126 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
128 if (this.IsValidated (schema.ValidationId))
131 // 3.8.6 Model Group Correct :: 2. Circular group disallowed.
132 if (Particle != null) { // in case of invalid schema.
133 Particle.parentIsGroupDefinition = true;
136 Particle.CheckRecursion (0, h, schema);
137 } catch (XmlSchemaException ex) {
138 error (h, ex.Message, ex);
139 this.isCircularDefinition = true;
142 errorCount += Particle.Validate (h,schema);
144 Particle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),
145 new ArrayList (), h, schema);
146 Particle.ValidateUniqueTypeAttribution (
147 new XmlSchemaObjectTable (), h, schema);
150 this.ValidationId = schema.ValidationId;
158 // {any attributes with non-schema namespace . . .}>
159 // Content: (annotation?, (all | choice | sequence)?)
161 internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
163 XmlSchemaGroup group = new XmlSchemaGroup();
164 reader.MoveToElement();
166 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
168 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
173 group.LineNumber = reader.LineNumber;
174 group.LinePosition = reader.LinePosition;
175 group.SourceUri = reader.BaseURI;
177 while(reader.MoveToNextAttribute())
179 if(reader.Name == "id")
181 group.Id = reader.Value;
183 else if(reader.Name == "name")
185 group.name = reader.Value;
187 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
189 error(h,reader.Name + " is not a valid attribute for group",null);
193 XmlSchemaUtil.ReadUnhandledAttribute(reader,group);
197 reader.MoveToElement();
198 if(reader.IsEmptyElement)
201 // Content: (annotation?, (all | choice | sequence)?)
203 while(reader.ReadNextElement())
205 if(reader.NodeType == XmlNodeType.EndElement)
207 if(reader.LocalName != xmlname)
208 error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);
211 if(level <= 1 && reader.LocalName == "annotation")
213 level = 2; //Only one annotation
214 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
215 if(annotation != null)
216 group.Annotation = annotation;
221 if(reader.LocalName == "all")
224 XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
226 group.Particle = all;
229 if(reader.LocalName == "choice")
232 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
234 group.Particle = choice;
237 if(reader.LocalName == "sequence")
240 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
242 group.Particle = sequence;
246 reader.RaiseInvalidElementError();