From aa7f2d4046722f4ed1ea63464f0573e4accb418d Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 13 Jul 2000 16:35:08 +0000 Subject: [PATCH] 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 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267778 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 12 +- .../apache/tools/ant/IntrospectionHelper.java | 16 +- src/main/org/apache/tools/ant/Path.java | 203 ++++++++++++++++++ .../org/apache/tools/ant/taskdefs/Java.java | 22 +- .../org/apache/tools/ant/AllJUnitTests.java | 1 + .../org/apache/tools/ant/PathTest.java | 172 +++++++++++++++ 6 files changed, 415 insertions(+), 11 deletions(-) create mode 100644 src/main/org/apache/tools/ant/Path.java create mode 100644 src/testcases/org/apache/tools/ant/PathTest.java diff --git a/build.xml b/build.xml index 9c25c987c..86b72b254 100644 --- a/build.xml +++ b/build.xml @@ -254,6 +254,7 @@ + @@ -264,9 +265,14 @@ + taskname="junit"> + + + + + + + diff --git a/src/main/org/apache/tools/ant/IntrospectionHelper.java b/src/main/org/apache/tools/ant/IntrospectionHelper.java index ed1227737..3b1efc12f 100644 --- a/src/main/org/apache/tools/ant/IntrospectionHelper.java +++ b/src/main/org/apache/tools/ant/IntrospectionHelper.java @@ -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; } diff --git a/src/main/org/apache/tools/ant/Path.java b/src/main/org/apache/tools/ant/Path.java new file mode 100644 index 000000000..519194ad8 --- /dev/null +++ b/src/main/org/apache/tools/ant/Path.java @@ -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 + * . + */ + +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. + * + * + * <sometask>
+ *   <somepath> + *     <pathelement location="/path/to/file.jar" /> + *     <pathelement path="/path/to/file2.jar:/path/to/class2;/path/to/class3" /> + *     <pathelement location="/path/to/file3.jar" /> + *     <pathelement location="/path/to/file4.jar" /> + *   </somepath> + * </sometask>
+ *
+ * + * The object implemention sometask must provide a method called + * createSomepath which returns an instance of Path. + * Nested path definitions are handled by the Path object and must be labeled + * pathelement.

+ * + * The path element takes a parameter path 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 + * null 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. + */ + +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 stefan.bodewig@megabit.net + */ + +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); + } + +}