001: package de.webman.anttasks;
002:
003: import java.io.*;
004: import org.apache.tools.ant.BuildException;
005: import org.apache.tools.ant.Task;
006:
007: /**
008: * ant task to append an input file to a output file
009: *
010: * this task might be added to the build script with
011: * <taskdef name="append-file" classname="de.webman.anttasks.AppendFile"/>
012: * where the name of the task is unimportant
013: *
014: * usage:
015: * <append-file file="inputfile" appendto="outputfile" failonerror=("true"|"false")/>
016: *
017: * inputfile the name of the file to append to the output file
018: * outputfile the name of the file the input file will be appended to
019: * if the file does not exist, the input file will be
020: * copied
021: * both files must be distinct
022: *
023: * failonerror optional argument,
024: * Stop the buildprocess
025: * if the input file could not be read
026: * or the output file can not be written
027: * (default is true)
028: */
029: public class AppendFile extends Task {
030:
031: private File infile;
032: private File outfile;
033: boolean failonerror = true;
034:
035: // The method executing the task
036: public void execute() throws BuildException {
037: String msg = null;
038: // file tests
039: if (infile == null) {
040: msg = "argument file must be given";
041: } else if (outfile == null) {
042: msg = "argument appendto must be given";
043: } else if (!infile.exists()) {
044: msg = "file " + infile + " does not exist";
045: } else if (!infile.isFile()) {
046: msg = "file " + infile + " is not a plain file";
047: } else if (!infile.canRead()) {
048: msg = "file " + infile + " is not readable";
049: } else if (infile.equals(outfile)) {
050: msg = "cannot append a file to itself";
051: } else if (outfile.exists()) {
052: if (!outfile.isFile()) {
053: msg = "file " + outfile + " is not a plain file";
054: } else if (!outfile.canWrite()) {
055: msg = "file " + outfile + " is not writeable";
056: }
057: }
058: // throw msg
059: if (msg != null) {
060: if (failonerror) {
061: throw new BuildException(msg);
062: } else {
063: System.out.println(msg);
064: return;
065: }
066: }
067: try {
068: // create file if it does not exist
069: if (outfile.createNewFile()) {
070: System.out.println("created file " + outfile);
071: }
072: // append now
073: FileInputStream fis = new FileInputStream(infile);
074: FileOutputStream fos = new FileOutputStream(outfile
075: .getPath(), true);
076: byte[] buff = new byte[1024];
077: int read;
078: while ((read = fis.read(buff)) > 0) {
079: fos.write(buff, 0, read);
080: }
081: fis.close();
082: fos.flush();
083: fos.close();
084: // report success
085: System.out.println("appended " + infile + " to " + outfile);
086: } catch (IOException e) {
087: throw new BuildException(e);
088: }
089: }
090:
091: /**
092: * the setter for the source file
093: */
094: public void setFile(File in) {
095: infile = in;
096: }
097:
098: /**
099: * the setter for the target file
100: */
101: public void setAppendto(File out) {
102: outfile = out;
103: }
104:
105: /**
106: * the setter for the failonerror argument
107: */
108: public void setFailonerror(boolean newValue) {
109: failonerror = newValue;
110: }
111: }
|