KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Patch


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.types.Commandline;
27
28 /**
29  * Patches a file by applying a 'diff' file to it; requires "patch" to be
30  * on the execution path.
31  *
32  * @since Ant 1.1
33  *
34  * @ant.task category="utility"
35  */

36 public class Patch extends Task {
37
38     private File JavaDoc originalFile;
39     private File JavaDoc directory;
40     private boolean havePatchfile = false;
41     private Commandline cmd = new Commandline();
42
43     /**
44      * The file to patch; optional if it can be inferred from
45      * the diff file
46      * @param file the file to patch
47      */

48     public void setOriginalfile(File JavaDoc file) {
49         originalFile = file;
50     }
51
52     /**
53      * The name of a file to send the output to, instead of patching
54      * the file(s) in place; optional.
55      * @param file the file to send the output to
56      * @since Ant 1.6
57      */

58     public void setDestfile(File JavaDoc file) {
59         if (file != null) {
60             cmd.createArgument().setValue("-o");
61             cmd.createArgument().setFile(file);
62         }
63     }
64
65     /**
66      * The file containing the diff output; required.
67      * @param file the file containing the diff output
68      */

69     public void setPatchfile(File JavaDoc file) {
70         if (!file.exists()) {
71             throw new BuildException("patchfile " + file + " doesn\'t exist",
72                                      getLocation());
73         }
74         cmd.createArgument().setValue("-i");
75         cmd.createArgument().setFile(file);
76         havePatchfile = true;
77     }
78
79     /**
80      * flag to create backups; optional, default=false
81      * @param backups if true create backups
82      */

83     public void setBackups(boolean backups) {
84         if (backups) {
85             cmd.createArgument().setValue("-b");
86         }
87     }
88
89     /**
90      * flag to ignore whitespace differences; default=false
91      * @param ignore if true ignore whitespace differences
92      */

93     public void setIgnorewhitespace(boolean ignore) {
94         if (ignore) {
95             cmd.createArgument().setValue("-l");
96         }
97     }
98
99     /**
100      * Strip the smallest prefix containing <i>num</i> leading slashes
101      * from filenames.
102      *
103      * <p>patch's <i>-p</i> option.
104      * @param num number of lines to strip
105      * @exception BuildException if num is < 0, or other errors
106      */

107     public void setStrip(int num) throws BuildException {
108         if (num < 0) {
109             throw new BuildException("strip has to be >= 0", getLocation());
110         }
111         cmd.createArgument().setValue("-p" + num);
112     }
113
114     /**
115      * Work silently unless an error occurs; optional, default=false
116      * @param q if true suppress set the -s option on the patch command
117      */

118     public void setQuiet(boolean q) {
119         if (q) {
120             cmd.createArgument().setValue("-s");
121         }
122     }
123
124     /**
125      * Assume patch was created with old and new files swapped; optional,
126      * default=false
127      * @param r if true set the -R option on the patch command
128      */

129     public void setReverse(boolean r) {
130         if (r) {
131             cmd.createArgument().setValue("-R");
132         }
133     }
134
135     /**
136      * The directory to run the patch command in, defaults to the
137      * project's base directory.
138      * @param directory the directory to run the patch command in
139      * @since Ant 1.5
140      */

141     public void setDir(File JavaDoc directory) {
142         this.directory = directory;
143     }
144
145     /**
146      * execute patch
147      * @throws BuildException when it all goes a bit pear shaped
148      */

149     public void execute() throws BuildException {
150         if (!havePatchfile) {
151             throw new BuildException("patchfile argument is required",
152                                      getLocation());
153         }
154         Commandline toExecute = (Commandline) cmd.clone();
155         toExecute.setExecutable("patch");
156
157         if (originalFile != null) {
158             toExecute.createArgument().setFile(originalFile);
159         }
160
161         Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
162                                                        Project.MSG_WARN),
163                                   null);
164         exe.setCommandline(toExecute.getCommandline());
165
166         if (directory != null) {
167             if (directory.exists() && directory.isDirectory()) {
168                 exe.setWorkingDirectory(directory);
169             } else if (!directory.isDirectory()) {
170                 throw new BuildException(directory + " is not a directory.",
171                                          getLocation());
172             } else {
173                 throw new BuildException("directory " + directory
174                                          + " doesn\'t exist", getLocation());
175             }
176         } else {
177             exe.setWorkingDirectory(getProject().getBaseDir());
178         }
179
180         log(toExecute.describeCommand(), Project.MSG_VERBOSE);
181         try {
182             exe.execute();
183         } catch (IOException JavaDoc e) {
184             throw new BuildException(e, getLocation());
185         }
186     }
187 }
188
Popular Tags