2 // System.Xml.Schema.XmlSchemaSequence.cs
\r
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
\r
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
\r
9 using System.Collections;
\r
10 using System.Xml.Serialization;
\r
13 namespace System.Xml.Schema
\r
16 /// Summary description for XmlSchemaSequence.
\r
18 public class XmlSchemaSequence : XmlSchemaGroupBase
\r
20 private XmlSchemaObjectCollection items;
\r
21 const string xmlname = "sequence";
\r
23 public XmlSchemaSequence()
\r
25 items = new XmlSchemaObjectCollection();
\r
28 [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
29 [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
30 [XmlElement("choice",typeof(XmlSchemaChoice),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
31 [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
32 [XmlElement("any",typeof(XmlSchemaAny),Namespace="http://www.w3.org/2001/XMLSchema")]
\r
33 public override XmlSchemaObjectCollection Items
\r
35 get{ return items; }
\r
38 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
\r
40 // If this is already compiled this time, simply skip.
\r
41 if (this.IsComplied (schema.CompilationId))
\r
44 XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
\r
45 CompileOccurence (h, schema);
\r
47 foreach(XmlSchemaObject obj in Items)
\r
49 if(obj is XmlSchemaElement ||
\r
50 obj is XmlSchemaGroupRef ||
\r
51 obj is XmlSchemaChoice ||
\r
52 obj is XmlSchemaSequence ||
\r
53 obj is XmlSchemaAny)
\r
55 errorCount += obj.Compile(h,schema);
\r
58 error(h, "Invalid schema object was specified in the particles of the sequence model group.");
\r
60 this.CompilationId = schema.CompilationId;
\r
65 internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
\r
67 if (OptimizedParticle != null)
\r
68 return OptimizedParticle;
\r
69 if (Items.Count == 0 || ValidatedMaxOccurs == 0) {
\r
70 OptimizedParticle = XmlSchemaParticle.Empty;
\r
71 return OptimizedParticle;
\r
73 if (!isTop && ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1) {
\r
74 if (Items.Count == 1)
\r
75 return ((XmlSchemaParticle) Items [0]).GetOptimizedParticle (false);
\r
78 XmlSchemaSequence seq = new XmlSchemaSequence ();
\r
80 for (int i = 0; i < Items.Count; i++) {
\r
81 XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
\r
82 p = p.GetOptimizedParticle (false);
\r
83 if (p == XmlSchemaParticle.Empty)
\r
86 else if (p is XmlSchemaSequence && p.ValidatedMinOccurs == 1 && p.ValidatedMaxOccurs == 1) {
\r
87 XmlSchemaSequence ps = p as XmlSchemaSequence;
\r
88 for (int pi = 0; pi < ps.Items.Count; pi++) {
\r
89 seq.Items.Add (ps.Items [pi]);
\r
90 seq.CompiledItems.Add (ps.Items [pi]);
\r
95 seq.CompiledItems.Add (p);
\r
98 if (seq.Items.Count == 0)
\r
99 OptimizedParticle = XmlSchemaParticle.Empty;
\r
101 OptimizedParticle = seq;
\r
102 return OptimizedParticle;
\r
105 internal override int Validate (ValidationEventHandler h, XmlSchema schema)
\r
107 if (IsValidated (schema.CompilationId))
\r
110 CompiledItems.Clear ();
\r
111 foreach (XmlSchemaParticle p in Items) {
\r
112 errorCount += p.Validate (h, schema); // This is basically extraneous for pointless item, but needed to check validation error.
\r
113 // XmlSchemaParticle particleInPoint = p.GetParticleWithoutPointless ();
\r
114 // if (particleInPoint != XmlSchemaParticle.Empty)
\r
115 // CompiledItems.Add (particleInPoint);
\r
116 CompiledItems.Add (p);
\r
119 ValidationId = schema.ValidationId;
\r
123 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
\r
124 ValidationEventHandler h, XmlSchema schema, bool raiseError)
\r
126 if (this == baseParticle) // quick check
\r
129 XmlSchemaElement el = baseParticle as XmlSchemaElement;
\r
133 error (h, "Invalid sequence paricle derivation.");
\r
137 XmlSchemaSequence seq = baseParticle as XmlSchemaSequence;
\r
140 if (!ValidateOccurenceRangeOK (seq, h, schema, raiseError))
\r
143 // If it is totally optional, then ignore their contents.
\r
144 if (seq.ValidatedMinOccurs == 0 && seq.ValidatedMaxOccurs == 0 &&
\r
145 this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
\r
147 return ValidateRecurse (seq, h, schema, raiseError);
\r
150 XmlSchemaAll all = baseParticle as XmlSchemaAll;
\r
152 // RecurseUnordered
\r
153 XmlSchemaObjectCollection already = new XmlSchemaObjectCollection ();
\r
154 for (int i = 0; i < this.Items.Count; i++) {
\r
155 XmlSchemaElement de = this.Items [i] as XmlSchemaElement;
\r
158 error (h, "Invalid sequence particle derivation by restriction from all.");
\r
161 foreach (XmlSchemaElement e in all.Items) {
\r
162 if (e.QualifiedName == de.QualifiedName) {
\r
163 if (already.Contains (e)) {
\r
165 error (h, "Base element particle is mapped to the derived element particle in a sequence two or more times.");
\r
169 if (!de.ValidateDerivationByRestriction (e, h, schema, raiseError))
\r
175 foreach (XmlSchemaElement e in all.Items)
\r
176 if (!already.Contains (e))
\r
177 if (!e.ValidateIsEmptiable ()) {
\r
179 error (h, "In base -all- particle, mapping-skipped base element which is not emptiable was found.");
\r
184 XmlSchemaAny any = baseParticle as XmlSchemaAny;
\r
186 // NSRecurseCheckCardinality
\r
187 return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
\r
189 XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
\r
190 if (choice != null) {
\r
192 // In fact it is not Recurse, but it looks almost common.
\r
193 return ValidateSeqRecurseMapSumCommon (choice, h, schema, false, true, raiseError);
\r
198 internal override decimal GetMinEffectiveTotalRange ()
\r
200 return GetMinEffectiveTotalRangeAllAndSequence ();
\r
203 internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
\r
204 ValidationEventHandler h, XmlSchema schema)
\r
206 foreach (XmlSchemaParticle p in this.Items) {
\r
207 p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
\r
208 if (p.ValidatedMinOccurs == p.ValidatedMaxOccurs)
\r
211 XmlSchemaObjectTable tmpTable = new XmlSchemaObjectTable ();
\r
212 ArrayList al = new ArrayList ();
\r
213 for (int i=0; i<Items.Count; i++) {
\r
214 XmlSchemaParticle p1 = Items [i] as XmlSchemaParticle;
\r
215 p1.ValidateUniqueParticleAttribution (tmpTable, al, h, schema);
\r
216 if (p1.ValidatedMinOccurs == p1.ValidatedMaxOccurs) {
\r
223 internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
\r
224 ValidationEventHandler h, XmlSchema schema)
\r
226 foreach (XmlSchemaParticle p in this.Items)
\r
227 p.ValidateUniqueTypeAttribution (labels, h, schema);
\r
232 // maxOccurs = (nonNegativeInteger | unbounded) : 1
\r
233 // minOccurs = nonNegativeInteger : 1
\r
234 // {any attributes with non-schema namespace . . .}>
\r
235 // Content: (annotation?, (element | group | choice | sequence | any)*)
\r
237 internal static XmlSchemaSequence Read(XmlSchemaReader reader, ValidationEventHandler h)
\r
239 XmlSchemaSequence sequence = new XmlSchemaSequence();
\r
240 reader.MoveToElement();
\r
242 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
\r
244 error(h,"Should not happen :1: XmlSchemaSequence.Read, name="+reader.Name,null);
\r
249 sequence.LineNumber = reader.LineNumber;
\r
250 sequence.LinePosition = reader.LinePosition;
\r
251 sequence.SourceUri = reader.BaseURI;
\r
253 while(reader.MoveToNextAttribute())
\r
255 if(reader.Name == "id")
\r
257 sequence.Id = reader.Value;
\r
259 else if(reader.Name == "maxOccurs")
\r
263 sequence.MaxOccursString = reader.Value;
\r
267 error(h,reader.Value + " is an invalid value for maxOccurs",e);
\r
270 else if(reader.Name == "minOccurs")
\r
274 sequence.MinOccursString = reader.Value;
\r
278 error(h,reader.Value + " is an invalid value for minOccurs",e);
\r
281 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
283 error(h,reader.Name + " is not a valid attribute for sequence",null);
\r
287 XmlSchemaUtil.ReadUnhandledAttribute(reader,sequence);
\r
291 reader.MoveToElement();
\r
292 if(reader.IsEmptyElement)
\r
295 // Content: (annotation?, (element | group | choice | sequence | any)*)
\r
297 while(reader.ReadNextElement())
\r
299 if(reader.NodeType == XmlNodeType.EndElement)
\r
301 if(reader.LocalName != xmlname)
\r
302 error(h,"Should not happen :2: XmlSchemaSequence.Read, name="+reader.Name,null);
\r
305 if(level <= 1 && reader.LocalName == "annotation")
\r
307 level = 2; //Only one annotation
\r
308 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
309 if(annotation != null)
\r
310 sequence.Annotation = annotation;
\r
315 if(reader.LocalName == "element")
\r
318 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
319 if(element != null)
\r
320 sequence.items.Add(element);
\r
323 if(reader.LocalName == "group")
\r
326 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
\r
328 sequence.items.Add(group);
\r
331 if(reader.LocalName == "choice")
\r
334 XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
\r
336 sequence.items.Add(choice);
\r
339 if(reader.LocalName == "sequence")
\r
342 XmlSchemaSequence seq = XmlSchemaSequence.Read(reader,h);
\r
344 sequence.items.Add(seq);
\r
347 if(reader.LocalName == "any")
\r
350 XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
\r
352 sequence.items.Add(any);
\r
356 reader.RaiseInvalidElementError();
\r