2006-03-09 Peter Dennis Bartok <pbartok@novell.com>
[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                 public override IXmlSchemaInfo SchemaInfo {
128                         get { return Reader.SchemaInfo; }
129                 }
130
131                 public override XmlReaderSettings Settings {
132                         get { return Reader.Settings; }
133                 }
134
135                 public override string Value {
136                         get { return initial ? String.Empty : Reader.Value; }
137                 }
138
139                 public override void Close ()
140                 {
141                         // do nothing
142                 }
143
144                 public override string GetAttribute (int i)
145                 {
146                         return initial ? null : Reader.GetAttribute (i);
147                 }
148
149                 public override string GetAttribute (string name)
150                 {
151                         return initial ? null : Reader.GetAttribute (name);
152                 }
153
154                 public override string GetAttribute (string local, string ns)
155                 {
156                         return initial ? null : Reader.GetAttribute (local, ns);
157                 }
158
159                 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
160                 {
161                         return nsResolver != null ? nsResolver.GetNamespacesInScope (scope) : 
162                                 new Dictionary<string, string> ();
163                 }
164
165                 public bool HasLineInfo ()
166                 {
167                         return li != null ? li.HasLineInfo () : false;
168                 }
169
170                 public override string LookupNamespace (string prefix)
171                 {
172                         return Reader.LookupNamespace (prefix);
173                 }
174
175                 string IXmlNamespaceResolver.LookupPrefix (string ns)
176                 {
177                         return nsResolver != null ? nsResolver.LookupPrefix (ns) : String.Empty;
178                 }
179
180                 public override bool MoveToFirstAttribute ()
181                 {
182                         return initial ? false : Reader.MoveToFirstAttribute ();
183                 }
184
185                 public override bool MoveToNextAttribute ()
186                 {
187                         return initial ? false : Reader.MoveToNextAttribute ();
188                 }
189
190                 public override void MoveToAttribute (int i)
191                 {
192                         if (!initial)
193                                 Reader.MoveToAttribute (i);
194                 }
195
196                 public override bool MoveToAttribute (string name)
197                 {
198                         return initial ? false : Reader.MoveToAttribute (name);
199                 }
200
201                 public override bool MoveToAttribute (string local, string ns)
202                 {
203                         return initial ? false : Reader.MoveToAttribute (local, ns);
204                 }
205
206                 public override bool MoveToElement ()
207                 {
208                         return initial ? false : Reader.MoveToElement ();
209                 }
210
211                 public override bool Read ()
212                 {
213                         if (initial) {
214                                 initial = false;
215                                 return true;
216                         }
217                         if (!read) {
218                                 read = true;
219                                 return !Reader.IsEmptyElement && Reader.Read ();
220                         }
221                         if (Reader.Depth > startDepth)
222                                 if (Reader.Read ())
223                                         return true;
224                         eof = true;
225                         return false;
226                 }
227
228                 public override bool ReadAttributeValue ()
229                 {
230                         if (initial || eof)
231                                 return false;
232                         return Reader.ReadAttributeValue ();
233                 }
234
235                 public override void ResolveEntity ()
236                 {
237                         if (initial || eof)
238                                 return;
239                         Reader.ResolveEntity ();
240                 }
241         }
242 }
243
244 #endif