XmlSchemaGroupBase.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // XmlSchemaGroupBase.cs
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Xml.Serialization;
  10. namespace System.Xml.Schema
  11. {
  12. public abstract class XmlSchemaGroupBase : XmlSchemaParticle
  13. {
  14. private XmlSchemaObjectCollection compiledItems;
  15. protected XmlSchemaGroupBase ()
  16. {
  17. compiledItems = new XmlSchemaObjectCollection ();
  18. }
  19. [XmlIgnore]
  20. public abstract XmlSchemaObjectCollection Items { get; }
  21. internal XmlSchemaObjectCollection CompiledItems
  22. {
  23. get{ return compiledItems; }
  24. }
  25. internal void CopyOptimizedItems (XmlSchemaGroupBase gb)
  26. {
  27. for (int i = 0; i < Items.Count; i++) {
  28. XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
  29. p = p.GetOptimizedParticle (false);
  30. if (p == XmlSchemaParticle.Empty)
  31. continue;
  32. gb.Items.Add (p);
  33. gb.CompiledItems.Add (p);
  34. }
  35. }
  36. internal override bool ParticleEquals (XmlSchemaParticle other)
  37. {
  38. XmlSchemaGroupBase gb = other as XmlSchemaGroupBase;
  39. if (gb == null)
  40. return false;
  41. if (this.GetType () != gb.GetType ())
  42. return false;
  43. if (this.ValidatedMaxOccurs != gb.ValidatedMaxOccurs ||
  44. this.ValidatedMinOccurs != gb.ValidatedMinOccurs)
  45. return false;
  46. if (this.CompiledItems.Count != gb.CompiledItems.Count)
  47. return false;
  48. for (int i = 0; i < CompiledItems.Count; i++) {
  49. XmlSchemaParticle p1 = this.CompiledItems [i] as XmlSchemaParticle;
  50. XmlSchemaParticle p2 = gb.CompiledItems [i] as XmlSchemaParticle;
  51. if (!p1.ParticleEquals (p2))
  52. return false;
  53. }
  54. return true;
  55. }
  56. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  57. {
  58. foreach (XmlSchemaParticle p in this.Items)
  59. p.CheckRecursion (depth, h, schema);
  60. }
  61. internal bool ValidateNSRecurseCheckCardinality (XmlSchemaAny any,
  62. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  63. {
  64. foreach (XmlSchemaParticle p in Items)
  65. if (!p.ValidateDerivationByRestriction (any, h, schema, raiseError))
  66. return false;
  67. return ValidateOccurenceRangeOK (any, h, schema, raiseError);
  68. }
  69. internal bool ValidateRecurse (XmlSchemaGroupBase baseGroup,
  70. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  71. {
  72. return ValidateSeqRecurseMapSumCommon (baseGroup, h, schema, false, false, raiseError);
  73. }
  74. internal bool ValidateSeqRecurseMapSumCommon (XmlSchemaGroupBase baseGroup,
  75. ValidationEventHandler h, XmlSchema schema, bool isLax, bool isMapAndSum, bool raiseError)
  76. {
  77. int index = 0;
  78. int baseIndex = 0;
  79. decimal baseOccured = 0;
  80. if (baseGroup.CompiledItems.Count == 0 && this.CompiledItems.Count > 0) {
  81. if (raiseError)
  82. error (h, "Invalid particle derivation by restriction was found. base particle does not contain particles.");
  83. return false;
  84. }
  85. for (int i = 0; i < CompiledItems.Count; i++) {
  86. // get non-empty derived particle
  87. XmlSchemaParticle pd = null;
  88. while (this.CompiledItems.Count > index) {
  89. pd = ((XmlSchemaParticle) this.CompiledItems [index]);//.GetOptimizedParticle (false);
  90. if (pd != XmlSchemaParticle.Empty)// && pd.ValidatedMaxOccurs > 0)
  91. break;
  92. else
  93. index++;
  94. }
  95. if (index >= CompiledItems.Count) {
  96. if (raiseError)
  97. error (h, "Invalid particle derivation by restriction was found. Cannot be mapped to base particle.");
  98. return false;
  99. }
  100. // get non-empty base particle
  101. XmlSchemaParticle pb = null;
  102. while (baseGroup.CompiledItems.Count > baseIndex) {
  103. pb = ((XmlSchemaParticle) baseGroup.CompiledItems [baseIndex]);//.GetOptimizedParticle (false);
  104. if (pb == XmlSchemaParticle.Empty && pb.ValidatedMaxOccurs > 0)
  105. continue;
  106. if (!pd.ValidateDerivationByRestriction (pb, h, schema, false)) {
  107. if (!isLax && !isMapAndSum && pb.MinOccurs > baseOccured && !pb.ValidateIsEmptiable ()) {
  108. if (raiseError)
  109. error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.");
  110. return false;
  111. }
  112. else {
  113. baseOccured = 0;
  114. baseIndex++;
  115. }
  116. } else {
  117. baseOccured += pb.ValidatedMinOccurs;
  118. if (baseOccured >= baseGroup.ValidatedMaxOccurs) {
  119. baseOccured = 0;
  120. baseIndex++;
  121. }
  122. index++;
  123. break;
  124. }
  125. }
  126. }
  127. if (this.CompiledItems.Count > 0 && index != this.CompiledItems.Count) {
  128. if (raiseError)
  129. error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");
  130. return false;
  131. }
  132. if (!isLax && !isMapAndSum) {
  133. if (baseOccured > 0)
  134. baseIndex++;
  135. for (int i = baseIndex; i < baseGroup.CompiledItems.Count; i++) {
  136. XmlSchemaParticle p = baseGroup.CompiledItems [i] as XmlSchemaParticle;
  137. if (!p.ValidateIsEmptiable ()) {
  138. if (raiseError)
  139. 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.");
  140. return false;
  141. }
  142. }
  143. }
  144. return true;
  145. }
  146. }
  147. }