XmlSchemaParticle.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. internal static XmlSchemaParticle Empty {
  14. get {
  15. if (empty == null) {
  16. empty = new EmptyParticle ();
  17. }
  18. return empty;
  19. }
  20. }
  21. decimal minOccurs, maxOccurs;
  22. string minstr, maxstr;
  23. static XmlSchemaParticle empty;
  24. decimal validatedMinOccurs = 1, validatedMaxOccurs = 1;
  25. internal int recursionDepth = -1;
  26. private decimal minEffectiveTotalRange = -1;
  27. internal bool parentIsGroupDefinition;
  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. #endregion
  116. internal XmlSchemaParticle OptimizedParticle;
  117. internal abstract XmlSchemaParticle GetOptimizedParticle (bool isTop);
  118. internal XmlSchemaParticle GetShallowClone ()
  119. {
  120. return (XmlSchemaParticle) MemberwiseClone ();
  121. }
  122. internal void CompileOccurence (ValidationEventHandler h, XmlSchema schema)
  123. {
  124. if (MinOccurs > MaxOccurs && !(MaxOccurs == 0 && MinOccursString == null))
  125. error(h,"minOccurs must be less than or equal to maxOccurs");
  126. else {
  127. if (MaxOccursString == "unbounded")
  128. this.validatedMaxOccurs = decimal.MaxValue;
  129. else
  130. this.validatedMaxOccurs = maxOccurs;
  131. if (this.validatedMaxOccurs == 0)
  132. this.validatedMinOccurs = 0;
  133. else
  134. this.validatedMinOccurs = minOccurs;
  135. }
  136. }
  137. internal override void CopyInfo (XmlSchemaParticle obj)
  138. {
  139. base.CopyInfo (obj);
  140. if (MaxOccursString == "unbounded")
  141. obj.maxOccurs = obj.validatedMaxOccurs = decimal.MaxValue;
  142. else
  143. obj.maxOccurs = obj.validatedMaxOccurs = this.ValidatedMaxOccurs;
  144. if (MaxOccurs == 0)
  145. obj.minOccurs = obj.validatedMinOccurs = 0;
  146. else
  147. obj.minOccurs = obj.validatedMinOccurs = this.ValidatedMinOccurs;
  148. if (MinOccursString != null)
  149. obj.MinOccursString = MinOccursString;
  150. if (MaxOccursString != null)
  151. obj.MaxOccursString = MaxOccursString;
  152. }
  153. internal virtual bool ValidateOccurenceRangeOK (XmlSchemaParticle other,
  154. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  155. {
  156. if ((this.ValidatedMinOccurs < other.ValidatedMinOccurs) ||
  157. (other.ValidatedMaxOccurs != decimal.MaxValue &&
  158. this.ValidatedMaxOccurs > other.ValidatedMaxOccurs)) {
  159. if (raiseError)
  160. error (h, "Invalid derivation occurence range was found.");
  161. return false;
  162. }
  163. return true;
  164. }
  165. internal virtual decimal GetMinEffectiveTotalRange ()
  166. {
  167. return ValidatedMinOccurs;
  168. }
  169. internal decimal GetMinEffectiveTotalRangeAllAndSequence ()
  170. {
  171. if (minEffectiveTotalRange >= 0)
  172. return minEffectiveTotalRange;
  173. decimal product = 0; //this.ValidatedMinOccurs;
  174. XmlSchemaObjectCollection col = null;
  175. if (this is XmlSchemaAll)
  176. col = ((XmlSchemaAll) this).Items;
  177. else
  178. col = ((XmlSchemaSequence) this).Items;
  179. foreach (XmlSchemaParticle p in col)
  180. product += p.GetMinEffectiveTotalRange ();
  181. minEffectiveTotalRange = product;
  182. return product;
  183. }
  184. // 3.9.6 Particle Emptiable
  185. internal virtual bool ValidateIsEmptiable ()
  186. {
  187. return this.validatedMinOccurs == 0 || this.GetMinEffectiveTotalRange () == 0;
  188. }
  189. internal abstract bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  190. ValidationEventHandler h, XmlSchema schema, bool raiseError);
  191. internal abstract void ValidateUniqueParticleAttribution (
  192. XmlSchemaObjectTable qnames, ArrayList nsNames,
  193. ValidationEventHandler h, XmlSchema schema);
  194. internal abstract void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  195. ValidationEventHandler h, XmlSchema schema);
  196. // See http://www.thaiopensource.com/relaxng/simplify.html
  197. internal abstract void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema);
  198. internal abstract bool ParticleEquals (XmlSchemaParticle other);
  199. #region Internal Class
  200. internal class EmptyParticle : XmlSchemaParticle
  201. {
  202. internal EmptyParticle ()
  203. {
  204. }
  205. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  206. {
  207. return this;
  208. }
  209. internal override bool ParticleEquals (XmlSchemaParticle other)
  210. {
  211. return other == this || other == XmlSchemaParticle.Empty;
  212. }
  213. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  214. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  215. {
  216. return true;
  217. }
  218. internal override void CheckRecursion (int depth,
  219. ValidationEventHandler h, XmlSchema schema)
  220. {
  221. // do nothing
  222. }
  223. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames,
  224. ArrayList nsNames, ValidationEventHandler h, XmlSchema schema)
  225. {
  226. // do nothing
  227. }
  228. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  229. ValidationEventHandler h, XmlSchema schema)
  230. {
  231. // do nothing
  232. }
  233. }
  234. #endregion
  235. }
  236. }