FileDocCategorySizeDatePackage
FileResource.javaAPI DocApache Ant 1.709843Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.types.resources

FileResource

public class FileResource extends org.apache.tools.ant.types.Resource implements Touchable
A Resource representation of a File.
since
Ant 1.7

Fields Summary
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
private static final int
NULL_FILE
private File
file
private File
baseDir
Constructors Summary
public FileResource()
Default constructor.


           
      
    
public FileResource(File b, String name)
Construct a new FileResource using the specified basedir and relative name.

param
b the basedir as File.
param
name the relative filename.

        setFile(FILE_UTILS.resolveFile(b, name));
        setBaseDir(b);
    
public FileResource(File f)
Construct a new FileResource from a File.

param
f the File represented.

        setFile(f);
    
public FileResource(org.apache.tools.ant.Project p, String s)
Constructor for Ant attribute introspection.

param
p the Project against which to resolve s.
param
s the absolute or Project-relative filename as a String.
see
org.apache.tools.ant.IntrospectionHelper

        this(p.resolveFile(s));
        setProject(p);
    
Methods Summary
public intcompareTo(java.lang.Object another)
Compare this FileResource to another Resource.

param
another the other Resource against which to compare.
return
a negative integer, zero, or a positive integer as this FileResource is less than, equal to, or greater than the specified Resource.

        if (isReference()) {
            return ((Comparable) getCheckedRef()).compareTo(another);
        }
        if (this.equals(another)) {
            return 0;
        }
        if (another.getClass().equals(getClass())) {
            FileResource otherfr = (FileResource) another;
            File f = getFile();
            if (f == null) {
                return -1;
            }
            File of = otherfr.getFile();
            if (of == null) {
                return 1;
            }
            return f.compareTo(of);
        }
        return super.compareTo(another);
    
public booleanequals(java.lang.Object another)
Compare another Object to this FileResource for equality.

param
another the other Object to compare.
return
true if another is a FileResource representing the same file.

        if (this == another) {
            return true;
        }
        if (isReference()) {
            return getCheckedRef().equals(another);
        }
        if (!(another.getClass().equals(getClass()))) {
            return false;
        }
        FileResource otherfr = (FileResource) another;
        return getFile() == null
            ? otherfr.getFile() == null
            : getFile().equals(otherfr.getFile());
    
public java.io.FilegetBaseDir()
Return the basedir to which the name is relative.

return
the basedir as File.

        return isReference()
            ? ((FileResource) getCheckedRef()).getBaseDir() : baseDir;
    
public java.io.FilegetFile()
Get the file represented by this FileResource.

return
the File.

        return isReference() ? ((FileResource) getCheckedRef()).getFile() : file;
    
public java.io.InputStreamgetInputStream()
Return an InputStream for reading the contents of this Resource.

return
an InputStream object.
throws
IOException if an error occurs.

        return isReference()
            ? ((Resource) getCheckedRef()).getInputStream()
            : new FileInputStream(getNotNullFile());
    
public longgetLastModified()
Get the modification time in milliseconds since 01.01.1970 .

return
0 if the resource does not exist.

        return isReference()
            ? ((Resource) getCheckedRef()).getLastModified()
            : getNotNullFile().lastModified();
    
public java.lang.StringgetName()
Get the name of this FileResource. If the basedir is set, the name will be relative to that. Otherwise the basename only will be returned.

return
the name of this resource.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getName();
        }
        File b = getBaseDir();
        return b == null ? getNotNullFile().getName()
            : FILE_UTILS.removeLeadingPath(b, getNotNullFile());
    
protected java.io.FilegetNotNullFile()
Get the file represented by this FileResource, ensuring it is not null.

return
the not-null File.
throws
BuildException if file is null.

        if (getFile() == null) {
            throw new BuildException("file attribute is null!");
        }
        return getFile();
    
public java.io.OutputStreamgetOutputStream()
Get an OutputStream for the Resource.

return
an OutputStream to which content can be written.
throws
IOException if unable to provide the content of this Resource as a stream.
throws
UnsupportedOperationException if OutputStreams are not supported for this Resource type.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getOutputStream();
        }
        File f = getNotNullFile();
        if (f.exists()) {
            if (f.isFile()) {
                f.delete();
            }
        } else {
            File p = f.getParentFile();
            if (p != null && !(p.exists())) {
                p.mkdirs();
            }
        }
        return new FileOutputStream(f);
    
public longgetSize()
Get the size of this Resource.

return
the size, as a long, 0 if the Resource does not exist.

        return isReference() ? ((Resource) getCheckedRef()).getSize()
            : getNotNullFile().length();
    
public inthashCode()
Get the hash code for this Resource.

return
hash code as int.

        if (isReference()) {
            return getCheckedRef().hashCode();
        }
        return MAGIC * (getFile() == null ? NULL_FILE : getFile().hashCode());
    
public booleanisDirectory()
Learn whether the resource is a directory.

return
boolean flag indicating if the resource is a directory.

        return isReference() ? ((Resource) getCheckedRef()).isDirectory()
            : getNotNullFile().isDirectory();
    
public booleanisExists()
Learn whether this file exists.

return
true if this resource exists.

        return isReference() ? ((Resource) getCheckedRef()).isExists()
            : getNotNullFile().exists();
    
public booleanisFilesystemOnly()
Fulfill the ResourceCollection contract.

return
whether this Resource is a FileResource.

        return !isReference()
            || ((FileResource) getCheckedRef()).isFilesystemOnly();
    
public voidsetBaseDir(java.io.File b)
Set the basedir for this FileResource.

param
b the basedir as File.

        checkAttributesAllowed();
        baseDir = b;
    
public voidsetFile(java.io.File f)
Set the File for this FileResource.

param
f the File to be represented.

        checkAttributesAllowed();
        file = f;
    
public voidsetRefid(org.apache.tools.ant.types.Reference r)
Overrides the super version.

param
r the Reference to set.

        if (file != null || baseDir != null) {
            throw tooManyAttributes();
        }
        super.setRefid(r);
    
public java.lang.StringtoString()
Get the string representation of this Resource.

return
this FileResource formatted as a String.

        if (isReference()) {
            return getCheckedRef().toString();
        }
        if (file == null) {
            return "(unbound file resource)";
        }
        String absolutePath = file.getAbsolutePath();
        return FILE_UTILS.normalize(absolutePath).getAbsolutePath();
    
public voidtouch(long modTime)
Implement the Touchable interface.

param
modTime new last modification time.

        if (isReference()) {
            ((FileResource) getCheckedRef()).touch(modTime);
            return;
        }
        getNotNullFile().setLastModified(modTime);