Files
mono/mcs/class/System.XML/System.Xml/XmlParserInput.cs
T
Atsushi Eno 80b1ad3644 2004-02-03 Atsushi Enomoto <[email protected]>
* ChangeLog : Fixed incorrect description that should be written for
	  DTDValidatingReader.cs but written for DTDReader.cs.
	* DTDValidatingReader.cs : Fixes described in the last DTDReader fix.

	* XmlChar.cs,
	  XmlConstructs.cs : Made XmlChar and XmlConstructs equivalent and
	  replaced XmlChar with XmlConstruts, renaming "XmlChar" to
	  "XmlCharCompact" and "XmlConstructs" to "XmlChar".
	  (XmlChar will be used for compact framework.)
	* DTDReader.cs, XmlParserInput.cs, XmlTextReader.cs :
	  XmlChar related changes.  Support for surrogate pair.
	  It cannot use TextReader's peek anymore (for surrogate pair).
	  Character range check for PI value, attribute value, text
	* XmlTextWriter.cs : Change openElements from Stack to ArrayList.
	  (It calls ToArray() every time.)

svn path=/trunk/mcs/; revision=22741
2004-02-03 15:04:03 +00:00

214 lines
4.2 KiB
C#

//
// System.Xml.XmlParserInput
//
// Author:
// Atsushi Enomoto ([email protected])
//
// (C)2003 Atsushi Enomoto
//
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Globalization;
namespace Mono.Xml.Native
{
public class XmlParserInput
{
#region ctor
public XmlParserInput (TextReader reader, string baseURI)
: this (reader, baseURI, 1, 1)
{
}
public XmlParserInput (TextReader reader, string baseURI, int line, int column)
{
this.reader = reader;
StreamReader sr = reader as StreamReader;
if (sr != null)
can_seek = sr.BaseStream.CanSeek;
this.line = line;
this.column = column;
this.baseURI = baseURI;
}
#endregion
#region Public Methods
// Read the next character and compare it against the
// specified character.
public void Close ()
{
this.reader.Close ();
}
public void Expect (int expected)
{
int ch = ReadChar ();
if (ch != expected) {
throw ReaderError (
String.Format (
"expected '{0}' ({1:X}) but found '{2}' ({3:X})",
(char)expected,
expected,
(char)ch,
ch));
}
}
public void Expect (string expected)
{
int len = expected.Length;
for(int i=0; i< len; i++)
Expect (expected[i]);
}
public void InsertParameterEntityBuffer (string value)
{
this.peBuffer.Insert (0, ' ' + value + ' ');
peStored = true;
}
public int PeekChar ()
{
if (peStored)
return peBuffer [0];
// if (can_seek)
// return reader.Peek ();
if (has_peek)
return peek_char;
peek_char = reader.Read ();
if (peek_char >= 0xD800 && peek_char <= 0xDBFF) {
int i = reader.Read ();
if (i >= 0xDC00 && i <= 0xDFFF)
peek_char += i;
// else
// peek_char = -1;
}
has_peek = true;
return peek_char;
}
public int ReadChar ()
{
int ch;
if (peStored) {
ch = peBuffer [0];
peBuffer.Remove (0, 1);
peStored = peBuffer.Length > 0;
// I decided not to add character to currentTag with respect to PERef value
return ch;
}
if (has_peek) {
ch = peek_char;
has_peek = false;
} else {
ch = reader.Read ();
if (ch >= 0xD800 && ch <= 0xDBFF) {
int i = reader.Read ();
if (i > 0xDC00 && i <= 0xDFFF)
ch += i;
// else
// ch = -1;
}
}
if (ch == '\n') {
line++;
column = 1;
} else {
column++;
}
currentMarkup.Append ((char) ch);
return ch;
}
#endregion
#region Public Properties
public string BaseURI {
get { return baseURI; }
}
public StringBuilder CurrentMarkup {
get { return this.currentMarkup; }
}
public bool HasPEBuffer {
get {
if (!peStored)
return false;
else if (peBuffer.ToString ().Trim (XmlChar.WhitespaceChars).Length == 0)
return false;
else
return true;
}
}
public int LineNumber {
get { return line; }
}
public int LinePosition {
get { return column; }
}
public bool InitialState {
get { return initialState; }
set { initialState = value; }
}
#endregion
#region Privates
TextReader reader;
bool can_seek;
bool has_peek;
int peek_char;
int line;
int column;
StringBuilder currentMarkup = new StringBuilder ();
StringBuilder peBuffer = new StringBuilder ();
string baseURI;
bool peStored = false;
bool initialState = true;
private int ParseCharReference (string name)
{
int ret = -1;
if (name.Length > 0 && name [0] == '#') {
if (name [1] == 'x')
ret = int.Parse (name.Substring (2, name.Length - 2), NumberStyles.None & NumberStyles.AllowHexSpecifier);
else
ret = int.Parse (name.Substring (1, name.Length - 1));
}
return ret;
}
private int ParseKnownEntityReference (string name)
{
switch (name) {
case "quot": return '"';
case "lt": return '<';
case "gt": return '>';
case "amp": return '&';
case "apos": return '\'';
}
return -1;
}
private XmlException ReaderError (string message)
{
return new XmlException (message, null, line, column);
}
#endregion
}
}