[Xml] Fix GetAttribute to handle null namespaces properly, add unit test.
[mono.git] / mcs / class / System.XML / System.Xml / XmlParserInput.cs
1 //
2 // System.Xml.XmlParserInput
3 //
4 // Author:
5 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
6 //
7 //      (C)2003 Atsushi Enomoto
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections;
32 using System.IO;
33 using System.Text;
34 using System.Xml;
35 using System.Globalization;
36 using Mono.Xml;
37
38 namespace System.Xml
39 {
40         internal class XmlParserInput
41         {
42                 class XmlParserInputSource
43                 {
44                         public readonly string BaseURI;
45                         readonly TextReader reader;
46                         public int state;
47                         public bool isPE;
48                         int line;
49                         int column;
50
51                         public XmlParserInputSource (
52                                 TextReader reader, string baseUri, bool pe,
53                                 int line, int column)
54                         {
55                                 BaseURI = baseUri;
56                                 this.reader = reader;
57                                 this.isPE = pe;
58                                 this.line = line;
59                                 this.column = column;
60                         }
61
62                         public int LineNumber {
63                                 get { return line; }
64                         }
65
66                         public int LinePosition {
67                                 get { return column; }
68                         }
69
70                         public void Close ()
71                         {
72                                 reader.Close ();
73                         }
74
75                         public int Read ()
76                         {
77                                 if (state == 2)
78                                         return -1;
79                                 if (isPE && state == 0) {
80                                         state = 1;
81                                         return ' ';
82                                 }
83                                 int v = reader.Read ();
84
85                                 if (v == '\n') {
86                                         line++;
87                                         column = 1;
88                                 } else if (v >= 0) {
89                                         column++;
90                                 }
91
92                                 if (v < 0 && state == 1) {
93                                         state = 2;
94                                         return ' ';
95                                 }
96                                 return v;
97                         }
98                 }
99
100                 #region ctor
101                 public XmlParserInput (TextReader reader, string baseURI)
102                         : this (reader, baseURI, 1, 0)
103                 {
104                 }
105
106                 public XmlParserInput (TextReader reader, string baseURI, int line, int column)
107                 {
108                         this.source = new XmlParserInputSource (reader, baseURI, false, line, column);
109                 }
110                 #endregion
111
112                 #region Public Methods
113                 // Read the next character and compare it against the
114                 // specified character.
115                 public void Close ()
116                 {
117                         while (sourceStack.Count > 0)
118                                 ((XmlParserInputSource) sourceStack.Pop ()).Close ();
119                         source.Close ();
120                 }
121
122                 public void Expect (int expected)
123                 {
124                         int ch = ReadChar ();
125
126                         if (ch != expected) {
127                                 throw ReaderError (
128                                         String.Format (CultureInfo.InvariantCulture, 
129                                                 "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
130                                                 (char)expected,
131                                                 expected,
132                                                 (char)ch,
133                                                 ch));
134                         }
135                 }
136
137                 public void Expect (string expected)
138                 {
139                         int len = expected.Length;
140                         for(int i=0; i< len; i++)
141                                 Expect (expected[i]);
142                 }
143
144                 public void PushPEBuffer (DTDParameterEntityDeclaration pe)
145                 {
146                         sourceStack.Push (source);
147                         source = new XmlParserInputSource (
148                                 new StringReader (pe.ReplacementText),
149                                 pe.ActualUri, true, 1, 0);
150                 }
151
152                 private int ReadSourceChar ()
153                 {
154                         int v = source.Read ();
155                         while (v < 0 && sourceStack.Count > 0) {
156                                 source = sourceStack.Pop () as XmlParserInputSource;
157                                 v = source.Read ();
158                         }
159                         return v;
160                 }
161
162                 public int PeekChar ()
163                 {
164                         if (has_peek)
165                                 return peek_char;
166
167                         peek_char = ReadSourceChar ();
168                         if (peek_char >= 0xD800 && peek_char <= 0xDBFF) {
169                                 peek_char = 0x10000+((peek_char-0xD800)<<10);
170                                 int i = ReadSourceChar ();
171                                 if (i >= 0xDC00 && i <= 0xDFFF)
172                                         peek_char += (i-0xDC00);
173                         }
174                         has_peek = true;
175                         return peek_char;
176                 }
177
178                 public int ReadChar ()
179                 {
180                         int ch;
181
182                         ch = PeekChar ();
183                         has_peek = false;
184
185                         return ch;
186                 }
187
188                 #endregion
189
190                 #region Public Properties
191                 public string BaseURI {
192                         get { return source.BaseURI; }
193                 }
194
195                 public bool HasPEBuffer {
196                         get { return sourceStack.Count > 0; }
197                 }
198                 
199                 public int LineNumber {
200                         get { return source.LineNumber; }
201                 }
202
203                 public int LinePosition {
204                         get { return source.LinePosition; }
205                 }
206
207                 public bool AllowTextDecl {
208                         get { return allowTextDecl; }
209                         set { allowTextDecl = value; }
210                 }
211
212                 #endregion
213
214                 #region Privates
215                 Stack sourceStack = new Stack ();
216                 XmlParserInputSource source;
217                 bool has_peek;
218                 int peek_char;
219                 bool allowTextDecl = true;
220
221                 private XmlException ReaderError (string message)
222                 {
223                         return new XmlException (message, null, LineNumber, LinePosition);
224                 }
225                 #endregion
226         }
227 }