2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml / XmlParserInput.cs
blob96b57760ec829c51c59f1dcbbb1170a6fef983ac
1 //
2 // System.Xml.XmlParserInput
3 //
4 // Author:
5 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
6 //
7 // (C)2003 Atsushi Enomoto
8 //
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.
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;
38 namespace System.Xml
40 internal class XmlParserInput
42 class XmlParserInputSource
44 public readonly string BaseURI;
45 readonly TextReader reader;
46 public int state;
47 public bool isPE;
48 int line;
49 int column;
51 public XmlParserInputSource (
52 TextReader reader, string baseUri, bool pe,
53 int line, int column)
55 BaseURI = baseUri;
56 this.reader = reader;
57 this.isPE = pe;
58 this.line = line;
59 this.column = column;
62 public int LineNumber {
63 get { return line; }
66 public int LinePosition {
67 get { return column; }
70 public void Close ()
72 reader.Close ();
75 public int Read ()
77 if (state == 2)
78 return -1;
79 if (isPE && state == 0) {
80 state = 1;
81 return ' ';
83 int v = reader.Read ();
85 if (v == '\n') {
86 line++;
87 column = 1;
88 } else if (v >= 0) {
89 column++;
92 if (v < 0 && state == 1) {
93 state = 2;
94 return ' ';
96 return v;
100 #region ctor
101 public XmlParserInput (TextReader reader, string baseURI)
102 : this (reader, baseURI, 1, 0)
106 public XmlParserInput (TextReader reader, string baseURI, int line, int column)
108 this.source = new XmlParserInputSource (reader, baseURI, false, line, column);
110 #endregion
112 #region Public Methods
113 // Read the next character and compare it against the
114 // specified character.
115 public void Close ()
117 while (sourceStack.Count > 0)
118 ((XmlParserInputSource) sourceStack.Pop ()).Close ();
119 source.Close ();
122 public void Expect (int expected)
124 int ch = ReadChar ();
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));
137 public void Expect (string expected)
139 int len = expected.Length;
140 for(int i=0; i< len; i++)
141 Expect (expected[i]);
144 public void PushPEBuffer (DTDParameterEntityDeclaration pe)
146 sourceStack.Push (source);
147 source = new XmlParserInputSource (
148 new StringReader (pe.ReplacementText),
149 pe.ActualUri, true, 1, 0);
152 private int ReadSourceChar ()
154 int v = source.Read ();
155 while (v < 0 && sourceStack.Count > 0) {
156 source = sourceStack.Pop () as XmlParserInputSource;
157 v = source.Read ();
159 return v;
162 public int PeekChar ()
164 if (has_peek)
165 return peek_char;
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);
174 has_peek = true;
175 return peek_char;
178 public int ReadChar ()
180 int ch;
182 ch = PeekChar ();
183 has_peek = false;
185 return ch;
188 #endregion
190 #region Public Properties
191 public string BaseURI {
192 get { return source.BaseURI; }
195 public bool HasPEBuffer {
196 get { return sourceStack.Count > 0; }
199 public int LineNumber {
200 get { return source.LineNumber; }
203 public int LinePosition {
204 get { return source.LinePosition; }
207 public bool AllowTextDecl {
208 get { return allowTextDecl; }
209 set { allowTextDecl = value; }
212 #endregion
214 #region Privates
215 Stack sourceStack = new Stack ();
216 XmlParserInputSource source;
217 bool has_peek;
218 int peek_char;
219 bool allowTextDecl = true;
221 private XmlException ReaderError (string message)
223 return new XmlException (message, null, LineNumber, LinePosition);
225 #endregion