Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
SimpleCharStream.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
19 
20 using System;
21 
22 namespace Lucene.Net.Demo.Html
23 {
24 
25  /// <summary> An implementation of interface CharStream, where the stream is assumed to
26  /// contain only ASCII characters (without unicode processing).
27  /// </summary>
28 
29  public class SimpleCharStream
30  {
31  public const bool staticFlag = false;
32  internal int bufsize;
33  internal int available;
34  internal int tokenBegin;
35  public int bufpos = - 1;
36  protected internal int[] bufline;
37  protected internal int[] bufcolumn;
38 
39  protected internal int column = 0;
40  protected internal int line = 1;
41 
42  protected internal bool prevCharIsCR = false;
43  protected internal bool prevCharIsLF = false;
44 
45  protected internal System.IO.StreamReader inputStream;
46 
47  protected internal char[] buffer;
48  protected internal int maxNextCharInd = 0;
49  protected internal int inBuf = 0;
50  protected internal int tabSize = 8;
51 
52  protected internal virtual void SetTabSize(int i)
53  {
54  tabSize = i;
55  }
56  protected internal virtual int GetTabSize(int i)
57  {
58  return tabSize;
59  }
60 
61 
62  protected internal virtual void ExpandBuff(bool wrapAround)
63  {
64  char[] newbuffer = new char[bufsize + 2048];
65  int[] newbufline = new int[bufsize + 2048];
66  int[] newbufcolumn = new int[bufsize + 2048];
67 
68  try
69  {
70  if (wrapAround)
71  {
72  Array.Copy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
73  Array.Copy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
74  buffer = newbuffer;
75 
76  Array.Copy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
77  Array.Copy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
78  bufline = newbufline;
79 
80  Array.Copy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
81  Array.Copy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
82  bufcolumn = newbufcolumn;
83 
84  maxNextCharInd = (bufpos += (bufsize - tokenBegin));
85  }
86  else
87  {
88  Array.Copy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
89  buffer = newbuffer;
90 
91  Array.Copy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
92  bufline = newbufline;
93 
94  Array.Copy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
95  bufcolumn = newbufcolumn;
96 
97  maxNextCharInd = (bufpos -= tokenBegin);
98  }
99  }
100  catch (System.Exception t)
101  {
102  throw new System.ApplicationException(t.Message);
103  }
104 
105 
106  bufsize += 2048;
107  available = bufsize;
108  tokenBegin = 0;
109  }
110 
111  protected internal virtual void FillBuff()
112  {
113  if (maxNextCharInd == available)
114  {
115  if (available == bufsize)
116  {
117  if (tokenBegin > 2048)
118  {
119  bufpos = maxNextCharInd = 0;
120  available = tokenBegin;
121  }
122  else if (tokenBegin < 0)
123  bufpos = maxNextCharInd = 0;
124  else
125  ExpandBuff(false);
126  }
127  else if (available > tokenBegin)
128  available = bufsize;
129  else if ((tokenBegin - available) < 2048)
130  ExpandBuff(true);
131  else
132  available = tokenBegin;
133  }
134 
135  int i;
136  try
137  {
138  try
139  {
140  if ((i = inputStream.Read(buffer, maxNextCharInd, available - maxNextCharInd)) == 0)
141  {
142  inputStream.Close();
143  throw new System.IO.IOException();
144  }
145  else
146  maxNextCharInd += i;
147  return;
148  }
149  catch (ObjectDisposedException ode)
150  {
151  throw new System.IO.IOException("cannot read from a closed Reader", ode);
152  }
153  }
154  catch (System.IO.IOException e)
155  {
156  --bufpos;
157  Backup(0);
158  if (tokenBegin == - 1)
159  tokenBegin = bufpos;
160  throw e;
161  }
162  }
163 
164  public virtual char BeginToken()
165  {
166  tokenBegin = - 1;
167  char c = ReadChar();
168  tokenBegin = bufpos;
169 
170  return c;
171  }
172 
173  protected internal virtual void UpdateLineColumn(char c)
174  {
175  column++;
176 
177  if (prevCharIsLF)
178  {
179  prevCharIsLF = false;
180  line += (column = 1);
181  }
182  else if (prevCharIsCR)
183  {
184  prevCharIsCR = false;
185  if (c == '\n')
186  {
187  prevCharIsLF = true;
188  }
189  else
190  line += (column = 1);
191  }
192 
193  switch (c)
194  {
195 
196  case '\r':
197  prevCharIsCR = true;
198  break;
199 
200  case '\n':
201  prevCharIsLF = true;
202  break;
203 
204  case '\t':
205  column--;
206  column += (tabSize - (column % tabSize));
207  break;
208 
209  default:
210  break;
211 
212  }
213 
214  bufline[bufpos] = line;
215  bufcolumn[bufpos] = column;
216  }
217 
218  public virtual char ReadChar()
219  {
220  if (inBuf > 0)
221  {
222  --inBuf;
223 
224  if (++bufpos == bufsize)
225  bufpos = 0;
226 
227  return buffer[bufpos];
228  }
229 
230  if (++bufpos >= maxNextCharInd)
231  FillBuff();
232 
233  char c = buffer[bufpos];
234 
235  UpdateLineColumn(c);
236  return (c);
237  }
238 
239  /// <deprecated>
240  /// </deprecated>
241  /// <seealso cref="GetEndColumn">
242  /// </seealso>
243  public virtual int GetColumn()
244  {
245  return bufcolumn[bufpos];
246  }
247 
248  /// <deprecated>
249  /// </deprecated>
250  /// <seealso cref="GetEndLine">
251  /// </seealso>
252 
253  public virtual int GetLine()
254  {
255  return bufline[bufpos];
256  }
257 
258  public virtual int GetEndColumn()
259  {
260  return bufcolumn[bufpos];
261  }
262 
263  public virtual int GetEndLine()
264  {
265  return bufline[bufpos];
266  }
267 
268  public virtual int GetBeginColumn()
269  {
270  return bufcolumn[tokenBegin];
271  }
272 
273  public virtual int GetBeginLine()
274  {
275  return bufline[tokenBegin];
276  }
277 
278  public virtual void Backup(int amount)
279  {
280 
281  inBuf += amount;
282  if ((bufpos -= amount) < 0)
283  bufpos += bufsize;
284  }
285 
286  public SimpleCharStream(System.IO.StreamReader dstream, int startline, int startcolumn, int buffersize)
287  {
288  inputStream = dstream;
289  line = startline;
290  column = startcolumn - 1;
291 
292  available = bufsize = buffersize;
293  buffer = new char[buffersize];
294  bufline = new int[buffersize];
295  bufcolumn = new int[buffersize];
296  }
297 
298  public SimpleCharStream(System.IO.StreamReader dstream, int startline, int startcolumn):this(dstream, startline, startcolumn, 4096)
299  {
300  }
301 
302  public SimpleCharStream(System.IO.StreamReader dstream):this(dstream, 1, 1, 4096)
303  {
304  }
305 
306  public virtual void ReInit(System.IO.StreamReader dstream, int startline, int startcolumn, int buffersize)
307  {
308  inputStream = dstream;
309  line = startline;
310  column = startcolumn - 1;
311 
312  if (buffer == null || buffersize != buffer.Length)
313  {
314  available = bufsize = buffersize;
315  buffer = new char[buffersize];
316  bufline = new int[buffersize];
317  bufcolumn = new int[buffersize];
318  }
319  prevCharIsLF = prevCharIsCR = false;
320  tokenBegin = inBuf = maxNextCharInd = 0;
321  bufpos = - 1;
322  }
323 
324  public virtual void ReInit(System.IO.StreamReader dstream, int startline, int startcolumn)
325  {
326  ReInit(dstream, startline, startcolumn, 4096);
327  }
328 
329  public virtual void ReInit(System.IO.StreamReader dstream)
330  {
331  ReInit(dstream, 1, 1, 4096);
332  }
333 
334  public SimpleCharStream(System.IO.Stream dstream, System.String encoding, int startline, int startcolumn, int buffersize):this(encoding == null?new System.IO.StreamReader(dstream, System.Text.Encoding.Default):new System.IO.StreamReader(dstream, System.Text.Encoding.GetEncoding(encoding)), startline, startcolumn, buffersize)
335  {
336  }
337 
338  public SimpleCharStream(System.IO.Stream dstream, int startline, int startcolumn, int buffersize):this(new System.IO.StreamReader(dstream, System.Text.Encoding.Default), startline, startcolumn, buffersize)
339  {
340  }
341 
342  public SimpleCharStream(System.IO.Stream dstream, System.String encoding, int startline, int startcolumn):this(dstream, encoding, startline, startcolumn, 4096)
343  {
344  }
345 
346  public SimpleCharStream(System.IO.Stream dstream, int startline, int startcolumn):this(dstream, startline, startcolumn, 4096)
347  {
348  }
349 
350  public SimpleCharStream(System.IO.Stream dstream, System.String encoding):this(dstream, encoding, 1, 1, 4096)
351  {
352  }
353 
354  public SimpleCharStream(System.IO.Stream dstream):this(dstream, 1, 1, 4096)
355  {
356  }
357 
358  public virtual void ReInit(System.IO.Stream dstream, System.String encoding, int startline, int startcolumn, int buffersize)
359  {
360  ReInit(encoding == null?new System.IO.StreamReader(dstream, System.Text.Encoding.Default):new System.IO.StreamReader(dstream, System.Text.Encoding.GetEncoding(encoding)), startline, startcolumn, buffersize);
361  }
362 
363  public virtual void ReInit(System.IO.Stream dstream, int startline, int startcolumn, int buffersize)
364  {
365  ReInit(new System.IO.StreamReader(dstream, System.Text.Encoding.Default), startline, startcolumn, buffersize);
366  }
367 
368  public virtual void ReInit(System.IO.Stream dstream, System.String encoding)
369  {
370  ReInit(dstream, encoding, 1, 1, 4096);
371  }
372 
373  public virtual void ReInit(System.IO.Stream dstream)
374  {
375  ReInit(dstream, 1, 1, 4096);
376  }
377  public virtual void ReInit(System.IO.Stream dstream, System.String encoding, int startline, int startcolumn)
378  {
379  ReInit(dstream, encoding, startline, startcolumn, 4096);
380  }
381  public virtual void ReInit(System.IO.Stream dstream, int startline, int startcolumn)
382  {
383  ReInit(dstream, startline, startcolumn, 4096);
384  }
385  public virtual System.String GetImage()
386  {
387  if (bufpos >= tokenBegin)
388  return new System.String(buffer, tokenBegin, bufpos - tokenBegin + 1);
389  else
390  return new System.String(buffer, tokenBegin, bufsize - tokenBegin) + new System.String(buffer, 0, bufpos + 1);
391  }
392 
393  public virtual char[] GetSuffix(int len)
394  {
395  char[] ret = new char[len];
396 
397  if ((bufpos + 1) >= len)
398  Array.Copy(buffer, bufpos - len + 1, ret, 0, len);
399  else
400  {
401  Array.Copy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
402  Array.Copy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
403  }
404 
405  return ret;
406  }
407 
408  public virtual void Done()
409  {
410  buffer = null;
411  bufline = null;
412  bufcolumn = null;
413  }
414 
415  /// <summary> Method to adjust line and column numbers for the start of a token.</summary>
416  public virtual void AdjustBeginLineColumn(int newLine, int newCol)
417  {
418  int start = tokenBegin;
419  int len;
420 
421  if (bufpos >= tokenBegin)
422  {
423  len = bufpos - tokenBegin + inBuf + 1;
424  }
425  else
426  {
427  len = bufsize - tokenBegin + bufpos + 1 + inBuf;
428  }
429 
430  int i = 0, j = 0, k = 0;
431  int nextColDiff = 0, columnDiff = 0;
432 
433  while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
434  {
435  bufline[j] = newLine;
436  nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
437  bufcolumn[j] = newCol + columnDiff;
438  columnDiff = nextColDiff;
439  i++;
440  }
441 
442  if (i < len)
443  {
444  bufline[j] = newLine++;
445  bufcolumn[j] = newCol + columnDiff;
446 
447  while (i++ < len)
448  {
449  if (bufline[j = start % bufsize] != bufline[++start % bufsize])
450  bufline[j] = newLine++;
451  else
452  bufline[j] = newLine;
453  }
454  }
455 
456  line = bufline[j];
457  column = bufcolumn[j];
458  }
459  }
460 }