pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / analysis / KeywordTokenizer.java
1 package org.apache.lucene.analysis;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.io.Reader;
22
23 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
24 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
25 import org.apache.lucene.util.AttributeSource;
26
27 /**
28  * Emits the entire input as a single token.
29  */
30 public final class KeywordTokenizer extends Tokenizer {
31   
32   private static final int DEFAULT_BUFFER_SIZE = 256;
33
34   private boolean done = false;
35   private int finalOffset;
36   private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
37   private OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
38   
39   public KeywordTokenizer(Reader input) {
40     this(input, DEFAULT_BUFFER_SIZE);
41   }
42
43   public KeywordTokenizer(Reader input, int bufferSize) {
44     super(input);
45     termAtt.resizeBuffer(bufferSize);
46   }
47
48   public KeywordTokenizer(AttributeSource source, Reader input, int bufferSize) {
49     super(source, input);
50     termAtt.resizeBuffer(bufferSize);
51   }
52
53   public KeywordTokenizer(AttributeFactory factory, Reader input, int bufferSize) {
54     super(factory, input);
55     termAtt.resizeBuffer(bufferSize);
56   }
57   
58   @Override
59   public final boolean incrementToken() throws IOException {
60     if (!done) {
61       clearAttributes();
62       done = true;
63       int upto = 0;
64       char[] buffer = termAtt.buffer();
65       while (true) {
66         final int length = input.read(buffer, upto, buffer.length-upto);
67         if (length == -1) break;
68         upto += length;
69         if (upto == buffer.length)
70           buffer = termAtt.resizeBuffer(1+buffer.length);
71       }
72       termAtt.setLength(upto);
73       finalOffset = correctOffset(upto);
74       offsetAtt.setOffset(correctOffset(0), finalOffset);
75       return true;
76     }
77     return false;
78   }
79   
80   @Override
81   public final void end() {
82     // set final offset 
83     offsetAtt.setOffset(finalOffset, finalOffset);
84   }
85
86   @Override
87   public void reset(Reader input) throws IOException {
88     super.reset(input);
89     this.done = false;
90   }
91 }