XmlIteratorNodeList.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // System.Xml.XmlIteratorNodeList
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2006 Novell Inc.
  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. using System.Xml.XPath;
  32. namespace System.Xml
  33. {
  34. internal class XmlIteratorNodeList : XmlNodeList
  35. {
  36. XPathNodeIterator source;
  37. XPathNodeIterator iterator;
  38. XmlNode previous;
  39. ArrayList list;
  40. bool finished;
  41. #region Constructors
  42. public XmlIteratorNodeList (XPathNodeIterator iter)
  43. {
  44. source = iter;
  45. iterator = iter.Clone ();
  46. list = new ArrayList ();
  47. }
  48. #endregion
  49. #region Properties
  50. public override int Count {
  51. get {
  52. /*
  53. // The performance on Count depends on the
  54. // iterator which is actually used. In some
  55. // iterators, getting iterator.Count is much
  56. // faster.
  57. // With (current) implementation in general,
  58. // those iterators that requires sorting is
  59. // likely to have already-computed arrays, so
  60. // for them getting Count does not impact on
  61. // performance.
  62. // But by default, getting iterator.Count means
  63. // it internally iterates all the nodes. That
  64. // might result in duplicate iteration (so
  65. // ineffective). So here I decided that it
  66. // just collects all the nodes to the list.
  67. if (!finished) {
  68. BaseIterator iter = iterator as BaseIterator;
  69. if (iter != null && iter.ReverseAxis || iter is SlashIterator)
  70. return iter.Count;
  71. while (iterator.MoveNext ())
  72. list.Add (((IHasXmlNode) iterator.Current).GetNode ());
  73. finished = true;
  74. }
  75. return list.Count;
  76. */
  77. // anyways such code that uses
  78. // XmlNodeList.Count already gives up
  79. // performance. Also, storing things in the
  80. // list causes extra memory consumption.
  81. return iterator.Count;
  82. }
  83. }
  84. #endregion
  85. #region Methods
  86. public override IEnumerator GetEnumerator ()
  87. {
  88. if (finished)
  89. return list.GetEnumerator ();
  90. else
  91. return new XPathNodeIteratorNodeListIterator (source);
  92. // return new XPathNodeIteratorNodeListIterator2 (this);
  93. }
  94. public override XmlNode Item (int index)
  95. {
  96. if (index < 0)
  97. return null;
  98. if (index < list.Count)
  99. return (XmlNode) list [index];
  100. index++;
  101. while (iterator.CurrentPosition < index) {
  102. if (!iterator.MoveNext ()) {
  103. finished = true;
  104. return null;
  105. }
  106. list.Add (((IHasXmlNode) iterator.Current).GetNode ());
  107. }
  108. return (XmlNode) list [index - 1];
  109. }
  110. #endregion
  111. class XPathNodeIteratorNodeListIterator : IEnumerator
  112. {
  113. XPathNodeIterator iter;
  114. XPathNodeIterator source;
  115. public XPathNodeIteratorNodeListIterator (XPathNodeIterator source)
  116. {
  117. this.source = source;
  118. Reset ();
  119. }
  120. public bool MoveNext ()
  121. {
  122. return iter.MoveNext ();
  123. }
  124. public object Current {
  125. get { return ((IHasXmlNode) iter.Current).GetNode (); }
  126. }
  127. public void Reset ()
  128. {
  129. iter = source.Clone ();
  130. }
  131. }
  132. /*
  133. class XPathNodeIteratorNodeListIterator2 : IEnumerator
  134. {
  135. int current = -1;
  136. XmlIteratorNodeList source;
  137. public XPathNodeIteratorNodeListIterator2 (XmlIteratorNodeList source)
  138. {
  139. this.source = source;
  140. }
  141. public bool MoveNext ()
  142. {
  143. return source [++current] != null;
  144. }
  145. public object Current {
  146. get { return source [current]; }
  147. }
  148. public void Reset ()
  149. {
  150. current = -1;
  151. }
  152. }
  153. */
  154. }
  155. }