org.kuali.core.db.torque.StringFilter Maven / Gradle / Ivy
package org.kuali.core.db.torque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This class provides logic for filtering strings based on regular expressions. Given a list of includePatterns and excludePatterns the
* filter method will remove strings from an Iterator if there is no match on an inclusion pattern or there is a match on an
* exclusion pattern
*/
public class StringFilter {
private static final Log log = LogFactory.getLog(StringFilter.class);
/**
* List of regular expressions. A string must match one of these to be included
*/
List includePatterns;
/**
* List of regular expressions. If there is a match on any of these the string is filtered out
*/
List excludePatterns;
List includes;
List excludes;
public StringFilter() {
this(null, null);
}
public StringFilter(List includePatterns, List excludePatterns) {
super();
this.includePatterns = includePatterns;
this.excludePatterns = excludePatterns;
}
/**
* Compile the string patterns into Pattern objects
*/
public void compilePatterns() {
includes = getPatterns(includePatterns);
excludes = getPatterns(excludePatterns);
}
public List getIncludePatterns() {
return includePatterns;
}
public void setIncludePatterns(List includePatterns) {
this.includePatterns = includePatterns;
}
public List getExcludePatterns() {
return excludePatterns;
}
public void setExcludePatterns(List excludePatterns) {
this.excludePatterns = excludePatterns;
}
/**
* Convert a List into List
*/
protected List getPatterns(List patterns) {
// If List is empty return an empty List
if (isEmpty(patterns)) {
return new ArrayList();
}
List regexPatterns = new ArrayList();
for (String pattern : patterns) {
Pattern regexPattern = Pattern.compile(pattern);
regexPatterns.add(regexPattern);
}
return regexPatterns;
}
/**
* Return true if the string matches any of the patterns
*/
protected boolean isMatch(String s, List patterns) {
// If patterns is null or zero size, there is no match
if (isEmpty(patterns)) {
return false;
}
// Loop through the patterns looking for a match
for (Pattern pattern : patterns) {
Matcher matcher = pattern.matcher(s);
// We found a match, return true
if (matcher.matches()) {
return true;
}
}
// We cycled through all of the patterns without finding a match
return false;
}
/**
* Return true if no inclusion patterns have been specified.
* Return true if there have been inclusion patterns specified and there is a match on at least one of them
* Return false otherwise.
*/
public boolean isInclude(String s) {
return isEmpty(includePatterns) || isMatch(s, includes);
}
/**
* Return true if the string matches one or more of the exclude patterns
*/
protected boolean isExclude(String s) {
return isMatch(s, excludes);
}
/**
* Return true if the List is null or has zero elements
*/
protected boolean isEmpty(List list) {
return list == null || list.size() == 0;
}
/**
* Invoke Iterator.remove() on any strings that match an exclusion pattern or do not match an inclusion pattern
*/
public void filter(Iterator itr) {
// Compile our Strings into Patterns
compilePatterns();
while (itr.hasNext()) {
String s = itr.next();
if (!isInclude(s)) {
// This string does not match an inclusion pattern
log.debug("No inclusion pattern match. Removing '" + s + "'");
itr.remove();
continue;
}
if (isExclude(s)) {
// This string matches an exclusion pattern
log.debug("Exclusion pattern matched. Removing '" + s + "'");
itr.remove();
continue;
}
}
}
public List getIncludes() {
return includes;
}
public List getExcludes() {
return excludes;
}
public static List getListFromCSV(String csv) {
if (StringUtils.isEmpty(csv)) {
return new ArrayList();
}
String[] tokens = csv.split(",");
List list = new ArrayList();
for (String token : tokens) {
list.add(token.trim());
}
return list;
}
}