XmlSchemaParticle.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Collections;
  5. using System.Xml.Serialization;
  6. namespace System.Xml.Schema
  7. {
  8. /// <summary>
  9. /// Summary description for XmlSchemaParticle.
  10. /// </summary>
  11. public abstract class XmlSchemaParticle : XmlSchemaAnnotated
  12. {
  13. decimal minOccurs, maxOccurs;
  14. string minstr, maxstr;
  15. static XmlSchemaParticle empty;
  16. decimal validatedMinOccurs, validatedMaxOccurs;
  17. internal int recursionDepth = -1;
  18. private decimal minEffectiveTotalRange = -1;
  19. internal bool parentIsGroupDefinition;
  20. internal static XmlSchemaParticle Empty {
  21. get {
  22. if (empty == null) {
  23. empty = new XmlSchemaParticleEmpty ();
  24. }
  25. return empty;
  26. }
  27. }
  28. protected XmlSchemaParticle()
  29. {
  30. minOccurs = decimal.One;
  31. maxOccurs = decimal.One;
  32. }
  33. #region Attributes
  34. [System.Xml.Serialization.XmlAttribute("minOccurs")]
  35. public string MinOccursString
  36. {
  37. get{ return minstr; }
  38. set
  39. {
  40. if (value == null) {
  41. minOccurs = decimal.One;
  42. minstr = value;
  43. return;
  44. }
  45. decimal val = decimal.Parse(value);
  46. if(val >= 0 && (val == Decimal.Truncate(val)))
  47. {
  48. minOccurs = val;
  49. minstr = val.ToString();
  50. }
  51. else
  52. {
  53. throw new XmlSchemaException
  54. ("MinOccursString must be a non-negative number",null);
  55. }
  56. }
  57. }
  58. [System.Xml.Serialization.XmlAttribute("maxOccurs")]
  59. public string MaxOccursString
  60. {
  61. get{ return maxstr; }
  62. set
  63. {
  64. if(value == "unbounded")
  65. {
  66. maxstr = value;
  67. maxOccurs = decimal.MaxValue;
  68. }
  69. else
  70. {
  71. decimal val = decimal.Parse(value);
  72. if(val >= 0 && (val == Decimal.Truncate(val)))
  73. {
  74. maxOccurs = val;
  75. maxstr = val.ToString();
  76. }
  77. else
  78. {
  79. throw new XmlSchemaException
  80. ("MaxOccurs must be a non-negative integer",null);
  81. }
  82. if (val == 0 && minstr == null)
  83. minOccurs = 0;
  84. }
  85. }
  86. }
  87. #endregion
  88. #region XmlIgnore
  89. [XmlIgnore]
  90. public decimal MinOccurs
  91. {
  92. get{ return minOccurs; }
  93. set
  94. {
  95. MinOccursString = value.ToString ();
  96. }
  97. }
  98. [XmlIgnore]
  99. public decimal MaxOccurs
  100. {
  101. get{ return maxOccurs; }
  102. set
  103. {
  104. MaxOccursString = value.ToString ();
  105. }
  106. }
  107. internal decimal ValidatedMinOccurs
  108. {
  109. get { return validatedMinOccurs; }
  110. }
  111. internal decimal ValidatedMaxOccurs
  112. {
  113. get { return validatedMaxOccurs; }
  114. }
  115. internal virtual XmlSchemaParticle ActualParticle
  116. {
  117. get { return this; }
  118. }
  119. #endregion
  120. internal void CompileOccurence (ValidationEventHandler h, XmlSchema schema)
  121. {
  122. if (MinOccurs > MaxOccurs && !(MaxOccurs == 0 && MinOccursString == null))
  123. error(h,"minOccurs must be less than or equal to maxOccurs");
  124. else {
  125. if (MaxOccursString == "unbounded")
  126. this.validatedMaxOccurs = decimal.MaxValue;
  127. else
  128. this.validatedMaxOccurs = maxOccurs;
  129. if (this.validatedMaxOccurs == 0)
  130. this.validatedMinOccurs = 0;
  131. else
  132. this.validatedMinOccurs = minOccurs;
  133. }
  134. }
  135. internal virtual void ValidateOccurenceRangeOK (XmlSchemaParticle other,
  136. ValidationEventHandler h, XmlSchema schema)
  137. {
  138. if ((this.ValidatedMinOccurs < other.ValidatedMinOccurs) ||
  139. (other.ValidatedMaxOccurs != decimal.MaxValue &&
  140. this.ValidatedMaxOccurs > other.ValidatedMaxOccurs))
  141. error (h, "Invalid derivation occurence range was found.");
  142. }
  143. internal virtual decimal GetMinEffectiveTotalRange ()
  144. {
  145. return 0;
  146. }
  147. internal decimal GetMinEffectiveTotalRangeAllAndSequence ()
  148. {
  149. if (minEffectiveTotalRange >= 0)
  150. return minEffectiveTotalRange;
  151. decimal product = 0; //this.ValidatedMinOccurs;
  152. XmlSchemaObjectCollection col = null;
  153. if (this is XmlSchemaAll)
  154. col = ((XmlSchemaAll) this).Items;
  155. else
  156. col = ((XmlSchemaSequence) this).Items;
  157. foreach (XmlSchemaParticle p in col)
  158. product += p.GetMinEffectiveTotalRange ();
  159. minEffectiveTotalRange = product;
  160. return product;
  161. }
  162. internal virtual bool ValidateIsEmptiable ()
  163. {
  164. return this.validatedMinOccurs == 0 || this.GetMinEffectiveTotalRange () == 0;
  165. }
  166. internal abstract void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  167. ValidationEventHandler h, XmlSchema schema);
  168. internal abstract void ValidateUniqueParticleAttribution (
  169. XmlSchemaObjectTable qnames, ArrayList nsNames,
  170. ValidationEventHandler h, XmlSchema schema);
  171. internal abstract void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  172. ValidationEventHandler h, XmlSchema schema);
  173. // See http://www.thaiopensource.com/relaxng/simplify.html
  174. internal abstract void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema);
  175. #region Internal Class
  176. public class XmlSchemaParticleEmpty : XmlSchemaParticle
  177. {
  178. internal XmlSchemaParticleEmpty ()
  179. {
  180. }
  181. internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  182. ValidationEventHandler h, XmlSchema schema)
  183. {
  184. // TODO
  185. }
  186. internal override void CheckRecursion (int depth,
  187. ValidationEventHandler h, XmlSchema schema)
  188. {
  189. // do nothing
  190. }
  191. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames,
  192. ArrayList nsNames, ValidationEventHandler h, XmlSchema schema)
  193. {
  194. // do nothing
  195. }
  196. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  197. ValidationEventHandler h, XmlSchema schema)
  198. {
  199. // do nothing
  200. }
  201. }
  202. #endregion
  203. }
  204. }