ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gle...@locus.apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs Copy.java Move.java Copydir.java Copyfile.java defaults.properties Delete.java Deltree.java Rename.java
Date Wed, 04 Oct 2000 04:34:03 GMT
glennm      00/10/03 21:34:02

  Modified:    src/main/org/apache/tools/ant/taskdefs Copydir.java
                        Copyfile.java defaults.properties Delete.java
                        Deltree.java Rename.java
  Added:       src/main/org/apache/tools/ant/taskdefs Copy.java Move.java
  Log:
  Consolidation of Copyfile, Copydir, Delete, Deltree, and Rename into
  Copy, Move, and Delete tasks.
  
  Revision  Changes    Path
  1.15      +2 -0      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copydir.java
  
  Index: Copydir.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copydir.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Copydir.java	2000/09/21 07:42:48	1.14
  +++ Copydir.java	2000/10/04 04:33:57	1.15
  @@ -95,6 +95,8 @@
       }
   
       public void execute() throws BuildException {
  +        log("DEPRECATED - The copydir task is deprecated.  Use copy instead.");
  +
           if (srcDir == null) {
               throw new BuildException("src attribute must be set!", 
                                        location);
  
  
  
  1.7       +2 -0      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copyfile.java
  
  Index: Copyfile.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copyfile.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Copyfile.java	2000/09/18 07:54:59	1.6
  +++ Copyfile.java	2000/10/04 04:33:58	1.7
  @@ -89,6 +89,8 @@
       }
   
       public void execute() throws BuildException {
  +        log("DEPRECATED - The copyfile task is deprecated.  Use copy instead.");
  +
           if (srcFile == null) {
               throw new BuildException("The src attribute must be present.", location);
           }
  
  
  
  1.43      +2 -0      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties
  
  Index: defaults.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties,v
  retrieving revision 1.42
  retrieving revision 1.43
  diff -u -r1.42 -r1.43
  --- defaults.properties	2000/09/21 09:00:34	1.42
  +++ defaults.properties	2000/10/04 04:33:58	1.43
  @@ -4,6 +4,8 @@
   chmod=org.apache.tools.ant.taskdefs.Chmod
   deltree=org.apache.tools.ant.taskdefs.Deltree
   delete=org.apache.tools.ant.taskdefs.Delete
  +copy=org.apache.tools.ant.taskdefs.Copy
  +move=org.apache.tools.ant.taskdefs.Move
   jar=org.apache.tools.ant.taskdefs.Jar
   copydir=org.apache.tools.ant.taskdefs.Copydir
   copyfile=org.apache.tools.ant.taskdefs.Copyfile
  
  
  
  1.8       +182 -51   jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Delete.java
  
  Index: Delete.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Delete.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Delete.java	2000/09/21 07:42:48	1.7
  +++ Delete.java	2000/10/04 04:33:58	1.8
  @@ -54,26 +54,39 @@
   package org.apache.tools.ant.taskdefs;
   
   import org.apache.tools.ant.*;
  +import org.apache.tools.ant.types.*;
   import java.io.*;
  +import java.util.*;
   
   /**
  - * Deletes a single file or a set of files defined by a pattern.
  + * Deletes a file or directory, or set of files defined by a fileset.
  + * The original delete task would delete a file, or a set of files 
  + * using the include/exclude syntax.  The deltree task would delete a 
  + * directory tree.  This task combines the functionality of these two
  + * originally distinct tasks.
  + * <p>Currently Delete extends MatchingTask.  This is intend <i>only</i>
  + * to provide backwards compatibility for a release.  The future position
  + * is to use nested filesets exclusively.</p>
    * 
    * @author stefano@apache.org
    * @author Tom Dimock <a href="mailto:tad1@cornell.edu">tad1@cornell.edu</a>
  + * @author Glenn McAllister <a href="mailto:glennm@ca.ibm.com">glennm@ca.ibm.com</a>
    */
   public class Delete extends MatchingTask {
  -    private File delDir = null;
  +    protected File file = null;
  +    protected File dir = null;
  +    protected Vector filesets = new Vector();
  +    protected boolean usedMatchingTask = false;
  +
       private int verbosity = Project.MSG_VERBOSE;
  -    private File f = null;
   
       /**
        * Set the name of a single file to be removed.
        * 
        * @param file the file to be deleted
        */
  -    public void setFile(String file) {
  -        f = project.resolveFile(file);
  +    public void setFile(File file) {
  +        this.file = file;
       } 
   
       /**
  @@ -81,8 +94,8 @@
        * 
        * @param dir the directory path.
        */
  -    public void setDir(String dir) {
  -        delDir = project.resolveFile(dir);
  +    public void setDir(File dir) {
  +        this.dir = dir;
       } 
   
       /**
  @@ -90,8 +103,8 @@
        * 
        * @param verbose "true" or "on"
        */
  -    public void setVerbose(String verbose) {
  -        if ("true".equalsIgnoreCase(verbose.trim()) || "on".equalsIgnoreCase(verbose.trim()))
{
  +    public void setVerbose(boolean verbose) {
  +        if (verbose) {
               this.verbosity = Project.MSG_INFO;
           } else {
               this.verbosity = Project.MSG_VERBOSE;
  @@ -99,60 +112,178 @@
       } 
   
       /**
  -     * Make it so.  Delete the file(s).
  -     * 
  -     * @throws BuildException
  +     * Adds a set of files (nested fileset attribute).
        */
  -    public void execute() throws BuildException {
  -        if (f == null && delDir == null) {
  -            throw new BuildException("<file> or <dir> attribute must be set!");
  -        } 
  -
  -        // old <delete> functionality must still work
  -        if (f != null) {
  -            if (f.exists()) {
  -                if (f.isDirectory()) {
  -                    log("Directory: " + f.getAbsolutePath() + " cannot be removed with
delete.  Use Deltree instead.");
  -                } else {
  -                    log("Deleting: " + f.getAbsolutePath());
  +    public void addFileset(FileSet set) {
  +        filesets.addElement(set);
  +    }
  + 
  +    /**
  +     * add a name entry on the include list
  +     */
  +    public PatternSet.NameEntry createInclude() {
  +        usedMatchingTask = true;
  +        return super.createInclude();
  +    }
  +    
  +    /**
  +     * add a name entry on the exclude list
  +     */
  +    public PatternSet.NameEntry createExclude() {
  +        usedMatchingTask = true;
  +        return super.createExclude();
  +    }
   
  -                    if (!f.delete()) {
  -                        throw new BuildException("Unable to delete file " + f.getAbsolutePath());
  -                    } 
  -                } 
  -            } 
  -        } 
  +    /**
  +     * add a set of patterns
  +     */
  +    public PatternSet createPatternSet() {
  +        usedMatchingTask = true;
  +        return super.createPatternSet();
  +    }
   
  -        // now we'll do the fancy pattern-driven deletes
  -        if (delDir == null) {
  -            return;
  -        } 
  +    /**
  +     * Sets the set of include patterns. Patterns may be separated by a comma
  +     * or a space.
  +     *
  +     * @param includes the string containing the include patterns
  +     */
  +    public void setIncludes(String includes) {
  +        usedMatchingTask = true;
  +        super.setIncludes(includes);
  +    }
   
  -        if (!delDir.exists()) {
  -            throw new BuildException("dir does not exist!");
  -        } 
  +    /**
  +     * Sets the set of exclude patterns. Patterns may be separated by a comma
  +     * or a space.
  +     *
  +     * @param excludes the string containing the exclude patterns
  +     */
  +    public void setExcludes(String excludes) {
  +        usedMatchingTask = true;
  +        super.setExcludes(excludes);
  +    }
   
  -        DirectoryScanner ds = super.getDirectoryScanner(delDir);
  -        String[] files = ds.getIncludedFiles();
  +    /**
  +     * Sets whether default exclusions should be used or not.
  +     *
  +     * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions 
  +     *                           should be used, "false"|"off"|"no" when they
  +     *                           shouldn't be used.
  +     */
  +    public void setDefaultexcludes(boolean useDefaultExcludes) {
  +        usedMatchingTask = true;
  +        super.setDefaultexcludes(useDefaultExcludes);
  +    }
   
  -        if (files.length > 0) {
  -            log("Deleting " + files.length + " file"
  -                + (files.length == 1 ? "" : "s")
  -                + " from " + delDir.getAbsolutePath());
  +    /**
  +     * Sets the name of the file containing the includes patterns.
  +     *
  +     * @param includesfile A string containing the filename to fetch
  +     * the include patterns from.  
  +     */
  +    public void setIncludesfile(File includesfile) {
  +        usedMatchingTask = true;
  +        super.setIncludesfile(includesfile);
  +    }
   
  -            for (int i = 0; i < files.length; i++) {
  -                File f = new File(delDir, files[i]);
  +    /**
  +     * Sets the name of the file containing the includes patterns.
  +     *
  +     * @param excludesfile A string containing the filename to fetch
  +     * the include patterns from.  
  +     */
  +    public void setExcludesfile(File excludesfile) {
  +        usedMatchingTask = true;
  +        super.setExcludesfile(excludesfile);
  +    }
   
  -                if (f.exists()) {
  -                    log("Deleting: " + f.getAbsolutePath(), verbosity);
  +    /**
  +     * Delete the file(s).
  +     */
  +    public void execute() throws BuildException {
  +        if (usedMatchingTask) {
  +            log("DEPRECATED - Use of the implicit FileSet is deprecated.  Use a nested
fileset element instead.");
  +        }
  +
  +        if (file == null && dir == null && filesets.size() == 0) {
  +            throw new BuildException("At least one of the file or dir attributes, or a
fileset element, must be set.");
  +        } 
   
  -                    if (!f.delete()) {
  -                        throw new BuildException("Unable to delete " + f.getAbsolutePath());
  +        // delete the single file
  +        if (file != null) {
  +            if (file.exists()) {
  +                if (file.isDirectory()) {
  +                    log("Directory " + file.getAbsolutePath() + " cannot be removed using
the file attribute.  Use dir instead.");
  +                } else {
  +                    log("Deleting: " + file.getAbsolutePath());
  +  
  +                    if (!file.delete()) {
  +                        throw new BuildException("Unable to delete file " + file.getAbsolutePath());
                       } 
                   } 
  -            } 
  -        } 
  +            } else {
  +                log("Could not find file " + file.getAbsolutePath() + " to delete.");
  +            }
  +        }
  +
  +        // delete the directory
  +        if (dir != null && !usedMatchingTask) {
  +            log("Deleting directory " + dir.getAbsolutePath());
  +            removeDir(dir);
  +        }
  +
  +        // delete the files in the filesets
  +        for (int i=0; i<filesets.size(); i++) {
  +            FileSet fs = (FileSet) filesets.elementAt(i);
  +            DirectoryScanner ds = fs.getDirectoryScanner(project);
  +            String[] files = ds.getIncludedFiles();
  +            removeFiles(fs.getDir(project), files);
  +        }
  +
  +        // delete the files from the default fileset
  +        if (usedMatchingTask && dir != null) {
  +            DirectoryScanner ds = super.getDirectoryScanner(dir);
  +            String [] files = ds.getIncludedFiles();
  +            removeFiles(dir, files);
  +        }
       } 
   
  +//************************************************************************
  +//  protected and private methods
  +//************************************************************************
  +
  +    protected void removeDir(File d) {
  +        String[] list = d.list();
  +        for (int i = 0; i < list.length; i++) {
  +            String s = list[i];
  +            File f = new File(d, s);
  +            if (f.isDirectory()) {
  +                removeDir(f);
  +            } else {
  +                log("Deleting " + f.getAbsolutePath(), verbosity);
  +                if (!f.delete()) {
  +                    throw new BuildException("Unable to delete file " + f.getAbsolutePath());
  +                }
  +            }
  +        }
  +        log("Deleting directory " + d.getAbsolutePath(), verbosity);
  +        if (!d.delete()) {
  +            throw new BuildException("Unable to delete directory " + dir.getAbsolutePath());
  +        }
  +    }
  +
  +    protected void removeFiles(File d, String[] files) {
  +        if (files.length > 0) {
  +            log("Deleting " + files.length + " files from " + d.getAbsolutePath());
  +            for (int j=0; j<files.length; j++) {
  +                File f = new File(d, files[j]);
  +                log("Deleting " + f.getAbsolutePath(), verbosity);
  +                if (!f.delete()) {
  +                    throw new BuildException("Unable to delete file " + f.getAbsolutePath());
  +                }
  +            }
  +        }
  +    }
   }
   
  
  
  
  1.7       +2 -0      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Deltree.java
  
  Index: Deltree.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Deltree.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Deltree.java	2000/09/18 07:55:00	1.6
  +++ Deltree.java	2000/10/04 04:33:58	1.7
  @@ -72,6 +72,8 @@
       }
       
       public void execute() throws BuildException {
  +        log("DEPRECATED - The deltree task is deprecated.  Use delete instead.");
  +
           if (dir == null) {
               throw new BuildException("dir attribute must be set!", location);
           } 
  
  
  
  1.3       +2 -0      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Rename.java
  
  Index: Rename.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Rename.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Rename.java	2000/09/18 07:55:01	1.2
  +++ Rename.java	2000/10/04 04:33:59	1.3
  @@ -102,6 +102,8 @@
        * thrown, if the rename operation fails.
        */
       public void execute() throws BuildException {
  +        log("DEPRECATED - The rename task is deprecated.  Use move instead.");
  +
           if (dest == null) {
               throw new BuildException("dest attribute is required", location);
           }
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copy.java
  
  Index: Copy.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.tools.ant.taskdefs;
  
  import org.apache.tools.ant.*;
  import org.apache.tools.ant.types.*;
  
  import java.io.*;
  import java.util.*;
  
  /**
   * A consolidated copy task.  Copies a file or directory to a new file 
   * or directory.  Files are only copied if the source file is newer
   * than the destination file, or when the destination file does not 
   * exist.  It is possible to explicitly overwrite existing files.</p>
   *
   * <p>This implementation is based on Arnout Kuiper's initial design
   * document, the following mailing list discussions, and the 
   * copyfile/copydir tasks.</p>
   *
   * @author Glenn McAllister <a href="mailto:glennm@ca.ibm.com">glennm@ca.ibm.com</a>
   */
  public class Copy extends Task {
      protected File file = null;     // the source file 
      protected File dir = null;      // the source directory
      protected File destFile = null; // the destination file 
      protected File destDir = null;  // the destination directory
      protected Vector filesets = new Vector();
  
      protected boolean filtering = false;
      protected boolean forceOverwrite = false;
      protected boolean flatten = false;
      protected int verbosity = Project.MSG_VERBOSE;
  
      protected Hashtable fileCopyMap = new Hashtable();
  
      /**
       * Sets a single source file to copy.
       */
      public void setFile(File file) {
          this.file = file;
      }
  
      /**
       * Sets a directory to copy.
       */
      public void setDir(File dir) {
          this.dir = dir;
      }
  
      /**
       * Sets the destination file.
       */
      public void setTofile(File destFile) {
          this.destFile = destFile;
      }
  
      /**
       * Sets the destination directory.
       */
      public void setTodir(File destDir) {
          this.destDir = destDir;
      }
  
      /**
       * Sets filtering.
       */
      public void setFiltering(boolean filtering) {
          this.filtering = filtering;
      }
  
      /**
       * Overwrite any existing destination file(s).
       */
      public void setOverwrite(boolean overwrite) {
          this.forceOverwrite = overwrite;
      }
  
      /**
       * When copying directory trees, the files can be "flattened"
       * into a single directory.  If there are multiple files with
       * the same name in the source directory tree, only the first
       * file will be copied into the "flattened" directory, unless
       * the forceoverwrite attribute is true.
       */
      public void setFlatten(boolean flatten) {
          this.flatten = flatten;
      }
  
      /**
       * Used to force listing of all names of copied files.
       */
      public void setVerbose(boolean verbose) {
          if (verbose) {
              this.verbosity = Project.MSG_INFO;
          } else {
              this.verbosity = Project.MSG_VERBOSE;
          } 
      } 
  
      /**
       * Adds a set of files (nested fileset attribute).
       */
      public void addFileset(FileSet set) {
          filesets.addElement(set);
      }
  
      /**
       * Performs the copy operation.
       */
      public void execute() throws BuildException {
          // make sure we don't have an illegal set of options
          validateAttributes();   
  
          // deal with the single file
          if (file != null) {
              if (destFile == null) {
                  destFile = new File(destDir, file.getName());
              }
  
              if (forceOverwrite || 
                  (file.lastModified() > destFile.lastModified())) {
                  fileCopyMap.put(file.getAbsolutePath(), destFile.getAbsolutePath());
              }
          }
  
          // deal with the directory
          if (dir != null) {
              DirectoryScanner ds = new DirectoryScanner();
              ds.setBasedir(dir);
              ds.scan();          // include EVERYTHING
  
              String[] srcFiles = ds.getIncludedFiles();
              scan(dir, destDir, srcFiles);                   // add to fileCopyMap
          }
  
          // deal with the filesets
          for (int i=0; i<filesets.size(); i++) {
              FileSet fs = (FileSet) filesets.elementAt(i);
              DirectoryScanner ds = fs.getDirectoryScanner(project);
  
              String[] srcFiles = ds.getIncludedFiles();
              scan(fs.getDir(project), destDir, srcFiles);   // add to fileCopyMap
          }
  
          // do all the copy operations now...
          doFileOperations();
      }
  
  //************************************************************************
  //  protected and private methods
  //************************************************************************
  
      /**
       * Ensure we have a consistent and legal set of attributes, and set
       * any internal flags necessary based on different combinations 
       * of attributes.
       */
      protected void validateAttributes() throws BuildException {
          if (file == null && dir == null && filesets.size() == 0) {
              throw new BuildException("Specify at least one source - a file, a dir, or a
fileset.");
          }
  
          if (destFile != null && destDir != null) {
              throw new BuildException("Only one of destfile and destdir may be set.");
          }
  
          if (destFile == null && destDir == null) {
              throw new BuildException("One of destfile or destdir must be set.");
          }
  
          if (dir != null && destFile != null) {
              throw new BuildException("Cannot copy a directory into a file.");
          }
  
          if (destFile != null && filesets.size() > 0) {
              throw new BuildException("Cannot concatenate multple files into a single file.");
          }
  
          if (destFile != null) {
              destDir = new File(destFile.getParent());   // be 1.1 friendly
          }
  
      }
  
      /**
       * Compares source files to destination files to see if they should be
       * copied.
       */
      protected void scan(File fromDir, File toDir, String[] files) {
          for (int i = 0; i < files.length; i++) {
              String filename = files[i];
              File src = new File(fromDir, filename);
              File dest;
              if (flatten) {
                  dest = new File(toDir, new File(filename).getName());
              } else {
                  dest = new File(toDir, filename);
              }
              if (forceOverwrite ||
                  (src.lastModified() > dest.lastModified())) {
                  fileCopyMap.put(src.getAbsolutePath(),
                                   dest.getAbsolutePath());
              }
          }
      }
  
      protected void doFileOperations() {
          if (fileCopyMap.size() > 0) {
              log("Copying " + fileCopyMap.size() + " files to " + 
                  destDir.getAbsolutePath() );
  
              Enumeration e = fileCopyMap.keys();
              while (e.hasMoreElements()) {
                  String fromFile = (String) e.nextElement();
                  String toFile = (String) fileCopyMap.get(fromFile);
  
                  try {
                      log("Copying " + fromFile + " to " + toFile, verbosity);
                      project.copyFile(fromFile, 
                                       toFile, 
                                       filtering, 
                                       forceOverwrite);
                  } catch (IOException ioe) {
                      String msg = "Failed to copy " + fromFile + " to " + toFile
                          + " due to " + ioe.getMessage();
                      throw new BuildException(msg, ioe, location);
                  }
              }
          }
      }
  
  }
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Move.java
  
  Index: Move.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.tools.ant.taskdefs;
  
  import org.apache.tools.ant.*;
  import org.apache.tools.ant.types.*;
  
  import java.io.*;
  import java.util.*;
  
  /**
   * Moves a file or directory to a new file or directory.  By default,
   * the destination is overwriten when existing.  When overwrite is
   * turned off, then files are only moved if the source file is 
   * newer than the destination file, or when the destination file does
   * not exist.</p>
   *
   * <p>Source files and directories are only deleted when the file or 
   * directory has been copied to the destination successfully.  Filtering
   * also works.</p>
   *
   * <p>This implementation is based on Arnout Kuiper's initial design
   * document, the following mailing list discussions, and the 
   * copyfile/copydir tasks.</p>
   *
   * @author Glenn McAllister <a href="mailto:glennm@ca.ibm.com">glennm@ca.ibm.com</a>
   */
  public class Move extends Copy {
  
      public Move() {
          super();
          forceOverwrite = true;
      }
  
      public void execute() throws BuildException {
          super.execute();
  
          // take care of the source directory
          if (dir != null && dir.exists()) {
              deleteDir(dir);
          }
      }
  
  //************************************************************************
  //  protected and private methods
  //************************************************************************
  
      protected void doFileOperations() {
          if (fileCopyMap.size() > 0) {
              log("Moving " + fileCopyMap.size() + " files to " + 
                  destDir.getAbsolutePath() );
  
              Enumeration e = fileCopyMap.keys();
              while (e.hasMoreElements()) {
                  String fromFile = (String) e.nextElement();
                  String toFile = (String) fileCopyMap.get(fromFile);
  
                  try {
                      log("Moving " + fromFile + " to " + toFile, verbosity);
                      project.copyFile(fromFile, 
                                       toFile, 
                                       filtering, 
                                       forceOverwrite);
                      File f = new File(fromFile);
                      if (!f.delete()) {
                          throw new BuildException("Unable to delete file " + f.getAbsolutePath());
                      }
                  } catch (IOException ioe) {
                      String msg = "Failed to copy " + fromFile + " to " + toFile
                          + " due to " + ioe.getMessage();
                      throw new BuildException(msg, ioe, location);
                  }
              }
          }
      }
  
      protected void deleteDir(File d) {
      	String[] list = d.list();
          for (int i = 0; i < list.length; i++) {
              String s = list[i];
              File f = new File(d, s);
              if (f.isDirectory()) {
                  deleteDir(f);
              } else {
                  throw new BuildException("UNEXPECTED ERROR - The file " + f.getAbsolutePath()
+ " should not exist!");
              }
          }
          log("Deleting directory " + d.getAbsolutePath(), verbosity);
          if (!d.delete()) {
  	       throw new BuildException("Unable to delete directory " + dir.getAbsolutePath());
  	   }
      }
  
  }
  
  
  

Mime
View raw message