60a2c93fc96ca6f4798dcb83cd4254c2ae6f6215
[mono.git] / mcs / class / System.XML / Mono.Xml / SubtreeXmlReader.cs
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.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Collections.Generic;
33 using System.Xml;
34 using System.Xml.Schema;
35
36 namespace Mono.Xml
37 {
38         internal class SubtreeXmlReader : XmlReader, IXmlLineInfo, IXmlNamespaceResolver
39         {
40                 int startDepth;
41                 bool eof;
42                 bool initial;
43                 bool read;
44                 XmlReader Reader;
45                 IXmlLineInfo li;
46                 IXmlNamespaceResolver nsResolver;
47
48                 public SubtreeXmlReader (XmlReader reader)
49                 {
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
57                 }
58
59                 public override int AttributeCount {
60                         get { return initial ? 0 : Reader.AttributeCount; }
61                 }
62
63                 public override bool CanReadBinaryContent {
64                         get { return Reader.CanReadBinaryContent; }
65                 }
66
67                 public override bool CanReadValueChunk {
68                         get { return Reader.CanReadValueChunk; }
69                 }
70
71                 public override int Depth {
72                         get { return Reader.Depth - startDepth; }
73                 }
74
75                 public override string BaseURI {
76                         get { return Reader.BaseURI; }
77                 }
78
79                 public override bool EOF {
80                         get { return eof || Reader.EOF; }
81                 }
82
83                 public override bool IsEmptyElement {
84                         get { return Reader.IsEmptyElement; }
85                 }
86
87                 public int LineNumber {
88                         get { return initial ? 0 : li != null ? li.LineNumber : 0; }
89                 }
90
91                 public int LinePosition {
92                         get { return initial ? 0 : li != null ? li.LinePosition : 0; }
93                 }
94
95                 public override bool HasValue {
96                         get { return initial ? false : Reader.HasValue; }
97                 }
98
99                 public override string LocalName {
100                         get { return initial ? String.Empty : Reader.LocalName; }
101                 }
102
103                 public override string Name {
104                         get { return initial ? String.Empty : Reader.Name; }
105                 }
106
107                 public override XmlNameTable NameTable {
108                         get { return Reader.NameTable; }
109                 }
110
111                 public override string NamespaceURI {
112                         get { return initial ? String.Empty : Reader.NamespaceURI; }
113                 }
114
115                 public override XmlNodeType NodeType {
116                         get { return initial ? XmlNodeType.None : Reader.NodeType; }
117                 }
118
119                 public override string Prefix {
120                         get { return initial ? String.Empty : Reader.Prefix; }
121                 }
122
123                 public override ReadState ReadState {
124                         get { return initial ? ReadState.Initial : eof ? ReadState.EndOfFile : Reader.ReadState ; }
125                 }
126
127 #if !NET_2_1
128                 public override IXmlSchemaInfo SchemaInfo {
129                         get { return Reader.SchemaInfo; }
130                 }
131 #endif
132
133                 public override XmlReaderSettings Settings {
134                         get { return Reader.Settings; }
135                 }
136
137                 public override string Value {
138                         get { return initial ? String.Empty : Reader.Value; }
139                 }
140
141                 public override void Close ()
142                 {
143                         // do nothing
144                 }
145
146                 public override string GetAttribute (int i)
147                 {
148                         return initial ? null : Reader.GetAttribute (i);
149                 }
150
151                 public override string GetAttribute (string name)
152                 {
153                         return initial ? null : Reader.GetAttribute (name);
154                 }
155
156                 public override string GetAttribute (string local, string ns)
157                 {
158                         return initial ? null : Reader.GetAttribute (local, ns);
159                 }
160
161                 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
162                 {
163                         return nsResolver != null ? nsResolver.GetNamespacesInScope (scope) : 
164                                 new Dictionary<string, string> ();
165                 }
166
167                 public bool HasLineInfo ()
168                 {
169                         return li != null ? li.HasLineInfo () : false;
170                 }
171
172                 public override string LookupNamespace (string prefix)
173                 {
174                         return Reader.LookupNamespace (prefix);
175                 }
176
177                 string IXmlNamespaceResolver.LookupPrefix (string ns)
178                 {
179                         return nsResolver != null ? nsResolver.LookupPrefix (ns) : String.Empty;
180                 }
181
182                 public override bool MoveToFirstAttribute ()
183                 {
184                         return initial ? false : Reader.MoveToFirstAttribute ();
185                 }
186
187                 public override bool MoveToNextAttribute ()
188                 {
189                         return initial ? false : Reader.MoveToNextAttribute ();
190                 }
191
192                 public override void MoveToAttribute (int i)
193                 {
194                         if (!initial)
195                                 Reader.MoveToAttribute (i);
196                 }
197
198                 public override bool MoveToAttribute (string name)
199                 {
200                         return initial ? false : Reader.MoveToAttribute (name);
201                 }
202
203                 public override bool MoveToAttribute (string local, string ns)
204                 {
205                         return initial ? false : Reader.MoveToAttribute (local, ns);
206                 }
207
208                 public override bool MoveToElement ()
209                 {
210                         return initial ? false : Reader.MoveToElement ();
211                 }
212
213                 public override bool Read ()
214                 {
215                         if (initial) {
216                                 initial = false;
217                                 return true;
218                         }
219                         if (!read) {
220                                 read = true;
221                                 return !Reader.IsEmptyElement && Reader.Read ();
222                         }
223                         if (Reader.Depth > startDepth)
224                                 if (Reader.Read ())
225                                         return true;
226                         eof = true;
227                         return false;
228                 }
229
230                 public override bool ReadAttributeValue ()
231                 {
232                         if (initial || eof)
233                                 return false;
234                         return Reader.ReadAttributeValue ();
235                 }
236
237                 public override void ResolveEntity ()
238                 {
239                         if (initial || eof)
240                                 return;
241                         Reader.ResolveEntity ();
242                 }
243         }
244 }
245
246 #endif