XmlParserInput.cs 5.0 KB

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