JFlexTokenizer.java revision 1185
588N/A/*
588N/A * CDDL HEADER START
588N/A *
588N/A * The contents of this file are subject to the terms of the
588N/A * Common Development and Distribution License (the "License").
588N/A * You may not use this file except in compliance with the License.
588N/A *
588N/A * See LICENSE.txt included in this distribution for the specific
588N/A * language governing permissions and limitations under the License.
588N/A *
588N/A * When distributing Covered Code, include this CDDL HEADER in each
588N/A * file and include the License file at LICENSE.txt.
588N/A * If applicable, add the following below this CDDL HEADER, with the
588N/A * fields enclosed by brackets "[]" replaced with your own identifying
588N/A * information: Portions Copyright [yyyy] [name of copyright owner]
588N/A *
588N/A * CDDL HEADER END
588N/A */
588N/A
588N/A/*
588N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
588N/A */
588N/A
588N/Apackage org.opensolaris.opengrok.analysis;
588N/A
588N/Aimport java.io.CharArrayReader;
588N/Aimport java.io.IOException;
588N/Aimport java.io.Reader;
588N/Aimport org.apache.lucene.analysis.Tokenizer;
588N/Aimport org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
588N/Aimport org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
588N/Aimport org.apache.lucene.analysis.tokenattributes.TermAttribute;
588N/A
588N/A/**
588N/A * this class was created because of lucene 2.4.1 update which introduced char[] in Tokens instead of String
588N/A * lucene 3.0.0 uses AttributeSource instead of Tokens to make things even easier :-D
588N/A *
588N/A * Generally this is a "template" for all new Tokenizers, so be carefull when changing it,
588N/A * it will impact almost ALL symbol tokenizers in OpenGrok ...
588N/A *
588N/A * Created on August 24, 2009
588N/A * @author Lubos Kosco
588N/A */
588N/A
588N/Apublic abstract class JFlexTokenizer extends Tokenizer {
588N/A
588N/A // default jflex scanner methods and variables
588N/A abstract public boolean yylex() throws IOException;
abstract public void yyreset(Reader reader);
abstract public void yyclose() throws IOException;
/**
* Reinitialize the tokenizer with new contents.
*
* @param contents a char buffer with text to tokenize
* @param length the number of characters to use from the char buffer
*/
public final void reInit(char[] contents, int length) {
yyreset(new CharArrayReader(contents, 0, length));
}
@Override
public final void close() throws IOException {
yyclose();
}
protected TermAttribute termAtt= addAttribute(TermAttribute.class);
protected OffsetAttribute offsetAtt= addAttribute(OffsetAttribute.class);
protected PositionIncrementAttribute posIncrAtt= addAttribute(PositionIncrementAttribute.class);
/**
* This will reinitalize internal AttributeImpls, or it returns false if end of input Reader ...
* @return false if no more tokens, otherwise true
* @throws java.io.IOException
*/
@Override
public boolean incrementToken() throws java.io.IOException {
return this.yylex();
}
protected void setAttribs(String str, int start, int end) {
//FIXME increasing below by one(default) might be tricky, need more analysis
this.posIncrAtt.setPositionIncrement(1);
this.termAtt.setTermBuffer(str);
this.offsetAtt.setOffset(start, end);
}
}