Browse Source

- Fixed the search for a file or directory using the <filepath> element

(changes are in checkFile(), in case anyone wants to review this
   and make sure I did it right).
 - Added a BuildException for when the "type" attribute is used with
   anything other than the "file" attribute.
 - Changed the "Searching..." output to DEBUG level, since that
   seemed more appropriate, and added "Found..." output for the
   VERBOSE level.
PR:
Obtained from:
Submitted by:
Reviewed by:


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269913 13f79535-47bb-0310-9956-ffa450edef68
master
Diane Holt 23 years ago
parent
commit
cad841b38a
1 changed files with 37 additions and 4 deletions
  1. +37
    -4
      src/main/org/apache/tools/ant/taskdefs/Available.java

+ 37
- 4
src/main/org/apache/tools/ant/taskdefs/Available.java View File

@@ -150,6 +150,9 @@ public class Available extends Task implements Condition {
}

if (type != null){
if (file == null){
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");
}
@@ -166,7 +169,11 @@ public class Available extends Task implements Condition {
}
if ((file != null) && !checkFile()) {
log("Unable to find " + file + " to set property " + property, Project.MSG_VERBOSE);
if (type != null) {
log("Unable to find " + type + " " + file.getName() + " to set property " + property, Project.MSG_VERBOSE);
} else {
log("Unable to find " + file.getName() + " to set property " + property, Project.MSG_VERBOSE);
}
return false;
}
@@ -188,9 +195,32 @@ public class Available extends Task implements Condition {
} else {
String[] paths = filepath.list();
for(int i = 0; i < paths.length; ++i) {
log("Searching " + paths[i], Project.MSG_VERBOSE);
if(new File(paths[i], file.getName()).isFile()) {
return true;
log("Searching " + paths[i], Project.MSG_DEBUG);
File filename = new File(paths[i]);
if (type != null) {
if (type.equalsIgnoreCase("dir")) {
String dir = filename.getParent();
if(dir != null) {
int index = dir.lastIndexOf(File.separator);
String dirname = dir.substring(index + 1);
if(dirname.equals(file.getName())) {
log("Found directory: " + dir, Project.MSG_VERBOSE);
return true;
}
}
} else if (type.equalsIgnoreCase("file")) {
if(filename.isFile()) {
if(filename.getName().equals(file.getName())) {
log("Found file: " + filename, Project.MSG_VERBOSE);
return true;
}
}
}
} else if(filename.isFile()) {
if(filename.getName().equals(file.getName())) {
log("Found file: " + filename, Project.MSG_VERBOSE);
return true;
}
}
}
}
@@ -200,11 +230,14 @@ public class Available extends Task implements Condition {
private boolean checkFile(File file) {
if (type != null) {
if (type.equalsIgnoreCase("dir")) {
log("Found directory: " + file, Project.MSG_VERBOSE);
return file.isDirectory();
} else if (type.equalsIgnoreCase("file")) {
log("Found file: " + file, Project.MSG_VERBOSE);
return file.isFile();
}
}
log("Found: " + file, Project.MSG_VERBOSE);
return file.exists();
}



Loading…
Cancel
Save