From cf83aba68a0a114701551e6f603c62079d8252da Mon Sep 17 00:00:00 2001 From: Diane Holt Date: Wed, 20 Mar 2002 02:48:15 +0000 Subject: [PATCH] Finally add the new and tasks. (yay!) PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271908 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/basename.html | 69 ++++++++++ docs/manual/CoreTasks/dirname.html | 54 ++++++++ src/etc/testcases/taskdefs/basename.xml | 25 ++++ src/etc/testcases/taskdefs/dirname.xml | 25 ++++ .../apache/tools/ant/taskdefs/Basename.java | 129 ++++++++++++++++++ .../apache/tools/ant/taskdefs/Dirname.java | 111 +++++++++++++++ .../tools/ant/taskdefs/defaults.properties | 2 + .../tools/ant/taskdefs/BasenameTest.java | 103 ++++++++++++++ .../tools/ant/taskdefs/DirnameTest.java | 104 ++++++++++++++ 9 files changed, 622 insertions(+) create mode 100644 docs/manual/CoreTasks/basename.html create mode 100644 docs/manual/CoreTasks/dirname.html create mode 100644 src/etc/testcases/taskdefs/basename.xml create mode 100644 src/etc/testcases/taskdefs/dirname.xml create mode 100644 src/main/org/apache/tools/ant/taskdefs/Basename.java create mode 100644 src/main/org/apache/tools/ant/taskdefs/Dirname.java create mode 100644 src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java create mode 100644 src/testcases/org/apache/tools/ant/taskdefs/DirnameTest.java diff --git a/docs/manual/CoreTasks/basename.html b/docs/manual/CoreTasks/basename.html new file mode 100644 index 000000000..c1181d758 --- /dev/null +++ b/docs/manual/CoreTasks/basename.html @@ -0,0 +1,69 @@ + + + + +Basename Task + + + + +

Basename

+

Description

+

+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 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

+ + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
fileThe path to take the basename of.Yes
propertyThe name of the property to set.Yes
suffixThe 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 @@ + + + + +Dirname Task + + + + +

Dirname

+

Description

+

+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. +

+

Parameters

+ + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
fileThe path to take the basename of.Yes
propertyThe name of the property to set.Yes
+ +

Examples

+
  <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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/taskdefs/dirname.xml b/src/etc/testcases/taskdefs/dirname.xml new file mode 100644 index 000000000..6f018da8a --- /dev/null +++ b/src/etc/testcases/taskdefs/dirname.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Basename.java b/src/main/org/apache/tools/ant/taskdefs/Basename.java new file mode 100644 index 000000000..6655af9d1 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/Basename.java @@ -0,0 +1,129 @@ +/* + * 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 + * . + */ + +package org.apache.tools.ant.taskdefs; + +import java.io.File; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; + +/** + * Task to determine the basename of a specified file, optionally minus a + * specified suffix. + * + * This task can accept the following attributes: + *
    + *
  • file + *
  • property + *
  • suffix + *
+ * The file and property attributes are required. The + * suffix attribute can be specified either with or without + * the ".", and the result will be the same (ie., the + * returned file name will be minus the .suffix). + *

+ * 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 + * . + */ + +package org.apache.tools.ant.taskdefs; + +import java.io.File; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; + +/** + * Task to determine the directory name of the specified file. + * + * This task can accept the following attributes: + *

    + *
  • file + *
  • property + *
+ * Both file and property are required. + *

+ * 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 + * . + */ + +package org.apache.tools.ant.taskdefs; + +import java.io.File; +import org.apache.tools.ant.BuildFileTest; + +/** + * @author Diane Holt + */ +public class BasenameTest extends BuildFileTest { + + public BasenameTest(String name) { + super(name); + } + + public void setUp() { + configureProject("src/etc/testcases/taskdefs/basename.xml"); + } + + public void test1() { + expectBuildException("test1", "required attribute missing"); + } + + public void test2() { + expectBuildException("test2", "required attribute missing"); + } + + public void test3() { + expectBuildException("test3", "required attribute missing"); + } + + public void test4() { + executeTarget("test4"); + String expected = "foo.txt"; + String checkprop = project.getProperty("file.w.suf"); + if (!checkprop.equals(expected)) { + fail("basename failed"); + } + } + + public void test5() { + executeTarget("test5"); + String expected = "foo"; + String checkprop = project.getProperty("file.wo.suf"); + if (!checkprop.equals(expected)) { + fail("basename failed"); + } + } + +} diff --git a/src/testcases/org/apache/tools/ant/taskdefs/DirnameTest.java b/src/testcases/org/apache/tools/ant/taskdefs/DirnameTest.java new file mode 100644 index 000000000..242b4d4a5 --- /dev/null +++ b/src/testcases/org/apache/tools/ant/taskdefs/DirnameTest.java @@ -0,0 +1,104 @@ +/* + * 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 + * . + */ + +package org.apache.tools.ant.taskdefs; + +import java.io.File; +import org.apache.tools.ant.BuildFileTest; + +/** + * @author Diane Holt + */ +public class DirnameTest extends BuildFileTest { + + public DirnameTest(String name) { + super(name); + } + + public void setUp() { + configureProject("src/etc/testcases/taskdefs/dirname.xml"); + } + + public void test1() { + expectBuildException("test1", "required attribute missing"); + } + + public void test2() { + expectBuildException("test2", "required attribute missing"); + } + + public void test3() { + expectBuildException("test3", "required attribute missing"); + } + + public void test4() { + executeTarget("test4"); + String filesep = System.getProperty("file.separator"); + String expected = filesep + "usr" + filesep + "local"; + String checkprop = project.getProperty("local.dir"); + if (!checkprop.equals(expected)) { + fail("dirname failed"); + } + } + + public void test5() { + executeTarget("test5"); + String expected = project.getProperty("basedir"); + String checkprop = project.getProperty("base.dir"); + if (!checkprop.equals(expected)) { + fail("dirname failed"); + } + } + +}