2
0

XmlParserInput.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 (0, ' ' + value + ' ');
  63. peStored = true;
  64. }
  65. public int PeekChar ()
  66. {
  67. if (peStored)
  68. return peBuffer [0];
  69. // if (can_seek)
  70. // return reader.Peek ();
  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. // else
  79. // peek_char = -1;
  80. }
  81. has_peek = true;
  82. return peek_char;
  83. }
  84. public int ReadChar ()
  85. {
  86. int ch;
  87. if (peStored) {
  88. ch = peBuffer [0];
  89. peBuffer.Remove (0, 1);
  90. peStored = peBuffer.Length > 0;
  91. // I decided not to add character to currentTag with respect to PERef value
  92. return ch;
  93. }
  94. if (has_peek) {
  95. ch = peek_char;
  96. has_peek = false;
  97. } else {
  98. ch = reader.Read ();
  99. if (ch >= 0xD800 && ch <= 0xDBFF) {
  100. int i = reader.Read ();
  101. if (i > 0xDC00 && i <= 0xDFFF)
  102. ch += i;
  103. // else
  104. // ch = -1;
  105. }
  106. }
  107. if (ch == '\n') {
  108. line++;
  109. column = 1;
  110. } else {
  111. column++;
  112. }
  113. currentMarkup.Append ((char) ch);
  114. return ch;
  115. }
  116. #endregion
  117. #region Public Properties
  118. public string BaseURI {
  119. get { return baseURI; }
  120. }
  121. public StringBuilder CurrentMarkup {
  122. get { return this.currentMarkup; }
  123. }
  124. public bool HasPEBuffer {
  125. get {
  126. if (!peStored)
  127. return false;
  128. else if (peBuffer.ToString ().Trim (XmlChar.WhitespaceChars).Length == 0)
  129. return false;
  130. else
  131. return true;
  132. }
  133. }
  134. public int LineNumber {
  135. get { return line; }
  136. }
  137. public int LinePosition {
  138. get { return column; }
  139. }
  140. public bool InitialState {
  141. get { return initialState; }
  142. set { initialState = value; }
  143. }
  144. #endregion
  145. #region Privates
  146. TextReader reader;
  147. bool can_seek;
  148. bool has_peek;
  149. int peek_char;
  150. int line;
  151. int column;
  152. StringBuilder currentMarkup = new StringBuilder ();
  153. StringBuilder peBuffer = new StringBuilder ();
  154. string baseURI;
  155. bool peStored = false;
  156. bool initialState = true;
  157. private int ParseCharReference (string name)
  158. {
  159. int ret = -1;
  160. if (name.Length > 0 && name [0] == '#') {
  161. if (name [1] == 'x')
  162. ret = int.Parse (name.Substring (2, name.Length - 2), NumberStyles.None & NumberStyles.AllowHexSpecifier);
  163. else
  164. ret = int.Parse (name.Substring (1, name.Length - 1));
  165. }
  166. return ret;
  167. }
  168. private int ParseKnownEntityReference (string name)
  169. {
  170. switch (name) {
  171. case "quot": return '"';
  172. case "lt": return '<';
  173. case "gt": return '>';
  174. case "amp": return '&';
  175. case "apos": return '\'';
  176. }
  177. return -1;
  178. }
  179. private XmlException ReaderError (string message)
  180. {
  181. return new XmlException (message, null, line, column);
  182. }
  183. #endregion
  184. }
  185. }