2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / Mono.Xml / SubtreeXmlReader.cs
blob38919bebb8c92ba2849590d77b119a6be7581bdf
1 //
2 // SubtreeXmlReader.cs - reads descendant nodes in XmlReader
3 //
4 // Author:
5 // Atsushi Enomoto <atsushi@ximian.com>
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.
29 #if NET_2_0
31 using System;
32 using System.Collections.Generic;
33 using System.Xml;
34 using System.Xml.Schema;
36 namespace Mono.Xml
38 internal class SubtreeXmlReader : XmlReader, IXmlLineInfo, IXmlNamespaceResolver
40 int startDepth;
41 bool eof;
42 bool initial;
43 bool read;
44 XmlReader Reader;
45 IXmlLineInfo li;
46 IXmlNamespaceResolver nsResolver;
48 public SubtreeXmlReader (XmlReader reader)
50 this.Reader = reader;
51 li = reader as IXmlLineInfo;
52 nsResolver = reader as IXmlNamespaceResolver;
53 initial = true;
54 startDepth = reader.Depth;
55 if (reader.ReadState == ReadState.Initial)
56 startDepth = -1; // end == the reader's end
59 public override int AttributeCount {
60 get { return initial ? 0 : Reader.AttributeCount; }
63 public override bool CanReadBinaryContent {
64 get { return Reader.CanReadBinaryContent; }
67 public override bool CanReadValueChunk {
68 get { return Reader.CanReadValueChunk; }
71 public override int Depth {
72 get { return Reader.Depth - startDepth; }
75 public override string BaseURI {
76 get { return Reader.BaseURI; }
79 public override bool EOF {
80 get { return eof || Reader.EOF; }
83 public override bool IsEmptyElement {
84 get { return Reader.IsEmptyElement; }
87 public int LineNumber {
88 get { return initial ? 0 : li != null ? li.LineNumber : 0; }
91 public int LinePosition {
92 get { return initial ? 0 : li != null ? li.LinePosition : 0; }
95 public override bool HasValue {
96 get { return initial || eof ? false : Reader.HasValue; }
99 public override string LocalName {
100 get { return initial || eof ? String.Empty : Reader.LocalName; }
103 public override string Name {
104 get { return initial || eof ? String.Empty : Reader.Name; }
107 public override XmlNameTable NameTable {
108 get { return Reader.NameTable; }
111 public override string NamespaceURI {
112 get { return initial || eof ? String.Empty : Reader.NamespaceURI; }
115 public override XmlNodeType NodeType {
116 get { return initial || eof ? XmlNodeType.None : Reader.NodeType; }
119 public override string Prefix {
120 get { return initial || eof ? String.Empty : Reader.Prefix; }
123 public override ReadState ReadState {
124 get { return initial ? ReadState.Initial : eof ? ReadState.EndOfFile : Reader.ReadState ; }
127 #if !NET_2_1
128 public override IXmlSchemaInfo SchemaInfo {
129 get { return Reader.SchemaInfo; }
131 #endif
133 public override XmlReaderSettings Settings {
134 get { return Reader.Settings; }
137 public override string Value {
138 get { return initial ? String.Empty : Reader.Value; }
141 public override void Close ()
143 while (Read ())
147 public override string GetAttribute (int i)
149 return initial ? null : Reader.GetAttribute (i);
152 public override string GetAttribute (string name)
154 return initial ? null : Reader.GetAttribute (name);
157 public override string GetAttribute (string local, string ns)
159 return initial ? null : Reader.GetAttribute (local, ns);
162 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
164 return nsResolver != null ? nsResolver.GetNamespacesInScope (scope) :
165 new Dictionary<string, string> ();
168 public bool HasLineInfo ()
170 return li != null ? li.HasLineInfo () : false;
173 public override string LookupNamespace (string prefix)
175 return Reader.LookupNamespace (prefix);
178 string IXmlNamespaceResolver.LookupPrefix (string ns)
180 return nsResolver != null ? nsResolver.LookupPrefix (ns) : String.Empty;
183 public override bool MoveToFirstAttribute ()
185 return initial ? false : Reader.MoveToFirstAttribute ();
188 public override bool MoveToNextAttribute ()
190 return initial ? false : Reader.MoveToNextAttribute ();
193 public override void MoveToAttribute (int i)
195 if (!initial)
196 Reader.MoveToAttribute (i);
199 public override bool MoveToAttribute (string name)
201 return initial ? false : Reader.MoveToAttribute (name);
204 public override bool MoveToAttribute (string local, string ns)
206 return initial ? false : Reader.MoveToAttribute (local, ns);
209 public override bool MoveToElement ()
211 return initial ? false : Reader.MoveToElement ();
214 public override bool Read ()
216 if (initial) {
217 initial = false;
218 return true;
220 if (!read) {
221 read = true;
222 Reader.MoveToElement ();
223 bool ret = !Reader.IsEmptyElement && Reader.Read ();
224 if (!ret)
225 eof = true;
226 return ret;
228 Reader.MoveToElement ();
229 if (Reader.Depth > startDepth)
230 if (Reader.Read ())
231 return true;
232 eof = true;
233 return false;
236 public override bool ReadAttributeValue ()
238 if (initial || eof)
239 return false;
240 return Reader.ReadAttributeValue ();
243 public override void ResolveEntity ()
245 if (initial || eof)
246 return;
247 Reader.ResolveEntity ();
252 #endif