FileDocCategorySizeDatePackage
PathConvert.javaAPI DocApache Ant 1.7015404Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

PathConvert

public class PathConvert extends org.apache.tools.ant.Task
Converts path and classpath information to a specific target OS format. The resulting formatted path is placed into the specified property.
since
Ant 1.4
ant.task
category="utility"

Fields Summary
private static boolean
onWindows
Set if we're running on windows
private org.apache.tools.ant.types.resources.Union
path
Path to be converted
private org.apache.tools.ant.types.Reference
refid
Reference to path/fileset to convert
private String
targetOS
The target OS type
private boolean
targetWindows
Set when targetOS is set to windows
private boolean
setonempty
Set if we should create a new property even if the result is empty
private String
property
The property to receive the conversion
private Vector
prefixMap
Path prefix map
private String
pathSep
User override on path sep char
private String
dirSep
User override on directory sep char
private org.apache.tools.ant.types.Mapper
mapper
Filename mapper
Constructors Summary
public PathConvert()
Construct a new instance of the PathConvert task.


                 
      
    
Methods Summary
public voidadd(org.apache.tools.ant.util.FileNameMapper fileNameMapper)
Add a nested filenamemapper.

param
fileNameMapper the mapper to add.
since
Ant 1.6.3

        Mapper m = new Mapper(getProject());
        m.add(fileNameMapper);
        addMapper(m);
    
public voidadd(org.apache.tools.ant.types.ResourceCollection rc)
Add an arbitrary ResourceCollection.

param
rc the ResourceCollection to add.
since
Ant 1.7

        if (isReference()) {
            throw noChildrenAllowed();
        }
        getPath().add(rc);
    
public voidaddMapper(org.apache.tools.ant.types.Mapper mapper)
Add a mapper to convert the file names.

param
mapper a Mapper value.

        if (this.mapper != null) {
            throw new BuildException(
                "Cannot define more than one mapper");
        }
        this.mapper = mapper;
    
public org.apache.tools.ant.taskdefs.PathConvert$MapEntrycreateMap()
Create a nested MAP element.

return
a Map to configure.

        MapEntry entry = new MapEntry();
        prefixMap.addElement(entry);
        return entry;
    
public org.apache.tools.ant.types.PathcreatePath()
Create a nested path element.

return
a Path to be used by Ant reflection.

        if (isReference()) {
            throw noChildrenAllowed();
        }
        Path result = new Path(getProject());
        add(result);
        return result;
    
public voidexecute()
Do the execution.

throws
BuildException if something is invalid.

        Union savedPath = path;
        String savedPathSep = pathSep; // may be altered in validateSetup
        String savedDirSep = dirSep; // may be altered in validateSetup

        try {
            // If we are a reference, create a Path from the reference
            if (isReference()) {
                Object o = refid.getReferencedObject(getProject());
                if (!(o instanceof ResourceCollection)) {
                    throw new BuildException("refid '" + refid.getRefId()
                        + "' does not refer to a resource collection.");
                }
                getPath().add((ResourceCollection) o);
            }
            validateSetup(); // validate our setup

            // Currently, we deal with only two path formats: Unix and Windows
            // And Unix is everything that is not Windows
            // (with the exception for NetWare and OS/2 below)

            // for NetWare and OS/2, piggy-back on Windows, since here and
            // in the apply code, the same assumptions can be made as with
            // windows - that \\ is an OK separator, and do comparisons
            // case-insensitive.
            String fromDirSep = onWindows ? "\\" : "/";

            StringBuffer rslt = new StringBuffer();

            // Get the list of path components in canonical form
            String[] elems = path.list();

            if (mapper != null) {
                FileNameMapper impl = mapper.getImplementation();
                List ret = new ArrayList();
                for (int i = 0; i < elems.length; ++i) {
                    String[] mapped = impl.mapFileName(elems[i]);
                    for (int m = 0; mapped != null && m < mapped.length; ++m) {
                        ret.add(mapped[m]);
                    }
                }
                elems = (String[]) ret.toArray(new String[ret.size()]);
            }
            for (int i = 0; i < elems.length; i++) {
                String elem = mapElement(elems[i]); // Apply the path prefix map

                // Now convert the path and file separator characters from the
                // current os to the target os.

                if (i != 0) {
                    rslt.append(pathSep);
                }
                StringTokenizer stDirectory =
                    new StringTokenizer(elem, fromDirSep, true);

                while (stDirectory.hasMoreTokens()) {
                    String token = stDirectory.nextToken();
                    rslt.append(fromDirSep.equals(token) ? dirSep : token);
                }
            }
            // Place the result into the specified property,
            // unless setonempty == false
            if (setonempty || rslt.length() > 0) {
                String value = rslt.toString();
                if (property == null) {
                    log(value);
                } else {
                    log("Set property " + property + " = " + value,
                        Project.MSG_VERBOSE);
                    getProject().setNewProperty(property, value);
                }
            }
        } finally {
            path = savedPath;
            dirSep = savedDirSep;
            pathSep = savedPathSep;
        }
    
private synchronized org.apache.tools.ant.types.resources.UniongetPath()

        if (path == null) {
            path = new Union();
            path.setProject(getProject());
        }
        return path;
    
public booleanisReference()
Learn whether the refid attribute of this element been set.

return
true if refid is valid.

        return refid != null;
    
private java.lang.StringmapElement(java.lang.String elem)
Apply the configured map to a path element. The map is used to convert between Windows drive letters and Unix paths. If no map is configured, then the input string is returned unchanged.

param
elem The path element to apply the map to.
return
String Updated element.


        int size = prefixMap.size();

        if (size != 0) {

            // Iterate over the map entries and apply each one.
            // Stop when one of the entries actually changes the element.

            for (int i = 0; i < size; i++) {
                MapEntry entry = (MapEntry) prefixMap.elementAt(i);
                String newElem = entry.apply(elem);

                // Note I'm using "!=" to see if we got a new object back from
                // the apply method.

                if (newElem != elem) {
                    elem = newElem;
                    break; // We applied one, so we're done
                }
            }
        }
        return elem;
    
private org.apache.tools.ant.BuildExceptionnoChildrenAllowed()
Creates an exception that indicates that this XML element must not have child elements if the refid attribute is set.

return
BuildException.

        return new BuildException("You must not specify nested "
             + "elements when using the refid attribute.");
    
public voidsetDirSep(java.lang.String sep)
Set the default directory separator string; defaults to current JVM {@link java.io.File#separator File.separator}.

param
sep directory separator string.

        dirSep = sep;
    
public voidsetPathSep(java.lang.String sep)
Set the default path separator string; defaults to current JVM {@link java.io.File#pathSeparator File.pathSeparator}.

param
sep path separator string.

        pathSep = sep;
    
public voidsetProperty(java.lang.String p)
Set the name of the property into which the converted path will be placed.

param
p the property name.

        property = p;
    
public voidsetRefid(org.apache.tools.ant.types.Reference r)
Add a reference to a Path, FileSet, DirSet, or FileList defined elsewhere.

param
r the reference to a path, fileset, dirset or filelist.

        if (path != null) {
            throw noChildrenAllowed();
        }
        refid = r;
    
public voidsetSetonempty(boolean setonempty)
Set whether the specified property will be set if the result is the empty string.

param
setonempty true or false.
since
Ant 1.5

         this.setonempty = setonempty;
     
public voidsetTargetos(java.lang.String target)
Set targetos to a platform to one of "windows", "unix", "netware", or "os/2"; current platform settings are used by default.

param
target the target os.
deprecated
since 1.5.x. Use the method taking a TargetOs argument instead.
see
#setTargetos(PathConvert.TargetOs)

        TargetOs to = new TargetOs();
        to.setValue(target);
        setTargetos(to);
    
public voidsetTargetos(org.apache.tools.ant.taskdefs.PathConvert$TargetOs target)
Set targetos to a platform to one of "windows", "unix", "netware", or "os/2"; current platform settings are used by default.

param
target the target os
since
Ant 1.5

        targetOS = target.getValue();

        // Currently, we deal with only two path formats: Unix and Windows
        // And Unix is everything that is not Windows

        // for NetWare and OS/2, piggy-back on Windows, since in the
        // validateSetup code, the same assumptions can be made as
        // with windows - that ; is the path separator

        targetWindows = !targetOS.equals("unix") && !targetOS.equals("tandem");
    
private voidvalidateSetup()
Validate that all our parameters have been properly initialized.

throws
BuildException if something is not set up properly.


        if (path == null) {
            throw new BuildException("You must specify a path to convert");
        }
        // Determine the separator strings.  The dirsep and pathsep attributes
        // override the targetOS settings.
        String dsep = File.separator;
        String psep = File.pathSeparator;

        if (targetOS != null) {
            psep = targetWindows ? ";" : ":";
            dsep = targetWindows ? "\\" : "/";
        }
        if (pathSep != null) {
            // override with pathsep=
            psep = pathSep;
        }
        if (dirSep != null) {
            // override with dirsep=
            dsep = dirSep;
        }
        pathSep = psep;
        dirSep = dsep;