XmlParserInput.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // System.Xml.XmlParserInput
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C)2003 Atsushi Enomoto
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Text;
  33. using System.Xml;
  34. using System.Globalization;
  35. using Mono.Xml;
  36. namespace System.Xml
  37. {
  38. internal class XmlParserInput
  39. {
  40. class XmlParserInputSource
  41. {
  42. public readonly string BaseURI;
  43. readonly TextReader reader;
  44. public int state;
  45. public bool isPE;
  46. int line;
  47. int column;
  48. public XmlParserInputSource (
  49. TextReader reader, string baseUri, bool pe,
  50. int line, int column)
  51. {
  52. BaseURI = baseUri;
  53. this.reader = reader;
  54. this.isPE = pe;
  55. this.line = line;
  56. this.column = column;
  57. }
  58. public int LineNumber {
  59. get { return line; }
  60. }
  61. public int LinePosition {
  62. get { return column; }
  63. }
  64. public void Close ()
  65. {
  66. reader.Close ();
  67. }
  68. public int Read ()
  69. {
  70. if (state == 2)
  71. return -1;
  72. if (isPE && state == 0) {
  73. state = 1;
  74. return ' ';
  75. }
  76. int v = reader.Read ();
  77. if (v == '\n') {
  78. line++;
  79. column = 1;
  80. } else if (v >= 0) {
  81. column++;
  82. }
  83. if (v < 0 && state == 1) {
  84. state = 2;
  85. return ' ';
  86. }
  87. return v;
  88. }
  89. }
  90. #region ctor
  91. public XmlParserInput (TextReader reader, string baseURI)
  92. : this (reader, baseURI, 1, 0)
  93. {
  94. }
  95. public XmlParserInput (TextReader reader, string baseURI, int line, int column)
  96. {
  97. this.source = new XmlParserInputSource (reader, baseURI, false, line, column);
  98. }
  99. #endregion
  100. #region Public Methods
  101. // Read the next character and compare it against the
  102. // specified character.
  103. public void Close ()
  104. {
  105. while (sourceStack.Count > 0)
  106. ((XmlParserInputSource) sourceStack.Pop ()).Close ();
  107. source.Close ();
  108. }
  109. public void Expect (int expected)
  110. {
  111. int ch = ReadChar ();
  112. if (ch != expected) {
  113. throw ReaderError (
  114. String.Format (CultureInfo.InvariantCulture,
  115. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  116. (char)expected,
  117. expected,
  118. (char)ch,
  119. ch));
  120. }
  121. }
  122. public void Expect (string expected)
  123. {
  124. int len = expected.Length;
  125. for(int i=0; i< len; i++)
  126. Expect (expected[i]);
  127. }
  128. public void PushPEBuffer (DTDParameterEntityDeclaration pe)
  129. {
  130. sourceStack.Push (source);
  131. source = new XmlParserInputSource (
  132. new StringReader (pe.ReplacementText),
  133. pe.ActualUri, true, 1, 0);
  134. }
  135. private int ReadSourceChar ()
  136. {
  137. int v = source.Read ();
  138. while (v < 0 && sourceStack.Count > 0) {
  139. source = sourceStack.Pop () as XmlParserInputSource;
  140. v = source.Read ();
  141. }
  142. return v;
  143. }
  144. public int PeekChar ()
  145. {
  146. if (has_peek)
  147. return peek_char;
  148. peek_char = ReadSourceChar ();
  149. if (peek_char >= 0xD800 && peek_char <= 0xDBFF) {
  150. peek_char = 0x10000+((peek_char-0xD800)<<10);
  151. int i = ReadSourceChar ();
  152. if (i >= 0xDC00 && i <= 0xDFFF)
  153. peek_char += (i-0xDC00);
  154. }
  155. has_peek = true;
  156. return peek_char;
  157. }
  158. public int ReadChar ()
  159. {
  160. int ch;
  161. ch = PeekChar ();
  162. has_peek = false;
  163. return ch;
  164. }
  165. #endregion
  166. #region Public Properties
  167. public string BaseURI {
  168. get { return source.BaseURI; }
  169. }
  170. public bool HasPEBuffer {
  171. get { return sourceStack.Count > 0; }
  172. }
  173. public int LineNumber {
  174. get { return source.LineNumber; }
  175. }
  176. public int LinePosition {
  177. get { return source.LinePosition; }
  178. }
  179. public bool AllowTextDecl {
  180. get { return allowTextDecl; }
  181. set { allowTextDecl = value; }
  182. }
  183. #endregion
  184. #region Privates
  185. Stack sourceStack = new Stack ();
  186. XmlParserInputSource source;
  187. bool has_peek;
  188. int peek_char;
  189. bool allowTextDecl = true;
  190. private XmlException ReaderError (string message)
  191. {
  192. return new XmlException (message, null, LineNumber, LinePosition);
  193. }
  194. #endregion
  195. }
  196. }