XmlParserInput.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #if FullTextParseSupport
  101. // The reader is positioned on the first character after
  102. // the leading '<!--'.
  103. public void ReadComment ()
  104. {
  105. parsedValueStart = currentMarkup.Length;
  106. while (PeekChar () != -1) {
  107. int ch = ReadChar ();
  108. if (ch == '-' && PeekChar () == '-') {
  109. ReadChar ();
  110. if (PeekChar () != '>')
  111. throw ReaderError ("comments cannot contain '--'");
  112. ReadChar ();
  113. break;
  114. }
  115. }
  116. parsedValueEnd = currentMarkup.Length - 3;
  117. }
  118. public void ReadName ()
  119. {
  120. ReadNameOrNmToken (false);
  121. }
  122. public void ReadNmToken ()
  123. {
  124. ReadNameOrNmToken (true);
  125. }
  126. // This method stop parse at '&' regardless of its kind.
  127. public void ReadNonMarkupValue (bool start)
  128. {
  129. if (start)
  130. parsedValueStart = currentMarkup.Length;
  131. int ch = PeekChar ();
  132. while (ch != '&' && ch != '<' && ch != -1) {
  133. ReadChar ();
  134. ch = PeekChar ();
  135. }
  136. parsedValueEnd = currentMarkup.Length;
  137. }
  138. public void ReadPEReference ()
  139. {
  140. Expect ('%');
  141. ReadName ();
  142. Expect (';');
  143. }
  144. public void ReadReference ()
  145. {
  146. Expect ('&');
  147. ReadName ();
  148. Expect (';');
  149. }
  150. public void SkipWhitespace ()
  151. {
  152. //FIXME: Should not skip if whitespaceHandling == WhiteSpaceHandling.None
  153. while (XmlConstructs.IsSpace (PeekChar ()))
  154. ReadChar ();
  155. }
  156. #endif
  157. #endregion
  158. #region Public Properties
  159. public string BaseURI {
  160. get { return baseURI; }
  161. }
  162. public StringBuilder CurrentMarkup {
  163. get { return this.currentMarkup; }
  164. }
  165. public int LineNumber {
  166. get { return line; }
  167. }
  168. public int LinePosition {
  169. get { return column; }
  170. }
  171. public string Name
  172. {
  173. get {
  174. return currentMarkup.ToString (parsedNameStart, parsedNameEnd - parsedNameStart);
  175. }
  176. }
  177. public string Value {
  178. get {
  179. return currentMarkup.ToString (parsedValueStart, parsedValueEnd - parsedValueStart);
  180. }
  181. }
  182. #endregion
  183. #region Privates
  184. private void ReadNameOrNmToken(bool isNameToken)
  185. {
  186. parsedNameStart = currentMarkup.Length;
  187. if(isNameToken) {
  188. if (!XmlConstructs.IsName ((char) PeekChar ()))
  189. throw ReaderError ("a name did not start with a legal character " + PeekChar ());
  190. }
  191. else {
  192. if (!XmlConstructs.IsNameStart ((char) PeekChar ()))
  193. throw ReaderError ("a name did not start with a valid character " + PeekChar () + "(" + (char) PeekChar () + ")");
  194. }
  195. ReadChar ();
  196. while (XmlConstructs.IsName (PeekChar ())) {
  197. ReadChar ();
  198. }
  199. parsedNameEnd = currentMarkup.Length;
  200. }
  201. // Privates
  202. TextReader reader;
  203. bool can_seek;
  204. bool has_peek;
  205. int peek_char;
  206. int line;
  207. int column;
  208. StringBuilder currentMarkup = new StringBuilder ();
  209. int parsedNameStart;
  210. int parsedNameEnd;
  211. int parsedValueStart;
  212. int parsedValueEnd;
  213. StringBuilder peBuffer = new StringBuilder ();
  214. string baseURI;
  215. private int ParseCharReference (string name)
  216. {
  217. int ret = -1;
  218. if (name.Length > 0 && name [0] == '#') {
  219. if (name [1] == 'x')
  220. ret = int.Parse (name.Substring (2, name.Length - 2), NumberStyles.None & NumberStyles.AllowHexSpecifier);
  221. else
  222. ret = int.Parse (name.Substring (1, name.Length - 1));
  223. }
  224. return ret;
  225. }
  226. private int ParseKnownEntityReference (string name)
  227. {
  228. switch (name) {
  229. case "quot": return '"';
  230. case "lt": return '<';
  231. case "gt": return '>';
  232. case "amp": return '&';
  233. case "apos": return '\'';
  234. }
  235. return -1;
  236. }
  237. private XmlException ReaderError (string message)
  238. {
  239. return new XmlException (message, line, column);
  240. }
  241. #endregion
  242. }
  243. }