mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-18 03:51:19 -10:00
* DTDObjectModel.cs : added XmlResolver related members. Added invalid entity recursion logic. Added encodingdecl check on textdecl. * DTDValidatingReader.cs : It now implements IHasXmlParserContext. * XmlChar.cs : IsPubidChar() should not allow TAB(	). * XmlDocumentType.cs : 1) internal CreateDocumentType() was now changed to receive DTDObjectModel to support other than XmlTextReader. 2) Most of its public member is now based on DTDObjectModel. * XmlDocument.cs : 1) Synchronous change with XmlDocumentType. 2) ReadNode() now considers XmlParserContext's DTDObjectModel for other than XmlTextReader (such as XmlValidatingReader). * XmlNode.cs : code cleanup only. * XmlParserInput.cs : added HasPEBuffer, used to check illegal nesting. * XmlTextReader.cs : 1) Illegal entity reference check logic was moved from ReadContent() to SetEntityReferenceProperties(). 2) Indentation change on ReadEntityReference(). 3) ReadAttribute() now checks reference to external entity reference. 4) Added textdecl encoding check. 5) DTDObjectModel fields are now set correctly. 6) added PERef markup nest check. 7) If PEDecl was not found, it might be WFC violation, not only be VC violation. 8) ReadEntityDecl() now receives its declared entity itself, and this method checks IsInternalSubset. * XmlValidatingReader.cs : 1) Added GetInternalPerserContext(). 2) ValidationType.None should be the same as Auto, not DTD (in the future it should keep xml schema's default values). Pending Stuff in XmlTextReader which breaks some NUnit tests; 1) SetEntityReferenceProperies() has check for illegal references. 2) ReadAttribute(bool) has similar check for illegal references. svn path=/trunk/mcs/; revision=17218
229 lines
4.5 KiB
C#
229 lines
4.5 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 + ' ');
|
|
}
|
|
|
|
public int PeekChar ()
|
|
{
|
|
if (peBuffer.Length > 0)
|
|
return peBuffer [0];
|
|
|
|
if (can_seek)
|
|
return reader.Peek ();
|
|
|
|
if (has_peek)
|
|
return peek_char;
|
|
|
|
peek_char = reader.Read ();
|
|
has_peek = true;
|
|
return peek_char;
|
|
}
|
|
|
|
public int ReadChar ()
|
|
{
|
|
int ch;
|
|
|
|
if (peBuffer.Length > 0) {
|
|
ch = peBuffer [0];
|
|
peBuffer.Remove (0, 1);
|
|
// 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 == '\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; }
|
|
}
|
|
|
|
private char [] wsChars = new char [] {' ', '\r', '\n', '\t'};
|
|
public bool HasPEBuffer {
|
|
get {
|
|
if (peBuffer.Length == 0)
|
|
return false;
|
|
else if (peBuffer.ToString ().Trim (wsChars).Length == 0)
|
|
return false;
|
|
else
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public int LineNumber {
|
|
get { return line; }
|
|
}
|
|
|
|
public int LinePosition {
|
|
get { return column; }
|
|
}
|
|
public string Name
|
|
{
|
|
get {
|
|
return currentMarkup.ToString (parsedNameStart, parsedNameEnd - parsedNameStart);
|
|
}
|
|
}
|
|
|
|
public string Value {
|
|
get {
|
|
return currentMarkup.ToString (parsedValueStart, parsedValueEnd - parsedValueStart);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Privates
|
|
private void ReadNameOrNmToken(bool isNameToken)
|
|
{
|
|
parsedNameStart = currentMarkup.Length;
|
|
if(isNameToken) {
|
|
if (!XmlChar.IsNameChar (PeekChar ()))
|
|
throw ReaderError ("a name did not start with a legal character " + PeekChar ());
|
|
}
|
|
else {
|
|
if (!XmlChar.IsFirstNameChar (PeekChar ()))
|
|
throw ReaderError ("a name did not start with a valid character " + PeekChar () + "(" + (char) PeekChar () + ")");
|
|
}
|
|
|
|
ReadChar ();
|
|
|
|
while (XmlChar.IsNameChar (PeekChar ())) {
|
|
ReadChar ();
|
|
}
|
|
|
|
parsedNameEnd = currentMarkup.Length;
|
|
}
|
|
|
|
// Privates
|
|
TextReader reader;
|
|
bool can_seek;
|
|
bool has_peek;
|
|
int peek_char;
|
|
int line;
|
|
int column;
|
|
StringBuilder currentMarkup = new StringBuilder ();
|
|
int parsedNameStart;
|
|
int parsedNameEnd;
|
|
int parsedValueStart;
|
|
int parsedValueEnd;
|
|
StringBuilder peBuffer = new StringBuilder ();
|
|
string baseURI;
|
|
|
|
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, line, column);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|