XmlParserInput.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 Expect (int expected)
  38. {
  39. int ch = ReadChar ();
  40. if (ch != expected) {
  41. throw ReaderError (
  42. String.Format (
  43. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  44. (char)expected,
  45. expected,
  46. (char)ch,
  47. ch));
  48. }
  49. }
  50. public void Expect (string expected)
  51. {
  52. int len = expected.Length;
  53. for(int i=0; i< len; i++)
  54. Expect (expected[i]);
  55. }
  56. public void InsertParameterEntityBuffer (string value)
  57. {
  58. this.peBuffer.Insert (0, value);
  59. }
  60. public int PeekChar ()
  61. {
  62. if (peBuffer.Length > 0)
  63. return peBuffer [0];
  64. if (can_seek)
  65. return reader.Peek ();
  66. if (has_peek)
  67. return peek_char;
  68. peek_char = reader.Read ();
  69. has_peek = true;
  70. return peek_char;
  71. }
  72. public int ReadChar ()
  73. {
  74. int ch;
  75. if (peBuffer.Length > 0) {
  76. ch = peBuffer [0];
  77. peBuffer.Remove (0, 1);
  78. // I decided not to add character to currentTag with respect to PERef value
  79. return ch;
  80. }
  81. if (has_peek) {
  82. ch = peek_char;
  83. has_peek = false;
  84. } else {
  85. ch = reader.Read ();
  86. }
  87. if (ch == '\n') {
  88. line++;
  89. column = 1;
  90. } else {
  91. column++;
  92. }
  93. currentMarkup.Append ((char) ch);
  94. return ch;
  95. }
  96. #if FullTextParseSupport
  97. // The reader is positioned on the first character after
  98. // the leading '<!--'.
  99. public void ReadComment ()
  100. {
  101. parsedValueStart = currentMarkup.Length;
  102. while (PeekChar () != -1) {
  103. int ch = ReadChar ();
  104. if (ch == '-' && PeekChar () == '-') {
  105. ReadChar ();
  106. if (PeekChar () != '>')
  107. throw ReaderError ("comments cannot contain '--'");
  108. ReadChar ();
  109. break;
  110. }
  111. }
  112. parsedValueEnd = currentMarkup.Length - 3;
  113. }
  114. public void ReadName ()
  115. {
  116. ReadNameOrNmToken (false);
  117. }
  118. public void ReadNmToken ()
  119. {
  120. ReadNameOrNmToken (true);
  121. }
  122. // This method stop parse at '&' regardless of its kind.
  123. public void ReadNonMarkupValue (bool start)
  124. {
  125. if (start)
  126. parsedValueStart = currentMarkup.Length;
  127. int ch = PeekChar ();
  128. while (ch != '&' && ch != '<' && ch != -1) {
  129. ReadChar ();
  130. ch = PeekChar ();
  131. }
  132. parsedValueEnd = currentMarkup.Length;
  133. }
  134. public void ReadPEReference ()
  135. {
  136. Expect ('%');
  137. ReadName ();
  138. Expect (';');
  139. }
  140. public void ReadReference ()
  141. {
  142. Expect ('&');
  143. ReadName ();
  144. Expect (';');
  145. }
  146. public void SkipWhitespace ()
  147. {
  148. //FIXME: Should not skip if whitespaceHandling == WhiteSpaceHandling.None
  149. while (XmlConstructs.IsSpace (PeekChar ()))
  150. ReadChar ();
  151. }
  152. #endif
  153. #endregion
  154. #region Public Properties
  155. public string BaseURI {
  156. get { return baseURI; }
  157. }
  158. public StringBuilder CurrentMarkup {
  159. get { return this.currentMarkup; }
  160. }
  161. public int LineNumber {
  162. get { return line; }
  163. }
  164. public int LinePosition {
  165. get { return column; }
  166. }
  167. public string Name
  168. {
  169. get {
  170. return currentMarkup.ToString (parsedNameStart, parsedNameEnd - parsedNameStart);
  171. }
  172. }
  173. public string Value {
  174. get {
  175. return currentMarkup.ToString (parsedValueStart, parsedValueEnd - parsedValueStart);
  176. }
  177. }
  178. #endregion
  179. #region Privates
  180. private void ReadNameOrNmToken(bool isNameToken)
  181. {
  182. parsedNameStart = currentMarkup.Length;
  183. if(isNameToken) {
  184. if (!XmlConstructs.IsName ((char) PeekChar ()))
  185. throw ReaderError ("a name did not start with a legal character " + PeekChar ());
  186. }
  187. else {
  188. if (!XmlConstructs.IsNameStart ((char) PeekChar ()))
  189. throw ReaderError ("a name did not start with a valid character " + PeekChar () + "(" + (char) PeekChar () + ")");
  190. }
  191. ReadChar ();
  192. while (XmlConstructs.IsName (PeekChar ())) {
  193. ReadChar ();
  194. }
  195. parsedNameEnd = currentMarkup.Length;
  196. }
  197. // Privates
  198. TextReader reader;
  199. bool can_seek;
  200. bool has_peek;
  201. int peek_char;
  202. int line;
  203. int column;
  204. StringBuilder currentMarkup = new StringBuilder ();
  205. int parsedNameStart;
  206. int parsedNameEnd;
  207. int parsedValueStart;
  208. int parsedValueEnd;
  209. StringBuilder peBuffer = new StringBuilder ();
  210. string baseURI;
  211. private int ParseCharReference (string name)
  212. {
  213. int ret = -1;
  214. if (name.Length > 0 && name [0] == '#') {
  215. if (name [1] == 'x')
  216. ret = int.Parse (name.Substring (2, name.Length - 2), NumberStyles.None & NumberStyles.AllowHexSpecifier);
  217. else
  218. ret = int.Parse (name.Substring (1, name.Length - 1));
  219. }
  220. return ret;
  221. }
  222. private int ParseKnownEntityReference (string name)
  223. {
  224. switch (name) {
  225. case "quot": return '"';
  226. case "lt": return '<';
  227. case "gt": return '>';
  228. case "amp": return '&';
  229. case "apos": return '\'';
  230. }
  231. return -1;
  232. }
  233. private XmlException ReaderError (string message)
  234. {
  235. return new XmlException (message, line, column);
  236. }
  237. #endregion
  238. }
  239. }