PageRenderTime 599ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Web/Test/mainsoft/MainsoftWebTest/HtmlAgilityPack/ParseReader.cs

https://github.com/ambroff/mono
C# | 448 lines | 295 code | 44 blank | 109 comment | 43 complexity | 9d837a072ca75df6f0e157a59405e8fc MD5 | raw file
  1. // HtmlAgilityPack V1.3.1.0 - Simon Mourier <simonm@microsoft.com>
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. namespace HtmlAgilityPack
  7. {
  8. /// <summary>
  9. /// Represents a rewindable buffered TextReader specifically well suited for parsing operations.
  10. /// </summary>
  11. public class ParseReader: Stream
  12. {
  13. private StringBuilder _sb;
  14. private int _baseReaderPosition;
  15. private int _maxReaderPosition;
  16. private int _position;
  17. private TextReader _baseReader;
  18. /// <summary>
  19. /// Initializes an instance of the ParserReader class, based on an existing TextReader instance.
  20. /// </summary>
  21. /// <param name="baseReader">The TextReader to base parsing on. Must not be null.</param>
  22. public ParseReader(TextReader baseReader)
  23. {
  24. if (baseReader == null)
  25. throw new ArgumentNullException("baseReader");
  26. _baseReader = baseReader;
  27. _sb = new StringBuilder();
  28. _position = 0;
  29. _baseReaderPosition = 0;
  30. _maxReaderPosition = int.MaxValue;
  31. }
  32. /// <summary>
  33. /// Gets the length in bytes of the stream.
  34. /// Always throws a NotSupportedException for the ParserReader class.
  35. /// </summary>
  36. public override long Length
  37. {
  38. get
  39. {
  40. throw new NotSupportedException();
  41. }
  42. }
  43. /// <summary>
  44. /// Gets or sets the position within the stream.
  45. /// </summary>
  46. public override long Position
  47. {
  48. get
  49. {
  50. return _position;
  51. }
  52. set
  53. {
  54. if (value < 0)
  55. throw new ArgumentException("value is negative: " + value + ".");
  56. if (value > int.MaxValue)
  57. throw new ArgumentException("value must not be larger than int32 MaxValue.");
  58. _position = (int)value;
  59. }
  60. }
  61. /// <summary>
  62. /// Checks the length of the underlying stream.
  63. /// </summary>
  64. /// <param name="length">The required length.</param>
  65. /// <returns>true if the underlying stream's length is greater than the required length, false otherwise.</returns>
  66. public bool CheckLength(int length)
  67. {
  68. if (length <= 0)
  69. throw new ArgumentException("length must be greater than zero.");
  70. if (BufferedTextLength >= length)
  71. return true;
  72. Seek(length, SeekOrigin.Begin);
  73. return (BufferedTextLength >= length);
  74. }
  75. /// <summary>
  76. /// Gets a value indicating whether the current stream supports seeking.
  77. /// Always returns true for the ParserReader class.
  78. /// </summary>
  79. public override bool CanSeek
  80. {
  81. get
  82. {
  83. return true;
  84. }
  85. }
  86. /// <summary>
  87. /// Gets a value indicating whether the current stream supports reading.
  88. /// Always returns true for the ParserReader class.
  89. /// </summary>
  90. public override bool CanRead
  91. {
  92. get
  93. {
  94. return true;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets a value indicating whether the current stream supports writing.
  99. /// Always returns false for the ParserReader class.
  100. /// </summary>
  101. public override bool CanWrite
  102. {
  103. get
  104. {
  105. return false;
  106. }
  107. }
  108. /// <summary>
  109. /// Sets the length of the current stream.
  110. /// Always throws a NotSupportedException for the ParserReader class.
  111. /// </summary>
  112. /// <param name="value">The desired length of the current stream in bytes.</param>
  113. public override void SetLength(long value)
  114. {
  115. throw new NotSupportedException();
  116. }
  117. /// <summary>
  118. /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
  119. /// </summary>
  120. public override void Flush()
  121. {
  122. // nothing to do
  123. }
  124. /// <summary>
  125. /// Gets the position within the underlying stream.
  126. /// </summary>
  127. public int BaseReaderPosition
  128. {
  129. get
  130. {
  131. return _baseReaderPosition;
  132. }
  133. }
  134. /// <summary>
  135. /// Gets the maximum position within the underlying stream.
  136. /// </summary>
  137. public int MaxReaderPosition
  138. {
  139. get
  140. {
  141. return _maxReaderPosition;
  142. }
  143. }
  144. private void CheckBaseReader()
  145. {
  146. if (_baseReader == null)
  147. throw new InvalidOperationException("Cannot read from a closed ParseReader.");
  148. }
  149. /// <summary>
  150. /// Closes the current underlying stream.
  151. /// </summary>
  152. public void CloseBaseReader()
  153. {
  154. if (_maxReaderPosition != int.MaxValue) // we have already closed it
  155. return;
  156. CheckBaseReader();
  157. _baseReader.Close();
  158. _baseReader = null;
  159. }
  160. private void InternalCloseBaseReader()
  161. {
  162. CloseBaseReader();
  163. _maxReaderPosition = _position;
  164. }
  165. /// <summary>
  166. /// Returns the next available character but does not consume it.
  167. /// </summary>
  168. /// <returns>The next character to be read, or -1 if no more characters are available.</returns>
  169. public int Peek()
  170. {
  171. if (_position < _baseReaderPosition)
  172. return Convert.ToInt32(this[_position]);
  173. if (_position == _maxReaderPosition)
  174. return -1;
  175. CheckBaseReader();
  176. int i = _baseReader.Peek();
  177. if (i < 0)
  178. {
  179. InternalCloseBaseReader();
  180. return i;
  181. }
  182. Debug.Assert(_position >= _baseReaderPosition);
  183. if (_position == _baseReaderPosition)
  184. {
  185. if (_sb.Length < (_position + 1))
  186. {
  187. _sb.Append(Convert.ToChar(i));
  188. }
  189. }
  190. return i;
  191. }
  192. /// <summary>
  193. /// Reads the next character and advances the character position by one character.
  194. /// </summary>
  195. /// <returns>The next character represented as an Int32, or -1 if no more characters are available.</returns>
  196. public int Read()
  197. {
  198. int i;
  199. if (_position < _baseReaderPosition)
  200. {
  201. i = Convert.ToInt32(_sb[_position]);
  202. _position++;
  203. return i;
  204. }
  205. if (_position == _maxReaderPosition)
  206. return -1;
  207. CheckBaseReader();
  208. i = _baseReader.Read();
  209. if (i < 0)
  210. {
  211. InternalCloseBaseReader();
  212. return i;
  213. }
  214. if (_position >= _baseReaderPosition)
  215. {
  216. if (_sb.Length < (_position + 1))
  217. {
  218. _sb.Append(Convert.ToChar(i));
  219. }
  220. }
  221. _baseReaderPosition++;
  222. _position++;
  223. return i;
  224. }
  225. /// <summary>
  226. /// Move the position starting from the current position.
  227. /// </summary>
  228. /// <param name="count">A character offset relative to the current position.</param>
  229. /// <returns>The new position.</returns>
  230. public int Seek(int count)
  231. {
  232. int i;
  233. if (count < 0)
  234. {
  235. if ((_position + count ) < 0)
  236. {
  237. i = _position;
  238. _position = 0;
  239. return i;
  240. }
  241. else
  242. {
  243. _position += count;
  244. return - count;
  245. }
  246. }
  247. for(i=0;i<count;i++)
  248. {
  249. int c = Read();
  250. if (c < 0)
  251. {
  252. break;
  253. }
  254. }
  255. return i;
  256. }
  257. /// <summary>
  258. /// Reads a string from the current position.
  259. /// </summary>
  260. /// <param name="count">The number of characters to read.</param>
  261. /// <returns>The read string or null, if an error occurred.</returns>
  262. public string ReadString(int count)
  263. {
  264. int first = (int)Position;
  265. Seek(count);
  266. int last = (int)Position;
  267. if (first >= _sb.Length)
  268. return null;
  269. return _sb.ToString(first, last - first);
  270. }
  271. /// <summary>
  272. /// Reads a string, represented as an array of System.Int32, from the current position.
  273. /// </summary>
  274. /// <param name="count">The number of characters to read.</param>
  275. /// <returns>The read string or null, if an error occurred.</returns>
  276. public int[] Read(int count)
  277. {
  278. string s = ReadString(count);
  279. if (s == null)
  280. return null;
  281. char[] chars = s.ToCharArray();
  282. int[] ints = new int[chars.Length];
  283. chars.CopyTo(ints, 0);
  284. return ints;
  285. }
  286. /// <summary>
  287. /// reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
  288. /// </summary>
  289. /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count- 1) replaced by the bytes read from the current source.</param>
  290. /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
  291. /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
  292. /// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
  293. public override int Read(byte[] buffer, int offset, int count)
  294. {
  295. if (buffer == null)
  296. throw new ArgumentNullException("buffer");
  297. // we don't really know how to read count bytes... so we read count chars
  298. string s = ReadString(count);
  299. if (s == null)
  300. return 0;
  301. byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s); // probably around 2*count bytes
  302. int read = 0;
  303. for(int i=0;i<bytes.Length;i++)
  304. {
  305. buffer[offset + i] = bytes[i];
  306. read++;
  307. if (read == count) // enough?
  308. break;
  309. }
  310. return read;
  311. }
  312. /// <summary>
  313. /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
  314. /// Always throws a NotSupportedException for the ParserReader class.
  315. /// </summary>
  316. /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
  317. /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
  318. /// <param name="count">The number of bytes to be written to the current stream.</param>
  319. public override void Write(byte[] buffer, int offset, int count)
  320. {
  321. throw new NotSupportedException();
  322. }
  323. /// <summary>
  324. /// Sets the position within the current stream.
  325. /// </summary>
  326. /// <param name="offset">A byte offset relative to the origin parameter.</param>
  327. /// <param name="origin">A value of type SeekOrigin indicating the reference point used to obtain the new position.</param>
  328. /// <returns>The new position within the current stream.</returns>
  329. public override long Seek(long offset, SeekOrigin origin)
  330. {
  331. if (offset > int.MaxValue)
  332. throw new ArgumentException("offset must not be larger than int32 MaxValue.");
  333. switch(origin)
  334. {
  335. case SeekOrigin.Begin:
  336. _position = 0;
  337. break;
  338. case SeekOrigin.End:
  339. Seek(int.MaxValue);
  340. break;
  341. case SeekOrigin.Current:
  342. break;
  343. }
  344. return Seek((int)offset);
  345. }
  346. /// <summary>
  347. /// Gets the character at the specified index or -1 if no more characters are available.
  348. /// </summary>
  349. public int this[int index]
  350. {
  351. get
  352. {
  353. if (index >= _baseReaderPosition)
  354. {
  355. int count = Seek(index - _baseReaderPosition);
  356. if (count < (index - _baseReaderPosition))
  357. return -1;
  358. int i = Peek();
  359. if (i < 0)
  360. return -1;
  361. }
  362. return _sb[index];
  363. }
  364. }
  365. /// <summary>
  366. /// Gets the length of the currently buffered text.
  367. /// </summary>
  368. public int BufferedTextLength
  369. {
  370. get
  371. {
  372. return _sb.Length;
  373. }
  374. }
  375. /// <summary>
  376. /// Gets the currently buffered text.
  377. /// </summary>
  378. public string BufferedText
  379. {
  380. get
  381. {
  382. return _sb.ToString();
  383. }
  384. }
  385. /// <summary>
  386. /// Extracts a string out of the buffered text.
  387. /// </summary>
  388. /// <param name="offset">The zero-based byte offset in buffered text at which to begin extracting.</param>
  389. /// <param name="length">The maximum number of bytes to be read from the buffered text.</param>
  390. /// <returns></returns>
  391. public string GetBufferedString(int offset, int length)
  392. {
  393. if (offset > BufferedTextLength)
  394. {
  395. return null;
  396. }
  397. if ((offset + length) > BufferedTextLength)
  398. {
  399. length -= (offset + length) - BufferedTextLength;
  400. }
  401. return BufferedText.Substring(offset, length);
  402. }
  403. }
  404. }