1 // Author: Dwivedi, Ajay kumar
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 using System
.Collections
;
26 using System
.Globalization
;
27 using System
.Xml
.Serialization
;
29 namespace System
.Xml
.Schema
32 /// Summary description for XmlSchemaParticle.
34 public abstract class XmlSchemaParticle
: XmlSchemaAnnotated
36 internal static XmlSchemaParticle Empty
{
39 empty
= new EmptyParticle ();
45 decimal minOccurs
, maxOccurs
;
46 string minstr
, maxstr
;
47 static XmlSchemaParticle empty
;
48 decimal validatedMinOccurs
= 1, validatedMaxOccurs
= 1;
49 internal int recursionDepth
= -1;
50 private decimal minEffectiveTotalRange
= -1;
51 internal bool parentIsGroupDefinition
;
53 protected XmlSchemaParticle()
55 minOccurs
= decimal.One
;
56 maxOccurs
= decimal.One
;
61 [System
.Xml
.Serialization
.XmlAttribute("minOccurs")]
62 public string MinOccursString
68 minOccurs
= decimal.One
;
73 decimal val
= decimal.Parse (value, CultureInfo
.InvariantCulture
);
74 if(val
>= 0 && (val
== Decimal
.Truncate(val
)))
77 minstr
= val
.ToString (CultureInfo
.InvariantCulture
);
81 throw new XmlSchemaException
82 ("MinOccursString must be a non-negative number",null);
87 [System
.Xml
.Serialization
.XmlAttribute("maxOccurs")]
88 public string MaxOccursString
93 if(value == "unbounded")
96 maxOccurs
= decimal.MaxValue
;
100 decimal val
= decimal.Parse (value, CultureInfo
.InvariantCulture
);
101 if(val
>= 0 && (val
== Decimal
.Truncate(val
)))
104 maxstr
= val
.ToString (CultureInfo
.InvariantCulture
);
108 throw new XmlSchemaException
109 ("MaxOccurs must be a non-negative integer",null);
111 if (val
== 0 && minstr
== null)
122 public decimal MinOccurs
124 get{ return minOccurs; }
127 MinOccursString
= value.ToString (CultureInfo
.InvariantCulture
);
132 public decimal MaxOccurs
134 get{ return maxOccurs; }
137 if (value == decimal.MaxValue
)
138 MaxOccursString
= "unbounded";
140 MaxOccursString
= value.ToString (CultureInfo
.InvariantCulture
);
144 internal decimal ValidatedMinOccurs
146 get { return validatedMinOccurs; }
149 internal decimal ValidatedMaxOccurs
151 get { return validatedMaxOccurs; }
152 // set { validatedMaxOccurs = value; }
156 internal XmlSchemaParticle OptimizedParticle
;
158 internal virtual XmlSchemaParticle
GetOptimizedParticle (bool isTop
)
163 internal XmlSchemaParticle
GetShallowClone ()
165 return (XmlSchemaParticle
) MemberwiseClone ();
168 internal void CompileOccurence (ValidationEventHandler h
, XmlSchema schema
)
170 if (MinOccurs
> MaxOccurs
&& !(MaxOccurs
== 0 && MinOccursString
== null))
171 error(h
,"minOccurs must be less than or equal to maxOccurs");
173 if (MaxOccursString
== "unbounded")
174 this.validatedMaxOccurs
= decimal.MaxValue
;
176 this.validatedMaxOccurs
= maxOccurs
;
177 if (this.validatedMaxOccurs
== 0)
178 this.validatedMinOccurs
= 0;
180 this.validatedMinOccurs
= minOccurs
;
184 internal override void CopyInfo (XmlSchemaParticle obj
)
187 if (MaxOccursString
== "unbounded")
188 obj
.maxOccurs
= obj
.validatedMaxOccurs
= decimal.MaxValue
;
190 obj
.maxOccurs
= obj
.validatedMaxOccurs
= this.ValidatedMaxOccurs
;
192 obj
.minOccurs
= obj
.validatedMinOccurs
= 0;
194 obj
.minOccurs
= obj
.validatedMinOccurs
= this.ValidatedMinOccurs
;
195 if (MinOccursString
!= null)
196 obj
.MinOccursString
= MinOccursString
;
197 if (MaxOccursString
!= null)
198 obj
.MaxOccursString
= MaxOccursString
;
201 internal virtual bool ValidateOccurenceRangeOK (XmlSchemaParticle other
,
202 ValidationEventHandler h
, XmlSchema schema
, bool raiseError
)
204 if ((this.ValidatedMinOccurs
< other
.ValidatedMinOccurs
) ||
205 (other
.ValidatedMaxOccurs
!= decimal.MaxValue
&&
206 this.ValidatedMaxOccurs
> other
.ValidatedMaxOccurs
)) {
208 error (h
, "Invalid derivation occurence range was found.");
214 internal virtual decimal GetMinEffectiveTotalRange ()
216 return ValidatedMinOccurs
;
219 internal decimal GetMinEffectiveTotalRangeAllAndSequence ()
221 if (minEffectiveTotalRange
>= 0)
222 return minEffectiveTotalRange
;
224 decimal product
= 0; //this.ValidatedMinOccurs;
225 XmlSchemaObjectCollection col
= null;
226 if (this is XmlSchemaAll
)
227 col
= ((XmlSchemaAll
) this).Items
;
229 col
= ((XmlSchemaSequence
) this).Items
;
230 foreach (XmlSchemaParticle p
in col
)
231 product
+= p
.GetMinEffectiveTotalRange ();
233 minEffectiveTotalRange
= product
;
237 // 3.9.6 Particle Emptiable
238 internal virtual bool ValidateIsEmptiable ()
240 return this.validatedMinOccurs
== 0 || this.GetMinEffectiveTotalRange () == 0;
243 internal virtual bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle
,
244 ValidationEventHandler h
, XmlSchema schema
, bool raiseError
)
249 internal virtual void ValidateUniqueParticleAttribution (
250 XmlSchemaObjectTable qnames
, ArrayList nsNames
,
251 ValidationEventHandler h
, XmlSchema schema
)
255 internal virtual void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels
,
256 ValidationEventHandler h
, XmlSchema schema
)
260 // See http://www.thaiopensource.com/relaxng/simplify.html
261 internal virtual void CheckRecursion (int depth
, ValidationEventHandler h
, XmlSchema schema
)
265 internal virtual bool ParticleEquals (XmlSchemaParticle other
)
270 #region Internal Class
271 internal class EmptyParticle
: XmlSchemaParticle
273 internal EmptyParticle ()
277 internal override XmlSchemaParticle
GetOptimizedParticle (bool isTop
)
282 internal override bool ParticleEquals (XmlSchemaParticle other
)
284 return other
== this || other
== XmlSchemaParticle
.Empty
;
287 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle
,
288 ValidationEventHandler h
, XmlSchema schema
, bool raiseError
)
293 internal override void CheckRecursion (int depth
,
294 ValidationEventHandler h
, XmlSchema schema
)
299 internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames
,
300 ArrayList nsNames
, ValidationEventHandler h
, XmlSchema schema
)
305 internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels
,
306 ValidationEventHandler h
, XmlSchema schema
)