From 1375d72ba33469d368c812a889ab11e9ffe6200e Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 15 Nov 2000 17:45:41 +0000 Subject: [PATCH] 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 . 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 --- bootstrap.bat | 2 +- bootstrap.sh | 1 + .../org/apache/tools/ant/taskdefs/Copy.java | 54 ++++---- .../apache/tools/ant/util/FileNameMapper.java | 93 +++++++++++++ .../tools/ant/util/FlatFileNameMapper.java | 85 ++++++++++++ .../apache/tools/ant/util/IdentityMapper.java | 83 ++++++++++++ .../tools/ant/util/SourceFileScanner.java | 122 ++++++++++++++++++ 7 files changed, 412 insertions(+), 28 deletions(-) create mode 100644 src/main/org/apache/tools/ant/util/FileNameMapper.java create mode 100644 src/main/org/apache/tools/ant/util/FlatFileNameMapper.java create mode 100644 src/main/org/apache/tools/ant/util/IdentityMapper.java create mode 100644 src/main/org/apache/tools/ant/util/SourceFileScanner.java diff --git a/bootstrap.bat b/bootstrap.bat index 31e29af5b..29bfc0e6f 100755 --- a/bootstrap.bat +++ b/bootstrap.bat @@ -44,7 +44,7 @@ mkdir %CLASSDIR% 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 diff --git a/bootstrap.sh b/bootstrap.sh index 3c5a8c7e0..e45276e75 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -51,6 +51,7 @@ echo ... Compiling Ant Classes ${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 diff --git a/src/main/org/apache/tools/ant/taskdefs/Copy.java b/src/main/org/apache/tools/ant/taskdefs/Copy.java index 583843780..ed027a6d4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Copy.java +++ b/src/main/org/apache/tools/ant/taskdefs/Copy.java @@ -56,6 +56,7 @@ package org.apache.tools.ant.taskdefs; 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 @@ public class Copy extends Task { * 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()); } } diff --git a/src/main/org/apache/tools/ant/util/FileNameMapper.java b/src/main/org/apache/tools/ant/util/FileNameMapper.java new file mode 100644 index 000000000..a8875c74a --- /dev/null +++ b/src/main/org/apache/tools/ant/util/FileNameMapper.java @@ -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 + * . + */ + +package org.apache.tools.ant.util; + +/** + * Interface to be used by SourceFileScanner. + * + *

Used to find the name of the target file(s) corresponding to a + * source file.

+ * + *

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.

+ * + * @author Stefan Bodewig + */ +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. + * + *

if the given rule doesn't apply to the source file, + * implementation must return null. SourceFileScanner will then + * omit the source file in question.

+ * + * @param sourceFileName the name of the source file relative to + * some given basedirectory. + */ + public String[] mapFileName(String sourceFileName); +} diff --git a/src/main/org/apache/tools/ant/util/FlatFileNameMapper.java b/src/main/org/apache/tools/ant/util/FlatFileNameMapper.java new file mode 100644 index 000000000..627ba668b --- /dev/null +++ b/src/main/org/apache/tools/ant/util/FlatFileNameMapper.java @@ -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 + * . + */ + +package org.apache.tools.ant.util; + +/** + * Implementation of FileNameMapper that always returns the source + * file name without any leading directory information. + * + *

This is the default FileNameMapper for the copy and move + * tasks if the flatten attribute has been set.

+ * + * @author Stefan Bodewig + */ +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()}; + } +} diff --git a/src/main/org/apache/tools/ant/util/IdentityMapper.java b/src/main/org/apache/tools/ant/util/IdentityMapper.java new file mode 100644 index 000000000..38789051b --- /dev/null +++ b/src/main/org/apache/tools/ant/util/IdentityMapper.java @@ -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 + * . + */ + +package org.apache.tools.ant.util; + +/** + * Implementation of FileNameMapper that always returns the source file name. + * + *

This is the default FileNameMapper for the copy and move + * tasks.

+ * + * @author Stefan Bodewig + */ +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}; + } +} diff --git a/src/main/org/apache/tools/ant/util/SourceFileScanner.java b/src/main/org/apache/tools/ant/util/SourceFileScanner.java new file mode 100644 index 000000000..7e8a4cae7 --- /dev/null +++ b/src/main/org/apache/tools/ant/util/SourceFileScanner.java @@ -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 + * . + */ + +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. + * + *

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.

+ * + * @author Stefan Bodewig + */ +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 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