FileDocCategorySizeDatePackage
Path.javaAPI DocApache Ant 1.7024539Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.types

Path

public class Path extends DataType implements Cloneable, ResourceCollection
This object represents a path as used by CLASSPATH or PATH environment variable. A path might also be described as a collection of unique filesystem resources.

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

Fields Summary
public static Path
systemClasspath
The system classpath as a Path object
public static Path
systemBootClasspath
The system bootclasspath as a Path object.
private static final Iterator
EMPTY_ITERATOR
private org.apache.tools.ant.types.resources.Union
union
Constructors Summary
public Path(org.apache.tools.ant.Project p, String path)
Invoked by IntrospectionHelper for setXXX(Path p) attribute setters.

param
p the Project for this path.
param
path the String path definition.


                              
         
        this(p);
        createPathElement().setPath(path);
    
public Path(org.apache.tools.ant.Project project)
Construct an empty Path.

param
project the Project for this path.

        setProject(project);
    
Methods Summary
public voidadd(org.apache.tools.ant.types.Path path)
Adds a nested path

param
path a Path to be added to the path
throws
BuildException on error
since
Ant 1.6

        if (path == this) {
            throw circularReference();
        }
        if (path.getProject() == null) {
            path.setProject(getProject());
        }
        add((ResourceCollection) path);
    
public voidadd(ResourceCollection c)
Add a nested ResourceCollection.

param
c the ResourceCollection to add.
since
Ant 1.7

        checkChildrenAllowed();
        if (c == null) {
            return;
        }
        if (union == null) {
            union = new Union();
            union.setProject(getProject());
            union.setCache(false);
        }
        union.add(c);
        setChecked(false);
    
public voidaddDirset(DirSet dset)
Adds a nested <dirset> element.

param
dset a DirSet to be added to the path
throws
BuildException on error

        if (dset.getProject() == null) {
            dset.setProject(getProject());
        }
        add(dset);
    
public voidaddExisting(org.apache.tools.ant.types.Path source)
Adds the components on the given path which exist to this Path. Components that don't exist aren't added.

param
source - source path whose components are examined for existence

         addExisting(source, false);
     
public voidaddExisting(org.apache.tools.ant.types.Path source, boolean tryUserDir)
Same as addExisting, but support classpath behavior if tryUserDir is true. Classpaths are relative to user dir, not the project base. That used to break jspc test

param
source the source path
param
tryUserDir if true try the user directory if the file is not present

        String[] list = source.list();
        File userDir = (tryUserDir) ? new File(System.getProperty("user.dir"))
                : null;

        for (int i = 0; i < list.length; i++) {
            File f = resolveFile(getProject(), list[i]);

            // probably not the best choice, but it solves the problem of
            // relative paths in CLASSPATH
            if (tryUserDir && !f.exists()) {
                f = new File(userDir, list[i]);
            }
            if (f.exists()) {
                setLocation(f);
            } else {
                log("dropping " + f + " from path as it doesn't exist",
                    Project.MSG_VERBOSE);
            }
        }
    
public voidaddExtdirs(org.apache.tools.ant.types.Path extdirs)
Emulation of extdirs feature in java >= 1.2. This method adds all files in the given directories (but not in sub-directories!) to the classpath, so that you don't have to specify them all one by one.

param
extdirs - Path to append files to

        if (extdirs == null) {
            String extProp = System.getProperty("java.ext.dirs");
            if (extProp != null) {
                extdirs = new Path(getProject(), extProp);
            } else {
                return;
            }
        }

        String[] dirs = extdirs.list();
        for (int i = 0; i < dirs.length; i++) {
            File dir = resolveFile(getProject(), dirs[i]);
            if (dir.exists() && dir.isDirectory()) {
                FileSet fs = new FileSet();
                fs.setDir(dir);
                fs.setIncludes("*");
                addFileset(fs);
            }
        }
    
public voidaddFilelist(FileList fl)
Adds a nested <filelist> element.

param
fl a FileList to be added to the path
throws
BuildException on error

        if (fl.getProject() == null) {
            fl.setProject(getProject());
        }
        add(fl);
    
public voidaddFileset(FileSet fs)
Adds a nested <fileset> element.

param
fs a FileSet to be added to the path
throws
BuildException on error

        if (fs.getProject() == null) {
            fs.setProject(getProject());
        }
        add(fs);
    
public voidaddJavaRuntime()
Add the Java Runtime classes to this Path instance.

        if (JavaEnvUtils.isKaffe()) {
            // newer versions of Kaffe (1.1.1+) won't have this,
            // but this will be sorted by FileSet anyway.
            File kaffeShare = new File(System.getProperty("java.home")
                                       + File.separator + "share"
                                       + File.separator + "kaffe");
            if (kaffeShare.isDirectory()) {
                FileSet kaffeJarFiles = new FileSet();
                kaffeJarFiles.setDir(kaffeShare);
                kaffeJarFiles.setIncludes("*.jar");
                addFileset(kaffeJarFiles);
            }
        } else if ("GNU libgcj".equals(System.getProperty("java.vm.name"))) {
            addExisting(systemBootClasspath);
        }

        if (System.getProperty("java.vendor").toLowerCase(Locale.US).indexOf("microsoft") >= 0) {
            // XXX is this code still necessary? is there any 1.2+ port?
            // Pull in *.zip from packages directory
            FileSet msZipFiles = new FileSet();
            msZipFiles.setDir(new File(System.getProperty("java.home")
                + File.separator + "Packages"));
            msZipFiles.setIncludes("*.ZIP");
            addFileset(msZipFiles);
        } else {
            // JDK 1.2+ seems to set java.home to the JRE directory.
            addExisting(new Path(null,
                                 System.getProperty("java.home")
                                 + File.separator + "lib"
                                 + File.separator + "rt.jar"));
            // Just keep the old version as well and let addExisting
            // sort it out.
            addExisting(new Path(null,
                                 System.getProperty("java.home")
                                 + File.separator + "jre"
                                 + File.separator + "lib"
                                 + File.separator + "rt.jar"));

            // Sun's and Apple's 1.4 have JCE and JSSE in separate jars.
            String[] secJars = {"jce", "jsse"};
            for (int i = 0; i < secJars.length; i++) {
                addExisting(new Path(null,
                                     System.getProperty("java.home")
                                     + File.separator + "lib"
                                     + File.separator + secJars[i] + ".jar"));
                addExisting(new Path(null,
                                     System.getProperty("java.home")
                                     + File.separator + ".."
                                     + File.separator + "Classes"
                                     + File.separator + secJars[i] + ".jar"));
            }

            // IBM's 1.4 has rt.jar split into 4 smaller jars and a combined
            // JCE/JSSE in security.jar.
            String[] ibmJars
                = {"core", "graphics", "security", "server", "xml"};
            for (int i = 0; i < ibmJars.length; i++) {
                addExisting(new Path(null,
                                     System.getProperty("java.home")
                                     + File.separator + "lib"
                                     + File.separator + ibmJars[i] + ".jar"));
            }

            // Added for MacOS X
            addExisting(new Path(null,
                                 System.getProperty("java.home")
                                 + File.separator + ".."
                                 + File.separator + "Classes"
                                 + File.separator + "classes.jar"));
            addExisting(new Path(null,
                                 System.getProperty("java.home")
                                 + File.separator + ".."
                                 + File.separator + "Classes"
                                 + File.separator + "ui.jar"));
        }
    
public voidappend(org.apache.tools.ant.types.Path other)
Append the contents of the other Path instance to this.

param
other a Path to be added to the path

        if (other == null) {
            return;
        }
        add(other);
    
protected ResourceCollectionassertFilesystemOnly(ResourceCollection rc)
Verify the specified ResourceCollection is filesystem-only.

param
rc the ResourceCollection to check.
throws
BuildException if rc is not filesystem-only.
return
the passed in ResourceCollection.

        if (rc != null && !(rc.isFilesystemOnly())) {
            throw new BuildException(getDataTypeName()
                + " allows only filesystem resources.");
        }
        return rc;
    
public java.lang.Objectclone()
Clone this Path.

return
Path with shallowly cloned Resource children.

        try {
            Path result = (Path) super.clone();
            result.union = union == null ? union : (Union) union.clone();
            return result;
        } catch (CloneNotSupportedException e) {
            throw new BuildException(e);
        }
    
private org.apache.tools.ant.types.PathconcatSpecialPath(java.lang.String defValue, org.apache.tools.ant.types.Path p)
Concatenates a class path in the order specified by the ${build.sysclasspath} property - using the supplied value if ${build.sysclasspath} has not been set.

        Path result = new Path(getProject());

        String order = defValue;
        if (getProject() != null) {
            String o = getProject().getProperty("build.sysclasspath");
            if (o != null) {
                order = o;
            }
        }
        if (order.equals("only")) {
            // only: the developer knows what (s)he is doing
            result.addExisting(p, true);

        } else if (order.equals("first")) {
            // first: developer could use a little help
            result.addExisting(p, true);
            result.addExisting(this);

        } else if (order.equals("ignore")) {
            // ignore: don't trust anyone
            result.addExisting(this);

        } else {
            // last: don't trust the developer
            if (!order.equals("last")) {
                log("invalid value for build.sysclasspath: " + order,
                    Project.MSG_WARN);
            }
            result.addExisting(this);
            result.addExisting(p, true);
        }
        return result;
    
public org.apache.tools.ant.types.PathconcatSystemBootClasspath(java.lang.String defValue)
Concatenates the system boot class path in the order specified by the ${build.sysclasspath} property - using the supplied value if ${build.sysclasspath} has not been set.

param
defValue the order ("first", "last", "only")
return
the concatenated path

        return concatSpecialPath(defValue, Path.systemBootClasspath);
    
public org.apache.tools.ant.types.PathconcatSystemClasspath()
Concatenates the system class path in the order specified by the ${build.sysclasspath} property - using "last" as default value.

return
the concatenated path

        return concatSystemClasspath("last");
    
public org.apache.tools.ant.types.PathconcatSystemClasspath(java.lang.String defValue)
Concatenates the system class path in the order specified by the ${build.sysclasspath} property - using the supplied value if ${build.sysclasspath} has not been set.

param
defValue the order ("first", "last", "only")
return
the concatenated path

        return concatSpecialPath(defValue, Path.systemClasspath);
    
public org.apache.tools.ant.types.PathcreatePath()
Creates a nested <path> element.

return
a Path to be configured
throws
BuildException on error

        Path p = new Path(getProject());
        add(p);
        return p;
    
public org.apache.tools.ant.types.Path$PathElementcreatePathElement()
Creates the nested <pathelement> element.

return
the PathElement to be configured
throws
BuildException on error

        if (isReference()) {
            throw noChildrenAllowed();
        }
        PathElement pe = new PathElement();
        add(pe);
        return pe;
    
protected synchronized voiddieOnCircularReference(java.util.Stack stk, org.apache.tools.ant.Project p)
Overrides the version of DataType to recurse on all DataType child elements that may have been added.

param
stk the stack of data types to use (recursively).
param
p the project to use to dereference the references.
throws
BuildException on error.

        if (isChecked()) {
            return;
        }
        if (isReference()) {
            super.dieOnCircularReference(stk, p);
        } else {
            if (union != null) {
                stk.push(union);
                invokeCircularReferenceCheck(union, stk, p);
                stk.pop();
            }
            setChecked(true);
        }
    
public synchronized booleanisFilesystemOnly()
Fulfill the ResourceCollection contract.

return
whether this is a filesystem-only resource collection.

        if (isReference()) {
            return ((Path) getCheckedRef()).isFilesystemOnly();
        }
        dieOnCircularReference();
        assertFilesystemOnly(union);
        return true;
    
public final synchronized java.util.Iteratoriterator()
Fulfill the ResourceCollection contract. The Iterator returned will throw ConcurrentModificationExceptions if ResourceCollections are added to this container while the Iterator is in use.

return
a "fail-fast" Iterator.

        if (isReference()) {
            return ((Path) getCheckedRef()).iterator();
        }
        dieOnCircularReference();
        return union == null ? EMPTY_ITERATOR
            : assertFilesystemOnly(union).iterator();
    
public java.lang.String[]list()
Returns all path elements defined by this and nested path objects.

return
list of path elements.

        if (isReference()) {
            return ((Path) getCheckedRef()).list();
        }
        return assertFilesystemOnly(union) == null
            ? new String[0] : union.list();
    
private static java.io.FileresolveFile(org.apache.tools.ant.Project project, java.lang.String relativeName)
Resolve a filename with Project's help - if we know one that is.

        return FileUtils.getFileUtils().resolveFile(
            (project == null) ? null : project.getBaseDir(), relativeName);
    
public voidsetLocation(java.io.File location)
Adds a element definition to the path.

param
location the location of the element to add (must not be null nor empty.
throws
BuildException on error

        checkAttributesAllowed();
        createPathElement().setLocation(location);
    
public voidsetPath(java.lang.String path)
Parses a path definition and creates single PathElements.

param
path the String path definition.
throws
BuildException on error

        checkAttributesAllowed();
        createPathElement().setPath(path);
    
public voidsetRefid(Reference r)
Makes this instance in effect a reference to another Path instance.

You must not set another attribute or nest elements inside this element if you make it a reference.

param
r the reference to another Path
throws
BuildException on error

        if (union != null) {
            throw tooManyAttributes();
        }
        super.setRefid(r);
    
public synchronized intsize()
Fulfill the ResourceCollection contract.

return
number of elements as int.

        if (isReference()) {
            return ((Path) getCheckedRef()).size();
        }
        dieOnCircularReference();
        return union == null ? 0 : assertFilesystemOnly(union).size();
    
public java.lang.StringtoString()
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.

        return isReference() ? getCheckedRef().toString()
            : union == null ? "" : union.toString();
    
public static java.lang.StringtranslateFile(java.lang.String source)
Returns its argument with all file separator characters replaced so that they match the local OS conventions.

param
source the path to convert
return
the converted path

        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 booleantranslateFileSep(java.lang.StringBuffer buffer, int pos)
Translates occurrences at a position of / or \ to correct separator of the current platform and returns whether it had to do a replacement.

param
buffer a buffer containing a string
param
pos the position in the string buffer to convert
return
true if the character was a / or \

        if (buffer.charAt(pos) == '/" || buffer.charAt(pos) == '\\") {
            buffer.setCharAt(pos, File.separatorChar);
            return true;
        }
        return false;
    
public static java.lang.String[]translatePath(org.apache.tools.ant.Project project, java.lang.String source)
Splits a PATH (with : or ; as separators) into its parts.

param
project the project to use
param
source a String value
return
an array of strings, one for each path element

        final Vector result = new Vector();
        if (source == null) {
            return new String[0];
        }
        PathTokenizer tok = new PathTokenizer(source);
        StringBuffer element = new StringBuffer();
        while (tok.hasMoreTokens()) {
            String pathElement = tok.nextToken();
            try {
                element.append(resolveFile(project, pathElement).getPath());
            } catch (BuildException e) {
                project.log("Dropping path element " + pathElement
                    + " as it is not valid relative to the project",
                    Project.MSG_VERBOSE);
            }
            for (int i = 0; i < element.length(); i++) {
                translateFileSep(element, i);
            }
            result.addElement(element.toString());
            element = new StringBuffer();
        }
        String[] res = new String[result.size()];
        result.copyInto(res);
        return res;