SubtreeXmlReader.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // SubtreeXmlReader.cs - reads descendant nodes in XmlReader
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (c) 2004 Novell Inc. All rights reserved
  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. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.Xml;
  32. namespace Mono.Xml
  33. {
  34. public class SubtreeXmlReader : XmlReader, IXmlLineInfo, IXmlNamespaceResolver
  35. {
  36. int startDepth;
  37. bool eof;
  38. bool initial;
  39. bool read;
  40. XmlReader Reader;
  41. IXmlLineInfo li;
  42. IXmlNamespaceResolver nsResolver;
  43. public SubtreeXmlReader (XmlReader reader)
  44. {
  45. this.Reader = reader;
  46. li = reader as IXmlLineInfo;
  47. nsResolver = reader as IXmlNamespaceResolver;
  48. initial = true;
  49. startDepth = reader.Depth;
  50. if (reader.ReadState == ReadState.Initial)
  51. startDepth = -1; // end == the reader's end
  52. }
  53. public override int AttributeCount {
  54. get { return initial ? 0 : Reader.AttributeCount; }
  55. }
  56. public override int Depth {
  57. get { return Reader.Depth - startDepth; }
  58. }
  59. public override string BaseURI {
  60. get { return Reader.BaseURI; }
  61. }
  62. public override bool EOF {
  63. get { return eof || Reader.EOF; }
  64. }
  65. public int LineNumber {
  66. get { return initial ? 0 : li != null ? li.LineNumber : 0; }
  67. }
  68. public int LinePosition {
  69. get { return initial ? 0 : li != null ? li.LinePosition : 0; }
  70. }
  71. public override bool HasValue {
  72. get { return initial ? false : Reader.HasValue; }
  73. }
  74. public override string LocalName {
  75. get { return initial ? String.Empty : Reader.LocalName; }
  76. }
  77. public override string Name {
  78. get { return initial ? String.Empty : Reader.Name; }
  79. }
  80. public override XmlNameTable NameTable {
  81. get { return Reader.NameTable; }
  82. }
  83. public override string NamespaceURI {
  84. get { return initial ? String.Empty : Reader.NamespaceURI; }
  85. }
  86. public override XmlNodeType NodeType {
  87. get { return initial ? XmlNodeType.None : Reader.NodeType; }
  88. }
  89. public override string Prefix {
  90. get { return initial ? String.Empty : Reader.Prefix; }
  91. }
  92. public override ReadState ReadState {
  93. get { return initial ? ReadState.Initial : eof ? ReadState.EndOfFile : Reader.ReadState ; }
  94. }
  95. public override XmlReaderSettings Settings {
  96. get { return Reader.Settings; }
  97. }
  98. public override string Value {
  99. get { return initial ? String.Empty : Reader.Value; }
  100. }
  101. public override void Close ()
  102. {
  103. // do nothing
  104. }
  105. public override string GetAttribute (int i)
  106. {
  107. return initial ? null : Reader.GetAttribute (i);
  108. }
  109. public override string GetAttribute (string name)
  110. {
  111. return initial ? null : Reader.GetAttribute (name);
  112. }
  113. public override string GetAttribute (string local, string ns)
  114. {
  115. return initial ? null : Reader.GetAttribute (local, ns);
  116. }
  117. IDictionary IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
  118. {
  119. return nsResolver != null ? nsResolver.GetNamespacesInScope (scope) : new Hashtable ();
  120. }
  121. public bool HasLineInfo ()
  122. {
  123. return li != null ? li.HasLineInfo () : false;
  124. }
  125. public override string LookupNamespace (string prefix)
  126. {
  127. return Reader.LookupNamespace (prefix);
  128. }
  129. string IXmlNamespaceResolver.LookupPrefix (string ns)
  130. {
  131. return nsResolver != null ? nsResolver.LookupPrefix (ns) : String.Empty;
  132. }
  133. string IXmlNamespaceResolver.LookupPrefix (string ns, bool atomizedNames)
  134. {
  135. return nsResolver != null ? nsResolver.LookupPrefix (ns, atomizedNames) : String.Empty;
  136. }
  137. public override bool MoveToFirstAttribute ()
  138. {
  139. return initial ? false : Reader.MoveToFirstAttribute ();
  140. }
  141. public override bool MoveToNextAttribute ()
  142. {
  143. return initial ? false : Reader.MoveToNextAttribute ();
  144. }
  145. public override void MoveToAttribute (int i)
  146. {
  147. if (!initial)
  148. Reader.MoveToAttribute (i);
  149. }
  150. public override bool MoveToAttribute (string name)
  151. {
  152. return initial ? false : Reader.MoveToAttribute (name);
  153. }
  154. public override bool MoveToAttribute (string local, string ns)
  155. {
  156. return initial ? false : Reader.MoveToAttribute (local, ns);
  157. }
  158. public override bool MoveToElement ()
  159. {
  160. return initial ? false : Reader.MoveToElement ();
  161. }
  162. public override bool Read ()
  163. {
  164. if (initial) {
  165. initial = false;
  166. return true;
  167. }
  168. if (!read) {
  169. read = true;
  170. return !Reader.IsEmptyElement && Reader.Read ();
  171. }
  172. if (Reader.Depth > startDepth)
  173. if (Reader.Read ())
  174. return true;
  175. eof = true;
  176. return false;
  177. }
  178. public override bool ReadAttributeValue ()
  179. {
  180. if (initial || eof)
  181. return false;
  182. return Reader.ReadAttributeValue ();
  183. }
  184. public override void ResolveEntity ()
  185. {
  186. if (initial || eof)
  187. return;
  188. Reader.ResolveEntity ();
  189. }
  190. }
  191. }
  192. #endif