From cf83aba68a0a114701551e6f603c62079d8252da Mon Sep 17 00:00:00 2001
From: Diane Holt
+Task to determine the basename of a specified file, optionally minus a
+specified suffix.
+
+When this task executes, it will set the specified property to the
+value of the last path element of the specified file. If
+Basename
+Description
+file
is a
+directory, the basename will be the last directory element. If
+file
is a full-path, relative-path, or simple filename,
+the basename will be the simple file name, without any directory elements.
+Parameters
+
+
+
+
+
+ Attribute
+ Description
+ Required
+
+
+ file
+ The path to take the basename of.
+ Yes
+
+
+ property
+ The name of the property to set.
+ Yes
+
+
+suffix
+ The suffix to remove from the resulting basename
+ (specified either with or with the "
+ .
").No
+ Examples
+ <basename property="jar.filename" file="${lib.jarfile}"/>
+will set jar.filename
to
+myjar.jar
, if lib.jarfile
is defined as either a
+full-path filename (eg., /usr/local/lib/myjar.jar
),
+a relative-path filename (eg., lib/myjar.jar
),
+or a simple filename (eg., myjar.jar
).
+ <basename property="cmdname" file="D:/usr/local/foo.exe" suffix=".exe"/>
+will set cmdname
to foo
.
+ <basename property="temp.dirname" file="${env.TEMP}"/>
+
+will set temp.dirname
to the last directory element of
+the path defined for the TEMP
environment variable.
Copyright © 2002 Apache Software Foundation. +All rights Reserved.
+ + + + diff --git a/docs/manual/CoreTasks/dirname.html b/docs/manual/CoreTasks/dirname.html new file mode 100644 index 000000000..e4a4f53fb --- /dev/null +++ b/docs/manual/CoreTasks/dirname.html @@ -0,0 +1,54 @@ + + + + ++Task to determine the directory path of a specified file. +
++When this task executes, it will set the specified property to the +value of the specified file up to, but not including, the last path +element. If the specified file is a path that ends in a filename, +the filename will be dropped. If the specified file is just a filename, +the directory will be the current directory. +
+Attribute | +Description | +Required | +
file | +The path to take the basename of. | +Yes | +
property | +The name of the property to set. | +Yes | +
<dirname property="antfile.dir" file="${ant.file}"/>+will set
antfile.dir
to the directory path for
+${ant.file}
.
+<dirname property="foo.dirname" file="foo.txt"+will set
foo.dirname
to the current directory.
+
+Copyright © 2002 Apache Software Foundation. +All rights Reserved.
+ + + + diff --git a/src/etc/testcases/taskdefs/basename.xml b/src/etc/testcases/taskdefs/basename.xml new file mode 100644 index 000000000..143819758 --- /dev/null +++ b/src/etc/testcases/taskdefs/basename.xml @@ -0,0 +1,25 @@ + + +
+ * When this task executes, it will set the specified property to the
+ * value of the last element in the specified file. If file is a
+ * directory, the basename will be the last directory element. If file
+ * is a full-path filename, the basename will be the simple file name.
+ * If a suffix is specified, and the specified file ends in that suffix,
+ * the basename will be the simple file name without the suffix.
+ *
+ * @author Diane Holt holtdl@apache.org
+ *
+ * @version $Revision$
+ *
+ * @ant.task category="property"
+ */
+
+public class Basename extends Task {
+ private File file;
+ private String property;
+ private String suffix;
+
+ // The setter for the `file' attribute
+ public void setFile(File file) {
+ this.file = file;
+ }
+
+ // The setter for the `property' attribute
+ public void setProperty(String property) {
+ this.property = property ;
+ }
+
+ // The setter for the `suffix' attribute
+ public void setSuffix(String suffix) {
+ this.suffix = suffix;
+ }
+
+
+ // The method executing the task
+ public void execute() throws BuildException {
+ String value;
+ if (property == null) {
+ throw new BuildException("property attribute required", location);
+ }
+ if (file == null) {
+ throw new BuildException("file attribute required", location);
+ } else {
+ value = file.getName();
+ if (suffix != null && value.endsWith(suffix)) {
+ int pos = value.indexOf('.');
+ value = value.substring(0, pos);
+ }
+ this.project.setProperty(property, value);
+ }
+ }
+}
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/Dirname.java b/src/main/org/apache/tools/ant/taskdefs/Dirname.java
new file mode 100644
index 000000000..c6559b7a2
--- /dev/null
+++ b/src/main/org/apache/tools/ant/taskdefs/Dirname.java
@@ -0,0 +1,111 @@
+/*
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2002 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", "Ant", 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
+ *
+ * When this task executes, it will set the specified property to the
+ * value of the specified file up to, but not including, the last path
+ * element. If file is a file, the directory will be the current
+ * directory.
+ *
+ * @author Diane Holt holtdl@apache.org
+ *
+ * @version $Revision$
+ *
+ * @ant.task category="property"
+ */
+
+public class Dirname extends Task {
+ private File file;
+ private String property;
+
+ // The setter for the `file' attribute
+ public void setFile(File file) {
+ this.file = file;
+ }
+
+ // The setter for the `property' attribute
+ public void setProperty(String property) {
+ this.property = property ;
+ }
+
+
+ // The method executing the task
+ public void execute() throws BuildException {
+ if (property == null) {
+ throw new BuildException("property attribute required", location);
+ }
+ if (file == null) {
+ throw new BuildException("file attribute required", location);
+ } else {
+ String value = file.getParent();
+ this.project.setProperty(property, value);
+ }
+ }
+}
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties
index e777055a5..157676153 100644
--- a/src/main/org/apache/tools/ant/taskdefs/defaults.properties
+++ b/src/main/org/apache/tools/ant/taskdefs/defaults.properties
@@ -61,6 +61,8 @@ input=org.apache.tools.ant.taskdefs.Input
loadfile=org.apache.tools.ant.taskdefs.LoadFile
manifest=org.apache.tools.ant.taskdefs.Manifest
loadproperties=org.apache.tools.ant.taskdefs.LoadProperties
+basename=org.apache.tools.ant.taskdefs.Basename
+dirname=org.apache.tools.ant.taskdefs.Dirname
# optional tasks
script=org.apache.tools.ant.taskdefs.optional.Script
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java b/src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java
new file mode 100644
index 000000000..00c0218fc
--- /dev/null
+++ b/src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java
@@ -0,0 +1,103 @@
+/*
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2000-2001 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", "Ant", 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
+ *