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

ConcatFilter

public final class ConcatFilter extends BaseParamFilterReader implements ChainableReader
Concats a file before and/or after the file.

Example:







Copies all java sources from src to build and adds the content of apache-license-java.txt add the beginning of each file.

since
1.6
version
2003-09-23

Fields Summary
private File
prepend
File to add before the content.
private File
append
File to add after the content.
private Reader
prependReader
Reader for prepend-file.
private Reader
appendReader
Reader for append-file.
Constructors Summary
public ConcatFilter()
Constructor for "dummy" instances.

see
BaseFilterReader#BaseFilterReader()


               
      
        super();
    
public ConcatFilter(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 java.io.Readerchain(java.io.Reader rdr)
Creates a new ConcatReader 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

        ConcatFilter newFilter = new ConcatFilter(rdr);
        newFilter.setPrepend(getPrepend());
        newFilter.setAppend(getAppend());
        // Usually the initialized is set to true. But here it must not.
        // Because the prepend and append readers have to be instantiated
        // on runtime
        //newFilter.setInitialized(true);
        return newFilter;
    
public java.io.FilegetAppend()
Returns append attribute.

return
append attribute

        return append;
    
public java.io.FilegetPrepend()
Returns prepend attribute.

return
prepend attribute

        return prepend;
    
private voidinitialize()
Scans the parameters list for the "lines" parameter and uses it to set the number of lines to be returned in the filtered stream. also scan for skip parameter.

        // get parameters
        Parameter[] params = getParameters();
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if ("prepend".equals(params[i].getName())) {
                    setPrepend(new File(params[i].getValue()));
                    continue;
                }
                if ("append".equals(params[i].getName())) {
                    setAppend(new File(params[i].getValue()));
                    continue;
                }
            }
        }
        if (prepend != null) {
            if (!prepend.isAbsolute()) {
                prepend = new File(getProject().getBaseDir(), prepend.getPath());
            }
            prependReader = new BufferedReader(new FileReader(prepend));
        }
        if (append != null) {
            if (!append.isAbsolute()) {
                append = new File(getProject().getBaseDir(), append.getPath());
            }
            appendReader = new BufferedReader(new FileReader(append));
        }
   
public intread()
Returns the next character in the filtered stream. If the desired number of lines have already been read, the resulting stream is effectively at an end. Otherwise, the next character from the underlying stream is read and returned.

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

        // do the "singleton" initialization
        if (!getInitialized()) {
            initialize();
            setInitialized(true);
        }

        int ch = -1;

        // The readers return -1 if they end. So simply read the "prepend"
        // after that the "content" and at the end the "append" file.
        if (prependReader != null) {
            ch = prependReader.read();
            if (ch == -1) {
                // I am the only one so I have to close the reader
                prependReader.close();
                prependReader = null;
            }
        }
        if (ch == -1) {
            ch = super.read();
        }
        if (ch == -1) {
            // don't call super.close() because that reader is used
            // on other places ...
            if (appendReader != null) {
                ch = appendReader.read();
                if (ch == -1) {
                    // I am the only one so I have to close the reader
                    appendReader.close();
                    appendReader = null;
                }
            }
        }

        return ch;
    
public voidsetAppend(java.io.File append)
Sets append attribute.

param
append new value

        this.append = append;
    
public voidsetPrepend(java.io.File prepend)
Sets prepend attribute.

param
prepend new value

        this.prepend = prepend;