Browse Source

Move FileUtils into util package.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269478 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
21426bf475
5 changed files with 69 additions and 28 deletions
  1. +13
    -9
      src/main/org/apache/tools/ant/Project.java
  2. +8
    -1
      src/main/org/apache/tools/ant/taskdefs/Copy.java
  3. +2
    -2
      src/main/org/apache/tools/ant/taskdefs/Move.java
  4. +7
    -2
      src/main/org/apache/tools/ant/taskdefs/Touch.java
  5. +39
    -14
      src/main/org/apache/tools/ant/util/FileUtils.java

+ 13
- 9
src/main/org/apache/tools/ant/Project.java View File

@@ -59,6 +59,7 @@ import java.util.*;
import java.text.*; import java.text.*;


import org.apache.tools.ant.types.FilterSet; import org.apache.tools.ant.types.FilterSet;
import org.apache.tools.ant.util.FileUtils;


/** /**
* Central representation of an Ant project. This class defines a * Central representation of an Ant project. This class defines a
@@ -140,7 +141,10 @@ public class Project {
} }
} }


private FileUtils fileUtils;

public Project() { public Project() {
fileUtils = FileUtils.getFileUtils();
} }


/** /**
@@ -690,7 +694,7 @@ public class Project {
* @deprecated * @deprecated
*/ */
public void copyFile(String sourceFile, String destFile) throws IOException { public void copyFile(String sourceFile, String destFile) throws IOException {
FileUtils.copyFile(sourceFile, destFile);
fileUtils.copyFile(sourceFile, destFile);
} }


/** /**
@@ -703,7 +707,7 @@ public class Project {
*/ */
public void copyFile(String sourceFile, String destFile, boolean filtering) public void copyFile(String sourceFile, String destFile, boolean filtering)
throws IOException { throws IOException {
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null);
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null);
} }


/** /**
@@ -717,7 +721,7 @@ public class Project {
*/ */
public void copyFile(String sourceFile, String destFile, boolean filtering, public void copyFile(String sourceFile, String destFile, boolean filtering,
boolean overwrite) throws IOException { boolean overwrite) throws IOException {
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, overwrite);
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, overwrite);
} }


/** /**
@@ -734,7 +738,7 @@ public class Project {
public void copyFile(String sourceFile, String destFile, boolean filtering, public void copyFile(String sourceFile, String destFile, boolean filtering,
boolean overwrite, boolean preserveLastModified) boolean overwrite, boolean preserveLastModified)
throws IOException { throws IOException {
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null,
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null,
overwrite, preserveLastModified); overwrite, preserveLastModified);
} }


@@ -747,7 +751,7 @@ public class Project {
* @deprecated * @deprecated
*/ */
public void copyFile(File sourceFile, File destFile) throws IOException { public void copyFile(File sourceFile, File destFile) throws IOException {
FileUtils.copyFile(sourceFile, destFile);
fileUtils.copyFile(sourceFile, destFile);
} }


/** /**
@@ -760,7 +764,7 @@ public class Project {
*/ */
public void copyFile(File sourceFile, File destFile, boolean filtering) public void copyFile(File sourceFile, File destFile, boolean filtering)
throws IOException { throws IOException {
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null);
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null);
} }


/** /**
@@ -774,7 +778,7 @@ public class Project {
*/ */
public void copyFile(File sourceFile, File destFile, boolean filtering, public void copyFile(File sourceFile, File destFile, boolean filtering,
boolean overwrite) throws IOException { boolean overwrite) throws IOException {
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, overwrite);
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, overwrite);
} }


/** /**
@@ -791,7 +795,7 @@ public class Project {
public void copyFile(File sourceFile, File destFile, boolean filtering, public void copyFile(File sourceFile, File destFile, boolean filtering,
boolean overwrite, boolean preserveLastModified) boolean overwrite, boolean preserveLastModified)
throws IOException { throws IOException {
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null,
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null,
overwrite, preserveLastModified); overwrite, preserveLastModified);
} }


@@ -806,7 +810,7 @@ public class Project {
+ " in JDK 1.1", Project.MSG_WARN); + " in JDK 1.1", Project.MSG_WARN);
return; return;
} }
FileUtils.setFileLastModified(file, time);
fileUtils.setFileLastModified(file, time);
log("Setting modification time for " + file, MSG_VERBOSE); log("Setting modification time for " + file, MSG_VERBOSE);
} }




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

@@ -93,7 +93,14 @@ public class Copy extends Task {


protected Mapper mapperElement = null; protected Mapper mapperElement = null;
private Vector filterSets = new Vector(); private Vector filterSets = new Vector();
private FileUtils fileUtils;
public Copy() {
fileUtils = FileUtils.getFileUtils();
}

protected FileUtils getFileUtils() {return fileUtils;}

/** /**
* Sets a single source file to copy. * Sets a single source file to copy.
*/ */
@@ -365,7 +372,7 @@ public class Copy extends Task {
for (Enumeration filterEnum = filterSets.elements(); filterEnum.hasMoreElements();) { for (Enumeration filterEnum = filterSets.elements(); filterEnum.hasMoreElements();) {
executionFilterSet.addFilterSet((FilterSet)filterEnum.nextElement()); executionFilterSet.addFilterSet((FilterSet)filterEnum.nextElement());
} }
FileUtils.copyFile(fromFile, toFile, executionFilterSet,
fileUtils.copyFile(fromFile, toFile, executionFilterSet,
forceOverwrite, preserveLastModified); forceOverwrite, preserveLastModified);
} catch (IOException ioe) { } catch (IOException ioe) {
String msg = "Failed to copy " + fromFile + " to " + toFile String msg = "Failed to copy " + fromFile + " to " + toFile


+ 2
- 2
src/main/org/apache/tools/ant/taskdefs/Move.java View File

@@ -108,8 +108,8 @@ public class Move extends Copy {
for (Enumeration filterEnum = getFilterSets().elements(); filterEnum.hasMoreElements();) { for (Enumeration filterEnum = getFilterSets().elements(); filterEnum.hasMoreElements();) {
executionFilterSet.addFilterSet((FilterSet)filterEnum.nextElement()); executionFilterSet.addFilterSet((FilterSet)filterEnum.nextElement());
} }
FileUtils.copyFile(fromFile, toFile, executionFilterSet,
forceOverwrite);
getFileUtils().copyFile(fromFile, toFile, executionFilterSet,
forceOverwrite);


File f = new File(fromFile); File f = new File(fromFile);
if (!f.delete()) { if (!f.delete()) {


+ 7
- 2
src/main/org/apache/tools/ant/taskdefs/Touch.java View File

@@ -89,6 +89,11 @@ public class Touch extends Task {
private long millis = -1; private long millis = -1;
private String dateTime; private String dateTime;
private Vector filesets = new Vector(); private Vector filesets = new Vector();
private FileUtils fileUtils;

public Touch() {
fileUtils = FileUtils.getFileUtils();
}


/** /**
* Sets a single source file to touch. If the file does not exist * Sets a single source file to touch. If the file does not exist
@@ -204,9 +209,9 @@ public class Touch extends Task {
} }


if (millis < 0) { if (millis < 0) {
project.setFileLastModified(file, System.currentTimeMillis());
fileUtils.setFileLastModified(file, System.currentTimeMillis());
} else { } else {
project.setFileLastModified(file, millis);
fileUtils.setFileLastModified(file, millis);
} }
} }




src/main/org/apache/tools/ant/FileUtils.java → src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -52,11 +52,13 @@
* <http://www.apache.org/>. * <http://www.apache.org/>.
*/ */


package org.apache.tools.ant;
package org.apache.tools.ant.util;


import java.io.*; import java.io.*;
import java.util.*;
import java.lang.reflect.Method;


import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FilterSet; import org.apache.tools.ant.types.FilterSet;


/** /**
@@ -76,13 +78,25 @@ public class FileUtils {
private static Object lockReflection = new Object(); private static Object lockReflection = new Object();
private static java.lang.reflect.Method setLastModified = null; private static java.lang.reflect.Method setLastModified = null;


/**
* Factory method.
*/
public static FileUtils getFileUtils() {
return new FileUtils();
}

/**
* Empty constructor.
*/
protected FileUtils() {}

/** /**
* Convienence method to copy a file from a source to a destination. * Convienence method to copy a file from a source to a destination.
* No filtering is performed. * No filtering is performed.
* *
* @throws IOException * @throws IOException
*/ */
public static void copyFile(String sourceFile, String destFile) throws IOException {
public void copyFile(String sourceFile, String destFile) throws IOException {
copyFile(new File(sourceFile), new File(destFile), null, false, false); copyFile(new File(sourceFile), new File(destFile), null, false, false);
} }


@@ -92,7 +106,7 @@ public class FileUtils {
* *
* @throws IOException * @throws IOException
*/ */
public static void copyFile(String sourceFile, String destFile, FilterSet filterSet)
public void copyFile(String sourceFile, String destFile, FilterSet filterSet)
throws IOException throws IOException
{ {
copyFile(new File(sourceFile), new File(destFile), filterSet, false, false); copyFile(new File(sourceFile), new File(destFile), filterSet, false, false);
@@ -105,7 +119,7 @@ public class FileUtils {
* *
* @throws IOException * @throws IOException
*/ */
public static void copyFile(String sourceFile, String destFile, FilterSet filterSet,
public void copyFile(String sourceFile, String destFile, FilterSet filterSet,
boolean overwrite) throws IOException { boolean overwrite) throws IOException {
copyFile(new File(sourceFile), new File(destFile), filterSet, copyFile(new File(sourceFile), new File(destFile), filterSet,
overwrite, false); overwrite, false);
@@ -120,7 +134,7 @@ public class FileUtils {
* *
* @throws IOException * @throws IOException
*/ */
public static void copyFile(String sourceFile, String destFile, FilterSet filterSet,
public void copyFile(String sourceFile, String destFile, FilterSet filterSet,
boolean overwrite, boolean preserveLastModified) boolean overwrite, boolean preserveLastModified)
throws IOException { throws IOException {
copyFile(new File(sourceFile), new File(destFile), filterSet, copyFile(new File(sourceFile), new File(destFile), filterSet,
@@ -133,7 +147,7 @@ public class FileUtils {
* *
* @throws IOException * @throws IOException
*/ */
public static void copyFile(File sourceFile, File destFile) throws IOException {
public void copyFile(File sourceFile, File destFile) throws IOException {
copyFile(sourceFile, destFile, null, false, false); copyFile(sourceFile, destFile, null, false, false);
} }


@@ -143,7 +157,7 @@ public class FileUtils {
* *
* @throws IOException * @throws IOException
*/ */
public static void copyFile(File sourceFile, File destFile, FilterSet filterSet)
public void copyFile(File sourceFile, File destFile, FilterSet filterSet)
throws IOException { throws IOException {
copyFile(sourceFile, destFile, filterSet, false, false); copyFile(sourceFile, destFile, filterSet, false, false);
} }
@@ -155,7 +169,7 @@ public class FileUtils {
* *
* @throws IOException * @throws IOException
*/ */
public static void copyFile(File sourceFile, File destFile, FilterSet filterSet,
public void copyFile(File sourceFile, File destFile, FilterSet filterSet,
boolean overwrite) throws IOException { boolean overwrite) throws IOException {
copyFile(sourceFile, destFile, filterSet, overwrite, false); copyFile(sourceFile, destFile, filterSet, overwrite, false);
} }
@@ -169,7 +183,7 @@ public class FileUtils {
* *
* @throws IOException * @throws IOException
*/ */
public static void copyFile(File sourceFile, File destFile, FilterSet filterSet,
public void copyFile(File sourceFile, File destFile, FilterSet filterSet,
boolean overwrite, boolean preserveLastModified) boolean overwrite, boolean preserveLastModified)
throws IOException { throws IOException {
@@ -229,11 +243,11 @@ public class FileUtils {
} }


/** /**
* Calls File.setLastModified(long time) in a Java 1.1 compatible way.
* see whether we have a setLastModified method in File and return it.
*/ */
public static void setFileLastModified(File file, long time) throws BuildException {
protected final Method getSetLastModified() {
if (Project.getJavaVersion() == Project.JAVA_1_1) { if (Project.getJavaVersion() == Project.JAVA_1_1) {
return;
return null;
} }
if (setLastModified == null) { if (setLastModified == null) {
synchronized (lockReflection) { synchronized (lockReflection) {
@@ -249,14 +263,25 @@ public class FileUtils {
} }
} }
} }
return setLastModified;
}

/**
* Calls File.setLastModified(long time) in a Java 1.1 compatible way.
*/
public void setFileLastModified(File file, long time) throws BuildException {
if (Project.getJavaVersion() == Project.JAVA_1_1) {
return;
}
Long[] times = new Long[1]; Long[] times = new Long[1];
if (time < 0) { if (time < 0) {
times[0] = new Long(System.currentTimeMillis()); times[0] = new Long(System.currentTimeMillis());
} else { } else {
times[0] = new Long(time); times[0] = new Long(time);
} }

try { try {
setLastModified.invoke(file, times);
getSetLastModified().invoke(file, times);
} catch (java.lang.reflect.InvocationTargetException ite) { } catch (java.lang.reflect.InvocationTargetException ite) {
Throwable nested = ite.getTargetException(); Throwable nested = ite.getTargetException();
throw new BuildException("Exception setting the modification time " throw new BuildException("Exception setting the modification time "

Loading…
Cancel
Save