1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.Task; 24 import org.apache.tools.ant.util.FileUtils; 25 import org.apache.tools.ant.util.FileNameMapper; 26 import org.apache.tools.ant.types.Path; 27 import org.apache.tools.ant.types.Reference; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 32 39 40 public class CopyPath extends Task { 41 42 44 public static final String ERROR_NO_DESTDIR = "No destDir specified"; 45 46 47 public static final String ERROR_NO_PATH = "No path specified"; 48 49 50 public static final String ERROR_NO_MAPPER = "No mapper specified"; 51 52 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 54 55 private FileNameMapper mapper; 57 58 private Path path; 59 60 private File destDir; 61 62 private long granularity = FILE_UTILS.getFileTimestampGranularity(); 64 65 private boolean preserveLastModified = false; 66 67 71 public void setDestDir(File destDir) { 72 this.destDir = destDir; 73 } 74 75 80 public void add(FileNameMapper newmapper) { 81 if (mapper != null) { 82 throw new BuildException("Only one mapper allowed"); 83 } 84 mapper = newmapper; 85 } 86 87 93 public void setPath(Path s) { 94 createPath().append(s); 95 } 96 97 103 public void setPathRef(Reference r) { 104 createPath().setRefid(r); 105 } 106 107 112 public Path createPath() { 113 if (path == null) { 114 path = new Path(getProject()); 115 } 116 return path; 117 } 118 119 126 public void setGranularity(long granularity) { 127 this.granularity = granularity; 128 } 129 130 135 public void setPreserveLastModified(boolean preserveLastModified) { 136 this.preserveLastModified = preserveLastModified; 137 } 138 139 146 protected void validateAttributes() throws BuildException { 147 if (destDir == null) { 148 throw new BuildException(ERROR_NO_DESTDIR); 149 } 150 if (mapper == null) { 151 throw new BuildException(ERROR_NO_MAPPER); 152 } 153 if (path == null) { 154 throw new BuildException(ERROR_NO_PATH); 155 } 156 } 157 158 164 public void execute() throws BuildException { 165 validateAttributes(); 166 String [] sourceFiles = path.list(); 167 if (sourceFiles.length == 0) { 168 log("Path is empty", Project.MSG_VERBOSE); 169 return; 170 } 171 172 for (int sources = 0; sources < sourceFiles.length; sources++) { 173 174 String sourceFileName = sourceFiles[sources]; 175 File sourceFile = new File (sourceFileName); 176 String [] toFiles = (String []) mapper.mapFileName(sourceFileName); 177 178 for (int i = 0; i < toFiles.length; i++) { 179 String destFileName = toFiles[i]; 180 File destFile = new File (destDir, destFileName); 181 182 if (sourceFile.equals(destFile)) { 183 log("Skipping self-copy of " + sourceFileName, Project.MSG_VERBOSE); 184 continue; 185 } 186 if (sourceFile.isDirectory()) { 187 log("Skipping directory " + sourceFileName); 188 continue; 189 } 190 try { 191 log("Copying " + sourceFile + " to " + destFile, Project.MSG_VERBOSE); 192 193 FILE_UTILS.copyFile(sourceFile, destFile, null, null, false, 194 preserveLastModified, null, null, getProject()); 195 } catch (IOException ioe) { 196 String msg = "Failed to copy " + sourceFile + " to " + destFile + " due to " 197 + ioe.getMessage(); 198 if (destFile.exists() && !destFile.delete()) { 199 msg += " and I couldn't delete the corrupt " + destFile; 200 } 201 throw new BuildException(msg, ioe, getLocation()); 202 } 203 } 204 } 205 } 206 } 207 | Popular Tags |