KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > anttasks > AppendFile


1 package de.webman.anttasks;
2
3 import java.io.*;
4 import org.apache.tools.ant.BuildException;
5 import org.apache.tools.ant.Task;
6
7
8 /**
9  * ant task to append an input file to a output file
10  *
11  * this task might be added to the build script with
12  * <taskdef name="append-file" classname="de.webman.anttasks.AppendFile"/>
13  * where the name of the task is unimportant
14  *
15  * usage:
16  * <append-file file="inputfile" appendto="outputfile" failonerror=("true"|"false")/>
17  *
18  * inputfile the name of the file to append to the output file
19  * outputfile the name of the file the input file will be appended to
20  * if the file does not exist, the input file will be
21  * copied
22  * both files must be distinct
23  *
24  * failonerror optional argument,
25  * Stop the buildprocess
26  * if the input file could not be read
27  * or the output file can not be written
28  * (default is true)
29  */

30 public class AppendFile extends Task {
31
32     private File infile;
33     private File outfile;
34     boolean failonerror = true;
35     
36     // The method executing the task
37
public void execute() throws BuildException {
38         String JavaDoc msg = null;
39         // file tests
40
if ( infile == null ){
41             msg = "argument file must be given";
42         }
43         else if ( outfile == null ){
44             msg = "argument appendto must be given";
45         }
46         else if ( ! infile.exists() ){
47             msg = "file " + infile + " does not exist";
48         }
49         else if ( ! infile.isFile() ){
50             msg = "file " + infile + " is not a plain file";
51         }
52         else if ( ! infile.canRead() ){
53             msg = "file " + infile + " is not readable";
54         }
55         else if ( infile.equals(outfile) ){
56                 msg = "cannot append a file to itself";
57         }
58         else if ( outfile.exists() ){
59             if ( ! outfile.isFile() ){
60                 msg = "file " + outfile + " is not a plain file";
61             }
62             else if ( ! outfile.canWrite() ){
63                 msg = "file " + outfile + " is not writeable";
64             }
65         }
66         // throw msg
67
if ( msg != null ){
68             if ( failonerror ){
69                 throw new BuildException(msg);
70             }
71             else{
72                 System.out.println(msg);
73                 return;
74             }
75         }
76         try{
77             // create file if it does not exist
78
if ( outfile.createNewFile() ){
79                 System.out.println("created file " + outfile);
80             }
81             // append now
82
FileInputStream fis = new FileInputStream(infile);
83             FileOutputStream fos = new FileOutputStream(outfile.getPath(), true);
84             byte[] buff = new byte[1024];
85             int read;
86             while ( (read = fis.read(buff)) > 0 ){
87                 fos.write(buff, 0, read);
88             }
89             fis.close();
90             fos.flush();
91             fos.close();
92             // report success
93
System.out.println("appended " + infile
94                                + " to " + outfile);
95         }
96         catch (IOException e){
97             throw new BuildException(e);
98         }
99     }
100     
101     /**
102      * the setter for the source file
103      */

104     public void setFile(File in){
105         infile = in;
106     }
107
108     /**
109      * the setter for the target file
110      */

111     public void setAppendto(File out){
112         outfile = out;
113     }
114
115      /**
116      * the setter for the failonerror argument
117      */

118     public void setFailonerror(boolean newValue){
119         failonerror = newValue;
120     }
121 }
122
Popular Tags