XmlSchemaParticle.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Collections;
  25. using System.Globalization;
  26. using System.Xml.Serialization;
  27. namespace System.Xml.Schema
  28. {
  29. /// <summary>
  30. /// Summary description for XmlSchemaParticle.
  31. /// </summary>
  32. public abstract class XmlSchemaParticle : XmlSchemaAnnotated
  33. {
  34. internal static XmlSchemaParticle Empty {
  35. get {
  36. if (empty == null) {
  37. empty = new EmptyParticle ();
  38. }
  39. return empty;
  40. }
  41. }
  42. decimal minOccurs, maxOccurs;
  43. string minstr, maxstr;
  44. static XmlSchemaParticle empty;
  45. decimal validatedMinOccurs = 1, validatedMaxOccurs = 1;
  46. internal int recursionDepth = -1;
  47. private decimal minEffectiveTotalRange = -1;
  48. internal bool parentIsGroupDefinition;
  49. protected XmlSchemaParticle()
  50. {
  51. minOccurs = decimal.One;
  52. maxOccurs = decimal.One;
  53. }
  54. #region Attributes
  55. [System.Xml.Serialization.XmlAttribute("minOccurs")]
  56. public string MinOccursString
  57. {
  58. get{ return minstr; }
  59. set
  60. {
  61. if (value == null) {
  62. minOccurs = decimal.One;
  63. minstr = value;
  64. return;
  65. }
  66. decimal val = decimal.Parse (value, CultureInfo.InvariantCulture);
  67. if(val >= 0 && (val == Decimal.Truncate(val)))
  68. {
  69. minOccurs = val;
  70. minstr = val.ToString (CultureInfo.InvariantCulture);
  71. }
  72. else
  73. {
  74. throw new XmlSchemaException
  75. ("MinOccursString must be a non-negative number",null);
  76. }
  77. }
  78. }
  79. [System.Xml.Serialization.XmlAttribute("maxOccurs")]
  80. public string MaxOccursString
  81. {
  82. get{ return maxstr; }
  83. set
  84. {
  85. if(value == "unbounded")
  86. {
  87. maxstr = value;
  88. maxOccurs = decimal.MaxValue;
  89. }
  90. else
  91. {
  92. decimal val = decimal.Parse (value, CultureInfo.InvariantCulture);
  93. if(val >= 0 && (val == Decimal.Truncate(val)))
  94. {
  95. maxOccurs = val;
  96. maxstr = val.ToString (CultureInfo.InvariantCulture);
  97. }
  98. else
  99. {
  100. throw new XmlSchemaException
  101. ("MaxOccurs must be a non-negative integer",null);
  102. }
  103. if (val == 0 && minstr == null)
  104. minOccurs = 0;
  105. }
  106. }
  107. }
  108. #endregion
  109. #region XmlIgnore
  110. [XmlIgnore]
  111. public decimal MinOccurs
  112. {
  113. get{ return minOccurs; }
  114. set
  115. {
  116. MinOccursString = value.ToString (CultureInfo.InvariantCulture);
  117. }
  118. }
  119. [XmlIgnore]
  120. public decimal MaxOccurs
  121. {
  122. get{ return maxOccurs; }
  123. set
  124. {
  125. MaxOccursString = value.ToString (CultureInfo.InvariantCulture);
  126. }
  127. }
  128. internal decimal ValidatedMinOccurs
  129. {
  130. get { return validatedMinOccurs; }
  131. }
  132. internal decimal ValidatedMaxOccurs
  133. {
  134. get { return validatedMaxOccurs; }
  135. // set { validatedMaxOccurs = value; }
  136. }
  137. #endregion
  138. internal XmlSchemaParticle OptimizedParticle;
  139. internal abstract XmlSchemaParticle GetOptimizedParticle (bool isTop);
  140. internal XmlSchemaParticle GetShallowClone ()
  141. {
  142. return (XmlSchemaParticle) MemberwiseClone ();
  143. }
  144. internal void CompileOccurence (ValidationEventHandler h, XmlSchema schema)
  145. {
  146. if (MinOccurs > MaxOccurs && !(MaxOccurs == 0 && MinOccursString == null))
  147. error(h,"minOccurs must be less than or equal to maxOccurs");
  148. else {
  149. if (MaxOccursString == "unbounded")
  150. this.validatedMaxOccurs = decimal.MaxValue;
  151. else
  152. this.validatedMaxOccurs = maxOccurs;
  153. if (this.validatedMaxOccurs == 0)
  154. this.validatedMinOccurs = 0;
  155. else
  156. this.validatedMinOccurs = minOccurs;
  157. }
  158. }
  159. internal override void CopyInfo (XmlSchemaParticle obj)
  160. {
  161. base.CopyInfo (obj);
  162. if (MaxOccursString == "unbounded")
  163. obj.maxOccurs = obj.validatedMaxOccurs = decimal.MaxValue;
  164. else
  165. obj.maxOccurs = obj.validatedMaxOccurs = this.ValidatedMaxOccurs;
  166. if (MaxOccurs == 0)
  167. obj.minOccurs = obj.validatedMinOccurs = 0;
  168. else
  169. obj.minOccurs = obj.validatedMinOccurs = this.ValidatedMinOccurs;
  170. if (MinOccursString != null)
  171. obj.MinOccursString = MinOccursString;
  172. if (MaxOccursString != null)
  173. obj.MaxOccursString = MaxOccursString;
  174. }
  175. internal virtual bool ValidateOccurenceRangeOK (XmlSchemaParticle other,
  176. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  177. {
  178. if ((this.ValidatedMinOccurs < other.ValidatedMinOccurs) ||
  179. (other.ValidatedMaxOccurs != decimal.MaxValue &&
  180. this.ValidatedMaxOccurs > other.ValidatedMaxOccurs)) {
  181. if (raiseError)
  182. error (h, "Invalid derivation occurence range was found.");
  183. return false;
  184. }
  185. return true;
  186. }
  187. internal virtual decimal GetMinEffectiveTotalRange ()
  188. {
  189. return ValidatedMinOccurs;
  190. }
  191. internal decimal GetMinEffectiveTotalRangeAllAndSequence ()
  192. {
  193. if (minEffectiveTotalRange >= 0)
  194. return minEffectiveTotalRange;
  195. decimal product = 0; //this.ValidatedMinOccurs;
  196. XmlSchemaObjectCollection col = null;
  197. if (this is XmlSchemaAll)
  198. col = ((XmlSchemaAll) this).Items;
  199. else
  200. col = ((XmlSchemaSequence) this).Items;
  201. foreach (XmlSchemaParticle p in col)
  202. product += p.GetMinEffectiveTotalRange ();
  203. minEffectiveTotalRange = product;
  204. return product;
  205. }
  206. // 3.9.6 Particle Emptiable
  207. internal virtual bool ValidateIsEmptiable ()
  208. {
  209. return this.validatedMinOccurs == 0 || this.GetMinEffectiveTotalRange () == 0;
  210. }
  211. internal abstract bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  212. ValidationEventHandler h, XmlSchema schema, bool raiseError);
  213. internal abstract void ValidateUniqueParticleAttribution (
  214. XmlSchemaObjectTable qnames, ArrayList nsNames,
  215. ValidationEventHandler h, XmlSchema schema);
  216. internal abstract void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  217. ValidationEventHandler h, XmlSchema schema);
  218. // See http://www.thaiopensource.com/relaxng/simplify.html
  219. internal abstract void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema);
  220. internal abstract bool ParticleEquals (XmlSchemaParticle other);
  221. #region Internal Class
  222. internal class EmptyParticle : XmlSchemaParticle
  223. {
  224. internal EmptyParticle ()
  225. {
  226. }
  227. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  228. {
  229. return this;
  230. }
  231. internal override bool ParticleEquals (XmlSchemaParticle other)
  232. {
  233. return other == this || other == XmlSchemaParticle.Empty;
  234. }
  235. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  236. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  237. {
  238. return true;
  239. }
  240. internal override void CheckRecursion (int depth,
  241. ValidationEventHandler h, XmlSchema schema)
  242. {
  243. // do nothing
  244. }
  245. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames,
  246. ArrayList nsNames, ValidationEventHandler h, XmlSchema schema)
  247. {
  248. // do nothing
  249. }
  250. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  251. ValidationEventHandler h, XmlSchema schema)
  252. {
  253. // do nothing
  254. }
  255. }
  256. #endregion
  257. }
  258. }