1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.DirectoryScanner; 27 import org.apache.tools.ant.types.FileSet; 28 import org.apache.tools.ant.types.FilterSet; 29 import org.apache.tools.ant.types.FilterSetCollection; 30 31 53 public class Move extends Copy { 54 55 61 public Move() { 62 super(); 63 setOverwrite(true); 64 } 65 66 67 protected void validateAttributes() throws BuildException { 68 if (file != null && file.isDirectory()) { 69 if ((destFile != null && destDir != null) 70 || (destFile == null && destDir == null)) { 71 throw new BuildException("One and only one of tofile and todir " 72 + "must be set."); 73 } 74 destFile = (destFile == null) 75 ? new File (destDir, file.getName()) : destFile; 76 destDir = (destDir == null) 77 ? destFile.getParentFile() : destDir; 78 79 completeDirMap.put(file, destFile); 80 file = null; 81 } else { 82 super.validateAttributes(); 83 } 84 } 85 86 90 94 protected void doFileOperations() { 95 if (completeDirMap.size() > 0) { 97 Enumeration e = completeDirMap.keys(); 98 while (e.hasMoreElements()) { 99 File fromDir = (File ) e.nextElement(); 100 File toDir = (File ) completeDirMap.get(fromDir); 101 boolean renamed = false; 102 try { 103 log("Attempting to rename dir: " + fromDir 104 + " to " + toDir, verbosity); 105 renamed = 106 renameFile(fromDir, toDir, filtering, forceOverwrite); 107 } catch (IOException ioe) { 108 String msg = "Failed to rename dir " + fromDir 109 + " to " + toDir 110 + " due to " + ioe.getMessage(); 111 throw new BuildException(msg, ioe, getLocation()); 112 } 113 if (!renamed) { 114 FileSet fs = new FileSet(); 115 fs.setProject(getProject()); 116 fs.setDir(fromDir); 117 addFileset(fs); 118 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 119 String [] files = ds.getIncludedFiles(); 120 String [] dirs = ds.getIncludedDirectories(); 121 scan(fromDir, toDir, files, dirs); 122 } 123 } 124 } 125 int moveCount = fileCopyMap.size(); 126 if (moveCount > 0) { log("Moving " + moveCount + " file" 128 + ((moveCount == 1) ? "" : "s") 129 + " to " + destDir.getAbsolutePath()); 130 131 Enumeration e = fileCopyMap.keys(); 132 while (e.hasMoreElements()) { 133 String fromFile = (String ) e.nextElement(); 134 135 File f = new File (fromFile); 136 boolean selfMove = false; 137 if (f.exists()) { String [] toFiles = (String []) fileCopyMap.get(fromFile); 139 for (int i = 0; i < toFiles.length; i++) { 140 String toFile = (String ) toFiles[i]; 141 142 if (fromFile.equals(toFile)) { 143 log("Skipping self-move of " + fromFile, verbosity); 144 selfMove = true; 145 146 continue; 149 } 150 File d = new File (toFile); 151 if ((i + 1) == toFiles.length && !selfMove) { 152 moveFile(f, d, filtering, forceOverwrite); 155 } else { 156 copyFile(f, d, filtering, forceOverwrite); 157 } 158 } 159 } 160 } 161 } 162 163 if (includeEmpty) { 164 Enumeration e = dirCopyMap.keys(); 165 int createCount = 0; 166 while (e.hasMoreElements()) { 167 String fromDirName = (String ) e.nextElement(); 168 String [] toDirNames = (String []) dirCopyMap.get(fromDirName); 169 boolean selfMove = false; 170 for (int i = 0; i < toDirNames.length; i++) { 171 172 if (fromDirName.equals(toDirNames[i])) { 173 log("Skipping self-move of " + fromDirName, verbosity); 174 selfMove = true; 175 continue; 176 } 177 178 File d = new File (toDirNames[i]); 179 if (!d.exists()) { 180 if (!d.mkdirs()) { 181 log("Unable to create directory " 182 + d.getAbsolutePath(), Project.MSG_ERR); 183 } else { 184 createCount++; 185 } 186 } 187 } 188 189 File fromDir = new File (fromDirName); 190 if (!selfMove && okToDelete(fromDir)) { 191 deleteDir(fromDir); 192 } 193 194 } 195 196 if (createCount > 0) { 197 log("Moved " + dirCopyMap.size() 198 + " empty director" 199 + (dirCopyMap.size() == 1 ? "y" : "ies") 200 + " to " + createCount 201 + " empty director" 202 + (createCount == 1 ? "y" : "ies") + " under " 203 + destDir.getAbsolutePath()); 204 } 205 } 206 } 207 208 212 private void moveFile(File fromFile, File toFile, 213 boolean filtering, boolean overwrite) { 214 boolean moved = false; 215 try { 216 log("Attempting to rename: " + fromFile 217 + " to " + toFile, verbosity); 218 moved = renameFile(fromFile, toFile, filtering, forceOverwrite); 219 } catch (IOException ioe) { 220 String msg = "Failed to rename " + fromFile 221 + " to " + toFile 222 + " due to " + ioe.getMessage(); 223 throw new BuildException(msg, ioe, getLocation()); 224 } 225 226 if (!moved) { 227 copyFile(fromFile, toFile, filtering, overwrite); 228 if (!fromFile.delete()) { 229 throw new BuildException("Unable to delete " 230 + "file " 231 + fromFile.getAbsolutePath()); 232 } 233 } 234 } 235 236 243 private void copyFile(File fromFile, File toFile, 244 boolean filtering, boolean overwrite) { 245 try { 246 log("Copying " + fromFile + " to " + toFile, 247 verbosity); 248 249 FilterSetCollection executionFilters = 250 new FilterSetCollection(); 251 if (filtering) { 252 executionFilters 253 .addFilterSet(getProject().getGlobalFilterSet()); 254 } 255 for (Enumeration filterEnum = 256 getFilterSets().elements(); 257 filterEnum.hasMoreElements();) { 258 executionFilters 259 .addFilterSet((FilterSet) filterEnum 260 .nextElement()); 261 } 262 263 getFileUtils().copyFile(fromFile, toFile, executionFilters, 264 getFilterChains(), 265 forceOverwrite, 266 getPreserveLastModified(), 267 getEncoding(), 268 getOutputEncoding(), 269 getProject()); 270 271 } catch (IOException ioe) { 272 String msg = "Failed to copy " + fromFile 273 + " to " + toFile 274 + " due to " + ioe.getMessage(); 275 throw new BuildException(msg, ioe, getLocation()); 276 } 277 } 278 279 280 286 protected boolean okToDelete(File d) { 287 String [] list = d.list(); 288 if (list == null) { 289 return false; 290 } 292 for (int i = 0; i < list.length; i++) { 293 String s = list[i]; 294 File f = new File (d, s); 295 if (f.isDirectory()) { 296 if (!okToDelete(f)) { 297 return false; 298 } 299 } else { 300 return false; } 302 } 303 304 return true; 305 } 306 307 311 protected void deleteDir(File d) { 312 deleteDir(d, false); 313 } 314 315 320 protected void deleteDir(File d, boolean deleteFiles) { 321 String [] list = d.list(); 322 if (list == null) { 323 return; 324 } 326 for (int i = 0; i < list.length; i++) { 327 String s = list[i]; 328 File f = new File (d, s); 329 if (f.isDirectory()) { 330 deleteDir(f); 331 } else if (deleteFiles && !(f.delete())) { 332 throw new BuildException("Unable to delete file " 333 + f.getAbsolutePath()); 334 } else { 335 throw new BuildException("UNEXPECTED ERROR - The file " 336 + f.getAbsolutePath() 337 + " should not exist!"); 338 } 339 } 340 log("Deleting directory " + d.getAbsolutePath(), verbosity); 341 if (!d.delete()) { 342 throw new BuildException("Unable to delete directory " 343 + d.getAbsolutePath()); 344 } 345 } 346 347 365 protected boolean renameFile(File sourceFile, File destFile, 366 boolean filtering, boolean overwrite) 367 throws IOException , BuildException { 368 369 boolean renamed = false; 370 if ((getFilterSets().size() + getFilterChains().size() == 0) 371 && !(filtering || destFile.isDirectory())) { 372 File parent = destFile.getParentFile(); 374 if (parent != null && !parent.exists()) { 375 parent.mkdirs(); 376 } 377 if (destFile.isFile() && !destFile.equals(sourceFile) 378 && !destFile.delete()) { 379 throw new BuildException("Unable to remove existing " 380 + "file " + destFile); 381 } 382 renamed = sourceFile.renameTo(destFile); 383 } 384 return renamed; 385 } 386 } 387 | Popular Tags |