2
0

XmlSchemaParticle.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. if (value == decimal.MaxValue)
  126. MaxOccursString = "unbounded";
  127. else
  128. MaxOccursString = value.ToString (CultureInfo.InvariantCulture);
  129. }
  130. }
  131. internal decimal ValidatedMinOccurs
  132. {
  133. get { return validatedMinOccurs; }
  134. }
  135. internal decimal ValidatedMaxOccurs
  136. {
  137. get { return validatedMaxOccurs; }
  138. // set { validatedMaxOccurs = value; }
  139. }
  140. #endregion
  141. internal XmlSchemaParticle OptimizedParticle;
  142. internal virtual XmlSchemaParticle GetOptimizedParticle (bool isTop)
  143. {
  144. return null;
  145. }
  146. internal XmlSchemaParticle GetShallowClone ()
  147. {
  148. return (XmlSchemaParticle) MemberwiseClone ();
  149. }
  150. internal void CompileOccurence (ValidationEventHandler h, XmlSchema schema)
  151. {
  152. if (MinOccurs > MaxOccurs && !(MaxOccurs == 0 && MinOccursString == null))
  153. error(h,"minOccurs must be less than or equal to maxOccurs");
  154. else {
  155. if (MaxOccursString == "unbounded")
  156. this.validatedMaxOccurs = decimal.MaxValue;
  157. else
  158. this.validatedMaxOccurs = maxOccurs;
  159. if (this.validatedMaxOccurs == 0)
  160. this.validatedMinOccurs = 0;
  161. else
  162. this.validatedMinOccurs = minOccurs;
  163. }
  164. }
  165. internal override void CopyInfo (XmlSchemaParticle obj)
  166. {
  167. base.CopyInfo (obj);
  168. if (MaxOccursString == "unbounded")
  169. obj.maxOccurs = obj.validatedMaxOccurs = decimal.MaxValue;
  170. else
  171. obj.maxOccurs = obj.validatedMaxOccurs = this.ValidatedMaxOccurs;
  172. if (MaxOccurs == 0)
  173. obj.minOccurs = obj.validatedMinOccurs = 0;
  174. else
  175. obj.minOccurs = obj.validatedMinOccurs = this.ValidatedMinOccurs;
  176. if (MinOccursString != null)
  177. obj.MinOccursString = MinOccursString;
  178. if (MaxOccursString != null)
  179. obj.MaxOccursString = MaxOccursString;
  180. }
  181. internal virtual bool ValidateOccurenceRangeOK (XmlSchemaParticle other,
  182. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  183. {
  184. if ((this.ValidatedMinOccurs < other.ValidatedMinOccurs) ||
  185. (other.ValidatedMaxOccurs != decimal.MaxValue &&
  186. this.ValidatedMaxOccurs > other.ValidatedMaxOccurs)) {
  187. if (raiseError)
  188. error (h, "Invalid derivation occurence range was found.");
  189. return false;
  190. }
  191. return true;
  192. }
  193. internal virtual decimal GetMinEffectiveTotalRange ()
  194. {
  195. return ValidatedMinOccurs;
  196. }
  197. internal decimal GetMinEffectiveTotalRangeAllAndSequence ()
  198. {
  199. if (minEffectiveTotalRange >= 0)
  200. return minEffectiveTotalRange;
  201. decimal product = 0; //this.ValidatedMinOccurs;
  202. XmlSchemaObjectCollection col = null;
  203. if (this is XmlSchemaAll)
  204. col = ((XmlSchemaAll) this).Items;
  205. else
  206. col = ((XmlSchemaSequence) this).Items;
  207. foreach (XmlSchemaParticle p in col)
  208. product += p.GetMinEffectiveTotalRange ();
  209. minEffectiveTotalRange = product;
  210. return product;
  211. }
  212. // 3.9.6 Particle Emptiable
  213. internal virtual bool ValidateIsEmptiable ()
  214. {
  215. return this.validatedMinOccurs == 0 || this.GetMinEffectiveTotalRange () == 0;
  216. }
  217. internal virtual bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  218. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  219. {
  220. return false;
  221. }
  222. internal virtual void ValidateUniqueParticleAttribution (
  223. XmlSchemaObjectTable qnames, ArrayList nsNames,
  224. ValidationEventHandler h, XmlSchema schema)
  225. {
  226. }
  227. internal virtual void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  228. ValidationEventHandler h, XmlSchema schema)
  229. {
  230. }
  231. // See http://www.thaiopensource.com/relaxng/simplify.html
  232. internal virtual void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  233. {
  234. }
  235. internal virtual bool ParticleEquals (XmlSchemaParticle other)
  236. {
  237. return false;
  238. }
  239. #region Internal Class
  240. internal class EmptyParticle : XmlSchemaParticle
  241. {
  242. internal EmptyParticle ()
  243. {
  244. }
  245. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  246. {
  247. return this;
  248. }
  249. internal override bool ParticleEquals (XmlSchemaParticle other)
  250. {
  251. return other == this || other == XmlSchemaParticle.Empty;
  252. }
  253. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  254. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  255. {
  256. return true;
  257. }
  258. internal override void CheckRecursion (int depth,
  259. ValidationEventHandler h, XmlSchema schema)
  260. {
  261. // do nothing
  262. }
  263. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames,
  264. ArrayList nsNames, ValidationEventHandler h, XmlSchema schema)
  265. {
  266. // do nothing
  267. }
  268. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  269. ValidationEventHandler h, XmlSchema schema)
  270. {
  271. // do nothing
  272. }
  273. }
  274. #endregion
  275. }
  276. }