ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@locus.apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/util FileNameMapper.java FlatFileNameMapper.java IdentityMapper.java SourceFileScanner.java
Date Wed, 15 Nov 2000 17:45:45 GMT
bodewig     00/11/15 09:45:43

  Modified:    .        bootstrap.bat bootstrap.sh
               src/main/org/apache/tools/ant/taskdefs Copy.java
  Added:       src/main/org/apache/tools/ant/util FileNameMapper.java
                        FlatFileNameMapper.java IdentityMapper.java
                        SourceFileScanner.java
  Log:
  First part of the scanDir refactoring. This is just a starter to show
  what I want to accomplish.
  
  All tasks using a scanDir method to find the source files that are
  newer than there targets should get a similar treatment as <copy>.
  
  The major benefit is not to have the functionality in one place but
  the added flexibility you get, when you allow the user to specify a
  custom FileNameMapper to a task (not there yet).
  
  Nothing (especially not the names) is carved into stone of course.
  
  Revision  Changes    Path
  1.19      +1 -1      jakarta-ant/bootstrap.bat
  
  Index: bootstrap.bat
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/bootstrap.bat,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- bootstrap.bat	2000/10/17 11:09:12	1.18
  +++ bootstrap.bat	2000/11/15 17:45:41	1.19
  @@ -44,7 +44,7 @@
   echo.
   echo ... Compiling Ant Classes
   
  -%JAVAC% -d %CLASSDIR% %TOOLS%\tar\*.java %TOOLS%\ant\*.java %TOOLS%\ant\types\*.java %TOOLS%\ant\taskdefs\*.java
  +%JAVAC% -d %CLASSDIR% %TOOLS%\tar\*.java %TOOLS%\ant\*.java %TOOLS%\ant\types\*.java %TOOLS%\ant\taskdefs\*.java
%TOOLS%\ant\util\*.java
   
   echo.
   echo ... Copying Required Files
  
  
  
  1.25      +1 -0      jakarta-ant/bootstrap.sh
  
  Index: bootstrap.sh
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/bootstrap.sh,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- bootstrap.sh	2000/10/17 11:09:12	1.24
  +++ bootstrap.sh	2000/11/15 17:45:41	1.25
  @@ -51,6 +51,7 @@
   ${JAVAC} -d ${CLASSDIR} ${TOOLS}/tar/*.java
   ${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/types/*.java
   ${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/*.java
  +${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/util/*.java
   ${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/taskdefs/*.java
   
   echo ... Copying Required Files
  
  
  
  1.7       +27 -27    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copy.java
  
  Index: Copy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copy.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Copy.java	2000/11/01 14:58:08	1.6
  +++ Copy.java	2000/11/15 17:45:41	1.7
  @@ -56,6 +56,7 @@
   
   import org.apache.tools.ant.*;
   import org.apache.tools.ant.types.*;
  +import org.apache.tools.ant.util.*;
   
   import java.io.*;
   import java.util.*;
  @@ -244,36 +245,35 @@
        * copied.
        */
       protected void scan(File fromDir, File toDir, String[] files, String[] dirs) {
  -        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());
  -            }
  +        FileNameMapper mapper = null;
  +        if (flatten) {
  +            mapper = new FlatFileNameMapper();
  +        } else {
  +            mapper = new IdentityMapper();
           }
   
  +        buildMap(fromDir, toDir, files, mapper, fileCopyMap);
  +
           if (includeEmpty) {
  -            for (int i = 0; i < dirs.length; i++) {
  -                String dname = dirs[i];
  -                File sd = new File(fromDir, dname);
  -                File dd;
  -                if (flatten) {
  -                    dd = new File(toDir, new File(dname).getName());
  -                } else {
  -                    dd = new File(toDir, dname);
  -                }
  -                if (forceOverwrite || (sd.lastModified() > dd.lastModified())) {
  -                    dirCopyMap.put(sd.getAbsolutePath(), dd.getAbsolutePath());
  -                }
  -            }
  +            buildMap(fromDir, toDir, dirs, mapper, dirCopyMap);
  +        }
  +    }
  +
  +    protected void buildMap(File fromDir, File toDir, String[] names,
  +                            FileNameMapper mapper, Hashtable map) {
  +
  +        String[] toCopy = null;
  +        if (forceOverwrite) {
  +            toCopy = names;
  +        } else {
  +            SourceFileScanner ds = new SourceFileScanner();
  +            toCopy = ds.restrict(names, fromDir, toDir, mapper);
  +        }
  +        
  +        for (int i = 0; i < toCopy.length; i++) {
  +            File src = new File(fromDir, toCopy[i]);
  +            File dest = new File(toDir, mapper.mapFileName(toCopy[i])[0]);
  +            map.put(src.getAbsolutePath(), dest.getAbsolutePath());
           }
       }
   
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/util/FileNameMapper.java
  
  Index: FileNameMapper.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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.util;
  
  /**
   * Interface to be used by SourceFileScanner.
   *
   * <p>Used to find the name of the target file(s) corresponding to a
   * source file.</p>
   *
   * <p>The rule by which the file names are transformed is specified
   * via the setFrom and setTo methods. The exact meaning of these is
   * implementation dependent.</p>
   *
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   */
  public interface FileNameMapper {
  
      /**
       * Sets the from part of the transformation rule.
       */
      public void setFrom(String from);
  
      /**
       * Sets the to part of the transformation rule.
       */
      public void setTo(String to);
  
      /**
       * Returns an array containing the target filename(s) for the
       * given source file.
       *
       * <p>if the given rule doesn't apply to the source file,
       * implementation must return null. SourceFileScanner will then
       * omit the source file in question.</p> 
       *
       * @param sourceFileName the name of the source file relative to
       *                       some given basedirectory.
       */
      public String[] mapFileName(String sourceFileName);
  }
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/util/FlatFileNameMapper.java
  
  Index: FlatFileNameMapper.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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.util;
  
  /**
   * Implementation of FileNameMapper that always returns the source
   * file name without any leading directory information.
   *
   * <p>This is the default FileNameMapper for the copy and move
   * tasks if the flatten attribute has been set.</p>
   *
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   */
  public class FlatFileNameMapper implements FileNameMapper {
  
      /**
       * Ignored.
       */
      public void setFrom(String from) {}
  
      /**
       * Ignored.
       */
      public void setTo(String to) {}
  
      /**
       * Returns an one-element array containing the source file name
       * without any leading directory information.
       */
      public String[] mapFileName(String sourceFileName) {
          return new String[] {new java.io.File(sourceFileName).getName()};
      }
  }
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/util/IdentityMapper.java
  
  Index: IdentityMapper.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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.util;
  
  /**
   * Implementation of FileNameMapper that always returns the source file name.
   *
   * <p>This is the default FileNameMapper for the copy and move
   * tasks.</p>
   *
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   */
  public class IdentityMapper implements FileNameMapper {
  
      /**
       * Ignored.
       */
      public void setFrom(String from) {}
  
      /**
       * Ignored.
       */
      public void setTo(String to) {}
  
      /**
       * Returns an one-element array containing the source file name.
       */
      public String[] mapFileName(String sourceFileName) {
          return new String[] {sourceFileName};
      }
  }
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/util/SourceFileScanner.java
  
  Index: SourceFileScanner.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 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.util;
  
  import java.io.File;
  import java.util.Vector;
  
  /**
   * Utility class that collects the functionality of the various
   * scanDir methods that have been scattered in several tasks before.
   *
   * <p>The only method returns an array of source files. The array is a
   * subset of the files given as a parameter and holds only those that
   * are newer than their corresponding target files.</p>
   *
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   */
  public class SourceFileScanner {
  
      /**
       * Restrict the given set of files to those that are newer than
       * their corresponding target files.
       *
       * @param files   the original set of files
       * @param srcDir  all files are relative to this directory
       * @param destDir target files live here
       * @param mapper  knows how to construct a target file names from
       *                source file names.
       */
      public String[] restrict(String[] files, File srcDir, File destDir,
                               FileNameMapper mapper) {
          Vector v = new Vector();
          for (int i=0; i< files.length; i++) {
  
              String[] targets = mapper.mapFileName(files[i]);
              if (targets == null || targets.length == 0) {
                  continue;
              }
  
              File src = new File(srcDir, files[i]);
              for (int j=0; j<targets.length; j++) {
                  File dest = new File(destDir, targets[j]);
                  if (!dest.exists() ||
                      src.lastModified() > dest.lastModified()) {
  
                      v.addElement(files[i]);
                      break;
                  }
              }
          }
          String[] result = new String[v.size()];
          v.copyInto(result);
          return result;
      }
  
      /**
       * Convinience layer on top of restrict that returns the source
       * files as File objects (containing absolute paths if srcDir is
       * absolute).
       */
      public File[] restrictAsFiles(String[] files, File srcDir, File destDir,
                               FileNameMapper mapper) {
          String[] res = restrict(files, srcDir, destDir, mapper);
          File[] result = new File[res.length];
          for (int i=0; i<res.length; i++) {
              result[i] = new File(srcDir, res[i]);
          }
          return result;
      }
  }
  
  
  

Mime
View raw message