Browse Source

setFoo(String) replaced with setFoo(ExtendedEnumeratedAttribute) for certain attributes of these tasks:

Available.java
Tar.java
Tstamp.java
setFoo(String) has been deprecated for the affected attributes.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270071 13f79535-47bb-0310-9956-ffa450edef68
master
Magesh Umasankar 23 years ago
parent
commit
e503ebd4c7
7 changed files with 342 additions and 173 deletions
  1. +5
    -0
      src/etc/testcases/taskdefs/available.xml
  2. +5
    -1
      src/etc/testcases/taskdefs/tar.xml
  3. +55
    -22
      src/main/org/apache/tools/ant/taskdefs/Available.java
  4. +144
    -64
      src/main/org/apache/tools/ant/taskdefs/Tar.java
  5. +80
    -42
      src/main/org/apache/tools/ant/taskdefs/Tstamp.java
  6. +36
    -31
      src/testcases/org/apache/tools/ant/taskdefs/AvailableTest.java
  7. +17
    -13
      src/testcases/org/apache/tools/ant/taskdefs/TarTest.java

+ 5
- 0
src/etc/testcases/taskdefs/available.xml View File

@@ -98,4 +98,9 @@
file="../this_dir_should_never_exist" type="dir"/> file="../this_dir_should_never_exist" type="dir"/>
</target> </target>


<target name="test19">
<available property="test"
file="available.xml" type="Foo"/>
</target>

</project> </project>

+ 5
- 1
src/etc/testcases/taskdefs/tar.xml View File

@@ -26,7 +26,11 @@
basedir="." basedir="."
includes="test5dir"/> includes="test5dir"/>
</target> </target>

<target name="test6">
<tar tarfile="blah" longfile="Foo"/>
</target>

<target name="cleanup"> <target name="cleanup">
<delete file="test4.tar"/> <delete file="test4.tar"/>
<delete file="test5.tar"/> <delete file="test5.tar"/>


+ 55
- 22
src/main/org/apache/tools/ant/taskdefs/Available.java View File

@@ -61,6 +61,7 @@ import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.condition.Condition; import org.apache.tools.ant.taskdefs.condition.Condition;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference; import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;
@@ -69,6 +70,7 @@ import org.apache.tools.ant.util.FileUtils;
* Will set the given property if the requested resource is available at runtime. * Will set the given property if the requested resource is available at runtime.
* *
* @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a> * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
* @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
*/ */


public class Available extends Task implements Condition { public class Available extends Task implements Condition {
@@ -78,7 +80,7 @@ public class Available extends Task implements Condition {
private String file; private String file;
private Path filepath; private Path filepath;
private String resource; private String resource;
private String type;
private FileDir type;
private Path classpath; private Path classpath;
private AntClassLoader loader; private AntClassLoader loader;
private String value = "true"; private String value = "true";
@@ -101,7 +103,7 @@ public class Available extends Task implements Condition {
public void setFilepath(Path filepath) { public void setFilepath(Path filepath) {
createFilepath().append(filepath); createFilepath().append(filepath);
} }
public Path createFilepath() { public Path createFilepath() {
if (this.filepath == null) { if (this.filepath == null) {
this.filepath = new Path(project); this.filepath = new Path(project);
@@ -131,7 +133,20 @@ public class Available extends Task implements Condition {
this.resource = resource; this.resource = resource;
} }


/**
* @deprecated setType(String) is deprecated and is replaced with
* setType(Available.FileDir) to make Ant's Introspection
* mechanism do the work and also to encapsulate operations on
* the type in its own class.
*/
public void setType(String type) { public void setType(String type) {
log("DEPRECATED - The setType(String) method has been deprecated."
+ " Use setType(Available.FileDir) instead.");
this.type = new FileDir();
this.type.setValue(type);
}

public void setType(FileDir type) {
this.type = type; this.type = type;
} }


@@ -144,7 +159,7 @@ public class Available extends Task implements Condition {
this.project.setProperty(property, value); this.project.setProperty(property, value);
} }
} }
public boolean eval() throws BuildException { public boolean eval() throws BuildException {
if (classname == null && file == null && resource == null) { if (classname == null && file == null && resource == null) {
throw new BuildException("At least one of (classname|file|resource) is required", location); throw new BuildException("At least one of (classname|file|resource) is required", location);
@@ -154,9 +169,6 @@ public class Available extends Task implements Condition {
if (file == null){ if (file == null){
throw new BuildException("The type attribute is only valid when specifying the file attribute."); throw new BuildException("The type attribute is only valid when specifying the file attribute.");
} }
if (!type.equalsIgnoreCase("file") && !type.equalsIgnoreCase("dir")){
throw new BuildException("Type must be one of either dir or file");
}
} }


if (classpath != null) { if (classpath != null) {
@@ -168,7 +180,7 @@ public class Available extends Task implements Condition {
log("Unable to load class " + classname + " to set property " + property, Project.MSG_VERBOSE); log("Unable to load class " + classname + " to set property " + property, Project.MSG_VERBOSE);
return false; return false;
} }
if ((file != null) && !checkFile()) { if ((file != null) && !checkFile()) {
if (type != null) { if (type != null) {
log("Unable to find " + type + " " + file + " to set property " + property, Project.MSG_VERBOSE); log("Unable to find " + type + " " + file + " to set property " + property, Project.MSG_VERBOSE);
@@ -177,7 +189,7 @@ public class Available extends Task implements Condition {
} }
return false; return false;
} }
if ((resource != null) && !checkResource(resource)) { if ((resource != null) && !checkResource(resource)) {
log("Unable to load resource " + resource + " to set property " + property, Project.MSG_VERBOSE); log("Unable to load resource " + resource + " to set property " + property, Project.MSG_VERBOSE);
return false; return false;
@@ -197,7 +209,7 @@ public class Available extends Task implements Condition {
String[] paths = filepath.list(); String[] paths = filepath.list();
for(int i = 0; i < paths.length; ++i) { for(int i = 0; i < paths.length; ++i) {
log("Searching " + paths[i], Project.MSG_DEBUG); log("Searching " + paths[i], Project.MSG_DEBUG);
/*
/*
** filepath can be a list of directory and/or ** filepath can be a list of directory and/or
** file names (gen'd via <fileset>) ** file names (gen'd via <fileset>)
** **
@@ -218,11 +230,11 @@ public class Available extends Task implements Condition {
if (type == null) { if (type == null) {
log("Found: " + path, Project.MSG_VERBOSE); log("Found: " + path, Project.MSG_VERBOSE);
return true; return true;
} else if (type.equalsIgnoreCase("dir")
} else if (type.isDir()
&& path.isDirectory()) { && path.isDirectory()) {
log("Found directory: " + path, Project.MSG_VERBOSE); log("Found directory: " + path, Project.MSG_VERBOSE);
return true; return true;
} else if (type.equalsIgnoreCase("file")
} else if (type.isFile()
&& path.isFile()) { && path.isFile()) {
log("Found file: " + path, Project.MSG_VERBOSE); log("Found file: " + path, Project.MSG_VERBOSE);
return true; return true;
@@ -230,16 +242,16 @@ public class Available extends Task implements Condition {
// not the requested type // not the requested type
return false; return false;
} }
FileUtils fileUtils = FileUtils.newFileUtils(); FileUtils fileUtils = FileUtils.newFileUtils();
File parent = fileUtils.getParentFile(path); File parent = fileUtils.getParentFile(path);
// ** full-pathname specified == parent dir of path in list // ** full-pathname specified == parent dir of path in list
if (parent != null && parent.exists()
if (parent != null && parent.exists()
&& file.equals(parent.getAbsolutePath())) { && file.equals(parent.getAbsolutePath())) {
if (type == null) { if (type == null) {
log("Found: " + parent, Project.MSG_VERBOSE); log("Found: " + parent, Project.MSG_VERBOSE);
return true; return true;
} else if (type.equalsIgnoreCase("dir")) {
} else if (type.isDir()) {
log("Found directory: " + parent, Project.MSG_VERBOSE); log("Found directory: " + parent, Project.MSG_VERBOSE);
return true; return true;
} }
@@ -249,25 +261,25 @@ public class Available extends Task implements Condition {


// ** simple name specified == path in list + name // ** simple name specified == path in list + name
if (path.exists() && path.isDirectory()) { if (path.exists() && path.isDirectory()) {
if (checkFile(new File(path, file),
if (checkFile(new File(path, file),
file + " in " + path)) { file + " in " + path)) {
return true; return true;
} }
} }
// ** simple name specified == parent dir + name // ** simple name specified == parent dir + name
if (parent != null && parent.exists()) { if (parent != null && parent.exists()) {
if (checkFile(new File(parent, file),
if (checkFile(new File(parent, file),
file + " in " + parent)) { file + " in " + parent)) {
return true; return true;
} }
} }
// ** simple name specified == parent of parent dir + name // ** simple name specified == parent of parent dir + name
if (parent != null) { if (parent != null) {
File grandParent = fileUtils.getParentFile(parent); File grandParent = fileUtils.getParentFile(parent);
if (grandParent != null && grandParent.exists()) { if (grandParent != null && grandParent.exists()) {
if (checkFile(new File(grandParent, file),
if (checkFile(new File(grandParent, file),
file + " in " + grandParent)) { file + " in " + grandParent)) {
return true; return true;
} }
@@ -280,12 +292,12 @@ public class Available extends Task implements Condition {


private boolean checkFile(File f, String text) { private boolean checkFile(File f, String text) {
if (type != null) { if (type != null) {
if (type.equalsIgnoreCase("dir")) {
if (type.isDir()) {
if( f.isDirectory()) { if( f.isDirectory()) {
log("Found directory: " + text, Project.MSG_VERBOSE); log("Found directory: " + text, Project.MSG_VERBOSE);
} }
return f.isDirectory(); return f.isDirectory();
} else if (type.equalsIgnoreCase("file")) {
} else if (type.isFile()) {
if( f.isFile()) { if( f.isFile()) {
log("Found file: " + text, Project.MSG_VERBOSE); log("Found file: " + text, Project.MSG_VERBOSE);
} }
@@ -306,7 +318,7 @@ public class Available extends Task implements Condition {
if (cL != null) { if (cL != null) {
return (cL.getResourceAsStream(resource) != null); return (cL.getResourceAsStream(resource) != null);
} else { } else {
return
return
(ClassLoader.getSystemResourceAsStream(resource) != null); (ClassLoader.getSystemResourceAsStream(resource) != null);
} }
} }
@@ -333,4 +345,25 @@ public class Available extends Task implements Condition {
return false; return false;
} }
} }

public static class FileDir extends EnumeratedAttribute {

private final static String[] values = {"file", "dir"};

public String[] getValues() {
return values;
}

public boolean isDir() {
return "dir".equalsIgnoreCase(getValue());
}

public boolean isFile() {
return "file".equalsIgnoreCase(getValue());
}

public String toString() {
return getValue();
}
}
} }

+ 144
- 64
src/main/org/apache/tools/ant/taskdefs/Tar.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 1999 The Apache Software Foundation. All rights
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -9,7 +9,7 @@
* are met: * are met:
* *
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer.
* *
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer in
@@ -17,15 +17,15 @@
* distribution. * distribution.
* *
* 3. The end-user documentation included with the redistribution, if * 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)." * Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself, * Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear. * if and wherever such third-party acknowlegements normally appear.
* *
* 4. The names "The Jakarta Project", "Ant", and "Apache Software * 4. The names "The Jakarta Project", "Ant", and "Apache Software
* Foundation" must not be used to endorse or promote products derived * Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* from this software without prior written permission. For written
* permission, please contact apache@apache.org. * permission, please contact apache@apache.org.
* *
* 5. Products derived from this software may not be called "Apache" * 5. Products derived from this software may not be called "Apache"
@@ -63,39 +63,58 @@ import java.util.Enumeration;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.util.SourceFileScanner; import org.apache.tools.ant.util.SourceFileScanner;
import org.apache.tools.ant.util.MergingMapper; import org.apache.tools.ant.util.MergingMapper;
import org.apache.tools.tar.TarOutputStream; import org.apache.tools.tar.TarOutputStream;
import org.apache.tools.tar.TarConstants; import org.apache.tools.tar.TarConstants;
import org.apache.tools.tar.TarEntry; import org.apache.tools.tar.TarEntry;
import org.apache.tools.ant.types.FileSet;


/** /**
* Creates a TAR archive. * Creates a TAR archive.
* *
* @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a> * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
* @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
*/ */


public class Tar extends MatchingTask { public class Tar extends MatchingTask {


// permissable values for longfile attribute
/**
* @deprecated Tar.WARN is deprecated and is replaced with
* Tar.TarLongFileMode.WARN
*/
public final static String WARN = "warn"; public final static String WARN = "warn";
/**
* @deprecated Tar.FAIL is deprecated and is replaced with
* Tar.TarLongFileMode.FAIL
*/
public final static String FAIL = "fail"; public final static String FAIL = "fail";
/**
* @deprecated Tar.TRUNCATE is deprecated and is replaced with
* Tar.TarLongFileMode.TRUNCATE
*/
public final static String TRUNCATE = "truncate"; public final static String TRUNCATE = "truncate";
/**
* @deprecated Tar.GNU is deprecated and is replaced with
* Tar.TarLongFileMode.GNU
*/
public final static String GNU = "gnu"; public final static String GNU = "gnu";
/**
* @deprecated Tar.OMIT is deprecated and is replaced with
* Tar.TarLongFileMode.OMIT
*/
public final static String OMIT = "omit"; public final static String OMIT = "omit";


private String[] validModes = new String[] {WARN, FAIL, TRUNCATE, GNU, OMIT};

File tarFile; File tarFile;
File baseDir; File baseDir;
String longFileMode = WARN;
private TarLongFileMode longFileMode = new TarLongFileMode();
Vector filesets = new Vector(); Vector filesets = new Vector();
Vector fileSetFiles = new Vector(); Vector fileSetFiles = new Vector();
/** /**
* Indicates whether the user has been warned about long files already. * Indicates whether the user has been warned about long files already.
*/ */
@@ -106,55 +125,70 @@ public class Tar extends MatchingTask {
filesets.addElement(fileset); filesets.addElement(fileset);
return fileset; return fileset;
} }
/** /**
* This is the name/location of where to create the tar file. * This is the name/location of where to create the tar file.
*/ */
public void setTarfile(File tarFile) { public void setTarfile(File tarFile) {
this.tarFile = tarFile; this.tarFile = tarFile;
} }
/** /**
* This is the base directory to look in for things to tar. * This is the base directory to look in for things to tar.
*/ */
public void setBasedir(File baseDir) { public void setBasedir(File baseDir) {
this.baseDir = baseDir; this.baseDir = baseDir;
} }
/** /**
* Set how to handle long files. * Set how to handle long files.
* *
* Allowable values are * Allowable values are
* truncate - paths are truncated to the maximum length * truncate - paths are truncated to the maximum length
* fail - patsh greater than the maximim cause a build exception
* fail - paths greater than the maximim cause a build exception
* warn - paths greater than the maximum cause a warning and GNU is used * warn - paths greater than the maximum cause a warning and GNU is used
* gnu - GNU extensions are used for any paths greater than the maximum. * gnu - GNU extensions are used for any paths greater than the maximum.
* omit - paths greater than the maximum are omitted from the archive * omit - paths greater than the maximum are omitted from the archive
* @deprecated setLongFile(String) is deprecated and is replaced with
* setLongFile(Tar.TarLongFileMode) to make Ant's Introspection
* mechanism do the work and also to encapsulate operations on
* the mode in its own class.
*/ */
public void setLongfile(String mode) { public void setLongfile(String mode) {
for (int i = 0; i < validModes.length; ++i) {
if (mode.equalsIgnoreCase(validModes[i])) {
this.longFileMode = mode;
return;
}
}
throw new BuildException("The longfile value " + mode + " is not a valid value");
log("DEPRECATED - The setLongfile(String) method has been deprecated."
+ " Use setLongfile(Tar.TarLongFileMode) instead.");
this.longFileMode = new TarLongFileMode();
longFileMode.setValue(mode);
}

/**
* Set how to handle long files.
*
* Allowable values are
* truncate - paths are truncated to the maximum length
* fail - paths greater than the maximim cause a build exception
* warn - paths greater than the maximum cause a warning and GNU is used
* gnu - GNU extensions are used for any paths greater than the maximum.
* omit - paths greater than the maximum are omitted from the archive
*/
public void setLongfile(TarLongFileMode mode) {
this.longFileMode = mode;
} }


public void execute() throws BuildException { public void execute() throws BuildException {
if (tarFile == null) { if (tarFile == null) {
throw new BuildException("tarfile attribute must be set!",
throw new BuildException("tarfile attribute must be set!",
location); location);
} }


if (tarFile.exists() && tarFile.isDirectory()) { if (tarFile.exists() && tarFile.isDirectory()) {
throw new BuildException("tarfile is a directory!",
throw new BuildException("tarfile is a directory!",
location); location);
} }


if (tarFile.exists() && !tarFile.canWrite()) { if (tarFile.exists() && !tarFile.canWrite()) {
throw new BuildException("Can not write to the specified tarfile!",
throw new BuildException("Can not write to the specified tarfile!",
location); location);
} }


@@ -162,29 +196,29 @@ public class Tar extends MatchingTask {
if (!baseDir.exists()) { if (!baseDir.exists()) {
throw new BuildException("basedir does not exist!", location); throw new BuildException("basedir does not exist!", location);
} }
// add the main fileset to the list of filesets to process. // add the main fileset to the list of filesets to process.
TarFileSet mainFileSet = new TarFileSet(fileset); TarFileSet mainFileSet = new TarFileSet(fileset);
mainFileSet.setDir(baseDir); mainFileSet.setDir(baseDir);
filesets.addElement(mainFileSet); filesets.addElement(mainFileSet);
} }
if (filesets.size() == 0) { if (filesets.size() == 0) {
throw new BuildException("You must supply either a basdir attribute or some nested filesets.",
throw new BuildException("You must supply either a basdir attribute or some nested filesets.",
location); location);
} }
// check if tr is out of date with respect to each // check if tr is out of date with respect to each
// fileset // fileset
boolean upToDate = true; boolean upToDate = true;
for (Enumeration e = filesets.elements(); e.hasMoreElements();) { for (Enumeration e = filesets.elements(); e.hasMoreElements();) {
TarFileSet fs = (TarFileSet)e.nextElement(); TarFileSet fs = (TarFileSet)e.nextElement();
String[] files = fs.getFiles(project); String[] files = fs.getFiles(project);
if (!archiveIsUpToDate(files)) { if (!archiveIsUpToDate(files)) {
upToDate = false; upToDate = false;
} }
for (int i = 0; i < files.length; ++i) { for (int i = 0; i < files.length; ++i) {
if (tarFile.equals(new File(fs.getDir(project), files[i]))) { if (tarFile.equals(new File(fs.getDir(project), files[i]))) {
throw new BuildException("A tar file cannot include itself", location); throw new BuildException("A tar file cannot include itself", location);
@@ -204,18 +238,18 @@ public class Tar extends MatchingTask {
try { try {
tOut = new TarOutputStream(new FileOutputStream(tarFile)); tOut = new TarOutputStream(new FileOutputStream(tarFile));
tOut.setDebug(true); tOut.setDebug(true);
if (longFileMode.equalsIgnoreCase(TRUNCATE)) {
if (longFileMode.isTruncateMode()) {
tOut.setLongFileMode(TarOutputStream.LONGFILE_TRUNCATE); tOut.setLongFileMode(TarOutputStream.LONGFILE_TRUNCATE);
} }
else if (longFileMode.equalsIgnoreCase(FAIL) ||
longFileMode.equalsIgnoreCase(OMIT)) {
else if (longFileMode.isFailMode() ||
longFileMode.isOmitMode()) {
tOut.setLongFileMode(TarOutputStream.LONGFILE_ERROR); tOut.setLongFileMode(TarOutputStream.LONGFILE_ERROR);
} }
else { else {
// warn or GNU // warn or GNU
tOut.setLongFileMode(TarOutputStream.LONGFILE_GNU); tOut.setLongFileMode(TarOutputStream.LONGFILE_GNU);
} }
longWarningGiven = false; longWarningGiven = false;
for (Enumeration e = filesets.elements(); e.hasMoreElements();) { for (Enumeration e = filesets.elements(); e.hasMoreElements();) {
TarFileSet fs = (TarFileSet)e.nextElement(); TarFileSet fs = (TarFileSet)e.nextElement();
@@ -250,27 +284,27 @@ public class Tar extends MatchingTask {
if (vPath.length() <= 0) { if (vPath.length() <= 0) {
return; return;
} }
if (file.isDirectory() && !vPath.endsWith("/")) { if (file.isDirectory() && !vPath.endsWith("/")) {
vPath += "/"; vPath += "/";
} }


try { try {
if (vPath.length() >= TarConstants.NAMELEN) { if (vPath.length() >= TarConstants.NAMELEN) {
if (longFileMode.equalsIgnoreCase(OMIT)) {
if (longFileMode.isOmitMode()) {
log("Omitting: "+ vPath, Project.MSG_INFO); log("Omitting: "+ vPath, Project.MSG_INFO);
return; return;
} else if (longFileMode.equalsIgnoreCase(WARN)) {
log("Entry: "+ vPath + " longer than " +
} else if (longFileMode.isWarnMode()) {
log("Entry: "+ vPath + " longer than " +
TarConstants.NAMELEN + " characters.", Project.MSG_WARN); TarConstants.NAMELEN + " characters.", Project.MSG_WARN);
if (!longWarningGiven) {
if (!longWarningGiven) {
log("Resulting tar file can only be processed successfully" log("Resulting tar file can only be processed successfully"
+ " by GNU compatible tar commands", Project.MSG_WARN); + " by GNU compatible tar commands", Project.MSG_WARN);
longWarningGiven = true; longWarningGiven = true;
} }
} else if (longFileMode.equalsIgnoreCase(FAIL)) {
} else if (longFileMode.isFailMode()) {
throw new BuildException( throw new BuildException(
"Entry: "+ vPath + " longer than " +
"Entry: "+ vPath + " longer than " +
TarConstants.NAMELEN + "characters.", location); TarConstants.NAMELEN + "characters.", location);
} }
} }
@@ -280,12 +314,12 @@ public class Tar extends MatchingTask {
if (!file.isDirectory()) { if (!file.isDirectory()) {
te.setSize(file.length()); te.setSize(file.length());
te.setMode(tarFileSet.getMode()); te.setMode(tarFileSet.getMode());
}
}
te.setUserName(tarFileSet.getUserName()); te.setUserName(tarFileSet.getUserName());
te.setGroupName(tarFileSet.getGroup()); te.setGroupName(tarFileSet.getGroup());
tOut.putNextEntry(te); tOut.putNextEntry(te);
if (!file.isDirectory()) { if (!file.isDirectory()) {
fIn = new FileInputStream(file); fIn = new FileInputStream(file);


@@ -296,8 +330,8 @@ public class Tar extends MatchingTask {
count = fIn.read(buffer, 0, buffer.length); count = fIn.read(buffer, 0, buffer.length);
} while (count != -1); } while (count != -1);
} }
tOut.closeEntry();
tOut.closeEntry();
} finally { } finally {
if (fIn != null) if (fIn != null)
fIn.close(); fIn.close();
@@ -313,21 +347,21 @@ public class Tar extends MatchingTask {


public static class TarFileSet extends FileSet { public static class TarFileSet extends FileSet {
private String[] files = null; private String[] files = null;
private int mode = 0100644; private int mode = 0100644;
private String userName = ""; private String userName = "";
private String groupName = ""; private String groupName = "";
public TarFileSet(FileSet fileset) { public TarFileSet(FileSet fileset) {
super(fileset); super(fileset);
} }
public TarFileSet() { public TarFileSet() {
super(); super();
} }
/** /**
* Get a list of files and directories specified in the fileset. * Get a list of files and directories specified in the fileset.
* @return a list of file and directory names, relative to * @return a list of file and directory names, relative to
@@ -343,33 +377,79 @@ public class Tar extends MatchingTask {
System.arraycopy(filesPerSe, 0, files, directories.length, System.arraycopy(filesPerSe, 0, files, directories.length,
filesPerSe.length); filesPerSe.length);
} }
return files; return files;
} }
public void setMode(String octalString) { public void setMode(String octalString) {
this.mode = 0100000 | Integer.parseInt(octalString, 8); this.mode = 0100000 | Integer.parseInt(octalString, 8);
} }
public int getMode() { public int getMode() {
return mode; return mode;
} }
public void setUserName(String userName) { public void setUserName(String userName) {
this.userName = userName; this.userName = userName;
} }
public String getUserName() { public String getUserName() {
return userName; return userName;
} }
public void setGroup(String groupName) { public void setGroup(String groupName) {
this.groupName = groupName; this.groupName = groupName;
} }
public String getGroup() { public String getGroup() {
return groupName; return groupName;
} }

}

/**
* Valid Modes for LongFile attribute to Tar Task
*
* @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
*/
public static class TarLongFileMode extends EnumeratedAttribute {

// permissable values for longfile attribute
public final static String WARN = "warn";
public final static String FAIL = "fail";
public final static String TRUNCATE = "truncate";
public final static String GNU = "gnu";
public final static String OMIT = "omit";

private final String[] validModes = {WARN, FAIL, TRUNCATE, GNU, OMIT};

public TarLongFileMode() {
super();
setValue(WARN);
}

public String[] getValues() {
return validModes;
}

public boolean isTruncateMode() {
return TRUNCATE.equalsIgnoreCase(getValue());
}

public boolean isWarnMode() {
return WARN.equalsIgnoreCase(getValue());
}

public boolean isGnuMode() {
return GNU.equalsIgnoreCase(getValue());
}

public boolean isFailMode() {
return FAIL.equalsIgnoreCase(getValue());
}

public boolean isOmitMode() {
return OMIT.equalsIgnoreCase(getValue());
}
} }
} }

+ 80
- 42
src/main/org/apache/tools/ant/taskdefs/Tstamp.java View File

@@ -58,15 +58,17 @@ import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.Location; import org.apache.tools.ant.Location;
import org.apache.tools.ant.types.EnumeratedAttribute;


import java.util.Vector;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Locale;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Calendar;
import java.util.StringTokenizer;
import java.util.Hashtable;
import java.util.Locale;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.Vector;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;


/** /**
@@ -76,12 +78,13 @@ import java.text.SimpleDateFormat;
* @author stefano@apache.org * @author stefano@apache.org
* @author roxspring@yahoo.com * @author roxspring@yahoo.com
* @author conor@cognet.com.au * @author conor@cognet.com.au
* @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
*/ */
public class Tstamp extends Task { public class Tstamp extends Task {


private Vector customFormats = new Vector(); private Vector customFormats = new Vector();
private String prefix = ""; private String prefix = "";
public void setPrefix(String prefix) { public void setPrefix(String prefix) {
this.prefix = prefix; this.prefix = prefix;
if (!this.prefix.endsWith(".")) { if (!this.prefix.endsWith(".")) {
@@ -119,7 +122,7 @@ public class Tstamp extends Task {
customFormats.addElement(cts); customFormats.addElement(cts);
return cts; return cts;
} }
public class CustomFormat public class CustomFormat
{ {
private TimeZone timeZone; private TimeZone timeZone;
@@ -131,22 +134,22 @@ public class Tstamp extends Task {
private int offset = 0; private int offset = 0;
private int field = Calendar.DATE; private int field = Calendar.DATE;
private String prefix=""; private String prefix="";
public CustomFormat(String prefix) public CustomFormat(String prefix)
{ {
this.prefix = prefix; this.prefix = prefix;
} }
public void setProperty(String propertyName) public void setProperty(String propertyName)
{ {
this.propertyName = prefix + propertyName; this.propertyName = prefix + propertyName;
} }
public void setPattern(String pattern) public void setPattern(String pattern)
{ {
this.pattern = pattern; this.pattern = pattern;
} }
public void setLocale(String locale) public void setLocale(String locale)
{ {
StringTokenizer st = new StringTokenizer( locale, " \t\n\r\f,"); StringTokenizer st = new StringTokenizer( locale, " \t\n\r\f,");
@@ -177,47 +180,35 @@ public class Tstamp extends Task {
public void setOffset(int offset) { public void setOffset(int offset) {
this.offset = offset; this.offset = offset;
} }

/**
* @deprecated setUnit(String) is deprecated and is replaced with
* setUnit(Tstamp.Unit) to make Ant's Introspection
* mechanism do the work and also to encapsulate operations on
* the unit in its own class.
*/
public void setUnit(String unit) { public void setUnit(String unit) {
if (unit.equalsIgnoreCase("millisecond")) {
field = Calendar.MILLISECOND;
}
else if (unit.equalsIgnoreCase("second")) {
field = Calendar.SECOND;
}
else if (unit.equalsIgnoreCase("minute")) {
field = Calendar.MINUTE;
}
else if (unit.equalsIgnoreCase("hour")) {
field = Calendar.HOUR_OF_DAY;
}
else if (unit.equalsIgnoreCase("day")) {
field = Calendar.DATE;
}
else if (unit.equalsIgnoreCase("week")) {
field = Calendar.WEEK_OF_YEAR;
}
else if (unit.equalsIgnoreCase("month")) {
field = Calendar.MONTH;
}
else if (unit.equalsIgnoreCase("year")) {
field = Calendar.YEAR;
}
else {
throw new BuildException(unit + " is not a unit supported by the tstamp task", getLocation());
}
}
log("DEPRECATED - The setUnit(String) method has been deprecated."
+ " Use setUnit(Tstamp.Unit) instead.");
Unit u = new Unit();
u.setValue(unit);
field = u.getCalendarField();
}

public void setUnit(Unit unit) {
field = unit.getCalendarField();
}

public void execute(Project project, Date date, Location location) public void execute(Project project, Date date, Location location)
{ {
if (propertyName == null) { if (propertyName == null) {
throw new BuildException("property attribute must be provided", location); throw new BuildException("property attribute must be provided", location);
} }

if (pattern == null) { if (pattern == null) {
throw new BuildException("pattern attribute must be provided", location); throw new BuildException("pattern attribute must be provided", location);
} }

SimpleDateFormat sdf; SimpleDateFormat sdf;
if (language == null) { if (language == null) {
sdf = new SimpleDateFormat(pattern); sdf = new SimpleDateFormat(pattern);
@@ -240,4 +231,51 @@ public class Tstamp extends Task {
project.setNewProperty(propertyName, sdf.format(date)); project.setNewProperty(propertyName, sdf.format(date));
} }
} }

public static class Unit extends EnumeratedAttribute {

private static final String MILLISECOND = "millisecond";
private static final String SECOND = "second";
private static final String MINUTE = "minute";
private static final String HOUR = "hour";
private static final String DAY = "day";
private static final String WEEK = "week";
private static final String MONTH = "month";
private static final String YEAR = "year";

private final static String[] units = {
MILLISECOND,
SECOND,
MINUTE,
HOUR,
DAY,
WEEK,
MONTH,
YEAR
};

private Hashtable calendarFields = new Hashtable();

public Unit() {
calendarFields.put(MILLISECOND,
new Integer(Calendar.MILLISECOND));
calendarFields.put(SECOND, new Integer(Calendar.SECOND));
calendarFields.put(MINUTE, new Integer(Calendar.MINUTE));
calendarFields.put(HOUR, new Integer(Calendar.HOUR_OF_DAY));
calendarFields.put(DAY, new Integer(Calendar.DATE));
calendarFields.put(WEEK, new Integer(Calendar.WEEK_OF_YEAR));
calendarFields.put(MONTH, new Integer(Calendar.MONTH));
calendarFields.put(YEAR, new Integer(Calendar.YEAR));
}

public int getCalendarField() {
String key = getValue().toLowerCase();
Integer i = (Integer) calendarFields.get(key);
return i.intValue();
}

public String[] getValues() {
return units;
}
}
} }

+ 36
- 31
src/testcases/org/apache/tools/ant/taskdefs/AvailableTest.java View File

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


import org.apache.tools.ant.BuildFileTest; import org.apache.tools.ant.BuildFileTest;
/** /**
* @author Nico Seessle <nico@seessle.de>
* @author Nico Seessle <nico@seessle.de>
*/ */
public class AvailableTest extends BuildFileTest {
public class AvailableTest extends BuildFileTest {


public AvailableTest(String name) {
public AvailableTest(String name) {
super(name); super(name);
}
public void setUp() {
}
public void setUp() {
configureProject("src/etc/testcases/taskdefs/available.xml"); configureProject("src/etc/testcases/taskdefs/available.xml");
} }
// Nothing specified -> Fail // Nothing specified -> Fail
public void test1() {
public void test1() {
expectBuildException("test1", "required argument not specified"); expectBuildException("test1", "required argument not specified");
} }


// Only property specified -> Fail // Only property specified -> Fail
public void test2() {
public void test2() {
expectBuildException("test2", "required argument not specified"); expectBuildException("test2", "required argument not specified");
} }
// Only file specified -> Fail // Only file specified -> Fail
public void test3() {
public void test3() {
expectBuildException("test3", "required argument not specified"); expectBuildException("test3", "required argument not specified");
} }


// file doesn't exist -> property 'test' == null // file doesn't exist -> property 'test' == null
public void test4() {
public void test4() {
executeTarget("test4"); executeTarget("test4");
assertTrue(project.getProperty("test") == null); assertTrue(project.getProperty("test") == null);
} }


// file does exist -> property 'test' == 'true' // file does exist -> property 'test' == 'true'
public void test5() {
public void test5() {
executeTarget("test5"); executeTarget("test5");
assertEquals("true", project.getProperty("test")); assertEquals("true", project.getProperty("test"));
} }
// resource doesn't exist -> property 'test' == null // resource doesn't exist -> property 'test' == null
public void test6() {
public void test6() {
executeTarget("test6"); executeTarget("test6");
assertTrue(project.getProperty("test") == null); assertTrue(project.getProperty("test") == null);
} }


// resource does exist -> property 'test' == 'true' // resource does exist -> property 'test' == 'true'
public void test7() {
public void test7() {
executeTarget("test7"); executeTarget("test7");
assertEquals("true", project.getProperty("test")); assertEquals("true", project.getProperty("test"));
} }


// class doesn't exist -> property 'test' == null // class doesn't exist -> property 'test' == null
public void test8() {
public void test8() {
executeTarget("test8"); executeTarget("test8");
assertTrue(project.getProperty("test") == null); assertTrue(project.getProperty("test") == null);
} }


// class does exist -> property 'test' == 'true' // class does exist -> property 'test' == 'true'
public void test9() {
public void test9() {
executeTarget("test9"); executeTarget("test9");
assertEquals("true", project.getProperty("test")); assertEquals("true", project.getProperty("test"));
} }
// All three specified and all three exist -> true // All three specified and all three exist -> true
public void test10() {
public void test10() {
executeTarget("test10"); executeTarget("test10");
assertEquals("true", project.getProperty("test")); assertEquals("true", project.getProperty("test"));
} }


// All three specified but class missing -> null // All three specified but class missing -> null
public void test11() {
public void test11() {
executeTarget("test11"); executeTarget("test11");
assertNull(project.getProperty("test")); assertNull(project.getProperty("test"));
} }


// Specified property-name is "" -> true // Specified property-name is "" -> true
public void test12() {
public void test12() {
executeTarget("test12"); executeTarget("test12");
assertNull(project.getProperty("test")); assertNull(project.getProperty("test"));
assertEquals("true", project.getProperty("")); assertEquals("true", project.getProperty(""));
} }


// Specified file is "" -> invalid files do not exist // Specified file is "" -> invalid files do not exist
public void test13() {
public void test13() {
executeTarget("test13"); executeTarget("test13");
assertNull(project.getProperty("test")); assertNull(project.getProperty("test"));
} }


// Specified file is "" actually a directory, so it should pass // Specified file is "" actually a directory, so it should pass
public void test13b() {
public void test13b() {
executeTarget("test13b"); executeTarget("test13b");
assertEquals("true", project.getProperty("test")); assertEquals("true", project.getProperty("test"));
} }
@@ -154,34 +154,39 @@ public class AvailableTest extends BuildFileTest {
/* /*
* returns non null IBM JDK 1.3 Linux * returns non null IBM JDK 1.3 Linux
*/ */
// public void test14() {
// public void test14() {
// executeTarget("test14"); // executeTarget("test14");
// assertEquals(project.getProperty("test"), null); // assertEquals(project.getProperty("test"), null);
// } // }
// Specified class is "" -> can not exist // Specified class is "" -> can not exist
public void test15() {
public void test15() {
executeTarget("test15"); executeTarget("test15");
assertNull(project.getProperty("test")); assertNull(project.getProperty("test"));
} }


// Specified dir is "" -> this is the current directory and should // Specified dir is "" -> this is the current directory and should
// always exist // always exist
public void test16() {
public void test16() {
executeTarget("test16"); executeTarget("test16");
assertEquals("true", project.getProperty("test")); assertEquals("true", project.getProperty("test"));
} }


// Specified dir is "../taskdefs" -> should exist since it's the
// Specified dir is "../taskdefs" -> should exist since it's the
// location of the buildfile used... // location of the buildfile used...
public void test17() {
public void test17() {
executeTarget("test17"); executeTarget("test17");
assertEquals("true", project.getProperty("test")); assertEquals("true", project.getProperty("test"));
} }


// Specified dir is "../this_dir_should_never_exist" -> null // Specified dir is "../this_dir_should_never_exist" -> null
public void test18() {
public void test18() {
executeTarget("test18"); executeTarget("test18");
assertNull(project.getProperty("test")); assertNull(project.getProperty("test"));
} }

// Invalid type specified
public void test19() {
expectBuildException("test19", "Invalid value for type attribute.");
}
} }

+ 17
- 13
src/testcases/org/apache/tools/ant/taskdefs/TarTest.java View File

@@ -56,37 +56,37 @@ package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.BuildFileTest; import org.apache.tools.ant.BuildFileTest;


/** /**
* @author Nico Seessle <nico@seessle.de>
* @author Nico Seessle <nico@seessle.de>
*/ */
public class TarTest extends BuildFileTest {
public TarTest(String name) {
public class TarTest extends BuildFileTest {
public TarTest(String name) {
super(name); super(name);
}
public void setUp() {
}
public void setUp() {
configureProject("src/etc/testcases/taskdefs/tar.xml"); configureProject("src/etc/testcases/taskdefs/tar.xml");
} }
public void test1() {
public void test1() {
expectBuildException("test1", "required argument not specified"); expectBuildException("test1", "required argument not specified");
} }


public void test2() {
public void test2() {
expectBuildException("test2", "required argument not specified"); expectBuildException("test2", "required argument not specified");
} }


public void test3() {
public void test3() {
expectBuildException("test3", "required argument not specified"); expectBuildException("test3", "required argument not specified");
} }


public void test4() {
public void test4() {
expectBuildException("test4", "tar cannot include itself"); expectBuildException("test4", "tar cannot include itself");
} }


public void test5() { public void test5() {
executeTarget("test5"); executeTarget("test5");
java.io.File f
java.io.File f
= new java.io.File("src/etc/testcases/taskdefs/test5.tar"); = new java.io.File("src/etc/testcases/taskdefs/test5.tar");


if (!f.exists()) { if (!f.exists()) {
@@ -94,6 +94,10 @@ public class TarTest extends BuildFileTest {
} }
} }


public void test6() {
expectBuildException("test6", "Invalid value specified for longfile attribute.");
}

public void tearDown() { public void tearDown() {
executeTarget("cleanup"); executeTarget("cleanup");
} }


Loading…
Cancel
Save