2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaChoice.cs
blob5f0a788a9132cb6ea5fc3b2f0e0ca83888a490fc
1 //
2 // System.Xml.Schema.XmlSchemaChoice.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 public class XmlSchemaChoice : XmlSchemaGroupBase
38 private XmlSchemaObjectCollection items;
39 const string xmlname = "choice";
40 private decimal minEffectiveTotalRange = -1;
42 public XmlSchemaChoice ()
44 items = new XmlSchemaObjectCollection();
47 [XmlElement("element",typeof(XmlSchemaElement))]
48 [XmlElement("group",typeof(XmlSchemaGroupRef))]
49 [XmlElement("choice",typeof(XmlSchemaChoice))]
50 [XmlElement("sequence",typeof(XmlSchemaSequence))]
51 [XmlElement("any",typeof(XmlSchemaAny))]
52 public override XmlSchemaObjectCollection Items
54 get{ return items; }
57 internal override void SetParent (XmlSchemaObject parent)
59 base.SetParent (parent);
60 foreach (XmlSchemaObject obj in Items)
61 obj.SetParent (this);
64 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
66 // If this is already compiled this time, simply skip.
67 if (CompilationId == schema.CompilationId)
68 return 0;
70 XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
71 CompileOccurence (h, schema);
73 if (Items.Count == 0)
74 this.warn (h, "Empty choice is unsatisfiable if minOccurs not equals to 0");
76 foreach(XmlSchemaObject obj in Items)
78 if(obj is XmlSchemaElement ||
79 obj is XmlSchemaGroupRef ||
80 obj is XmlSchemaChoice ||
81 obj is XmlSchemaSequence ||
82 obj is XmlSchemaAny)
84 errorCount += obj.Compile(h,schema);
86 else
87 error(h, "Invalid schema object was specified in the particles of the choice model group.");
89 this.CompilationId = schema.CompilationId;
90 return errorCount;
93 internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
95 if (OptimizedParticle != null)
96 return OptimizedParticle;
98 if (Items.Count == 0 || ValidatedMaxOccurs == 0)
99 OptimizedParticle = XmlSchemaParticle.Empty;
100 // LAMESPEC: Regardless of isTop, it should remove pointless particle. It seems ContentTypeParticle design bug.
101 else if (!isTop && Items.Count == 1 && ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1)
102 OptimizedParticle = ((XmlSchemaParticle) Items [0]).GetOptimizedParticle (false);
103 else {
104 XmlSchemaChoice c = new XmlSchemaChoice ();
105 CopyInfo (c);
106 for (int i = 0; i < Items.Count; i++) {
107 XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
108 p = p.GetOptimizedParticle (false);
109 if (p == XmlSchemaParticle.Empty)
110 continue;
111 else if (p is XmlSchemaChoice && p.ValidatedMinOccurs == 1 && p.ValidatedMaxOccurs == 1) {
112 XmlSchemaChoice pc = p as XmlSchemaChoice;
113 for (int ci = 0; ci < pc.Items.Count; ci++) {
114 c.Items.Add (pc.Items [ci]);
115 c.CompiledItems.Add (pc.Items [ci]);
118 else {
119 c.Items.Add (p);
120 c.CompiledItems.Add (p);
123 if (c.Items.Count == 0)
124 OptimizedParticle = XmlSchemaParticle.Empty;
125 else
126 OptimizedParticle = c;
128 return OptimizedParticle;
131 internal override int Validate (ValidationEventHandler h, XmlSchema schema)
133 if (IsValidated (schema.CompilationId))
134 return errorCount;
136 CompiledItems.Clear ();
137 foreach (XmlSchemaParticle p in Items) {
138 errorCount += p.Validate (h, schema); // This is basically extraneous for pointless item, but needed to check validation error.
139 CompiledItems.Add (p);
142 ValidationId = schema.ValidationId;
143 return errorCount;
146 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
147 ValidationEventHandler h, XmlSchema schema, bool raiseError)
149 XmlSchemaAny any = baseParticle as XmlSchemaAny;
150 if (any != null) {
151 // NSRecurseCheckCardinality
152 return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
155 XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
156 if (choice != null) {
157 // RecurseLax
158 if (!ValidateOccurenceRangeOK (choice, h, schema, raiseError))
159 return false;
161 // If it is totally optional, then ignore their contents.
162 if (choice.ValidatedMinOccurs == 0 && choice.ValidatedMaxOccurs == 0 &&
163 this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
164 return true;
165 // return ValidateRecurseLax (choice, h, schema, raiseError);
166 return this.ValidateSeqRecurseMapSumCommon (choice, h, schema, true, false, raiseError);
169 if (raiseError)
170 error (h, "Invalid choice derivation by restriction was found.");
171 return false;
175 private bool ValidateRecurseLax (XmlSchemaGroupBase baseGroup,
176 ValidationEventHandler h, XmlSchema schema, bool raiseError)
178 int index = 0;
179 for (int i = 0; i < baseGroup.CompiledItems.Count; i++) {
180 XmlSchemaParticle pb = (XmlSchemaParticle) baseGroup.CompiledItems [i];
181 pb = pb.GetOptimizedParticle (false);
182 if (pb == XmlSchemaParticle.Empty)
183 continue;
184 XmlSchemaParticle pd = null;
185 while (this.CompiledItems.Count > index) {
186 pd = (XmlSchemaParticle) this.CompiledItems [index];
187 pd = pd.GetOptimizedParticle (false);
188 index++;
189 if (pd != XmlSchemaParticle.Empty)
190 break;
192 if (!ValidateParticleSection (ref index, pd, pb, h, schema, raiseError))
193 continue;
195 if (this.CompiledItems.Count > 0 && index != this.CompiledItems.Count) {
196 if (raiseError)
197 error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");
198 return false;
200 return true;
203 private bool ValidateParticleSection (ref int index, XmlSchemaParticle pd, XmlSchemaParticle pb, ValidationEventHandler h, XmlSchema schema, bool raiseError)
205 if (pd == pb) // they are same particle
206 return true;
208 if (pd != null) {
209 // XmlSchemaElement el = pd as XmlSchemaElement;
210 XmlSchemaParticle pdx = pd;
211 // if (el != null && el.SubstitutingElements.Count > 0)
212 // pdx = el.SubstitutingChoice;
214 if (!pdx.ValidateDerivationByRestriction (pb, h, schema, false)) {
215 if (!pb.ValidateIsEmptiable ()) {
216 if (raiseError)
217 error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.");
218 return false;
220 else {
221 index--; // try the same derived particle and next base particle.
222 return false;
225 } else if (!pb.ValidateIsEmptiable ()) {
226 if (raiseError)
227 error (h, "Invalid particle derivation by restriction was found. Base schema particle has non-emptiable sub particle that is not mapped to the derived particle.");
228 return false;
231 return true;
234 internal override decimal GetMinEffectiveTotalRange ()
236 if (minEffectiveTotalRange >= 0)
237 return minEffectiveTotalRange;
239 decimal product = 0; //this.ValidatedMinOccurs;
240 if (Items.Count == 0)
241 product = 0;
242 else {
243 foreach (XmlSchemaParticle p in this.Items) {
244 decimal got = p.GetMinEffectiveTotalRange ();
245 if (product > got)
246 product= got;
249 minEffectiveTotalRange = product;
250 return product;
253 internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
254 ValidationEventHandler h, XmlSchema schema)
256 foreach (XmlSchemaParticle p in this.Items)
257 p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
260 internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
261 ValidationEventHandler h, XmlSchema schema)
263 foreach (XmlSchemaParticle p in this.Items)
264 p.ValidateUniqueTypeAttribution (labels, h, schema);
267 //<choice
268 // id = ID
269 // maxOccurs = (nonNegativeInteger | unbounded) : 1
270 // minOccurs = nonNegativeInteger : 1
271 // {any attributes with non-schema namespace . . .}>
272 // Content: (annotation?, (element | group | choice | sequence | any)*)
273 //</choice>
274 internal static XmlSchemaChoice Read(XmlSchemaReader reader, ValidationEventHandler h)
276 XmlSchemaChoice choice = new XmlSchemaChoice();
277 reader.MoveToElement();
279 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
281 error(h,"Should not happen :1: XmlSchemaChoice.Read, name="+reader.Name,null);
282 reader.SkipToEnd();
283 return null;
286 choice.LineNumber = reader.LineNumber;
287 choice.LinePosition = reader.LinePosition;
288 choice.SourceUri = reader.BaseURI;
290 while(reader.MoveToNextAttribute())
292 if(reader.Name == "id")
294 choice.Id = reader.Value;
296 else if(reader.Name == "maxOccurs")
300 choice.MaxOccursString = reader.Value;
302 catch(Exception e)
304 error(h,reader.Value + " is an invalid value for maxOccurs",e);
307 else if(reader.Name == "minOccurs")
311 choice.MinOccursString = reader.Value;
313 catch(Exception e)
315 error(h,reader.Value + " is an invalid value for minOccurs",e);
318 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
320 error(h,reader.Name + " is not a valid attribute for choice",null);
322 else
324 XmlSchemaUtil.ReadUnhandledAttribute(reader,choice);
328 reader.MoveToElement();
329 if(reader.IsEmptyElement)
330 return choice;
332 // Content: (annotation?, (element | group | choice | sequence | any)*)
333 int level = 1;
334 while(reader.ReadNextElement())
336 if(reader.NodeType == XmlNodeType.EndElement)
338 if(reader.LocalName != xmlname)
339 error(h,"Should not happen :2: XmlSchemaChoice.Read, name="+reader.Name,null);
340 break;
342 if(level <= 1 && reader.LocalName == "annotation")
344 level = 2; //Only one annotation
345 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
346 if(annotation != null)
347 choice.Annotation = annotation;
348 continue;
350 if(level <=2)
352 if(reader.LocalName == "element")
354 level = 2;
355 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
356 if(element != null)
357 choice.items.Add(element);
358 continue;
360 if(reader.LocalName == "group")
362 level = 2;
363 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
364 if(group != null)
365 choice.items.Add(group);
366 continue;
368 if(reader.LocalName == "choice")
370 level = 2;
371 XmlSchemaChoice ch = XmlSchemaChoice.Read(reader,h);
372 if(ch != null)
373 choice.items.Add(ch);
374 continue;
376 if(reader.LocalName == "sequence")
378 level = 2;
379 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
380 if(sequence != null)
381 choice.items.Add(sequence);
382 continue;
384 if(reader.LocalName == "any")
386 level = 2;
387 XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
388 if(any != null)
389 choice.items.Add(any);
390 continue;
393 reader.RaiseInvalidElementError();
395 return choice;