1 package org.apache.lucene.analysis;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import org.apache.lucene.util.AttributeSource;
22 import java.io.Reader;
23 import java.io.IOException;
25 /** A Tokenizer is a TokenStream whose input is a Reader.
27 This is an abstract class; subclasses must override {@link #incrementToken()}
29 NOTE: Subclasses overriding {@link #incrementToken()} must
30 call {@link AttributeSource#clearAttributes()} before
33 public abstract class Tokenizer extends TokenStream {
34 /** The text source for this Tokenizer. */
35 protected Reader input;
37 /** Construct a tokenizer with null input. */
38 protected Tokenizer() {}
40 /** Construct a token stream processing the given input. */
41 protected Tokenizer(Reader input) {
42 this.input = CharReader.get(input);
45 /** Construct a tokenizer with null input using the given AttributeFactory. */
46 protected Tokenizer(AttributeFactory factory) {
50 /** Construct a token stream processing the given input using the given AttributeFactory. */
51 protected Tokenizer(AttributeFactory factory, Reader input) {
53 this.input = CharReader.get(input);
56 /** Construct a token stream processing the given input using the given AttributeSource. */
57 protected Tokenizer(AttributeSource source) {
61 /** Construct a token stream processing the given input using the given AttributeSource. */
62 protected Tokenizer(AttributeSource source, Reader input) {
64 this.input = CharReader.get(input);
67 /** By default, closes the input Reader. */
69 public void close() throws IOException {
72 // LUCENE-2387: don't hold onto Reader after close, so
78 /** Return the corrected offset. If {@link #input} is a {@link CharStream} subclass
79 * this method calls {@link CharStream#correctOffset}, else returns <code>currentOff</code>.
80 * @param currentOff offset as seen in the output
81 * @return corrected offset based on the input
82 * @see CharStream#correctOffset
84 protected final int correctOffset(int currentOff) {
85 return (input instanceof CharStream) ? ((CharStream) input).correctOffset(currentOff) : currentOff;
88 /** Expert: Reset the tokenizer to a new reader. Typically, an
89 * analyzer (in its reusableTokenStream method) will use
90 * this to re-use a previously created tokenizer. */
91 public void reset(Reader input) throws IOException {