XmlNodeListChildren.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // System.Xml.XmlNodeList
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. namespace System.Xml
  32. {
  33. internal class XmlNodeListChildren : XmlNodeList
  34. {
  35. #region Enumerator
  36. private class Enumerator : IEnumerator
  37. {
  38. IHasXmlChildNode parent;
  39. XmlLinkedNode currentChild;
  40. bool passedLastNode;
  41. internal Enumerator (IHasXmlChildNode parent)
  42. {
  43. currentChild = null;
  44. this.parent = parent;
  45. passedLastNode = false;
  46. }
  47. public virtual object Current {
  48. get {
  49. if ((currentChild == null) ||
  50. (parent.LastLinkedChild == null) ||
  51. (passedLastNode == true))
  52. throw new InvalidOperationException();
  53. return currentChild;
  54. }
  55. }
  56. public virtual bool MoveNext()
  57. {
  58. bool movedNext = true;
  59. if (parent.LastLinkedChild == null) {
  60. movedNext = false;
  61. }
  62. else if (currentChild == null) {
  63. currentChild = parent.LastLinkedChild.NextLinkedSibling;
  64. }
  65. else {
  66. if (Object.ReferenceEquals(currentChild, parent.LastLinkedChild)) {
  67. movedNext = false;
  68. passedLastNode = true;
  69. }
  70. else {
  71. currentChild = currentChild.NextLinkedSibling;
  72. }
  73. }
  74. return movedNext;
  75. }
  76. public virtual void Reset()
  77. {
  78. currentChild = null;
  79. }
  80. }
  81. #endregion
  82. #region Fields
  83. IHasXmlChildNode parent;
  84. #endregion
  85. #region Constructors
  86. public XmlNodeListChildren(IHasXmlChildNode parent)
  87. {
  88. this.parent = parent;
  89. }
  90. #endregion
  91. #region Properties
  92. public override int Count {
  93. get {
  94. int count = 0;
  95. if (parent.LastLinkedChild != null) {
  96. XmlLinkedNode currentChild = parent.LastLinkedChild.NextLinkedSibling;
  97. count = 1;
  98. while (!Object.ReferenceEquals(currentChild, parent.LastLinkedChild)) {
  99. currentChild = currentChild.NextLinkedSibling;
  100. count++;
  101. }
  102. }
  103. return count;
  104. }
  105. }
  106. #endregion
  107. #region Methods
  108. public override IEnumerator GetEnumerator ()
  109. {
  110. return new Enumerator(parent);
  111. }
  112. public override XmlNode Item (int index)
  113. {
  114. XmlNode requestedNode = null;
  115. // Return null if index is out of range. by DOM design.
  116. if (Count <= index)
  117. return null;
  118. // Instead of checking for && index < Count which has to walk
  119. // the whole list to get a count, we'll just keep a count since
  120. // we have to walk the list anyways to get to index.
  121. if ((index >= 0) && (parent.LastLinkedChild != null)) {
  122. XmlLinkedNode currentChild = parent.LastLinkedChild.NextLinkedSibling;
  123. int count = 0;
  124. while ((count < index) && !Object.ReferenceEquals(currentChild, parent.LastLinkedChild))
  125. {
  126. currentChild = currentChild.NextLinkedSibling;
  127. count++;
  128. }
  129. if (count == index) {
  130. requestedNode = currentChild;
  131. }
  132. }
  133. return requestedNode;
  134. }
  135. #endregion
  136. }
  137. }