XmlParserInput.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. private char [] wsChars = new char [] {' ', '\r', '\n', '\t'};
  111. public bool HasPEBuffer {
  112. get {
  113. if (!peStored)
  114. return false;
  115. else if (peBuffer.ToString ().Trim (wsChars).Length == 0)
  116. return false;
  117. else
  118. return true;
  119. }
  120. }
  121. public int LineNumber {
  122. get { return line; }
  123. }
  124. public int LinePosition {
  125. get { return column; }
  126. }
  127. public string Name
  128. {
  129. get {
  130. return currentMarkup.ToString (parsedNameStart, parsedNameEnd - parsedNameStart);
  131. }
  132. }
  133. public string Value {
  134. get {
  135. return currentMarkup.ToString (parsedValueStart, parsedValueEnd - parsedValueStart);
  136. }
  137. }
  138. #endregion
  139. #region Privates
  140. private void ReadNameOrNmToken(bool isNameToken)
  141. {
  142. parsedNameStart = currentMarkup.Length;
  143. if(isNameToken) {
  144. if (!XmlChar.IsNameChar (PeekChar ()))
  145. throw ReaderError ("a name did not start with a legal character " + PeekChar ());
  146. }
  147. else {
  148. if (!XmlChar.IsFirstNameChar (PeekChar ()))
  149. throw ReaderError ("a name did not start with a valid character " + PeekChar () + "(" + (char) PeekChar () + ")");
  150. }
  151. ReadChar ();
  152. while (XmlChar.IsNameChar (PeekChar ())) {
  153. ReadChar ();
  154. }
  155. parsedNameEnd = currentMarkup.Length;
  156. }
  157. // Privates
  158. TextReader reader;
  159. bool can_seek;
  160. bool has_peek;
  161. int peek_char;
  162. int line;
  163. int column;
  164. StringBuilder currentMarkup = new StringBuilder ();
  165. int parsedNameStart;
  166. int parsedNameEnd;
  167. int parsedValueStart;
  168. int parsedValueEnd;
  169. StringBuilder peBuffer = new StringBuilder ();
  170. string baseURI;
  171. bool peStored = false;
  172. private int ParseCharReference (string name)
  173. {
  174. int ret = -1;
  175. if (name.Length > 0 && name [0] == '#') {
  176. if (name [1] == 'x')
  177. ret = int.Parse (name.Substring (2, name.Length - 2), NumberStyles.None & NumberStyles.AllowHexSpecifier);
  178. else
  179. ret = int.Parse (name.Substring (1, name.Length - 1));
  180. }
  181. return ret;
  182. }
  183. private int ParseKnownEntityReference (string name)
  184. {
  185. switch (name) {
  186. case "quot": return '"';
  187. case "lt": return '<';
  188. case "gt": return '>';
  189. case "amp": return '&';
  190. case "apos": return '\'';
  191. }
  192. return -1;
  193. }
  194. private XmlException ReaderError (string message)
  195. {
  196. return new XmlException (message, line, column);
  197. }
  198. #endregion
  199. }
  200. }