FileDocCategorySizeDatePackage
StripLineComments.javaAPI DocApache Ant 1.706724Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.filters

StripLineComments

public final class StripLineComments extends BaseParamFilterReader implements ChainableReader
This filter strips line comments. Example:
<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>

Fields Summary
private static final String
COMMENTS_KEY
Parameter name for the comment prefix.
private Vector
comments
Vector that holds the comment prefixes.
private String
line
The line that has been read ahead.
Constructors Summary
public StripLineComments()
Constructor for "dummy" instances.

see
BaseFilterReader#BaseFilterReader()


               
      
        super();
    
public StripLineComments(Reader in)
Creates a new filtered reader.

param
in A Reader object providing the underlying stream. Must not be null.

        super(in);
    
Methods Summary
public voidaddConfiguredComment(org.apache.tools.ant.filters.StripLineComments$Comment comment)
Adds a comment element to the list of prefixes.

param
comment The comment element to add to the list of comment prefixes to strip. Must not be null.

        comments.addElement(comment.getValue());
    
public java.io.Readerchain(java.io.Reader rdr)
Creates a new StripLineComments 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

        StripLineComments newFilter = new StripLineComments(rdr);
        newFilter.setComments(getComments());
        newFilter.setInitialized(true);
        return newFilter;
    
private java.util.VectorgetComments()
Returns the list of comment prefixes to strip.

return
the list of comment prefixes to strip.

        return comments;
    
private voidinitialize()
Parses the parameters to set the comment prefixes.

        Parameter[] params = getParameters();
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if (COMMENTS_KEY.equals(params[i].getType())) {
                    comments.addElement(params[i].getValue());
                }
            }
        }
    
public intread()
Returns the next character in the filtered stream, only including lines from the original stream which don't start with any of the specified comment prefixes.

return
the next character in the resulting stream, or -1 if the end of the resulting stream has been reached
exception
IOException if the underlying stream throws an IOException during reading

        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 commentsSize = comments.size();

            while (line != null) {
                for (int i = 0; i < commentsSize; i++) {
                    String comment = (String) comments.elementAt(i);
                    if (line.startsWith(comment)) {
                        line = null;
                        break;
                    }
                }

                if (line == null) {
                    // line started with comment
                    line = readLine();
                } else {
                    break;
                }
            }

            if (line != null) {
                return read();
            }
        }

        return ch;
    
private voidsetComments(java.util.Vector comments)
Sets the list of comment prefixes to strip.

param
comments A list of strings, each of which is a prefix for a comment line. Must not be null.

        this.comments = comments;