XmlParserInput.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // System.Xml.XmlParserInput
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C)2003 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. using System.Xml;
  13. using System.Globalization;
  14. namespace Mono.Xml.Native
  15. {
  16. public class XmlParserInput
  17. {
  18. #region ctor
  19. public XmlParserInput (TextReader reader, string baseURI)
  20. : this (reader, baseURI, 1, 1)
  21. {
  22. }
  23. public XmlParserInput (TextReader reader, string baseURI, int line, int column)
  24. {
  25. this.reader = reader;
  26. StreamReader sr = reader as StreamReader;
  27. if (sr != null)
  28. can_seek = sr.BaseStream.CanSeek;
  29. this.line = line;
  30. this.column = column;
  31. this.baseURI = baseURI;
  32. }
  33. #endregion
  34. #region Public Methods
  35. // Read the next character and compare it against the
  36. // specified character.
  37. public void Close ()
  38. {
  39. this.reader.Close ();
  40. }
  41. public void Expect (int expected)
  42. {
  43. int ch = ReadChar ();
  44. if (ch != expected) {
  45. throw ReaderError (
  46. String.Format (
  47. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  48. (char)expected,
  49. expected,
  50. (char)ch,
  51. ch));
  52. }
  53. }
  54. public void Expect (string expected)
  55. {
  56. int len = expected.Length;
  57. for(int i=0; i< len; i++)
  58. Expect (expected[i]);
  59. }
  60. public void InsertParameterEntityBuffer (string value)
  61. {
  62. this.peBuffer.Insert (peBufferIndex, ' ');
  63. this.peBuffer.Insert (peBufferIndex + 1, value);
  64. this.peBuffer.Insert (peBufferIndex + value.Length + 1, ' ');
  65. peStored = true;
  66. }
  67. public int PeekChar ()
  68. {
  69. if (peStored)
  70. return peBuffer [peBufferIndex];
  71. if (has_peek)
  72. return peek_char;
  73. peek_char = reader.Read ();
  74. if (peek_char >= 0xD800 && peek_char <= 0xDBFF) {
  75. int i = reader.Read ();
  76. if (i >= 0xDC00 && i <= 0xDFFF)
  77. peek_char += i;
  78. }
  79. has_peek = true;
  80. return peek_char;
  81. }
  82. public int ReadChar ()
  83. {
  84. int ch;
  85. if (peStored) {
  86. ch = peBuffer [peBufferIndex];
  87. peBufferIndex++;
  88. if (peBufferIndex == peBuffer.Length) {
  89. peStored = false;
  90. peBuffer.Length = 0;
  91. peBufferIndex = 0;
  92. }
  93. // I decided not to add character to currentTag with respect to PERef value
  94. return ch;
  95. }
  96. if (has_peek) {
  97. ch = peek_char;
  98. has_peek = false;
  99. } else {
  100. ch = reader.Read ();
  101. if (ch >= 0xD800 && ch <= 0xDBFF) {
  102. int i = reader.Read ();
  103. if (i > 0xDC00 && i <= 0xDFFF)
  104. ch += i;
  105. }
  106. }
  107. if (ch == '\n') {
  108. line++;
  109. column = 1;
  110. } else {
  111. column++;
  112. }
  113. return ch;
  114. }
  115. #endregion
  116. #region Public Properties
  117. public string BaseURI {
  118. get { return baseURI; }
  119. }
  120. public bool HasPEBuffer {
  121. get { return peStored; }
  122. }
  123. public int LineNumber {
  124. get { return line; }
  125. }
  126. public int LinePosition {
  127. get { return column; }
  128. }
  129. public bool InitialState {
  130. get { return initialState; }
  131. set { initialState = value; }
  132. }
  133. #endregion
  134. #region Privates
  135. TextReader reader;
  136. bool can_seek;
  137. bool has_peek;
  138. int peek_char;
  139. int line;
  140. int column;
  141. StringBuilder peBuffer = new StringBuilder ();
  142. string baseURI;
  143. bool peStored = false;
  144. bool initialState = true;
  145. int peBufferIndex;
  146. private int ParseCharReference (string name)
  147. {
  148. int ret = -1;
  149. if (name.Length > 0 && name [0] == '#') {
  150. if (name [1] == 'x')
  151. ret = int.Parse (name.Substring (2, name.Length - 2), NumberStyles.None & NumberStyles.AllowHexSpecifier);
  152. else
  153. ret = int.Parse (name.Substring (1, name.Length - 1));
  154. }
  155. return ret;
  156. }
  157. private int ParseKnownEntityReference (string name)
  158. {
  159. switch (name) {
  160. case "quot": return '"';
  161. case "lt": return '<';
  162. case "gt": return '>';
  163. case "amp": return '&';
  164. case "apos": return '\'';
  165. }
  166. return -1;
  167. }
  168. private XmlException ReaderError (string message)
  169. {
  170. return new XmlException (message, null, line, column);
  171. }
  172. #endregion
  173. }
  174. }