816N/A/*
816N/A * CDDL HEADER START
816N/A *
816N/A * The contents of this file are subject to the terms of the
816N/A * Common Development and Distribution License (the "License").
816N/A * You may not use this file except in compliance with the License.
816N/A *
816N/A * See LICENSE.txt included in this distribution for the specific
816N/A * language governing permissions and limitations under the License.
816N/A *
816N/A * When distributing Covered Code, include this CDDL HEADER in each
816N/A * file and include the License file at LICENSE.txt.
816N/A * If applicable, add the following below this CDDL HEADER, with the
816N/A * fields enclosed by brackets "[]" replaced with your own identifying
816N/A * information: Portions Copyright [yyyy] [name of copyright owner]
816N/A *
816N/A * CDDL HEADER END
816N/A */
816N/A
816N/A/*
1416N/A * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
816N/A */
816N/Apackage org.opensolaris.opengrok.analysis;
816N/A
1056N/Aimport java.io.CharArrayReader;
1057N/Aimport java.io.IOException;
1056N/Aimport java.io.Reader;
1416N/Aimport java.io.StringReader;
1382N/Aimport java.util.Stack;
816N/Aimport org.apache.lucene.analysis.Tokenizer;
1318N/Aimport org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
928N/Aimport org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
928N/Aimport org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
816N/A
816N/A/**
928N/A *
1416N/A * Generally this is a "template" for all new Tokenizers, so be carefull when
1416N/A * changing it, it will impact almost ALL symbol tokenizers in OpenGrok ...
816N/A *
816N/A * Created on August 24, 2009
1416N/A *
816N/A * @author Lubos Kosco
816N/A */
816N/Apublic abstract class JFlexTokenizer extends Tokenizer {
816N/A
1392N/A protected Stack<Integer> stack = new Stack<Integer>();
1382N/A
928N/A // default jflex scanner methods and variables
1057N/A abstract public boolean yylex() throws IOException;
1416N/A
1056N/A abstract public void yyreset(Reader reader);
1416N/A
1057N/A abstract public void yyclose() throws IOException;
1416N/A
1382N/A abstract public void yybegin(int newState);
1416N/A
1382N/A abstract public int yystate();
1382N/A
1369N/A public JFlexTokenizer(java.io.Reader input) {
1369N/A super(input);
1369N/A }
1056N/A
1056N/A /**
1056N/A * Reinitialize the tokenizer with new contents.
1056N/A *
1056N/A * @param contents a char buffer with text to tokenize
1056N/A * @param length the number of characters to use from the char buffer
1056N/A */
1056N/A public final void reInit(char[] contents, int length) {
1416N/A this.yyreset(new CharArrayReader(contents, 0, length));
1416N/A }
1416N/A
1416N/A public final void reInit(String s) {
1416N/A this.yyreset(new StringReader(s));
1416N/A }
1416N/A
1416N/A /**
1416N/A * Reinitialize the tokenizer with new reader.
1416N/A *
1416N/A * @param reader new reader for this tokenizer
1416N/A */
1416N/A public final void reInit(Reader reader) {
1416N/A this.yyreset(reader);
1056N/A }
1056N/A
1057N/A @Override
1057N/A public final void close() throws IOException {
1416N/A this.yyclose();
1057N/A }
1416N/A protected CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
1416N/A protected OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
1416N/A protected PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
1416N/A protected int finalOffset;
849N/A
816N/A /**
1416N/A * This will re-initalize internal AttributeImpls, or it returns false if
1416N/A * end of input Reader ...
1416N/A *
928N/A * @return false if no more tokens, otherwise true
816N/A * @throws java.io.IOException
1190N/A */
816N/A @Override
1344N/A public final boolean incrementToken() throws java.io.IOException {
1190N/A return this.yylex();
928N/A }
928N/A
1004N/A protected void setAttribs(String str, int start, int end) {
1416N/A clearAttributes();
930N/A //FIXME increasing below by one(default) might be tricky, need more analysis
1416N/A // after lucene upgrade to 3.5 below is most probably not even needed
1416N/A this.posIncrAtt.setPositionIncrement(1);
1318N/A this.termAtt.setEmpty();
1318N/A this.termAtt.append(str);
928N/A this.offsetAtt.setOffset(start, end);
928N/A }
1382N/A
1382N/A public void yypush(int newState) {
1382N/A this.stack.push(yystate());
1416N/A this.yybegin(newState);
1382N/A }
1382N/A
1382N/A public void yypop() {
1416N/A this.yybegin(this.stack.pop());
1382N/A }
816N/A}