com.borland.primetime.vfs
Class ZipFilesystem

java.lang.Object
  |
  +--com.borland.primetime.vfs.AbstractFilesystem
        |
        +--com.borland.primetime.vfs.ZipFilesystem

public class ZipFilesystem
extends AbstractFilesystem
implements Filesystem

The ZipFilesystem provides a direct mapping of Filesystem operations onto zip file entries. The protocol, host and anchor portions of a Url are ignored by the zip filesystem. It is assumed that the VFS will direct only "zip" protocol Url actions to this filesystem.

The file portion of the Url must be of the format [<zipfile>]/ to be correctly parsed by the zip filesystem. For example, the Url "zip:///[c:/foo.zip]/foo.java" refers to the file "foo.java" in the zip file c:/foo.zip. Like the file filesystem, all directory separators are forward slashes regardless of the underlying operating system.

Note that the use of the square bracket, which may also be a legal character in a filename on some file systems, is a potential problem in many cases. The presence of a ']' in the path to a zip file will prevent any access to the contents of the Zip, and ']' in a relative path has special meaning.


Field Summary
static ZipFilesystem FILESYSTEM
           
static java.lang.String PROTOCOL
           
 
Fields inherited from interface com.borland.primetime.vfs.Filesystem
TYPE_BOTH, TYPE_DIRECTORY, TYPE_FILE
 
Method Summary
 void delete(Url url)
          Always fails because zip filesystems are always read-only.
 boolean exists(Url url)
          Tests if the zip file entry represented by the specified Url actually exists.
 boolean fileEquals(Url url, Url otherUrl)
          Compares the file portion of two Url objects.
 int fileHashCode(Url url)
          Produces a hash value for the file portion of a Url compatible with the definition of equality provided by fileEquals.
 Url getAbsoluteUrl(java.lang.String newHost, java.lang.String absoluteFilePath)
          Assists in interpreting a relative path by creating a full Url for a zip path if it represents an absolute path.
 Url getCanonicalUrl(Url url)
          Reports the actual filename for zip filename portion of the specified Url.
 Url[] getChildren(Url url, RegularExpression[] patterns, int type)
          Retrieves a list of Urls representing file and directory entries within the directory represented by the specified Url.
 java.io.File getFileObject(Url url)
          Describes a Url in terms of a java.io.File instance.
 java.io.InputStream getInputStream(Url url)
          Creates an InputStream that reads from the zip file entry represented by the specified Url.
 long getLastModified(Url url)
          Reports the time at which the zip file entry represented by the specified Url was last modified.
 java.lang.String getName(Url url)
          Returns a short name for the Url.
 java.io.OutputStream getOutputStream(Url url, boolean makeBackup)
          Always fails because zip filesystems are always read-only.
 Url getParent(Url url)
          Returns a Url with the last subpath from the supplied Url's file path removed.
 java.lang.String getProtocol()
           
 java.lang.String getRelativePath(Url root, Url target, boolean onlyChildren)
          Create a relative path from an root Url to specified target Url.
 Url getRelativeUrl(Url root, java.lang.String relativePath)
          Create a new Url from an existing root Url and a relative component.
static void initOpenTool(byte majorVersion, byte minorVersion)
          OpenTool initialization
 boolean isDirectory(Url url)
          Tests if the zip path represented by the specified Url maps to a directory containing one of the actual zip entries.
 boolean isReadOnly(Url url)
          All entries in a zip filesystem are read only.
 boolean isValid(Url url)
          Determines whether the state of a Url instance is valid.
static java.lang.String stripArchivePrefix(java.lang.String filename)
          Will remove the name of the archive from the front of a string Example: [C:/JBUILDER/myclasses/Untitled1.jar]/untitled7/DataModule2.class will return "/untitled7/DataModule2.class"
 
Methods inherited from class com.borland.primetime.vfs.AbstractFilesystem
getChild
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROTOCOL

public static final java.lang.String PROTOCOL

FILESYSTEM

public static final ZipFilesystem FILESYSTEM
Method Detail

initOpenTool

public static void initOpenTool(byte majorVersion,
                                byte minorVersion)
OpenTool initialization

getProtocol

public java.lang.String getProtocol()
Specified by:
getProtocol in interface Filesystem

getInputStream

public java.io.InputStream getInputStream(Url url)
                                   throws java.io.IOException
Creates an InputStream that reads from the zip file entry represented by the specified Url.
Specified by:
getInputStream in interface Filesystem
Parameters:
url - The zip file entry to be read.
Returns:
The InputStream.
Throws:
java.io.IOException - If the Url specifies a resource that could not be read.

getOutputStream

public java.io.OutputStream getOutputStream(Url url,
                                            boolean makeBackup)
                                     throws java.io.IOException
Always fails because zip filesystems are always read-only.
Specified by:
getOutputStream in interface Filesystem
Parameters:
url - Ignored.
makeBackup - Ignored.
Returns:
This method never returns.
Throws:
java.io.IOException - Always.

isReadOnly

public boolean isReadOnly(Url url)
All entries in a zip filesystem are read only.
Specified by:
isReadOnly in interface Filesystem
Parameters:
url - Ignored.
Returns:
True.

getLastModified

public long getLastModified(Url url)
Reports the time at which the zip file entry represented by the specified Url was last modified.
Specified by:
getLastModified in interface Filesystem
Parameters:
url - The zip entry to be tested.
Returns:
The zip file entry's timestamp, or Buffer.MODIFIED_NEVER if the zip file entry does not exist.

getCanonicalUrl

public Url getCanonicalUrl(Url url)
Reports the actual filename for zip filename portion of the specified Url. On Win32 this may result in a change in capitalization of the first portion of a zip Url, on Unix the canonical Url is identical to the original Url.
Specified by:
getCanonicalUrl in interface Filesystem
Overrides:
getCanonicalUrl in class AbstractFilesystem
Parameters:
url - The zip file and entry.
Returns:
The canonical representation of the zip file, plus the original entry portion. If no matching zip file exists, the original Url is returned.

exists

public boolean exists(Url url)
Tests if the zip file entry represented by the specified Url actually exists.
Specified by:
exists in interface Filesystem
Parameters:
url - The zip file entry to be tested.
Returns:
True if the entry exists, false otherwise.

delete

public void delete(Url url)
            throws java.io.IOException
Always fails because zip filesystems are always read-only.
Specified by:
delete in interface Filesystem
Parameters:
url - Ignored.
Throws:
java.io.IOException - Always.

isDirectory

public boolean isDirectory(Url url)
Tests if the zip path represented by the specified Url maps to a directory containing one of the actual zip entries.
Specified by:
isDirectory in interface Filesystem
Parameters:
url - The zip path to be tested.
Returns:
True if the specified path contains zip entries, false otherwise.

getChildren

public Url[] getChildren(Url url,
                         RegularExpression[] patterns,
                         int type)
Retrieves a list of Urls representing file and directory entries within the directory represented by the specified Url.
Specified by:
getChildren in interface Filesystem
Parameters:
url - The logical directory to scan.
pattern - An array of regular expressions constraining the set of values that will be returned. Only children whose final path component exactly matches at least one of the patterns will be represented in the resulting array. Note that a null array matches all children and that directories are not compared against the patterns, only files.
type - The constants TYPE_DIRECTORY, TYPE_FILE, and TYPE_BOTH can be used to control the type of resources that are represented in the result array.
Returns:
An array of valid Url objects representing file and directorie entries. If none exist, an empty Url array is returned.

getParent

public Url getParent(Url url)
Returns a Url with the last subpath from the supplied Url's file path removed. The anchor for the parent Url is always set to null, but the filesystem and protocol are typically inherited from the supplied Url.

The parent of a ZipFilesystem Url may belong to the FileFilesystem if the closing square bracket is removed along with the final subpath.

Specified by:
getParent in interface Filesystem
Overrides:
getParent in class AbstractFilesystem
Parameters:
url - The Url for which a parent is desired.
Returns:
The Url that serves as a logical parent the the supplied Url, or null if there is no parent Url, such as when the file path contains only a single subpath.

getAbsoluteUrl

public Url getAbsoluteUrl(java.lang.String newHost,
                          java.lang.String absoluteFilePath)
Assists in interpreting a relative path by creating a full Url for a zip path if it represents an absolute path.
Overrides:
getAbsoluteUrl in class AbstractFilesystem
Returns:
The Url defined by the absolute path provided, or null if the path is not actually an absolute path.

getName

public java.lang.String getName(Url url)
Returns a short name for the Url.
Specified by:
getName in interface Filesystem
Overrides:
getName in class AbstractFilesystem
Returns:
The last subpath in the file portion of the Url.

getFileObject

public java.io.File getFileObject(Url url)
Describes a Url in terms of a java.io.File instance. This will result in a reference to the outer archive file regardless of the actual internal reference since there is only one physical file present.
Specified by:
getFileObject in interface Filesystem
Overrides:
getFileObject in class AbstractFilesystem
Parameters:
url - The Url in question.
Returns:
An appropriate File object representation, or null if no equivelant file exists.

getRelativeUrl

public Url getRelativeUrl(Url root,
                          java.lang.String relativePath)
Create a new Url from an existing root Url and a relative component. This method doesn't need to process the anchor portion of the relative path since it has already been removed by the VFS.

Deals with the additional complications of zip-relative pathing, which may start from a zip and "escape" by backing up enough times that the path no longer has a trailing square bracket. The overridden getChild detects this case.

Deals with absolute paths by using the FileFilesystem implementation of getAbsoluteUrl.

Specified by:
getRelativeUrl in interface Filesystem
Overrides:
getRelativeUrl in class AbstractFilesystem
Parameters:
root - The Url root.
relativePath - The relative path from the root.
Returns:
The newly derived Url.

fileHashCode

public int fileHashCode(Url url)
Produces a hash value for the file portion of a Url compatible with the definition of equality provided by fileEquals.
Specified by:
fileHashCode in interface Filesystem
Overrides:
fileHashCode in class AbstractFilesystem
Parameters:
url - The Url to produce a hash value based on.
Returns:
The appropriate hash value.

fileEquals

public boolean fileEquals(Url url,
                          Url otherUrl)
Compares the file portion of two Url objects. The path to the zip file itself is compared according to platform-specific filesystem rules, while the remainder of the path is always compared in a case-sensitive manner.
Specified by:
fileEquals in interface Filesystem
Overrides:
fileEquals in class AbstractFilesystem
Parameters:
url - The first of the two Urls to be compared.
otherUrl - The second of the two Urls to be compared.
Returns:
True if the file portions of the two Urls are identical, false otherwise.

isValid

public boolean isValid(Url url)
Determines whether the state of a Url instance is valid. In addition to the basic requirements, the Url must not have a host component, the file component must begin with an open square bracket and must contain a closing square bracket.
Specified by:
isValid in interface Filesystem
Overrides:
isValid in class AbstractFilesystem
Parameters:
url - The Url in question.
Returns:
True if the Url is valid, false otherwise.

getRelativePath

public java.lang.String getRelativePath(Url root,
                                        Url target,
                                        boolean onlyChildren)
Create a relative path from an root Url to specified target Url. The anchor portions are taken care of by the VFS and are guaranteed to be null in this method.
Specified by:
getRelativePath in interface Filesystem
Overrides:
getRelativePath in class AbstractFilesystem
Parameters:
root - The Url root.
target - The Url target.
onlyChildren - True if the target must be a descendant of the root, false if absolute paths and paths with leading "../" entries should be considered.
Returns:
The relative path to the target Url from this Url, or null if there is no valid path from the root Url to the target Url.

stripArchivePrefix

public static java.lang.String stripArchivePrefix(java.lang.String filename)
Will remove the name of the archive from the front of a string Example: [C:/JBUILDER/myclasses/Untitled1.jar]/untitled7/DataModule2.class will return "/untitled7/DataModule2.class"