FileDocCategorySizeDatePackage
Length.javaAPI DocApache Ant 1.7010071Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs

Length

public class Length extends org.apache.tools.ant.Task implements org.apache.tools.ant.taskdefs.condition.Condition
Gets lengths: of files/resources, byte size; of strings, length (optionally trimmed). The task is overloaded in this way for semantic reasons, much like Available.
since
Ant 1.6.3

Fields Summary
private static final String
ALL
private static final String
EACH
private static final String
STRING
private static final String
LENGTH_REQUIRED
private String
property
private String
string
private Boolean
trim
private String
mode
private org.apache.tools.ant.types.Comparison
when
private Long
length
private org.apache.tools.ant.types.resources.Resources
resources
Constructors Summary
Methods Summary
public synchronized voidadd(org.apache.tools.ant.types.FileSet fs)
Add a FileSet.

param
fs the FileSet to add.

        add((ResourceCollection) fs);
    
public synchronized voidadd(org.apache.tools.ant.types.ResourceCollection c)
Add a ResourceCollection.

param
c the ResourceCollection to add.
since
Ant 1.7

        if (c == null) {
            return;
        }
        resources = (resources == null) ? new Resources() : resources;
        resources.add(c);
    
public booleaneval()
Fulfill the condition contract.

return
true if the condition is true.
throws
BuildException if an error occurs.

        validate();
        if (length == null) {
            throw new BuildException(LENGTH_REQUIRED);
        }
        Long ell = null;
        if (STRING.equals(mode)) {
            ell = new Long(getLength(string, getTrim()));
        } else {
            ConditionHandler h = new ConditionHandler();
            handleResources(h);
            ell = new Long(h.getLength());
        }
        return when.evaluate(ell.compareTo(length));
    
public voidexecute()
Execute the length task.

        validate();
        PrintStream ps = new PrintStream((property != null)
            ? (OutputStream) new PropertyOutputStream(getProject(), property)
            : (OutputStream) new LogOutputStream(this, Project.MSG_INFO));

        if (STRING.equals(mode)) {
            ps.print(getLength(string, getTrim()));
            ps.close();
        } else if (EACH.equals(mode)) {
            handleResources(new EachHandler(ps));
        } else if (ALL.equals(mode)) {
            handleResources(new AllHandler(ps));
        }
    
private static longgetLength(java.lang.String s, boolean t)

        return (t ? s.trim() : s).length();
    
public booleangetTrim()
Learn whether strings will be trimmed.

return
boolean trim setting.

        return trim != null && trim.booleanValue();
    
private voidhandleResources(org.apache.tools.ant.taskdefs.Length$Handler h)

        for (Iterator i = resources.iterator(); i.hasNext();) {
            Resource r = (Resource) i.next();
            if (!r.isExists()) {
                log(r + " does not exist", Project.MSG_ERR);
            } else if (r.isDirectory()) {
                log(r + " is a directory; length unspecified",
                    Project.MSG_ERR);
            } else {
                h.handle(r);
            }
        }
        h.complete();
    
public synchronized voidsetFile(java.io.File file)
Set the single file for this task.

param
file the File whose length to retrieve.

        add(new FileResource(file));
    
public synchronized voidsetLength(long ell)
Set the target count number for use as a Condition.

param
ell the long length to compare with.

        length = new Long(ell);
    
public synchronized voidsetMode(org.apache.tools.ant.taskdefs.Length$FileMode m)
Set the execution mode for working with files.

param
m the FileMode to use.

        this.mode = m.getValue();
    
public synchronized voidsetProperty(java.lang.String property)
The property in which the length will be stored.

param
property the String property key.


                        
         
        this.property = property;
    
public synchronized voidsetString(java.lang.String string)
Set the string whose length to get.

param
string String.

        this.string = string;
        this.mode = STRING;
    
public synchronized voidsetTrim(boolean trim)
Set whether to trim in string mode.

param
trim boolean.

        this.trim = trim ? Boolean.TRUE : Boolean.FALSE;
    
public synchronized voidsetWhen(org.apache.tools.ant.taskdefs.Length$When w)
Set the comparison for use as a Condition.

param
w EnumeratedAttribute When.
see
org.apache.tools.ant.types.Comparison

        setWhen((Comparison) w);
    
public synchronized voidsetWhen(org.apache.tools.ant.types.Comparison c)
Set the comparison for use as a Condition.

param
c Comparison.
see
org.apache.tools.ant.types.Comparison
since
Ant 1.7

        when = c;
    
private voidvalidate()

        if (string != null) {
            if (resources != null) {
                throw new BuildException("the string length function"
                    + " is incompatible with the file/resource length function");
            }
            if (!(STRING.equals(mode))) {
                throw new BuildException("the mode attribute is for use"
                    + " with the file/resource length function");
            }
        } else if (resources != null) {
            if (!(EACH.equals(mode) || ALL.equals(mode))) {
                throw new BuildException("invalid mode setting for"
                    + " file/resource length function: \"" + mode + "\"");
            } else if (trim != null) {
                throw new BuildException("the trim attribute is"
                    + " for use with the string length function only");
            }
        } else {
            throw new BuildException("you must set either the string attribute"
                + " or specify one or more files using the file attribute or"
                + " nested resource collections");
        }