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.
34 using System
.Globalization
;
38 internal class XmlParserInput
41 public XmlParserInput (TextReader reader
, string baseURI
)
42 : this (reader
, baseURI
, 1, 1)
46 public XmlParserInput (TextReader reader
, string baseURI
, int line
, int column
)
50 StreamReader sr
= reader
as StreamReader
;
52 can_seek
= sr
.BaseStream
.CanSeek
;
56 this.baseURI
= baseURI
;
60 #region Public Methods
61 // Read the next character and compare it against the
62 // specified character.
68 public void Expect (int expected
)
74 String
.Format (CultureInfo
.InvariantCulture
,
75 "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
83 public void Expect (string expected
)
85 int len
= expected
.Length
;
86 for(int i
=0; i
< len
; i
++)
90 public void InsertParameterEntityBuffer (string value)
92 this.peBuffer
.Insert (peBufferIndex
, ' ');
93 this.peBuffer
.Insert (peBufferIndex
+ 1, value);
94 this.peBuffer
.Insert (peBufferIndex
+ value.Length
+ 1, ' ');
98 public int PeekChar ()
101 return peBuffer
[peBufferIndex
];
106 peek_char
= reader
.Read ();
107 if (peek_char
>= 0xD800 && peek_char
<= 0xDBFF) {
108 int i
= reader
.Read ();
109 if (i
>= 0xDC00 && i
<= 0xDFFF)
116 public int ReadChar ()
121 ch
= peBuffer
[peBufferIndex
];
123 if (peBufferIndex
== peBuffer
.Length
) {
128 // I decided not to add character to currentTag with respect to PERef value
137 if (ch
>= 0xD800 && ch
<= 0xDBFF) {
138 int i
= reader
.Read ();
139 if (i
> 0xDC00 && i
<= 0xDFFF)
155 #region Public Properties
156 public string BaseURI
{
157 get { return baseURI; }
160 public bool HasPEBuffer
{
161 get { return peStored; }
164 public int LineNumber
{
168 public int LinePosition
{
169 get { return column; }
172 public bool InitialState
{
173 get { return initialState; }
174 set { initialState = value; }
186 StringBuilder peBuffer
= new StringBuilder ();
188 bool peStored
= false;
189 bool initialState
= true;
192 private int ParseCharReference (string name
)
195 if (name
.Length
> 0 && name
[0] == '#') {
197 ret
= int.Parse (name
.Substring (2, name
.Length
- 2), NumberStyles
.None
& NumberStyles
.AllowHexSpecifier
, CultureInfo
.InvariantCulture
);
199 ret
= int.Parse (name
.Substring (1, name
.Length
- 1), CultureInfo
.InvariantCulture
);
204 private int ParseKnownEntityReference (string name
)
207 case "quot": return '"';
208 case "lt": return '<';
209 case "gt": return '>';
210 case "amp": return '&';
211 case "apos": return '\'';
216 private XmlException
ReaderError (string message
)
218 return new XmlException (message
, null, line
, column
);