Browse Source

Finally add the new <basename> and <dirname> 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
master
Diane Holt 23 years ago
parent
commit
cf83aba68a
9 changed files with 622 additions and 0 deletions
  1. +69
    -0
      docs/manual/CoreTasks/basename.html
  2. +54
    -0
      docs/manual/CoreTasks/dirname.html
  3. +25
    -0
      src/etc/testcases/taskdefs/basename.xml
  4. +25
    -0
      src/etc/testcases/taskdefs/dirname.xml
  5. +129
    -0
      src/main/org/apache/tools/ant/taskdefs/Basename.java
  6. +111
    -0
      src/main/org/apache/tools/ant/taskdefs/Dirname.java
  7. +2
    -0
      src/main/org/apache/tools/ant/taskdefs/defaults.properties
  8. +103
    -0
      src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java
  9. +104
    -0
      src/testcases/org/apache/tools/ant/taskdefs/DirnameTest.java

+ 69
- 0
docs/manual/CoreTasks/basename.html View File

@@ -0,0 +1,69 @@
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Basename Task</title>
</head>

<body>

<h2><a name="echo">Basename</a></h2>
<h3>Description</h3>
<p>
Task to determine the basename of a specified file, optionally minus a
specified suffix.
</p>
<p>
When this task executes, it will set the specified property to the
value of the last path element of the specified file. If <code>file</code> is a
directory, the basename will be the last directory element. If
<code>file</code> is a full-path, relative-path, or simple filename,
the basename will be the simple file name, without any directory elements.
</p>
<p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">file</td>
<td valign="top">The path to take the basename of.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">property</td>
<td valign="top">The name of the property to set.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">suffix</td>
<td valign="top">The suffix to remove from the resulting basename
(specified either with or with the &quot;<code>.</code>&quot;).</td>
<td valign="top" align="center">No</td>
</tr>
</table>

<h3>Examples</h3>
<pre> &lt;basename property=&quot;jar.filename&quot; file=&quot;${lib.jarfile}&quot;/&gt;</pre>
will set <code>jar.filename</code> to
<code>myjar.jar</code>, if <code>lib.jarfile</code> is defined as either a
full-path filename (eg., <code>/usr/local/lib/myjar.jar</code>),
a relative-path filename (eg., <code>lib/myjar.jar</code>),
or a simple filename (eg., <code>myjar.jar</code>).
<pre> &lt;basename property=&quot;cmdname&quot; file=&quot;D:/usr/local/foo.exe&quot; suffix=&quot;.exe&quot;/&gt;</pre>
will set <code>cmdname</code> to <code>foo</code>.
<pre> &lt;basename property=&quot;temp.dirname&quot; file=&quot;${env.TEMP}&quot;/&gt;</pre>

will set <code>temp.dirname</code> to the last directory element of
the path defined for the <code>TEMP</code> environment variable.</p>

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

</body>
</html>


+ 54
- 0
docs/manual/CoreTasks/dirname.html View File

@@ -0,0 +1,54 @@
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Dirname Task</title>
</head>

<body>

<h2><a name="echo">Dirname</a></h2>
<h3>Description</h3>
<p>
Task to determine the directory path of a specified file.
</p>
<p>
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.
</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">file</td>
<td valign="top">The path to take the basename of.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">property</td>
<td valign="top">The name of the property to set.</td>
<td valign="top" align="center">Yes</td>
</tr>
</table>

<h3>Examples</h3>
<pre> &lt;dirname property=&quot;antfile.dir&quot; file=&quot;${ant.file}&quot;/&gt;</pre>
will set <code>antfile.dir</code> to the directory path for
<code>${ant.file}</code>.
<pre> &lt;dirname property=&quot;foo.dirname&quot; file=&quot;foo.txt&quot;</pre>
will set <code>foo.dirname</code> to the current directory.</p>

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

</body>
</html>


+ 25
- 0
src/etc/testcases/taskdefs/basename.xml View File

@@ -0,0 +1,25 @@
<?xml version="1.0"?>

<project name="xxx-test" basedir="." default="test1">

<target name="test1">
<basename/>
</target>

<target name="test2">
<basename property="propname"/>
</target>

<target name="test3">
<basename file="filename"/>
</target>

<target name="test4">
<basename property="file.w.suf" file="${user.dir}/foo.txt"/>
</target>

<target name="test5">
<basename property="file.wo.suf" file="foo.txt" suffix="txt"/>
</target>

</project>

+ 25
- 0
src/etc/testcases/taskdefs/dirname.xml View File

@@ -0,0 +1,25 @@
<?xml version="1.0"?>

<project name="xxx-test" basedir="." default="test1">

<target name="test1">
<dirname/>
</target>

<target name="test2">
<dirname property="propname"/>
</target>

<target name="test3">
<dirname file="filename"/>
</target>

<target name="test4">
<dirname property="local.dir" file="/usr/local/foo.txt"/>
</target>

<target name="test5">
<dirname property="base.dir" file="foo.txt"/>
</target>

</project>

+ 129
- 0
src/main/org/apache/tools/ant/taskdefs/Basename.java View File

@@ -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
* <http://www.apache.org/>.
*/

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:
* <ul>
* <li>file
* <li>property
* <li>suffix
* </ul>
* The <b>file</b> and <b>property</b> attributes are required. The
* <b>suffix</b> attribute can be specified either with or without
* the &quot;.&quot;, and the result will be the same (ie., the
* returned file name will be minus the .suffix).
* <p>
* 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 <a href="mailto:holtdl@apache.org">holtdl@apache.org</a>
*
* @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);
}
}
}


+ 111
- 0
src/main/org/apache/tools/ant/taskdefs/Dirname.java View File

@@ -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
* <http://www.apache.org/>.
*/

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:
* <ul>
* <li>file
* <li>property
* </ul>
* Both <b>file</b> and <b>property</b> are required.
* <p>
* 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 <a href="mailto:holtdl@apache.org">holtdl@apache.org</a>
*
* @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);
}
}
}


+ 2
- 0
src/main/org/apache/tools/ant/taskdefs/defaults.properties View File

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


+ 103
- 0
src/testcases/org/apache/tools/ant/taskdefs/BasenameTest.java View File

@@ -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
* <http://www.apache.org/>.
*/

package org.apache.tools.ant.taskdefs;

import java.io.File;
import org.apache.tools.ant.BuildFileTest;

/**
* @author Diane Holt <holtdl@apache.org>
*/
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");
}
}
}

+ 104
- 0
src/testcases/org/apache/tools/ant/taskdefs/DirnameTest.java View File

@@ -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
* <http://www.apache.org/>.
*/

package org.apache.tools.ant.taskdefs;

import java.io.File;
import org.apache.tools.ant.BuildFileTest;

/**
* @author Diane Holt <holtdl@apache.org>
*/
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");
}
}
}

Loading…
Cancel
Save