SubtreeXmlReader.cs 6.2 KB

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