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