@@ -3,6 +3,7 @@ package org.apache.tools.ant.filters;
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Vector;
import org.apache.tools.ant.types.Parameter;
@@ -14,19 +15,29 @@ import org.apache.tools.ant.types.Parameterizable;
* Example:
* =======
*
* <filterreader classname="org.apache.tools.ant.filters.StripLineComments">
* <param type="comment" value="#"/>
* <param type="comment" value="--"/>
* <param type="comment" value="REM "/>
* <param type="comment" value="rem "/>
* <param type="comment" value="//"/>
* <striplinecomments>
* <comment value="#"/>
* <comment value="--"/>
* <comment value="REM "/>
* <comment value="rem "/>
* <comment value="//"/>
* </striplinecomments>
*
* Or:
*
* <filterreader classname="org.apache.tools.ant.filters.StripLineComments">
* <param type="comment" value="#"/>
* <param type="comment" value="--"/>
* <param type="comment" value="REM "/>
* <param type="comment" value="rem "/>
* <param type="comment" value="//"/>
* </filterreader>
*
* @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
*/
public final class StripLineComments
extends FilterReader
implements Parameterizable
implements Parameterizable, CloneableReader
{
private static final String COMMENTS_KEY = "comment";
@@ -34,12 +45,26 @@ public final class StripLineComments
private boolean initialized = false;
private final Vector comments = new Vector();
private int commentsSize = 0;
private Vector comments = new Vector();
private String line = null;
/**
* This constructor is a dummy constructor and is
* not meant to be used by any class other than Ant's
* introspection mechanism. This will close the filter
* that is created making it useless for further operations.
*/
public StripLineComments() {
// Dummy constructor to be invoked by Ant's Introspector
super(new StringReader(new String()));
try {
close();
} catch (IOException ioe) {
// Ignore
}
}
/**
* Create a new filtered reader.
*
@@ -50,9 +75,9 @@ public final class StripLineComments
}
public final int read() throws IOException {
if (!initialized ) {
if (!getInitialized() ) {
initialize();
initialized = true ;
setInitialized(true) ;
}
int ch = -1;
@@ -78,6 +103,7 @@ public final class StripLineComments
}
if (line != null) {
int commentsSize = comments.size();
for (int i = 0; i < commentsSize; i++) {
String comment = (String) comments.elementAt(i);
if (line.startsWith(comment)) {
@@ -115,12 +141,39 @@ public final class StripLineComments
return n;
}
public final void addConfiguredComment(final Comment comment) {
comments.addElement(comment.getValue());
}
private void setComments(final Vector comments) {
this.comments = comments;
}
private final Vector getComments() {
return comments;
}
private final void setInitialized(final boolean initialized) {
this.initialized = initialized;
}
private final boolean getInitialized() {
return initialized;
}
public final Reader clone(final Reader rdr) {
StripLineComments newFilter = new StripLineComments(rdr);
newFilter.setComments(getComments());
newFilter.setInitialized(true);
return newFilter;
}
/**
* Set Parameters
*/
public final void setParameters(final Parameter[] parameters) {
this.parameters = parameters;
initialized = false;
setInitialized(false) ;
}
private final void initialize() {
@@ -130,7 +183,18 @@ public final class StripLineComments
comments.addElement(parameters[i].getValue());
}
}
commentsSize = comments.size();
}
}
public static class Comment {
private String value;
public final void setValue(String comment) {
value = comment;
}
public final String getValue() {
return value;
}
}
}