ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ivan Ivanov <rambiusparkisan...@yahoo.com>
Subject Re: Replacing with File Size
Date Thu, 03 Mar 2005 17:13:30 GMT
> 
> Ant 1.6.3 (and recent nightly builds) will contain a
> length task that can set a property to the length of
> a file or string.

OK, but why a task only for a file length? The mailing
list archives contains examples for file length, last
modified date and a check whether a directory is empty
and all these are done with javascript "wrappers" of
java.io.File methods. Today I wrote a task that does
these without Javascript. I am sending the very task,
a test case for it and if needed I will write the
docs.

I myself tested it only on Linux.

=== The task =====
package org.apache.tools.ant.taskdefs;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

/**
 * Retrieves information about a given file.
 *
 * @author Ivan Ivanov
 * @version $Revision$
 */
public class FileInfoTask extends Task {
    
    /**
     * Stores the supported attributes.
     */
    private static List attributes;
    
    static {
        attributes = new ArrayList();
        attributes.add("isReadable");
        attributes.add("isWritable");
        attributes.add("exists");        
        attributes.add("parent");
        attributes.add("isDirectory");
        attributes.add("isFile");
        attributes.add("hidden");
        attributes.add("lastModified");
        attributes.add("length");
        attributes.add("countFiles");
    }
    
    /**
     * The name of the file whose information will be
retrieved. 
     */
    private String file;
    
    /**exists
     * The file attribute that will be retrieved.
     */
    private String attribute;
    
    /**
     * The property where the information will be
stored.
     */
    private String property;
    
    /**
     * Creates a new instance of FileInfo
     */
    public FileInfoTask() {
    }
    
    public void execute() throws BuildException {
        if (file == null) {
            throw new BuildException("Provide a valid
file attribute");
        }
        if (attribute == null) {
            throw new BuildException("Provide a valid
attribute");
        }
        if (property == null) {
            throw new BuildException("Provide a valid
property name");
        }

        File f = getProject().resolveFile(file);
        int i = attributes.indexOf(attribute);
        String result = null;
        switch(i) {
            case 0: 
                if (f.canRead()) {
                    result = "true";
                }
                break;
            case 1:
                if (f.canWrite()) {
                    result = "true";
                }
                break;
            case 2:
                if (f.exists()) {
                    result = "true";
                }
                break;
            case 3:
                result = f.getParent();
                break;
            case 4:
                if (f.isDirectory()) {
                    result = "true";
                }
                break;
            case 5:
                if (f.isFile()) {
                    result = "true";
                }
                break;
            case 6:
                if (f.isHidden()) {
                    result = "true";
                }
                break;
            case 7:
                result =
String.valueOf(f.lastModified());
                break;
            case 8:
                result = String.valueOf(f.length());
                break;
            case 9:
                String[] list = f.list();
                int l;
                if ((list != null) && (l =
list.length) > 0) {
                    result = String.valueOf(l);
                }
                break;                
            default:
                getProject().log(
                    "Supported attributes are " +
String.valueOf(attributes),
                    Project.MSG_VERBOSE);
                throw new BuildException("Attribute "
+ attribute + " is not supported. " +
                    "Run ant -verbose to see the
supported ones.");
        }
        
        if (result != null) {
            getProject().log("Setting " + result + "
for attribute " + attribute + " in property " +
property, Project.MSG_VERBOSE);
            getProject().setNewProperty(property,
result);
        }
    }
    
    public void setFile(String file) {
        this.file = file;
    }
    
    public String getFile() {
        return file;
    }
    
    public void setAttribute(String attribute) {
        this.attribute = attribute;
    }
    
    public String getAttribute() {
        return attribute;
    }
    
    public void setProperty(String property) {
        this.property = property;
    }
    
    public String getProperty() {
        return property;
    }
}
=== End of The task =====

=== The testcases =====
package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.BuildFileTest;

import java.text.SimpleDateFormat;

import java.util.Date;

/**
 * @author Ivan Ivanov
 * @version $Revision$
 */
public class FileInfoTest extends BuildFileTest {
    
    /**
     * Creates a new instance of FileInfoTest
     */
    public FileInfoTest(String name) {
        super(name);
    }
    
    public void setUp() {
       
configureProject("tests/resources/fileinfo.xml");
    }
    
    public void testNoFilename() {
        expectBuildException("noFilename", "No file
name given");
    }
    
    public void testNoProperty() {
        expectBuildException("noProperty", "No
property name given");
    }
    
    public void testNoAttribute() {
        expectBuildException("noAttribute", "No
property name given");
    }
    
    public void testReadable() {
        executeTarget("readable");
        assertPropertySet("isReadable");
        assertPropertyUnset("isNotReadable");
    }
    
    public void testWritable() {
        executeTarget("writable");
        assertPropertySet("isWritable");
        assertPropertyUnset("isNotWritable");
    }
    
    public void testExists() {
        executeTarget("exists");
        assertPropertySet("exists");
        assertPropertyUnset("notExists");
    }
    
    public void testParent() {     
        expectPropertySet("parent", "parent",
            getProject().getProperty("basedir"));
    }
    
    public void testIsFile() {
        executeTarget("isFile");
        assertPropertySet("isFile");
        assertPropertyUnset("isDir");
    }
    
    public void testIsDir() {
        executeTarget("isDirectory");
        assertPropertyUnset("isFile");
        assertPropertySet("isDir");
    }
    
    public void testIsHidden() {
        executeTarget("isHidden");
        assertPropertySet("isHidden");
        assertPropertyUnset("isNotHidden");
    }
    
    public void testLastModified() throws Exception {
        SimpleDateFormat sdf = new
SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date d = sdf.parse("04/14/1981 22:00:00");
        long expected = d.getTime();
        executeTarget("lastModified");        
        String modified =
getProject().getProperty("modified");
        assertNotNull("The modified property is not
set", modified);        
        long actual = Long.parseLong(modified);
        assertEquals(expected, actual);
    }
    
    public void testLength() {
        executeTarget("length");
        assertPropertyEquals("zeroLength", "0");
        assertPropertyEquals("length", "40");
    }
    
    public void testContainFiles() {
        executeTarget("countFiles");
        assertPropertyEquals("files", "3");
        assertPropertyUnset("nofiles");
    }    
    
}

=== The test and sample script =====
<?xml version="1.0" encoding="UTF-8"?>
<project>
    <target name="-init">
        <path id="cp">
            <fileset file="${project.jar}"/>
        </path>
        <taskdef name="fileinfo"
           
classname="org.apache.tools.ant.taskdefs.FileInfoTask"
            classpathref="cp"/>
    </target>
    
    <target name="noFilename" depends="-init">
        <fileinfo property="p" attribute="a"/>
    </target>

    <target name="noProperty" depends="-init">
        <fileinfo file="f" attribute="a"/>
    </target>
    
    <target name="noAttribute" depends="-init">
        <fileinfo file="f" property="p"/>
    </target>
    
    <target name="readable" depends="-init">
        <touch file="readable"/>
        <fileinfo file="readable"
attribute="isReadable" property="isReadable"/>
        
        <delete file="readable"/>
        <touch file="notReadable"/>
        <!-- Here we should disable reading in a
cross-platform manner
        but I know it only for Linux -->
        <exec executable="chmod">
            <arg value="u-r"/>
            <arg value="notReadable"/>
        </exec>
        <fileinfo file="notReadable"
attribute="isReadable" property="isNotReadable"/>
        <delete file="notReadable"/>        
    </target>
    
    <target name="writable" depends="-init">
        <touch file="writable"/>
        <fileinfo file="writable"
attribute="isWritable" property="isWritable"/>
        
        <delete file="writable"/>
        <touch file="notWritable"/>
        <exec executable="chmod">
            <arg value="u-w"/>
            <arg value="notWritable"/>
        </exec>
        <fileinfo file="notWritable"
attribute="isWritable" property="isNotWritable"/>
        <delete file="notWritable"/>        
    </target>
    
    <target name="exists" depends="-init">
        <touch file="exists"/>
        <fileinfo file="exists" attribute="exists"
property="exists"/>
        <delete file="exists"/>
        
        <delete file="notExists" failonerror="false"/>
        <fileinfo file="notExists" attribute="exists"
property="notExists"/>        
    </target>
    
    <target name="parent" depends="-init">
        <touch file="child"/>
        <fileinfo file="child" attribute="parent"
property="parent"/>
        <delete file="child"/>
    </target>

    <target name="isFile" depends="-init">
        <touch file="file"/>
        <fileinfo file="file" attribute="isFile"
property="isFile"/>
        <fileinfo file="file" attribute="isDirectory"
property="isDir"/>
        <delete file="file"/>
    </target>
    
    <target name="isDirectory" depends="-init">
        <mkdir dir="dir"/>
        <fileinfo file="dir" attribute="isFile"
property="isFile"/>
        <fileinfo file="dir" attribute="isDirectory"
property="isDir"/>
        <delete dir="dir"/>
    </target>
    
    <target name="isHidden" depends="-init">
        <touch file=".hidden"/>
        <fileinfo file=".hidden" attribute="hidden"
property="isHidden"/>
        <delete file=".hidden"/>
        
        <touch file="notHidden"/>
        <fileinfo file="notHidden" attribute="hidden"
property="isNotHidden"/>
        <delete file="notHidden"/>
    </target>
    
    <target name="lastModified" depends="-init">
        <touch file="modified" datetime="04/14/1981
10:00:00 PM"/>
        <fileinfo file="modified"
attribute="lastModified" property="modified"/>
        <delete file="modified"/>
    </target>
    
    <target name="length" depends="-init">
        <touch file="empty"/>
        <fileinfo file="empty" attribute="length"
property="zeroLength"/>
        <delete file="empty"/>
        
        <echo message="I am an auxiliary file and my
size is 40"
            file="auxiliary"/>
        <fileinfo file="auxiliary" attribute="length"
property="length"/>
        <echo>$${length} = ${length}</echo>
        <delete file="auxiliary"/>
    </target>
    
    <target name="countFiles" depends="-init">
        <mkdir dir="dir"/>
        <touch file="dir/file1"/>
        <touch file="dir/file2"/>
        <touch file="dir/file3"/>
        <fileinfo file="dir" attribute="countFiles"
property="files"/>
        <delete dir="dir"/>
        
        <mkdir dir="empty"/>
        <fileinfo file="empty" attribute="countFiles"
property="nofiles"/>
        <delete dir="empty"/>
    </target>
</project>
=== End of The test and sample script =====

I hope you will like it.

Regards Ivan


	
		
__________________________________ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message