2
0

XmlParserInput.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.IO;
  31. using System.Text;
  32. using System.Xml;
  33. using System.Globalization;
  34. namespace System.Xml
  35. {
  36. internal class XmlParserInput
  37. {
  38. #region ctor
  39. public XmlParserInput (TextReader reader, string baseURI)
  40. : this (reader, baseURI, 1, 1)
  41. {
  42. }
  43. public XmlParserInput (TextReader reader, string baseURI, int line, int column)
  44. {
  45. this.reader = reader;
  46. StreamReader sr = reader as StreamReader;
  47. if (sr != null)
  48. can_seek = sr.BaseStream.CanSeek;
  49. this.line = line;
  50. this.column = column;
  51. this.baseURI = baseURI;
  52. }
  53. #endregion
  54. #region Public Methods
  55. // Read the next character and compare it against the
  56. // specified character.
  57. public void Close ()
  58. {
  59. this.reader.Close ();
  60. }
  61. public void Expect (int expected)
  62. {
  63. int ch = ReadChar ();
  64. if (ch != expected) {
  65. throw ReaderError (
  66. String.Format (CultureInfo.InvariantCulture,
  67. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  68. (char)expected,
  69. expected,
  70. (char)ch,
  71. ch));
  72. }
  73. }
  74. public void Expect (string expected)
  75. {
  76. int len = expected.Length;
  77. for(int i=0; i< len; i++)
  78. Expect (expected[i]);
  79. }
  80. public void InsertParameterEntityBuffer (string value)
  81. {
  82. this.peBuffer.Insert (peBufferIndex, ' ');
  83. this.peBuffer.Insert (peBufferIndex + 1, value);
  84. this.peBuffer.Insert (peBufferIndex + value.Length + 1, ' ');
  85. peStored = true;
  86. }
  87. public int PeekChar ()
  88. {
  89. if (peStored)
  90. return peBuffer [peBufferIndex];
  91. if (has_peek)
  92. return peek_char;
  93. peek_char = reader.Read ();
  94. if (peek_char >= 0xD800 && peek_char <= 0xDBFF) {
  95. int i = reader.Read ();
  96. if (i >= 0xDC00 && i <= 0xDFFF)
  97. peek_char += i;
  98. }
  99. has_peek = true;
  100. return peek_char;
  101. }
  102. public int ReadChar ()
  103. {
  104. int ch;
  105. if (peStored) {
  106. ch = peBuffer [peBufferIndex];
  107. peBufferIndex++;
  108. if (peBufferIndex == peBuffer.Length) {
  109. peStored = false;
  110. peBuffer.Length = 0;
  111. peBufferIndex = 0;
  112. }
  113. // I decided not to add character to currentTag with respect to PERef value
  114. return ch;
  115. }
  116. if (has_peek) {
  117. ch = peek_char;
  118. has_peek = false;
  119. } else {
  120. ch = reader.Read ();
  121. if (ch >= 0xD800 && ch <= 0xDBFF) {
  122. int i = reader.Read ();
  123. if (i > 0xDC00 && i <= 0xDFFF)
  124. ch += i;
  125. }
  126. }
  127. if (ch == '\n') {
  128. line++;
  129. column = 1;
  130. } else {
  131. column++;
  132. }
  133. return ch;
  134. }
  135. #endregion
  136. #region Public Properties
  137. public string BaseURI {
  138. get { return baseURI; }
  139. }
  140. public bool HasPEBuffer {
  141. get { return peStored; }
  142. }
  143. public int LineNumber {
  144. get { return line; }
  145. }
  146. public int LinePosition {
  147. get { return column; }
  148. }
  149. public bool InitialState {
  150. get { return initialState; }
  151. set { initialState = value; }
  152. }
  153. #endregion
  154. #region Privates
  155. TextReader reader;
  156. bool can_seek;
  157. bool has_peek;
  158. int peek_char;
  159. int line;
  160. int column;
  161. StringBuilder peBuffer = new StringBuilder ();
  162. string baseURI;
  163. bool peStored = false;
  164. bool initialState = true;
  165. int peBufferIndex;
  166. private int ParseCharReference (string name)
  167. {
  168. int ret = -1;
  169. if (name.Length > 0 && name [0] == '#') {
  170. if (name [1] == 'x')
  171. ret = int.Parse (name.Substring (2, name.Length - 2), NumberStyles.None & NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
  172. else
  173. ret = int.Parse (name.Substring (1, name.Length - 1), CultureInfo.InvariantCulture);
  174. }
  175. return ret;
  176. }
  177. private int ParseKnownEntityReference (string name)
  178. {
  179. switch (name) {
  180. case "quot": return '"';
  181. case "lt": return '<';
  182. case "gt": return '>';
  183. case "amp": return '&';
  184. case "apos": return '\'';
  185. }
  186. return -1;
  187. }
  188. private XmlException ReaderError (string message)
  189. {
  190. return new XmlException (message, null, line, column);
  191. }
  192. #endregion
  193. }
  194. }