2 // System.Xml.Schema.XmlSchemaChoice.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
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
57 internal override void SetParent (XmlSchemaObject parent
)
59 base.SetParent (parent
);
60 foreach (XmlSchemaObject obj
in Items
)
64 internal override int Compile(ValidationEventHandler h
, XmlSchema schema
)
66 // If this is already compiled this time, simply skip.
67 if (CompilationId
== schema
.CompilationId
)
70 XmlSchemaUtil
.CompileID(Id
, this, schema
.IDCollection
, h
);
71 CompileOccurence (h
, schema
);
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
||
84 errorCount
+= obj
.Compile(h
,schema
);
87 error(h
, "Invalid schema object was specified in the particles of the choice model group.");
89 this.CompilationId
= schema
.CompilationId
;
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);
104 XmlSchemaChoice c
= new XmlSchemaChoice ();
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
)
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
]);
120 c
.CompiledItems
.Add (p
);
123 if (c
.Items
.Count
== 0)
124 OptimizedParticle
= XmlSchemaParticle
.Empty
;
126 OptimizedParticle
= c
;
128 return OptimizedParticle
;
131 internal override int Validate (ValidationEventHandler h
, XmlSchema schema
)
133 if (IsValidated (schema
.CompilationId
))
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
;
146 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle
,
147 ValidationEventHandler h
, XmlSchema schema
, bool raiseError
)
149 XmlSchemaAny any
= baseParticle
as XmlSchemaAny
;
151 // NSRecurseCheckCardinality
152 return ValidateNSRecurseCheckCardinality (any
, h
, schema
, raiseError
);
155 XmlSchemaChoice choice
= baseParticle
as XmlSchemaChoice
;
156 if (choice
!= null) {
158 if (!ValidateOccurenceRangeOK (choice
, h
, schema
, raiseError
))
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)
165 // return ValidateRecurseLax (choice, h, schema, raiseError);
166 return this.ValidateSeqRecurseMapSumCommon (choice
, h
, schema
, true, false, raiseError
);
170 error (h
, "Invalid choice derivation by restriction was found.");
175 private bool ValidateRecurseLax (XmlSchemaGroupBase baseGroup,
176 ValidationEventHandler h, XmlSchema schema, bool raiseError)
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)
184 XmlSchemaParticle pd = null;
185 while (this.CompiledItems.Count > index) {
186 pd = (XmlSchemaParticle) this.CompiledItems [index];
187 pd = pd.GetOptimizedParticle (false);
189 if (pd != XmlSchemaParticle.Empty)
192 if (!ValidateParticleSection (ref index, pd, pb, h, schema, raiseError))
195 if (this.CompiledItems.Count > 0 && index != this.CompiledItems.Count) {
197 error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");
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
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 ()) {
217 error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.");
221 index--; // try the same derived particle and next base particle.
225 } else if (!pb.ValidateIsEmptiable ()) {
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.");
234 internal override decimal GetMinEffectiveTotalRange ()
236 if (minEffectiveTotalRange
>= 0)
237 return minEffectiveTotalRange
;
239 decimal product
= 0; //this.ValidatedMinOccurs;
240 if (Items
.Count
== 0)
243 foreach (XmlSchemaParticle p
in this.Items
) {
244 decimal got
= p
.GetMinEffectiveTotalRange ();
249 minEffectiveTotalRange
= 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
);
269 // maxOccurs = (nonNegativeInteger | unbounded) : 1
270 // minOccurs = nonNegativeInteger : 1
271 // {any attributes with non-schema namespace . . .}>
272 // Content: (annotation?, (element | group | choice | sequence | any)*)
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);
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
;
304 error(h
,reader
.Value
+ " is an invalid value for maxOccurs",e
);
307 else if(reader
.Name
== "minOccurs")
311 choice
.MinOccursString
= reader
.Value
;
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);
324 XmlSchemaUtil
.ReadUnhandledAttribute(reader
,choice
);
328 reader
.MoveToElement();
329 if(reader
.IsEmptyElement
)
332 // Content: (annotation?, (element | group | choice | sequence | any)*)
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);
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
;
352 if(reader
.LocalName
== "element")
355 XmlSchemaElement element
= XmlSchemaElement
.Read(reader
,h
);
357 choice
.items
.Add(element
);
360 if(reader
.LocalName
== "group")
363 XmlSchemaGroupRef
group = XmlSchemaGroupRef
.Read(reader
,h
);
365 choice
.items
.Add(group);
368 if(reader
.LocalName
== "choice")
371 XmlSchemaChoice ch
= XmlSchemaChoice
.Read(reader
,h
);
373 choice
.items
.Add(ch
);
376 if(reader
.LocalName
== "sequence")
379 XmlSchemaSequence sequence
= XmlSchemaSequence
.Read(reader
,h
);
381 choice
.items
.Add(sequence
);
384 if(reader
.LocalName
== "any")
387 XmlSchemaAny any
= XmlSchemaAny
.Read(reader
,h
);
389 choice
.items
.Add(any
);
393 reader
.RaiseInvalidElementError();