ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan.Mate...@rzf.fin-nrw.de
Subject RE: prepend file to multiple files
Date Tue, 16 Sep 2003 12:27:59 GMT
Quick hack.
I think I will do a little more work (test, doc) and commit that.



--8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-
------8-<-----

package org.apache.tools.ant.filters;

import java.io.IOException;
import java.io.Reader;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import org.apache.tools.ant.types.Parameter;

public final class ConcatReader extends BaseParamFilterReader
    implements ChainableReader {

    private File before;
    private File after;

    Reader beforeReader = new EmptyReader();
    Reader afterReader = new EmptyReader();

    public ConcatReader() {
        super();
    }

    public ConcatReader(final Reader in) {
        super(in);
    }

    public final int read() throws IOException {
        if (!getInitialized()) {
            initialize();
            setInitialized(true);
        }

        int ch = -1;

        ch = beforeReader.read();
        if (ch == -1) {
            ch = super.read();
        }
        if (ch == -1) {
            ch = afterReader.read();
        }

        return ch;
    }

    public final void setBefore(final File before) {
        this.before = before;
    }

    public final File getBefore() {
        return before;
    }

    public final void setAfter(final File after) {
        this.after = after;
    }

    public final File getAfter() {
        return after;
    }

    public final Reader chain(final Reader rdr) {
        ConcatReader newFilter = new ConcatReader(rdr);
        newFilter.setBefore(getBefore());
        newFilter.setAfter(getAfter());
        newFilter.setInitialized(true);
        return newFilter;
    }

    private final void initialize() throws IOException {
        // get parameters
        Parameter[] params = getParameters();
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if ("before".equals(params[i].getName())) {
                    before = new File(params[i].getValue());
                    continue;
                }
                if ("after".equals(params[i].getName())) {
                    after = new File(params[i].getValue());
                    continue;
                }
            }
        }
        if (before!=null) {
            beforeReader = new BufferedReader(new FileReader(before));
        }
        if (after!=null) {
            afterReader = new BufferedReader(new FileReader(after));
        }
   }

   private class EmptyReader extends Reader {
       public int read(char[] ch, int i1, int i2) { return -1; }
       public void close() {}
   }

}
--8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-
------8-<-----
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="test" basedir="." default="main">
    <target name="main">
        <delete dir="to"/>
        <copy todir="to">
            <fileset dir="src" includes="*.java"/>
            <filterchain>
                <filterreader
classname="org.apache.tools.ant.filters.ConcatReader">
                    <param name="before" value="license.java.txt"/>
                </filterreader>
            </filterchain>
        </copy>
        <copy todir="to">
            <fileset dir="src" includes="*.html"/>
            <filterchain>
                <filterreader
classname="org.apache.tools.ant.filters.ConcatReader">
                    <param name="after" value="license.html.txt"/>
                </filterreader>
            </filterchain>
        </copy>
    </target>
</project>
--8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-
------8-<-----

That little test works for me.

Thoughts?


Jan






> -----Original Message-----
> From: Daniel M. Bikel [mailto:dbikel@linc.cis.upenn.edu]
> Sent: Monday, September 15, 2003 6:56 PM
> To: user@ant.apache.org
> Subject: prepend file to multiple files
> 
> 
> Hi.  I need to prepend a short license description (contained 
> in file) to
> all my source files when they are copied to the deployment 
> "src" directory.
> Even though there have been similar posts on this list, I 
> cannot find a way
> to do this.  Put another way, I have a "src" directory which I want to
> recursively copy to a "deploy/src" directory (this is easily 
> done using the
> copy task), *but* I also want every file ending up in 
> deploy/src to have a
> license prepended to the beginning, and I want to do this *without*
> modiyfing my original source files (i.e,. I *could* modify 
> every one of my
> existing source files to contain the line /* @LICENSE@ */, 
> but I don't want
> to do this).
> 
> Thanks in advance!
> 
> --Dan.
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message