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