2 // System.Xml.XmlParserInput
5 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 // (C)2003 Atsushi Enomoto
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
35 using System.Globalization;
40 internal class XmlParserInput
42 class XmlParserInputSource
44 public readonly string BaseURI;
45 readonly TextReader reader;
51 public XmlParserInputSource (
52 TextReader reader, string baseUri, bool pe,
62 public int LineNumber {
66 public int LinePosition {
67 get { return column; }
79 if (isPE && state == 0) {
83 int v = reader.Read ();
92 if (v < 0 && state == 1) {
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);
112 #region Public Methods
113 // Read the next character and compare it against the
114 // specified character.
117 while (sourceStack.Count > 0)
118 ((XmlParserInputSource) sourceStack.Pop ()).Close ();
122 public void Expect (int expected)
124 int ch = ReadChar ();
126 if (ch != expected) {
128 String.Format (CultureInfo.InvariantCulture,
129 "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
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;
162 public int PeekChar ()
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);
178 public int ReadChar ()
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; }
215 Stack sourceStack = new Stack ();
216 XmlParserInputSource source;
219 bool allowTextDecl = true;
221 private XmlException ReaderError (string message)
223 return new XmlException (message, null, LineNumber, LinePosition);