fix for different results message
[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 int LineNumber {
84                         get { return initial ? 0 : li != null ? li.LineNumber : 0; }
85                 }
86
87                 public int LinePosition {
88                         get { return initial ? 0 : li != null ? li.LinePosition : 0; }
89                 }
90
91                 public override bool HasValue {
92                         get { return initial ? false : Reader.HasValue; }
93                 }
94
95                 public override string LocalName {
96                         get { return initial ? String.Empty : Reader.LocalName; }
97                 }
98
99                 public override string Name {
100                         get { return initial ? String.Empty : Reader.Name; }
101                 }
102
103                 public override XmlNameTable NameTable {
104                         get { return Reader.NameTable; }
105                 }
106
107                 public override string NamespaceURI {
108                         get { return initial ? String.Empty : Reader.NamespaceURI; }
109                 }
110
111                 public override XmlNodeType NodeType {
112                         get { return initial ? XmlNodeType.None : Reader.NodeType; }
113                 }
114
115                 public override string Prefix {
116                         get { return initial ? String.Empty : Reader.Prefix; }
117                 }
118
119                 public override ReadState ReadState {
120                         get { return initial ? ReadState.Initial : eof ? ReadState.EndOfFile : Reader.ReadState ; }
121                 }
122
123                 public override IXmlSchemaInfo SchemaInfo {
124                         get { return Reader.SchemaInfo; }
125                 }
126
127                 public override XmlReaderSettings Settings {
128                         get { return Reader.Settings; }
129                 }
130
131                 public override string Value {
132                         get { return initial ? String.Empty : Reader.Value; }
133                 }
134
135                 public override void Close ()
136                 {
137                         // do nothing
138                 }
139
140                 public override string GetAttribute (int i)
141                 {
142                         return initial ? null : Reader.GetAttribute (i);
143                 }
144
145                 public override string GetAttribute (string name)
146                 {
147                         return initial ? null : Reader.GetAttribute (name);
148                 }
149
150                 public override string GetAttribute (string local, string ns)
151                 {
152                         return initial ? null : Reader.GetAttribute (local, ns);
153                 }
154
155                 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
156                 {
157                         return nsResolver != null ? nsResolver.GetNamespacesInScope (scope) : 
158                                 new Dictionary<string, string> ();
159                 }
160
161                 public bool HasLineInfo ()
162                 {
163                         return li != null ? li.HasLineInfo () : false;
164                 }
165
166                 public override string LookupNamespace (string prefix)
167                 {
168                         return Reader.LookupNamespace (prefix);
169                 }
170
171                 string IXmlNamespaceResolver.LookupPrefix (string ns)
172                 {
173                         return nsResolver != null ? nsResolver.LookupPrefix (ns) : String.Empty;
174                 }
175
176                 public override bool MoveToFirstAttribute ()
177                 {
178                         return initial ? false : Reader.MoveToFirstAttribute ();
179                 }
180
181                 public override bool MoveToNextAttribute ()
182                 {
183                         return initial ? false : Reader.MoveToNextAttribute ();
184                 }
185
186                 public override void MoveToAttribute (int i)
187                 {
188                         if (!initial)
189                                 Reader.MoveToAttribute (i);
190                 }
191
192                 public override bool MoveToAttribute (string name)
193                 {
194                         return initial ? false : Reader.MoveToAttribute (name);
195                 }
196
197                 public override bool MoveToAttribute (string local, string ns)
198                 {
199                         return initial ? false : Reader.MoveToAttribute (local, ns);
200                 }
201
202                 public override bool MoveToElement ()
203                 {
204                         return initial ? false : Reader.MoveToElement ();
205                 }
206
207                 public override bool Read ()
208                 {
209                         if (initial) {
210                                 initial = false;
211                                 return true;
212                         }
213                         if (!read) {
214                                 read = true;
215                                 return !Reader.IsEmptyElement && Reader.Read ();
216                         }
217                         if (Reader.Depth > startDepth)
218                                 if (Reader.Read ())
219                                         return true;
220                         eof = true;
221                         return false;
222                 }
223
224                 public override bool ReadAttributeValue ()
225                 {
226                         if (initial || eof)
227                                 return false;
228                         return Reader.ReadAttributeValue ();
229                 }
230
231                 public override void ResolveEntity ()
232                 {
233                         if (initial || eof)
234                                 return;
235                         Reader.ResolveEntity ();
236                 }
237         }
238 }
239
240 #endif