extracted common parts of the tokenizing filters to create the AbstractTokenizingFilter

Former-commit-id: 5ecb6ad7f16de5d9a22789b4cfbbf38c40a8fb35
This commit is contained in:
Jeremy Long
2013-06-23 22:33:07 -04:00
parent d27a6235f0
commit 59fd89bf68

View File

@@ -0,0 +1,80 @@
/*
* This file is part of Dependency-Check.
*
* Dependency-Check is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Dependency-Check is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* Dependency-Check. If not, see http://www.gnu.org/licenses/.
*
* Copyright (c) 2013 Jeremy Long. All Rights Reserved.
*/
package org.owasp.dependencycheck.data.lucene;
import java.util.LinkedList;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
/**
* An abstract tokenizing filter that can be used as the base for a tokenizing
* filter.
*
* @author Jeremy Long (jeremy.long@gmail.com)
*/
public abstract class AbstractTokenizingFilter extends TokenFilter {
/**
* The char term attribute.
*/
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
/**
* Gets the CharTermAttribute.
* @return the CharTermAttribute
*/
protected CharTermAttribute getTermAtt() {
return termAtt;
}
/**
* A collection of tokens to add to the stream.
*/
private final LinkedList<String> tokens;
/**
* Gets the list of tokens.
* @return the list of tokens
*/
protected LinkedList<String> getTokens() {
return tokens;
}
/**
* Constructs a new AbstractTokenizingFilter.
*
* @param stream the TokenStream that this filter will process
*/
public AbstractTokenizingFilter(TokenStream stream) {
super(stream);
tokens = new LinkedList<String>();
}
/**
* Adds a term, if one exists, from the tokens collection.
*
* @return whether or not a new term was added
*/
protected boolean addTerm() {
final boolean termAdded = tokens.size() > 0;
if (termAdded) {
final String version = tokens.pop();
clearAttributes();
termAtt.append(version);
}
return termAdded;
}
}