Browse Source

Add encoding attribute to copy/move so that filtered operations work

on encodings different than the platform's default as well.

PR: Christopher Taylor <cstaylor@nanshu.com>

some year 2002 fixes.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271540 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
2f31d0c964
7 changed files with 95 additions and 10 deletions
  1. +1
    -1
      docs/manual/CoreTasks/property.html
  2. +33
    -1
      src/main/org/apache/tools/ant/taskdefs/Copy.java
  3. +6
    -2
      src/main/org/apache/tools/ant/taskdefs/Move.java
  4. +1
    -1
      src/main/org/apache/tools/ant/taskdefs/Property.java
  5. +1
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/ide/VAJWorkspaceScanner.java
  6. +52
    -3
      src/main/org/apache/tools/ant/util/FileUtils.java
  7. +1
    -1
      src/testcases/org/apache/tools/ant/taskdefs/PropertyTest.java

+ 1
- 1
docs/manual/CoreTasks/property.html View File

@@ -147,7 +147,7 @@ Two of the values are shown being echoed.
</p>

<hr>
<p align="center">Copyright &copy; 2001 Apache Software Foundation. All rights
<p align="center">Copyright &copy; 2001-2002 Apache Software Foundation. All rights
Reserved.</p>
</body>
</html>


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

@@ -88,6 +88,8 @@ import java.util.Enumeration;
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
* @author <A href="gholam@xtra.co.nz">Michael McCallum</A>
* @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
*
* @version $Revision$
*/
public class Copy extends Task {
protected File file = null; // the source file
@@ -109,6 +111,7 @@ public class Copy extends Task {
protected Mapper mapperElement = null;
private Vector filterSets = new Vector();
private FileUtils fileUtils;
private String encoding = null;

public Copy() {
fileUtils = FileUtils.newFileUtils();
@@ -163,6 +166,16 @@ public class Copy extends Task {
preserveLastModified = preserve;
}

/**
* Whether to give the copied files the same last modified time as
* the original files.
*
* @since 1.32, Ant 1.5
*/
public boolean getPreserveLastModified() {
return preserveLastModified;
}

/**
* Get the filtersets being applied to this operation.
*
@@ -234,6 +247,24 @@ public class Copy extends Task {
return mapperElement;
}

/**
* Sets the character encoding
*
* @since 1.32, Ant 1.5
*/
public void setEncoding (String encoding) {
this.encoding = encoding;
}

/**
* @return the character encoding, <code>null</code> if not set.
*
* @since 1.32, Ant 1.5
*/
public String getEncoding() {
return encoding;
}

/**
* Performs the copy operation.
*/
@@ -422,7 +453,8 @@ public class Copy extends Task {
executionFilters.addFilterSet((FilterSet)filterEnum.nextElement());
}
fileUtils.copyFile(fromFile, toFile, executionFilters,
forceOverwrite, preserveLastModified);
forceOverwrite, preserveLastModified,
encoding);
} catch (IOException ioe) {
String msg = "Failed to copy " + fromFile + " to " + toFile
+ " due to " + ioe.getMessage();


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

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -81,6 +81,8 @@ import java.util.Enumeration;
*
* @author Glenn McAllister <a href="mailto:glennm@ca.ibm.com">glennm@ca.ibm.com</a>
* @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
*
* @version $Revision$
*/
public class Move extends Copy {

@@ -155,7 +157,9 @@ public class Move extends Copy {
executionFilters.addFilterSet((FilterSet)filterEnum.nextElement());
}
getFileUtils().copyFile(f, d, executionFilters,
forceOverwrite);
forceOverwrite,
getPreserveLastModified(),
getEncoding());

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


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

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without


+ 1
- 1
src/main/org/apache/tools/ant/taskdefs/optional/ide/VAJWorkspaceScanner.java View File

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without


+ 52
- 3
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -64,6 +64,8 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.lang.reflect.Method;
import java.text.DecimalFormat;
@@ -140,7 +142,7 @@ public class FileUtils {
overwrite, false);
}

/**
/**
* Convienence method to copy a file from a source to a
* destination specifying if token filtering must be used, if
* source files may overwrite newer destination files and the
@@ -156,6 +158,25 @@ public class FileUtils {
overwrite, preserveLastModified);
}

/**
* Convienence method to copy a file from a source to a
* destination specifying if token filtering must be used, if
* source files may overwrite newer destination files and the
* last modified time of <code>destFile</code> file should be made equal
* to the last modified time of <code>sourceFile</code>.
*
* @throws IOException
*
* @since 1.14, Ant 1.5
*/
public void copyFile(String sourceFile, String destFile,
FilterSetCollection filters, boolean overwrite,
boolean preserveLastModified, String encoding)
throws IOException {
copyFile(new File(sourceFile), new File(destFile), filters,
overwrite, preserveLastModified, encoding);
}

/**
* Convienence method to copy a file from a source to a destination.
* No filtering is performed.
@@ -201,6 +222,26 @@ public class FileUtils {
public void copyFile(File sourceFile, File destFile, FilterSetCollection filters,
boolean overwrite, boolean preserveLastModified)
throws IOException {
copyFile(sourceFile, destFile, filters, overwrite,
preserveLastModified, null);
}

/**
* Convienence method to copy a file from a source to a
* destination specifying if token filtering must be used, if
* source files may overwrite newer destination files, the last
* modified time of <code>destFile</code> file should be made
* equal to the last modified time of <code>sourceFile</code> and
* which character encoding to assume.
*
* @throws IOException
*
* @since 1.14, Ant 1.5
*/
public void copyFile(File sourceFile, File destFile,
FilterSetCollection filters, boolean overwrite,
boolean preserveLastModified, String encoding)
throws IOException {
if (overwrite || !destFile.exists() ||
destFile.lastModified() < sourceFile.lastModified()) {
@@ -217,8 +258,16 @@ public class FileUtils {
}

if (filters != null && filters.hasFilters()) {
BufferedReader in = new BufferedReader(new FileReader(sourceFile));
BufferedWriter out = new BufferedWriter(new FileWriter(destFile));
BufferedReader in = null;
BufferedWriter out = null;

if (encoding == null) {
in = new BufferedReader(new FileReader(sourceFile));
out = new BufferedWriter(new FileWriter(destFile));
} else {
in = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile), encoding));
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile), encoding));
}

int length;
String newline = null;


+ 1
- 1
src/testcases/org/apache/tools/ant/taskdefs/PropertyTest.java View File

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without


Loading…
Cancel
Save