copied mono-api-diff.cs from mono-2-2 branch so new patch can be applied and history...
[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 || eof ? false : Reader.HasValue; }
97                 }
98
99                 public override string LocalName {
100                         get { return initial || eof ? String.Empty : Reader.LocalName; }
101                 }
102
103                 public override string Name {
104                         get { return initial || eof ? 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 || eof ? String.Empty : Reader.NamespaceURI; }
113                 }
114
115                 public override XmlNodeType NodeType {
116                         get { return initial || eof ? XmlNodeType.None : Reader.NodeType; }
117                 }
118
119                 public override string Prefix {
120                         get { return initial || eof ? 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                         while (Read ())
144                                 ;
145                 }
146
147                 public override string GetAttribute (int i)
148                 {
149                         return initial ? null : Reader.GetAttribute (i);
150                 }
151
152                 public override string GetAttribute (string name)
153                 {
154                         return initial ? null : Reader.GetAttribute (name);
155                 }
156
157                 public override string GetAttribute (string local, string ns)
158                 {
159                         return initial ? null : Reader.GetAttribute (local, ns);
160                 }
161
162                 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
163                 {
164                         return nsResolver != null ? nsResolver.GetNamespacesInScope (scope) : 
165                                 new Dictionary<string, string> ();
166                 }
167
168                 public bool HasLineInfo ()
169                 {
170                         return li != null ? li.HasLineInfo () : false;
171                 }
172
173                 public override string LookupNamespace (string prefix)
174                 {
175                         return Reader.LookupNamespace (prefix);
176                 }
177
178                 string IXmlNamespaceResolver.LookupPrefix (string ns)
179                 {
180                         return nsResolver != null ? nsResolver.LookupPrefix (ns) : String.Empty;
181                 }
182
183                 public override bool MoveToFirstAttribute ()
184                 {
185                         return initial ? false : Reader.MoveToFirstAttribute ();
186                 }
187
188                 public override bool MoveToNextAttribute ()
189                 {
190                         return initial ? false : Reader.MoveToNextAttribute ();
191                 }
192
193                 public override void MoveToAttribute (int i)
194                 {
195                         if (!initial)
196                                 Reader.MoveToAttribute (i);
197                 }
198
199                 public override bool MoveToAttribute (string name)
200                 {
201                         return initial ? false : Reader.MoveToAttribute (name);
202                 }
203
204                 public override bool MoveToAttribute (string local, string ns)
205                 {
206                         return initial ? false : Reader.MoveToAttribute (local, ns);
207                 }
208
209                 public override bool MoveToElement ()
210                 {
211                         return initial ? false : Reader.MoveToElement ();
212                 }
213
214                 public override bool Read ()
215                 {
216                         if (initial) {
217                                 initial = false;
218                                 return true;
219                         }
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;
227                         }
228                         Reader.MoveToElement ();
229                         if (Reader.Depth > startDepth)
230                                 if (Reader.Read ())
231                                         return true;
232                         eof = true;
233                         return false;
234                 }
235
236                 public override bool ReadAttributeValue ()
237                 {
238                         if (initial || eof)
239                                 return false;
240                         return Reader.ReadAttributeValue ();
241                 }
242
243                 public override void ResolveEntity ()
244                 {
245                         if (initial || eof)
246                                 return;
247                         Reader.ResolveEntity ();
248                 }
249         }
250 }
251
252 #endif