XmlParserInput.cs 3.9 KB

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