1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.IOException ; 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 36 public class Patch extends Task { 37 38 private File originalFile; 39 private File directory; 40 private boolean havePatchfile = false; 41 private Commandline cmd = new Commandline(); 42 43 48 public void setOriginalfile(File file) { 49 originalFile = file; 50 } 51 52 58 public void setDestfile(File file) { 59 if (file != null) { 60 cmd.createArgument().setValue("-o"); 61 cmd.createArgument().setFile(file); 62 } 63 } 64 65 69 public void setPatchfile(File 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 83 public void setBackups(boolean backups) { 84 if (backups) { 85 cmd.createArgument().setValue("-b"); 86 } 87 } 88 89 93 public void setIgnorewhitespace(boolean ignore) { 94 if (ignore) { 95 cmd.createArgument().setValue("-l"); 96 } 97 } 98 99 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 118 public void setQuiet(boolean q) { 119 if (q) { 120 cmd.createArgument().setValue("-s"); 121 } 122 } 123 124 129 public void setReverse(boolean r) { 130 if (r) { 131 cmd.createArgument().setValue("-R"); 132 } 133 } 134 135 141 public void setDir(File directory) { 142 this.directory = directory; 143 } 144 145 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 e) { 184 throw new BuildException(e, getLocation()); 185 } 186 } 187 } 188 | Popular Tags |