[monkeydoc] Merge/add monkeydoc to master.
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Analysis / KeywordTokenizer.cs
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 using System;
19
20 using OffsetAttribute = Mono.Lucene.Net.Analysis.Tokenattributes.OffsetAttribute;
21 using TermAttribute = Mono.Lucene.Net.Analysis.Tokenattributes.TermAttribute;
22 using AttributeSource = Mono.Lucene.Net.Util.AttributeSource;
23
24 namespace Mono.Lucene.Net.Analysis
25 {
26         
27         /// <summary> Emits the entire input as a single token.</summary>
28         public class KeywordTokenizer:Tokenizer
29         {
30                 
31                 private const int DEFAULT_BUFFER_SIZE = 256;
32                 
33                 private bool done;
34                 private int finalOffset;
35                 private TermAttribute termAtt;
36                 private OffsetAttribute offsetAtt;
37                 
38                 public KeywordTokenizer(System.IO.TextReader input):this(input, DEFAULT_BUFFER_SIZE)
39                 {
40                 }
41                 
42                 public KeywordTokenizer(System.IO.TextReader input, int bufferSize):base(input)
43                 {
44                         Init(bufferSize);
45                 }
46                 
47                 public KeywordTokenizer(AttributeSource source, System.IO.TextReader input, int bufferSize):base(source, input)
48                 {
49                         Init(bufferSize);
50                 }
51                 
52                 public KeywordTokenizer(AttributeFactory factory, System.IO.TextReader input, int bufferSize):base(factory, input)
53                 {
54                         Init(bufferSize);
55                 }
56                 
57                 private void  Init(int bufferSize)
58                 {
59                         this.done = false;
60                         termAtt = (TermAttribute) AddAttribute(typeof(TermAttribute));
61                         offsetAtt = (OffsetAttribute) AddAttribute(typeof(OffsetAttribute));
62                         termAtt.ResizeTermBuffer(bufferSize);
63                 }
64                 
65                 public override bool IncrementToken()
66                 {
67                         if (!done)
68                         {
69                                 ClearAttributes();
70                                 done = true;
71                                 int upto = 0;
72                                 char[] buffer = termAtt.TermBuffer();
73                                 while (true)
74                                 {
75                                         int length = input.Read(buffer, upto, buffer.Length - upto);
76                                         if (length == 0)
77                                                 break;
78                                         upto += length;
79                                         if (upto == buffer.Length)
80                                                 buffer = termAtt.ResizeTermBuffer(1 + buffer.Length);
81                                 }
82                                 termAtt.SetTermLength(upto);
83                                 finalOffset = CorrectOffset(upto);
84                                 offsetAtt.SetOffset(CorrectOffset(0), finalOffset);
85                                 return true;
86                         }
87                         return false;
88                 }
89                 
90                 public override void  End()
91                 {
92                         // set final offset 
93                         offsetAtt.SetOffset(finalOffset, finalOffset);
94                 }
95                 
96                 /// <deprecated> Will be removed in Lucene 3.0. This method is final, as it should
97                 /// not be overridden. Delegates to the backwards compatibility layer. 
98                 /// </deprecated>
99         [Obsolete("Will be removed in Lucene 3.0. This method is final, as it should not be overridden. Delegates to the backwards compatibility layer. ")]
100                 public override Token Next(Token reusableToken)
101                 {
102                         return base.Next(reusableToken);
103                 }
104                 
105                 /// <deprecated> Will be removed in Lucene 3.0. This method is final, as it should
106                 /// not be overridden. Delegates to the backwards compatibility layer. 
107                 /// </deprecated>
108         [Obsolete("Will be removed in Lucene 3.0. This method is final, as it should not be overridden. Delegates to the backwards compatibility layer. ")]
109                 public override Token Next()
110                 {
111                         return base.Next();
112                 }
113                 
114                 public override void  Reset(System.IO.TextReader input)
115                 {
116                         base.Reset(input);
117                         this.done = false;
118                 }
119         }
120 }