Browse Source

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.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268190 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
1375d72ba3
7 changed files with 412 additions and 28 deletions
  1. +1
    -1
      bootstrap.bat
  2. +1
    -0
      bootstrap.sh
  3. +27
    -27
      src/main/org/apache/tools/ant/taskdefs/Copy.java
  4. +93
    -0
      src/main/org/apache/tools/ant/util/FileNameMapper.java
  5. +85
    -0
      src/main/org/apache/tools/ant/util/FlatFileNameMapper.java
  6. +83
    -0
      src/main/org/apache/tools/ant/util/IdentityMapper.java
  7. +122
    -0
      src/main/org/apache/tools/ant/util/SourceFileScanner.java

+ 1
- 1
bootstrap.bat View File

@@ -44,7 +44,7 @@ mkdir %CLASSDIR%
echo. echo.
echo ... Compiling Ant Classes 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.
echo ... Copying Required Files echo ... Copying Required Files


+ 1
- 0
bootstrap.sh View File

@@ -51,6 +51,7 @@ echo ... Compiling Ant Classes
${JAVAC} -d ${CLASSDIR} ${TOOLS}/tar/*.java ${JAVAC} -d ${CLASSDIR} ${TOOLS}/tar/*.java
${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/types/*.java ${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/types/*.java
${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/*.java ${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/*.java
${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/util/*.java
${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/taskdefs/*.java ${JAVAC} -d ${CLASSDIR} ${TOOLS}/ant/taskdefs/*.java


echo ... Copying Required Files echo ... Copying Required Files


+ 27
- 27
src/main/org/apache/tools/ant/taskdefs/Copy.java View File

@@ -56,6 +56,7 @@ package org.apache.tools.ant.taskdefs;


import org.apache.tools.ant.*; import org.apache.tools.ant.*;
import org.apache.tools.ant.types.*; import org.apache.tools.ant.types.*;
import org.apache.tools.ant.util.*;


import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
@@ -244,36 +245,35 @@ public class Copy extends Task {
* copied. * copied.
*/ */
protected void scan(File fromDir, File toDir, String[] files, String[] dirs) { 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) { 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());
} }
} }




+ 93
- 0
src/main/org/apache/tools/ant/util/FileNameMapper.java View File

@@ -0,0 +1,93 @@
/*
* 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);
}

+ 85
- 0
src/main/org/apache/tools/ant/util/FlatFileNameMapper.java View File

@@ -0,0 +1,85 @@
/*
* 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()};
}
}

+ 83
- 0
src/main/org/apache/tools/ant/util/IdentityMapper.java View File

@@ -0,0 +1,83 @@
/*
* 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};
}
}

+ 122
- 0
src/main/org/apache/tools/ant/util/SourceFileScanner.java View File

@@ -0,0 +1,122 @@
/*
* 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;
}
}

Loading…
Cancel
Save