/*
* 作成日: 2004/06/13
* License Apache 2.0
*org.apache.tools.ant.fileters.LineContains を元にしています。
*/
package org.jpn.xucker.ant.filter;
import java.io.IOException;
import java.io.Reader;
import java.util.Vector;
import org.apache.tools.ant.filters.BaseParamFilterReader;
import org.apache.tools.ant.filters.ChainableReader;
import org.apache.tools.ant.types.Parameter;
public final class LineWhicheverNotContains
extends BaseParamFilterReader
implements ChainableReader {
private static final String NOTCONTAINS_KEY = "notcontains";
private Vector notcontains = new Vector();
private String line = null;
public LineWhicheverNotContains() {
super();
}
public LineWhicheverNotContains(final Reader in) {
super(in);
}
public final int read() throws IOException {
if (!getInitialized()) {
initialize();
setInitialized(true);
}
int ch = -1;
if (line != null) {
ch = line.charAt(0);
if (line.length() == 1) {
line = null;
} else {
line = line.substring(1);
}
} else {
line = readLine();
final int containsSize = notcontains.size();
while (line != null) {
boolean isNotContains=false;
if(containsSize==0)
isNotContains=true;
for (int i = 0; i < containsSize; i++) {
String containsStr = (String) notcontains.elementAt(i);
if (line.indexOf(containsStr) == -1) {
isNotContains=true;
break;
}
}
if(isNotContains==false){
line=null;
}
if (line == null) {
// line didn't match
line = readLine();
} else {
break;
}
}
if (line != null) {
return read();
}
}
return ch;
}
/**
* Adds a contains element.
*
* @param contains The contains element to add.
* Must not be null.
*/
public final void addConfiguredContains(final NotContains contains) {
this.notcontains.addElement(contains.getValue());
}
/**
* Sets the vector of words which must be contained within a line read
* from the original stream in order for it to match this filter.
*
* @param contains A vector of words which must be contained within a line
* in order for it to match in this filter. Must not be null.
*/
private void setNotContains(final Vector contains) {
this.notcontains = contains;
}
/**
* Returns the vector of words which must be contained within a line read
* from the original stream in order for it to match this filter.
*
* @return the vector of words which must be contained within a line read
* from the original stream in order for it to match this filter. The
* returned object is "live" - in other words, changes made to the
* returned object are mirrored in the filter.
*/
private final Vector getNotContains() {
return notcontains;
}
/**
* Creates a new LineContains using the passed in
* Reader for instantiation.
*
* @param rdr A Reader object providing the underlying stream.
* Must not be null.
*
* @return a new filter based on this configuration, but filtering
* the specified reader
*/
public final Reader chain(final Reader rdr) {
LineWhicheverNotContains newFilter = new LineWhicheverNotContains(rdr);
newFilter.setNotContains(getNotContains());
newFilter.setInitialized(true);
return newFilter;
}
/**
* Parses the parameters to add user-defined contains strings.
*/
private final void initialize() {
Parameter[] params = getParameters();
if (params != null) {
for (int i = 0; i < params.length; i++) {
if (NOTCONTAINS_KEY.equals(params[i].getType())) {
notcontains.addElement(params[i].getValue());
}
}
}
}
/**
* Holds a contains element
*/
public static class NotContains {
/** User defined contains string */
private String value;
/**
* Sets the contains string
*
* @param contains The contains string to set.
* Must not be null.
*/
public final void setValue(String contains) {
value = contains;
}
/**
* Returns the contains string.
*
* @return the contains string for this element
*/
public final String getValue() {
return value;
}
}
}