XmlParserInput.cs 4.5 KB

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