Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » analysis » [javadoc | source]
    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 org.apache.lucene.util.AttributeSource;
   21   
   22   import java.io.Reader;
   23   import java.io.IOException;
   24   
   25   /** A Tokenizer is a TokenStream whose input is a Reader.
   26     <p>
   27     This is an abstract class; subclasses must override {@link #incrementToken()}
   28     <p>
   29     NOTE: Subclasses overriding {@link #incrementToken()} must
   30     call {@link AttributeSource#clearAttributes()} before
   31     setting attributes.
   32    */
   33   public abstract class Tokenizer extends TokenStream {
   34     /** The text source for this Tokenizer. */
   35     protected Reader input;
   36   
   37     /** Construct a tokenizer with null input. */
   38     protected Tokenizer() {}
   39     
   40     /** Construct a token stream processing the given input. */
   41     protected Tokenizer(Reader input) {
   42       this.input = CharReader.get(input);
   43     }
   44     
   45     /** Construct a tokenizer with null input using the given AttributeFactory. */
   46     protected Tokenizer(AttributeFactory factory) {
   47       super(factory);
   48     }
   49   
   50     /** Construct a token stream processing the given input using the given AttributeFactory. */
   51     protected Tokenizer(AttributeFactory factory, Reader input) {
   52       super(factory);
   53       this.input = CharReader.get(input);
   54     }
   55   
   56     /** Construct a token stream processing the given input using the given AttributeSource. */
   57     protected Tokenizer(AttributeSource source) {
   58       super(source);
   59     }
   60   
   61     /** Construct a token stream processing the given input using the given AttributeSource. */
   62     protected Tokenizer(AttributeSource source, Reader input) {
   63       super(source);
   64       this.input = CharReader.get(input);
   65     }
   66     
   67     /** By default, closes the input Reader. */
   68     @Override
   69     public void close() throws IOException {
   70       input.close();
   71     }
   72     
   73     /** Return the corrected offset. If {@link #input} is a {@link CharStream} subclass
   74      * this method calls {@link CharStream#correctOffset}, else returns <code>currentOff</code>.
   75      * @param currentOff offset as seen in the output
   76      * @return corrected offset based on the input
   77      * @see CharStream#correctOffset
   78      */
   79     protected final int correctOffset(int currentOff) {
   80       return (input instanceof CharStream) ? ((CharStream) input).correctOffset(currentOff) : currentOff;
   81     }
   82   
   83     /** Expert: Reset the tokenizer to a new reader.  Typically, an
   84      *  analyzer (in its reusableTokenStream method) will use
   85      *  this to re-use a previously created tokenizer. */
   86     public void reset(Reader input) throws IOException {
   87       this.input = input;
   88     }
   89   }
   90   

Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » analysis » [javadoc | source]