2 // System.Xml.Schema.XmlSchemaSequence.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 /// Summary description for XmlSchemaSequence.
39 public class XmlSchemaSequence : XmlSchemaGroupBase
41 private XmlSchemaObjectCollection items;
42 const string xmlname = "sequence";
44 public XmlSchemaSequence()
46 items = new XmlSchemaObjectCollection();
49 [XmlElement("element",typeof(XmlSchemaElement))]
50 [XmlElement("group",typeof(XmlSchemaGroupRef))]
51 [XmlElement("choice",typeof(XmlSchemaChoice))]
52 [XmlElement("sequence",typeof(XmlSchemaSequence))]
53 [XmlElement("any",typeof(XmlSchemaAny))]
54 public override XmlSchemaObjectCollection Items
59 internal override void SetParent (XmlSchemaObject parent)
61 base.SetParent (parent);
62 foreach (XmlSchemaObject obj in Items)
66 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
68 // If this is already compiled this time, simply skip.
69 if (CompilationId == schema.CompilationId)
72 XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
73 CompileOccurence (h, schema);
75 foreach(XmlSchemaObject obj in Items)
77 if(obj is XmlSchemaElement ||
78 obj is XmlSchemaGroupRef ||
79 obj is XmlSchemaChoice ||
80 obj is XmlSchemaSequence ||
83 errorCount += obj.Compile(h,schema);
86 error(h, "Invalid schema object was specified in the particles of the sequence model group.");
88 this.CompilationId = schema.CompilationId;
93 internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
95 if (OptimizedParticle != null)
96 return OptimizedParticle;
97 if (Items.Count == 0 || ValidatedMaxOccurs == 0) {
98 OptimizedParticle = XmlSchemaParticle.Empty;
99 return OptimizedParticle;
101 if (!isTop && ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1) {
102 if (Items.Count == 1)
103 return ((XmlSchemaParticle) Items [0]).GetOptimizedParticle (false);
106 XmlSchemaSequence seq = new XmlSchemaSequence ();
108 for (int i = 0; i < Items.Count; i++) {
109 XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
110 p = p.GetOptimizedParticle (false);
111 if (p == XmlSchemaParticle.Empty)
114 else if (p is XmlSchemaSequence && p.ValidatedMinOccurs == 1 && p.ValidatedMaxOccurs == 1) {
115 XmlSchemaSequence ps = p as XmlSchemaSequence;
116 for (int pi = 0; pi < ps.Items.Count; pi++) {
117 seq.Items.Add (ps.Items [pi]);
118 seq.CompiledItems.Add (ps.Items [pi]);
123 seq.CompiledItems.Add (p);
126 if (seq.Items.Count == 0)
127 OptimizedParticle = XmlSchemaParticle.Empty;
129 OptimizedParticle = seq;
130 return OptimizedParticle;
133 internal override int Validate (ValidationEventHandler h, XmlSchema schema)
135 if (IsValidated (schema.CompilationId))
138 CompiledItems.Clear ();
139 foreach (XmlSchemaParticle p in Items) {
140 errorCount += p.Validate (h, schema); // This is basically extraneous for pointless item, but needed to check validation error.
141 // XmlSchemaParticle particleInPoint = p.GetParticleWithoutPointless ();
142 // if (particleInPoint != XmlSchemaParticle.Empty)
143 // CompiledItems.Add (particleInPoint);
144 CompiledItems.Add (p);
147 ValidationId = schema.ValidationId;
151 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
152 ValidationEventHandler h, XmlSchema schema, bool raiseError)
154 if (this == baseParticle) // quick check
157 XmlSchemaElement el = baseParticle as XmlSchemaElement;
161 error (h, "Invalid sequence paricle derivation.");
165 XmlSchemaSequence seq = baseParticle as XmlSchemaSequence;
168 if (!ValidateOccurenceRangeOK (seq, h, schema, raiseError))
171 // If it is totally optional, then ignore their contents.
172 if (seq.ValidatedMinOccurs == 0 && seq.ValidatedMaxOccurs == 0 &&
173 this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
175 return ValidateRecurse (seq, h, schema, raiseError);
178 XmlSchemaAll all = baseParticle as XmlSchemaAll;
181 XmlSchemaObjectCollection already = new XmlSchemaObjectCollection ();
182 for (int i = 0; i < this.Items.Count; i++) {
183 XmlSchemaElement de = this.Items [i] as XmlSchemaElement;
186 error (h, "Invalid sequence particle derivation by restriction from all.");
189 foreach (XmlSchemaElement e in all.Items) {
190 if (e.QualifiedName == de.QualifiedName) {
191 if (already.Contains (e)) {
193 error (h, "Base element particle is mapped to the derived element particle in a sequence two or more times.");
197 if (!de.ValidateDerivationByRestriction (e, h, schema, raiseError))
203 foreach (XmlSchemaElement e in all.Items)
204 if (!already.Contains (e))
205 if (!e.ValidateIsEmptiable ()) {
207 error (h, "In base -all- particle, mapping-skipped base element which is not emptiable was found.");
212 XmlSchemaAny any = baseParticle as XmlSchemaAny;
214 // NSRecurseCheckCardinality
215 return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
217 XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
218 if (choice != null) {
220 // In fact it is not Recurse, but it looks almost common.
221 return ValidateSeqRecurseMapSumCommon (choice, h, schema, false, true, raiseError);
226 internal override decimal GetMinEffectiveTotalRange ()
228 return GetMinEffectiveTotalRangeAllAndSequence ();
231 internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
232 ValidationEventHandler h, XmlSchema schema)
234 ValidateUPAOnHeadingOptionalComponents (qnames, nsNames, h, schema);
235 ValidateUPAOnItems (qnames, nsNames, h, schema);
238 void ValidateUPAOnHeadingOptionalComponents (XmlSchemaObjectTable qnames, ArrayList nsNames,
239 ValidationEventHandler h, XmlSchema schema)
241 // heading optional components
242 foreach (XmlSchemaParticle p in this.Items) {
243 p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
244 if (p.ValidatedMinOccurs != 0)
249 void ValidateUPAOnItems (XmlSchemaObjectTable qnames, ArrayList nsNames,
250 ValidationEventHandler h, XmlSchema schema)
252 // non-optional components
253 XmlSchemaObjectTable elems = new XmlSchemaObjectTable ();
254 ArrayList wildcards = new ArrayList ();
255 XmlSchemaObjectTable tmpElems = new XmlSchemaObjectTable ();
256 ArrayList tmpWildcards = new ArrayList ();
257 for (int i=0; i<Items.Count; i++) {
258 XmlSchemaParticle p1 = Items [i] as XmlSchemaParticle;
259 p1.ValidateUniqueParticleAttribution (elems, wildcards, h, schema);
260 if (p1.ValidatedMinOccurs == p1.ValidatedMaxOccurs) {
265 if (p1.ValidatedMinOccurs != 0) {
266 foreach (XmlQualifiedName n in tmpElems.Names)
267 elems.Set (n, null); // remove
268 foreach (object o in tmpWildcards)
269 wildcards.Remove (o);
271 foreach (XmlQualifiedName n in elems.Names)
272 tmpElems.Set (n, elems [n]);
273 tmpWildcards.Clear ();
274 tmpWildcards.AddRange (wildcards);
279 internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
280 ValidationEventHandler h, XmlSchema schema)
282 foreach (XmlSchemaParticle p in this.Items)
283 p.ValidateUniqueTypeAttribution (labels, h, schema);
288 // maxOccurs = (nonNegativeInteger | unbounded) : 1
289 // minOccurs = nonNegativeInteger : 1
290 // {any attributes with non-schema namespace . . .}>
291 // Content: (annotation?, (element | group | choice | sequence | any)*)
293 internal static XmlSchemaSequence Read(XmlSchemaReader reader, ValidationEventHandler h)
295 XmlSchemaSequence sequence = new XmlSchemaSequence();
296 reader.MoveToElement();
298 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
300 error(h,"Should not happen :1: XmlSchemaSequence.Read, name="+reader.Name,null);
305 sequence.LineNumber = reader.LineNumber;
306 sequence.LinePosition = reader.LinePosition;
307 sequence.SourceUri = reader.BaseURI;
309 while(reader.MoveToNextAttribute())
311 if(reader.Name == "id")
313 sequence.Id = reader.Value;
315 else if(reader.Name == "maxOccurs")
319 sequence.MaxOccursString = reader.Value;
323 error(h,reader.Value + " is an invalid value for maxOccurs",e);
326 else if(reader.Name == "minOccurs")
330 sequence.MinOccursString = reader.Value;
334 error(h,reader.Value + " is an invalid value for minOccurs",e);
337 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
339 error(h,reader.Name + " is not a valid attribute for sequence",null);
343 XmlSchemaUtil.ReadUnhandledAttribute(reader,sequence);
347 reader.MoveToElement();
348 if(reader.IsEmptyElement)
351 // Content: (annotation?, (element | group | choice | sequence | any)*)
353 while(reader.ReadNextElement())
355 if(reader.NodeType == XmlNodeType.EndElement)
357 if(reader.LocalName != xmlname)
358 error(h,"Should not happen :2: XmlSchemaSequence.Read, name="+reader.Name,null);
361 if(level <= 1 && reader.LocalName == "annotation")
363 level = 2; //Only one annotation
364 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
365 if(annotation != null)
366 sequence.Annotation = annotation;
371 if(reader.LocalName == "element")
374 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
376 sequence.items.Add(element);
379 if(reader.LocalName == "group")
382 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
384 sequence.items.Add(group);
387 if(reader.LocalName == "choice")
390 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
392 sequence.items.Add(choice);
395 if(reader.LocalName == "sequence")
398 XmlSchemaSequence seq = XmlSchemaSequence.Read(reader,h);
400 sequence.items.Add(seq);
403 if(reader.LocalName == "any")
406 XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
408 sequence.items.Add(any);
412 reader.RaiseInvalidElementError();