net.sf.mmm.util.resource.base
Class FileResource

java.lang.Object
  extended by net.sf.mmm.util.resource.base.AbstractDataResource
      extended by net.sf.mmm.util.resource.base.AbstractBrowsableResource
          extended by net.sf.mmm.util.resource.base.FileResource
All Implemented Interfaces:
BrowsableResource, DataResource

public class FileResource
extends AbstractBrowsableResource

This is the implementation of the BrowsableResource interface for a resource that is a regular File.

Since:
1.0.2
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Nested Class Summary
protected  class FileResource.FileResourceIterable
          This inner class is an Iterable of FileResources.
protected static class FileResource.FileResourceIterator
          This inner class is an Iterator of FileResources.
 
Field Summary
private  File file
          The File to adapt.
static String SCHEME_PREFIX
          The scheme-prefix for this type of BrowsableResource.
 
Constructor Summary
FileResource(File file)
          The constructor.
FileResource(String filePath)
          The constructor.
 
Method Summary
 Iterable<BrowsableResource> getChildResources()
          This method iterates the immediate child-resources contained in this BrowsableResource.
 Date getLastModificationDate()
          This method gets the last modification date of the DataResource if available and supported.
 String getName()
          This method gets the name of the resource.
 String getPath()
          This method gets the path of this resource.
 String getSchemePrefix()
          This method gets the scheme-prefix of absolute URIs for this type of DataResource.
 long getSize()
          This method gets the size (content-length) of this resource.
 String getUri()
          This method gets a string identifying this DataResource.
 URL getUrl()
          This method gets this resource as URL.
 boolean isData()
          This method determines if this resource has potentially data available.
 boolean isFolder()
          This method determines if this BrowsableResource is a folder that potentially contains other resources.
 BrowsableResource navigate(String resourcePath)
          This method creates a new DataResource pointing to the given resourcePath based on this resource.
 OutputStream openOutputStream()
          This method opens an output-stream in order to write data to the resource.
 InputStream openStream()
          This method opens this resource for reading.
 
Methods inherited from class net.sf.mmm.util.resource.base.AbstractBrowsableResource
getChildResources
 
Methods inherited from class net.sf.mmm.util.resource.base.AbstractDataResource
isAvailable, isModifiedSince, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.mmm.util.resource.api.DataResource
isAvailable, isModifiedSince
 

Field Detail

SCHEME_PREFIX

public static final String SCHEME_PREFIX
The scheme-prefix for this type of BrowsableResource.

See Also:
Constant Field Values

file

private final File file
The File to adapt.

Constructor Detail

FileResource

public FileResource(File file)
The constructor.

Parameters:
file - is the File to represent.

FileResource

public FileResource(String filePath)
The constructor.

Parameters:
filePath - is the path to the File to represent.
Method Detail

getSchemePrefix

public String getSchemePrefix()
This method gets the scheme-prefix of absolute URIs for this type of DataResource. The scheme-prefix has the following form: <scheme>:<suffix> where <suffix> is the empty string or something like //.

Specified by:
getSchemePrefix in class AbstractDataResource
Returns:
the scheme-prefix of this resource.

getPath

public String getPath()
This method gets the path of this resource. Please note that the path is including the name of the resource.
ATTENTION:
The result of this method may differ to the path used in the URL when this resource has been created.

Specified by:
getPath in interface DataResource
Overrides:
getPath in class AbstractDataResource
Returns:
the path that was used to identify this resource when creating.

getSize

public long getSize()
             throws ResourceNotAvailableException
This method gets the size (content-length) of this resource. This is a default implementation. Override if there is a more performing way to implement this.

Specified by:
getSize in interface DataResource
Overrides:
getSize in class AbstractDataResource
Returns:
the size of this resource.
Throws:
ResourceNotAvailableException - if this resource is NOT available.

openStream

public InputStream openStream()
This method opens this resource for reading. This is a default implementation. Override if there is a more performing way to implement this.

Specified by:
openStream in interface DataResource
Overrides:
openStream in class AbstractDataResource
Returns:
the input stream where to read from.
See Also:
URL.openStream()

openOutputStream

public OutputStream openOutputStream()
                              throws ResourceNotWritableException
This method opens an output-stream in order to write data to the resource.

Specified by:
openOutputStream in interface DataResource
Overrides:
openOutputStream in class AbstractDataResource
Returns:
the OutputStream to write to the resource.
Throws:
ResourceNotWritableException - if the resource is NOT writable (e.g. read-only).

getUrl

public URL getUrl()
           throws ResourceNotAvailableException
This method gets this resource as URL.

Returns:
the URL that represents this resource.
Throws:
ResourceNotAvailableException - if an URL can NOT be created because the represented resource does not exist.

getUri

public String getUri()
This method gets a string identifying this DataResource. In most cases this will be the same as string-representation of the URL. However this method will not throw an exception.

Specified by:
getUri in interface DataResource
Overrides:
getUri in class AbstractDataResource
Returns:
the URI as string.

getName

public String getName()
This method gets the name of the resource. It is analog to a filename.

Specified by:
getName in interface DataResource
Overrides:
getName in class AbstractDataResource
Returns:
the name of the resource.

isData

public boolean isData()
This method determines if this resource has potentially data available. Unlike DataResource.isAvailable() this method will not invoke expensive operations like connecting to remote URLs. If this method will return false, then DataResource.isAvailable() would also have returned false. However in case of true only DataResource.isAvailable() can guarantee if a resource really exists and contains data. E.g. if the resource points to a File then this method can check if it is a data-file. So in case it points to a directory or does not exist at all in the filesystem, this method will return false. Please also note that this may invoke expensive operations if the according directory path points to something like a network share. You should also be aware that the state of DataResource.isData() and DataResource.isAvailable() can change at any time so you never have a full guarantee if some data exists or NOT. However in most cases it is very improbable that this status changes when you read the resource immediately after the check.

Returns:
true if this resource points to potential data, false otherwise.

isFolder

public boolean isFolder()
This method determines if this BrowsableResource is a folder that potentially contains other resources. Otherwise if this is no folder, BrowsableResource.getChildResources() will be empty (return an empty Iterable). However BrowsableResource.getChildResources() can also be empty, if this is a folder.
In order to determine if this BrowsableResource is containing data, please use DataResource.isAvailable(). Please note that BrowsableResource.isFolder() and DataResource.isAvailable() can both return true or both return false.

Returns:
true if this is a folder, false otherwise.

getLastModificationDate

public Date getLastModificationDate()
This method gets the last modification date of the DataResource if available and supported.

Returns:
the last modification Date or null if not available or supported.

navigate

public BrowsableResource navigate(String resourcePath)
This method creates a new DataResource pointing to the given resourcePath based on this resource.
E.g. if this resource points to the file "/etc/init.d/rc" and relativePath would be "../apt/sources.list" the resulting resource would point to "/etc/apt/sources.list".

Parameters:
resourcePath - is the absolute or relative path pointing to a new resource. If it is a relative path, it is interpreted relative to the parent URI (directory) of this resource.
Returns:
is the resource pointing to the given path (relative to this resource).

getChildResources

public Iterable<BrowsableResource> getChildResources()
This method iterates the immediate child-resources contained in this BrowsableResource.
If this is no folder, this method will always return an empty Iterable.

Returns:
an Iterable of the child-resources.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.