XmlParserInput.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. peek_char = 0x10000+((peek_char-0xD800)<<10);
  93. int i = reader.Read ();
  94. if (i >= 0xDC00 && i <= 0xDFFF)
  95. peek_char += (i-0xDC00);
  96. }
  97. has_peek = true;
  98. return peek_char;
  99. }
  100. public int ReadChar ()
  101. {
  102. int ch;
  103. if (peStored) {
  104. ch = peBuffer [peBufferIndex];
  105. peBufferIndex++;
  106. if (peBufferIndex == peBuffer.Length) {
  107. peStored = false;
  108. peBuffer.Length = 0;
  109. peBufferIndex = 0;
  110. }
  111. // I decided not to add character to currentTag with respect to PERef value
  112. return ch;
  113. }
  114. ch = PeekChar ();
  115. has_peek = false;
  116. if (ch == '\n') {
  117. line++;
  118. column = 1;
  119. } else {
  120. column++;
  121. }
  122. return ch;
  123. }
  124. #endregion
  125. #region Public Properties
  126. public string BaseURI {
  127. get { return baseURI; }
  128. }
  129. public bool HasPEBuffer {
  130. get { return peStored; }
  131. }
  132. public int LineNumber {
  133. get { return line; }
  134. }
  135. public int LinePosition {
  136. get { return column; }
  137. }
  138. public bool InitialState {
  139. get { return initialState; }
  140. set { initialState = value; }
  141. }
  142. #endregion
  143. #region Privates
  144. TextReader reader;
  145. bool has_peek;
  146. int peek_char;
  147. int line;
  148. int column;
  149. StringBuilder peBuffer = new StringBuilder ();
  150. string baseURI;
  151. bool peStored = false;
  152. bool initialState = true;
  153. int peBufferIndex;
  154. private int ParseCharReference (string name)
  155. {
  156. int ret = -1;
  157. if (name.Length > 0 && name [0] == '#') {
  158. if (name [1] == 'x')
  159. ret = int.Parse (name.Substring (2, name.Length - 2), NumberStyles.None & NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
  160. else
  161. ret = int.Parse (name.Substring (1, name.Length - 1), CultureInfo.InvariantCulture);
  162. }
  163. return ret;
  164. }
  165. private int ParseKnownEntityReference (string name)
  166. {
  167. switch (name) {
  168. case "quot": return '"';
  169. case "lt": return '<';
  170. case "gt": return '>';
  171. case "amp": return '&';
  172. case "apos": return '\'';
  173. }
  174. return -1;
  175. }
  176. private XmlException ReaderError (string message)
  177. {
  178. return new XmlException (message, null, line, column);
  179. }
  180. #endregion
  181. }
  182. }