2
0

XmlSchemaGroupBase.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // XmlSchemaGroupBase.cs
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Xml.Serialization;
  30. namespace System.Xml.Schema
  31. {
  32. public abstract class XmlSchemaGroupBase : XmlSchemaParticle
  33. {
  34. private XmlSchemaObjectCollection compiledItems;
  35. protected XmlSchemaGroupBase ()
  36. {
  37. compiledItems = new XmlSchemaObjectCollection ();
  38. }
  39. [XmlIgnore]
  40. public abstract XmlSchemaObjectCollection Items { get; }
  41. internal XmlSchemaObjectCollection CompiledItems
  42. {
  43. get{ return compiledItems; }
  44. }
  45. internal void CopyOptimizedItems (XmlSchemaGroupBase gb)
  46. {
  47. for (int i = 0; i < Items.Count; i++) {
  48. XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
  49. p = p.GetOptimizedParticle (false);
  50. if (p == XmlSchemaParticle.Empty)
  51. continue;
  52. gb.Items.Add (p);
  53. gb.CompiledItems.Add (p);
  54. }
  55. }
  56. internal override bool ParticleEquals (XmlSchemaParticle other)
  57. {
  58. XmlSchemaGroupBase gb = other as XmlSchemaGroupBase;
  59. if (gb == null)
  60. return false;
  61. if (this.GetType () != gb.GetType ())
  62. return false;
  63. if (this.ValidatedMaxOccurs != gb.ValidatedMaxOccurs ||
  64. this.ValidatedMinOccurs != gb.ValidatedMinOccurs)
  65. return false;
  66. if (this.CompiledItems.Count != gb.CompiledItems.Count)
  67. return false;
  68. for (int i = 0; i < CompiledItems.Count; i++) {
  69. XmlSchemaParticle p1 = this.CompiledItems [i] as XmlSchemaParticle;
  70. XmlSchemaParticle p2 = gb.CompiledItems [i] as XmlSchemaParticle;
  71. if (!p1.ParticleEquals (p2))
  72. return false;
  73. }
  74. return true;
  75. }
  76. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  77. {
  78. foreach (XmlSchemaParticle p in this.Items)
  79. p.CheckRecursion (depth, h, schema);
  80. }
  81. internal bool ValidateNSRecurseCheckCardinality (XmlSchemaAny any,
  82. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  83. {
  84. foreach (XmlSchemaParticle p in Items)
  85. if (!p.ValidateDerivationByRestriction (any, h, schema, raiseError))
  86. return false;
  87. return ValidateOccurenceRangeOK (any, h, schema, raiseError);
  88. }
  89. internal bool ValidateRecurse (XmlSchemaGroupBase baseGroup,
  90. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  91. {
  92. return ValidateSeqRecurseMapSumCommon (baseGroup, h, schema, false, false, raiseError);
  93. }
  94. internal bool ValidateSeqRecurseMapSumCommon (XmlSchemaGroupBase baseGroup,
  95. ValidationEventHandler h, XmlSchema schema, bool isLax, bool isMapAndSum, bool raiseError)
  96. {
  97. int index = 0;
  98. int baseIndex = 0;
  99. decimal baseOccured = 0;
  100. if (baseGroup.CompiledItems.Count == 0 && this.CompiledItems.Count > 0) {
  101. if (raiseError)
  102. error (h, "Invalid particle derivation by restriction was found. base particle does not contain particles.");
  103. return false;
  104. }
  105. for (int i = 0; i < CompiledItems.Count; i++) {
  106. // get non-empty derived particle
  107. XmlSchemaParticle pd = null;
  108. while (this.CompiledItems.Count > index) {
  109. pd = ((XmlSchemaParticle) this.CompiledItems [index]);//.GetOptimizedParticle (false);
  110. if (pd != XmlSchemaParticle.Empty)// && pd.ValidatedMaxOccurs > 0)
  111. break;
  112. else
  113. index++;
  114. }
  115. if (index >= CompiledItems.Count) {
  116. if (raiseError)
  117. error (h, "Invalid particle derivation by restriction was found. Cannot be mapped to base particle.");
  118. return false;
  119. }
  120. // get non-empty base particle
  121. XmlSchemaParticle pb = null;
  122. while (baseGroup.CompiledItems.Count > baseIndex) {
  123. pb = ((XmlSchemaParticle) baseGroup.CompiledItems [baseIndex]);//.GetOptimizedParticle (false);
  124. if (pb == XmlSchemaParticle.Empty && pb.ValidatedMaxOccurs > 0)
  125. continue;
  126. if (!pd.ValidateDerivationByRestriction (pb, h, schema, false)) {
  127. if (!isLax && !isMapAndSum && pb.MinOccurs > baseOccured && !pb.ValidateIsEmptiable ()) {
  128. if (raiseError)
  129. error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.");
  130. return false;
  131. }
  132. else {
  133. baseOccured = 0;
  134. baseIndex++;
  135. }
  136. } else {
  137. baseOccured += pb.ValidatedMinOccurs;
  138. if (baseOccured >= baseGroup.ValidatedMaxOccurs) {
  139. baseOccured = 0;
  140. baseIndex++;
  141. }
  142. index++;
  143. break;
  144. }
  145. }
  146. }
  147. if (this.CompiledItems.Count > 0 && index != this.CompiledItems.Count) {
  148. if (raiseError)
  149. error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");
  150. return false;
  151. }
  152. if (!isLax && !isMapAndSum) {
  153. if (baseOccured > 0)
  154. baseIndex++;
  155. for (int i = baseIndex; i < baseGroup.CompiledItems.Count; i++) {
  156. XmlSchemaParticle p = baseGroup.CompiledItems [i] as XmlSchemaParticle;
  157. if (!p.ValidateIsEmptiable ()) {
  158. if (raiseError)
  159. error (h, "Invalid particle derivation by restriction was found. There is a base particle which does not have mapped derived particle and is not emptiable.");
  160. return false;
  161. }
  162. }
  163. }
  164. return true;
  165. }
  166. }
  167. }