2 // System.Xml.Schema.XmlSchemaChoice.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
15 public class XmlSchemaChoice : XmlSchemaGroupBase
\r
17 private XmlSchemaObjectCollection items;
\r
18 const string xmlname = "choice";
\r
19 private decimal minEffectiveTotalRange = -1;
\r
21 public XmlSchemaChoice ()
\r
23 items = new XmlSchemaObjectCollection();
\r
26 [XmlElement("element",typeof(XmlSchemaElement),Namespace=XmlSchema.Namespace)]
\r
27 [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace=XmlSchema.Namespace)]
\r
28 [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]
\r
29 [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]
\r
30 [XmlElement("any",typeof(XmlSchemaAny),Namespace=XmlSchema.Namespace)]
\r
31 public override XmlSchemaObjectCollection Items
\r
33 get{ return items; }
\r
36 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
\r
38 // If this is already compiled this time, simply skip.
\r
39 if (this.IsComplied (schema.CompilationId))
\r
42 XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
\r
43 CompileOccurence (h, schema);
\r
45 if (Items.Count == 0)
\r
46 this.warn (h, "Empty choice is unsatisfiable if minOccurs not equals to 0");
\r
48 foreach(XmlSchemaObject obj in Items)
\r
50 if(obj is XmlSchemaElement ||
\r
51 obj is XmlSchemaGroupRef ||
\r
52 obj is XmlSchemaChoice ||
\r
53 obj is XmlSchemaSequence ||
\r
54 obj is XmlSchemaAny)
\r
56 errorCount += obj.Compile(h,schema);
\r
59 error(h, "Invalid schema object was specified in the particles of the choice model group.");
\r
61 this.CompilationId = schema.CompilationId;
\r
65 internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
\r
67 if (OptimizedParticle != null)
\r
68 return OptimizedParticle;
\r
70 if (Items.Count == 0 || ValidatedMaxOccurs == 0)
\r
71 OptimizedParticle = XmlSchemaParticle.Empty;
\r
72 // FIXME: Regardless of isTop, it should remove pointless particle.
\r
73 else if (!isTop && Items.Count == 1 && ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1)
\r
74 OptimizedParticle = ((XmlSchemaParticle) Items [0]).GetOptimizedParticle (false);
\r
76 XmlSchemaChoice c = new XmlSchemaChoice ();
\r
78 for (int i = 0; i < Items.Count; i++) {
\r
79 XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
\r
80 p = p.GetOptimizedParticle (false);
\r
81 if (p == XmlSchemaParticle.Empty)
\r
83 else if (p is XmlSchemaChoice && p.ValidatedMinOccurs == 1 && p.ValidatedMaxOccurs == 1) {
\r
84 XmlSchemaChoice pc = p as XmlSchemaChoice;
\r
85 for (int ci = 0; ci < pc.Items.Count; ci++) {
\r
86 c.Items.Add (pc.Items [ci]);
\r
87 c.CompiledItems.Add (pc.Items [ci]);
\r
92 c.CompiledItems.Add (p);
\r
95 if (c.Items.Count == 0)
\r
96 OptimizedParticle = XmlSchemaParticle.Empty;
\r
98 OptimizedParticle = c;
\r
100 return OptimizedParticle;
\r
103 internal override int Validate (ValidationEventHandler h, XmlSchema schema)
\r
105 if (IsValidated (schema.CompilationId))
\r
108 CompiledItems.Clear ();
\r
109 foreach (XmlSchemaParticle p in Items) {
\r
110 errorCount += p.Validate (h, schema); // This is basically extraneous for pointless item, but needed to check validation error.
\r
111 CompiledItems.Add (p);
\r
114 ValidationId = schema.ValidationId;
\r
118 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
\r
119 ValidationEventHandler h, XmlSchema schema, bool raiseError)
\r
121 XmlSchemaAny any = baseParticle as XmlSchemaAny;
\r
123 // NSRecurseCheckCardinality
\r
124 return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
\r
127 XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
\r
128 if (choice != null) {
\r
130 if (!ValidateOccurenceRangeOK (choice, h, schema, raiseError))
\r
133 // If it is totally optional, then ignore their contents.
\r
134 if (choice.ValidatedMinOccurs == 0 && choice.ValidatedMaxOccurs == 0 &&
\r
135 this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
\r
137 // return ValidateRecurseLax (choice, h, schema, raiseError);
\r
138 return this.ValidateSeqRecurseMapSumCommon (choice, h, schema, true, false, raiseError);
\r
142 error (h, "Invalid choice derivation by restriction was found.");
\r
146 private bool ValidateRecurseLax (XmlSchemaGroupBase baseGroup,
\r
147 ValidationEventHandler h, XmlSchema schema, bool raiseError)
\r
150 for (int i = 0; i < baseGroup.CompiledItems.Count; i++) {
\r
151 XmlSchemaParticle pb = (XmlSchemaParticle) baseGroup.CompiledItems [i];
\r
152 pb = pb.GetOptimizedParticle (false);
\r
153 if (pb == XmlSchemaParticle.Empty)
\r
155 XmlSchemaParticle pd = null;
\r
156 while (this.CompiledItems.Count > index) {
\r
157 pd = (XmlSchemaParticle) this.CompiledItems [index];
\r
158 pd = pd.GetOptimizedParticle (false);
\r
160 if (pd != XmlSchemaParticle.Empty)
\r
163 if (!ValidateParticleSection (ref index, pd, pb, h, schema, raiseError))
\r
166 if (this.CompiledItems.Count > 0 && index != this.CompiledItems.Count) {
\r
168 error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");
\r
174 private bool ValidateParticleSection (ref int index, XmlSchemaParticle pd, XmlSchemaParticle pb, ValidationEventHandler h, XmlSchema schema, bool raiseError)
\r
176 if (pd == pb) // they are same particle
\r
180 XmlSchemaElement el = pd as XmlSchemaElement;
\r
181 XmlSchemaParticle pdx = pd;
\r
182 // if (el != null && el.SubstitutingElements.Count > 0)
\r
183 // pdx = el.SubstitutingChoice;
\r
185 if (!pdx.ValidateDerivationByRestriction (pb, h, schema, false)) {
\r
186 if (!pb.ValidateIsEmptiable ()) {
\r
188 error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.");
\r
192 index--; // try the same derived particle and next base particle.
\r
196 } else if (!pb.ValidateIsEmptiable ()) {
\r
198 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.");
\r
205 internal override decimal GetMinEffectiveTotalRange ()
\r
207 if (minEffectiveTotalRange >= 0)
\r
208 return minEffectiveTotalRange;
\r
210 decimal product = 0; //this.ValidatedMinOccurs;
\r
211 if (Items.Count == 0)
\r
214 foreach (XmlSchemaParticle p in this.Items) {
\r
215 decimal got = p.GetMinEffectiveTotalRange ();
\r
220 minEffectiveTotalRange = product;
\r
224 internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
\r
225 ValidationEventHandler h, XmlSchema schema)
\r
227 foreach (XmlSchemaParticle p in this.Items)
\r
228 p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
\r
231 internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
\r
232 ValidationEventHandler h, XmlSchema schema)
\r
234 foreach (XmlSchemaParticle p in this.Items)
\r
235 p.ValidateUniqueTypeAttribution (labels, h, schema);
\r
240 // maxOccurs = (nonNegativeInteger | unbounded) : 1
\r
241 // minOccurs = nonNegativeInteger : 1
\r
242 // {any attributes with non-schema namespace . . .}>
\r
243 // Content: (annotation?, (element | group | choice | sequence | any)*)
\r
245 internal static XmlSchemaChoice Read(XmlSchemaReader reader, ValidationEventHandler h)
\r
247 XmlSchemaChoice choice = new XmlSchemaChoice();
\r
248 reader.MoveToElement();
\r
250 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
\r
252 error(h,"Should not happen :1: XmlSchemaChoice.Read, name="+reader.Name,null);
\r
253 reader.SkipToEnd();
\r
257 choice.LineNumber = reader.LineNumber;
\r
258 choice.LinePosition = reader.LinePosition;
\r
259 choice.SourceUri = reader.BaseURI;
\r
261 while(reader.MoveToNextAttribute())
\r
263 if(reader.Name == "id")
\r
265 choice.Id = reader.Value;
\r
267 else if(reader.Name == "maxOccurs")
\r
271 choice.MaxOccursString = reader.Value;
\r
275 error(h,reader.Value + " is an invalid value for maxOccurs",e);
\r
278 else if(reader.Name == "minOccurs")
\r
282 choice.MinOccursString = reader.Value;
\r
286 error(h,reader.Value + " is an invalid value for minOccurs",e);
\r
289 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
291 error(h,reader.Name + " is not a valid attribute for choice",null);
\r
295 XmlSchemaUtil.ReadUnhandledAttribute(reader,choice);
\r
299 reader.MoveToElement();
\r
300 if(reader.IsEmptyElement)
\r
303 // Content: (annotation?, (element | group | choice | sequence | any)*)
\r
305 while(reader.ReadNextElement())
\r
307 if(reader.NodeType == XmlNodeType.EndElement)
\r
309 if(reader.LocalName != xmlname)
\r
310 error(h,"Should not happen :2: XmlSchemaChoice.Read, name="+reader.Name,null);
\r
313 if(level <= 1 && reader.LocalName == "annotation")
\r
315 level = 2; //Only one annotation
\r
316 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
317 if(annotation != null)
\r
318 choice.Annotation = annotation;
\r
323 if(reader.LocalName == "element")
\r
326 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
327 if(element != null)
\r
328 choice.items.Add(element);
\r
331 if(reader.LocalName == "group")
\r
334 XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
\r
336 choice.items.Add(group);
\r
339 if(reader.LocalName == "choice")
\r
342 XmlSchemaChoice ch = XmlSchemaChoice.Read(reader,h);
\r
344 choice.items.Add(ch);
\r
347 if(reader.LocalName == "sequence")
\r
350 XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
\r
351 if(sequence != null)
\r
352 choice.items.Add(sequence);
\r
355 if(reader.LocalName == "any")
\r
358 XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
\r
360 choice.items.Add(any);
\r
364 reader.RaiseInvalidElementError();
\r