Browse Source

Added Thomas Haas Path class.

I've added some minor modifications to his class

* Added a String constructor to make it usable as an attribute

* renamed the nested element from element to pathelement

* Used Conor's PathTokenizer to split the path into parts.

The Java task has been modified to use this class both for the
classpath attribute as well as for an nested classpath element -
documentation will follow.

See the runtests target in build.xml for an example of its usage.

Submitted by:	Thomas Haas <thomas.haas@softwired-inc.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267778 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 25 years ago
parent
commit
aa7f2d4046
6 changed files with 415 additions and 11 deletions
  1. +9
    -3
      build.xml
  2. +12
    -4
      src/main/org/apache/tools/ant/IntrospectionHelper.java
  3. +203
    -0
      src/main/org/apache/tools/ant/Path.java
  4. +18
    -4
      src/main/org/apache/tools/ant/taskdefs/Java.java
  5. +1
    -0
      src/testcases/org/apache/tools/ant/AllJUnitTests.java
  6. +172
    -0
      src/testcases/org/apache/tools/ant/PathTest.java

+ 9
- 3
build.xml View File

@@ -254,6 +254,7 @@
<exclude name="**/AllJUnitTests.java" unless="junit.present" />
<exclude name="**/EnumeratedAttributeTest.java" unless="junit.present" />
<exclude name="**/IntrospectionHelperTest.java" unless="junit.present" />
<exclude name="**/PathTest.java" unless="junit.present" />
</javac>
</target>

@@ -264,9 +265,14 @@
<java fork="yes"
classname="junit.textui.TestRunner"
args="org.apache.tools.ant.AllJUnitTests"
taskname="junit"
classpath="${lib.dir}/${name}.jar:${build.tests}:${classpath}:${java.class.path}"
/>
taskname="junit">
<classpath>
<pathelement location="${lib.dir}/${name}.jar" />
<pathelement location="${build.tests}" />
<pathelement path="${classpath}" />
<pathelement path="${java.class.path}" />
</classpath>
</java>
</target>

</project>


+ 12
- 4
src/main/org/apache/tools/ant/IntrospectionHelper.java View File

@@ -116,10 +116,18 @@ public class IntrospectionHelper {
Class returnType = m.getReturnType();
Class[] args = m.getParameterTypes();

// not really user settable properties
if ("setLocation".equals(name) ||
"setDescription".equals(name) ||
"setTaskType".equals(name)) {
// not really user settable properties on tasks
if (org.apache.tools.ant.Task.class.isAssignableFrom(bean)
&& args.length == 1 &&
(
(
"setLocation".equals(name) && org.apache.tools.ant.Location.class.equals(args[0])
) || (
"setDescription".equals(name) && java.lang.String.class.equals(args[0])
) || (
"setTaskType".equals(name) && java.lang.String.class.equals(args[0])
)
)) {
continue;
}


+ 203
- 0
src/main/org/apache/tools/ant/Path.java View File

@@ -0,0 +1,203 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 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", "Tomcat", 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;

import java.io.File;
import java.util.Vector;
import java.util.StringTokenizer;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

/**
* This object represents a path as used by CLASSPATH or PATH
* environment variable.
*
* <code>
* &lt;sometask&gt;<br>
* &nbsp;&nbsp;&lt;somepath&gt;
* &nbsp;&nbsp;&nbsp;&nbsp;&lt;pathelement location="/path/to/file.jar" /&gt;
* &nbsp;&nbsp;&nbsp;&nbsp;&lt;pathelement path="/path/to/file2.jar:/path/to/class2;/path/to/class3" /&gt;
* &nbsp;&nbsp;&nbsp;&nbsp;&lt;pathelement location="/path/to/file3.jar" /&gt;
* &nbsp;&nbsp;&nbsp;&nbsp;&lt;pathelement location="/path/to/file4.jar" /&gt;
* &nbsp;&nbsp;&lt;/somepath&gt;
* &lt;/sometask&gt;<br>
* </code>
*
* The object implemention <code>sometask</code> must provide a method called
* <code>createSomepath</code> which returns an instance of <code>Path</code>.
* Nested path definitions are handled by the Path object and must be labeled
* <code>pathelement</code>.<p>
*
* The path element takes a parameter <code>path</code> which will be parsed
* and split into single elements. It will usually be used
* to define a path from an environment variable.
*
* @author Thomas.Haas@softwired-inc.com
*/

public class Path {

private Vector definition;

public Path(String path) {
this();
setPath(path);
}

public Path() {
definition = new Vector();
}

/**
* Adds a element definition to the path.
* @param location the location of the element to add (must not be
* <code>null</code> nor empty.
*/
public void setLocation(String location) {
if (location != null && location.length() > 0) {
definition.addElement(translateFile(location));
}
}


/**
* Parses a path definition and creates single PathElements.
* @param path the path definition.
*/
public void setPath(String path) {
final Vector elements = translatePath(path);
for (int i=0; i < elements.size(); i++) {
definition.addElement(elements.elementAt(i));
}
}


public Path createPathElement() {
return this;
}


/**
* Returns all path elements defined by this and netsed path objects.
* @return list of path elements.
*/
public String[] list() {
final String[] result = new String[definition.size()];
definition.copyInto(result);
return result;
}


/**
* Returns a textual representation of the path, which can be used as
* CLASSPATH or PATH environment variable definition.
* @return a textual representation of the path.
*/
public String toString() {
final String[] list = list();

// empty path return empty string
if (list.length == 0) return "";

// path containing one or more elements
final StringBuffer result = new StringBuffer(list[0].toString());
for (int i=1; i < list.length; i++) {
result.append(File.pathSeparatorChar);
result.append(list[i]);
}

return result.toString();
}



public static Vector translatePath(String source) {
final Vector result = new Vector();
if (source == null) return result;

PathTokenizer tok = new PathTokenizer(source);
StringBuffer element = new StringBuffer();
while (tok.hasMoreTokens()) {
element.setLength(0);
element.append(tok.nextToken());
for (int i=0; i<element.length(); i++) {
translateFileSep(element, i);
}
result.addElement(element.toString());
}
return result;
}


public static String translateFile(String source) {
if (source == null) return "";

final StringBuffer result = new StringBuffer(source);
for (int i=0; i < result.length(); i++) {
translateFileSep(result, i);
}

return result.toString();
}


protected static boolean translateFileSep(StringBuffer buffer, int pos) {
if (buffer.charAt(pos) == '/' || buffer.charAt(pos) == '\\') {
buffer.setCharAt(pos, File.separatorChar);
return true;
}
return false;
}
}

+ 18
- 4
src/main/org/apache/tools/ant/taskdefs/Java.java View File

@@ -69,7 +69,7 @@ public class Java extends Exec {
private String classname = null;
private String args = null;
private String jvmargs = null;
private String classpath = null;
private Path classpath = null;
private boolean fork = false;
/**
@@ -97,7 +97,7 @@ public class Java extends Exec {
b.append("java ");
if (classpath != null) {
b.append("-classpath ");
b.append(classpath);
b.append(classpath.toString());
b.append(" ");
}
if (jvmargs != null) {
@@ -126,10 +126,24 @@ public class Java extends Exec {
/**
* Set the classpath to be used for this compilation.
*/
public void setClasspath(String s) {
this.classpath = project.translatePath(s);
public void setClasspath(Path s) {
if (this.classpath == null) {
this.classpath = s;
} else {
this.classpath.setPath(s.toString());
}
}
/**
* Creates a nested classpath element
*/
public Path createClasspath() {
if (classpath == null) {
classpath = new Path();
}
return classpath;
}

/**
* Set the source file (deprecated).
*/


+ 1
- 0
src/testcases/org/apache/tools/ant/AllJUnitTests.java View File

@@ -72,6 +72,7 @@ public class AllJUnitTests extends TestCase {
public static Test suite() {
TestSuite suite = new TestSuite(IntrospectionHelperTest.class);
suite.addTest(new TestSuite(EnumeratedAttributeTest.class));
suite.addTest(new TestSuite(PathTest.class));
return suite;
}
}

+ 172
- 0
src/testcases/org/apache/tools/ant/PathTest.java View File

@@ -0,0 +1,172 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 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", "Tomcat", 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;

import junit.framework.TestCase;
import junit.framework.AssertionFailedError;

import java.io.File;

/**
* JUnit 3 testcases for org.apache.tools.ant.Path
*
* @author Stefan Bodewig <a href="mailto:stefan.bodewig@megabit.net">stefan.bodewig@megabit.net</a>
*/

public class PathTest extends TestCase {

public static boolean isUnixStyle = File.pathSeparatorChar == ':';

public PathTest(String name) {
super(name);
}

// actually tests constructor as well as setPath
public void testConstructor() {
Path p = new Path("/a:/b");
String[] l = p.list();
assertEquals("two items, Unix style", 2, l.length);
if (isUnixStyle) {
assertEquals("/a", l[0]);
assertEquals("/b", l[1]);
} else {
assertEquals("\\a", l[0]);
assertEquals("\\b", l[1]);
}

p = new Path("\\a;\\b");
l = p.list();
assertEquals("two items, DOS style", 2, l.length);
if (isUnixStyle) {
assertEquals("/a", l[0]);
assertEquals("/b", l[1]);
} else {
assertEquals("\\a", l[0]);
assertEquals("\\b", l[1]);
}

p = new Path("\\a;\\b:/c");
l = p.list();
assertEquals("three items, mixed style", 3, l.length);
if (isUnixStyle) {
assertEquals("/a", l[0]);
assertEquals("/b", l[1]);
assertEquals("/c", l[2]);
} else {
assertEquals("\\a", l[0]);
assertEquals("\\b", l[1]);
assertEquals("\\c", l[2]);
}

p = new Path("c:\\test");
l = p.list();
if (isUnixStyle) {
assertEquals("no drives on Unix", 2, l.length);
assertEquals("c", l[0]);
assertEquals("/test", l[1]);
} else {
assertEquals("drives on DOS", 1, l.length);
assertEquals("c:\\test", l[0]);
}

p = new Path("c:/test");
l = p.list();
if (isUnixStyle) {
assertEquals("no drives on Unix", 2, l.length);
assertEquals("c", l[0]);
assertEquals("/test", l[1]);
} else {
assertEquals("drives on DOS", 1, l.length);
assertEquals("c:\\test", l[0]);
}
}

public void testSetLocation() {
Path p = new Path();
p.setLocation("/a");
String[] l = p.list();
if (isUnixStyle) {
assertEquals(1, l.length);
assertEquals("/a", l[0]);
} else {
assertEquals(1, l.length);
assertEquals("\\a", l[0]);
}

p = new Path();
p.setLocation("\\a");
l = p.list();
if (isUnixStyle) {
assertEquals(1, l.length);
assertEquals("/a", l[0]);
} else {
assertEquals(1, l.length);
assertEquals("\\a", l[0]);
}
}

public void testAppending() {
Path p = new Path("/a:/b");
String[] l = p.list();
assertEquals("2 after construction", 2, l.length);
p.setLocation("/c");
l = p.list();
assertEquals("3 after setLocation", 3, l.length);
p.setPath("\\d;\\e");
l = p.list();
assertEquals("5 after setPath", 5, l.length);
}

}

Loading…
Cancel
Save