FileDocCategorySizeDatePackage
FileLocator.javaAPI DocAzureus 3.0.3.418536Fri Mar 12 11:00:20 GMT 2004org.pf.file

FileLocator

public class FileLocator extends Object
This class mainly supports access to files which can be in the normal file directory structure or inside zip archives. The main purpose is to provide methods that transparently treat files the same way whether they are in the normal directory structure or inside archives. The syntax is simply to allow archive names in a path name at any place where a sub-directory name can be.
Examples:
  • d:\temp\archive.zip\config\nls.properties
  • /usr/java/jdk1.3/src.jar/java/io/File.java
author
Manfred Duchrow
version
1.3

Fields Summary
private static final boolean
DEBUG
private static final String
FILE_PROTOCOL_INDICATOR
private static final String
ARCHIVE_INDICATOR
private FileLocator
parent
private File
file
private ZipFile
zipFile
private boolean
exists
private Exception
exception
Constructors Summary
private FileLocator()
Initialize the new instance with default values.

    super() ;
  
Methods Summary
protected java.util.zip.ZipFilearchive()
Returns the file this locator presents as opened zip file or null in any case of error.

		if ( this.getZipFile() == null )
		{
			this.setZipFile( new ZipFile( this.fileRef() ) ) ;
		}
		return this.getZipFile() ;
	
protected java.util.zip.ZipEntryarchiveEntry()

		return this.entryFromArchive( this.getFile().getPath() ) ;
	
protected java.util.zip.ZipFilecontainer()
Returns the zip file which is presented by the parent container or null in any case of error.

		if ( this.isInArchive() )
			return this.getParent().archive() ;
		else
			return null ;
	
protected java.io.FileconvertFromURLSyntax(java.io.File file)

		String newStr ;
		
		newStr = file.getPath().substring( FILE_PROTOCOL_INDICATOR.length() ) ;
		newStr = str().replaceAll( newStr, ARCHIVE_INDICATOR, File.separator ) ;
		
		return new File( newStr ) ;
	
public static org.pf.file.FileLocatorcreate(java.io.File file)
Create a file locator that corresponds to the given file name.

  	FileLocator locator = new FileLocator() ;
		
		return locator.createFrom( file ) ;
  
public static org.pf.file.FileLocatorcreate(java.lang.String filename)
Create a file locator that corresponds to the given file name.

		return create( new File( filename ) ) ;
  
protected org.pf.file.FileLocatorcreateFrom(java.io.File filePath)

		FileLocator locator	= null ;
		String[] parts 			= null ;
		File path						= filePath ;
		
		if ( path.getPath().startsWith( FILE_PROTOCOL_INDICATOR ) )
			path = this.convertFromURLSyntax( path ) ;
			
		parts = str().parts( path.getPath(), File.separator ) ;
		try
		{
		 	locator = this.initFromPath( parts, path.getPath().startsWith( File.separator ) ) ;
		}
		catch ( Exception ex )
		{
			this.setException( ex ) ;
			this.doesNotExist( path ) ;
			locator = this ;
		}
		return locator ;
	
private org.pf.file.FileLocatorcreateFrom(org.pf.file.FileLocator aParent, java.lang.String[] pathElements)

  	this.setParent( aParent ) ;
  	return this.initFromPath( pathElements, false ) ;
  
protected booleandoesElementExist(java.io.File element)

		if ( this.isInArchive() )
		{
			return doesElementExistInArchive( element.getPath() ) ;			
		}
		else
		{
			return element.exists() ;
		}
	
protected booleandoesElementExistInArchive(java.lang.String elementName)

		ZipEntry entry ;

		entry = this.entryFromArchive( elementName ) ;
				
		return ( entry != null ) ;
	
protected voiddoesNotExist(java.io.File file)

		this.setExists( false ) ;
		this.setFile( file ) ;
	
protected java.util.zip.ZipEntryentryFromArchive(java.lang.String elementName)

		ZipEntry entry ;
		ZipFile archive ;
		String name ;
		
		name = str().replaceAll( elementName, "\\", "/" ) ;
		archive = this.container() ;
		entry = archive.getEntry( name ) ;

		if (DEBUG)
		{
			// if ( entry == null ) com.mdcs.joi.Inspector.inspect( name ) ;
			System.out.print( archive.getName() + "::" + name + " --- "
								+ ( entry != null ) ) ; 
			if ( entry == null )
			{
				System.out.println() ;				
			}
			else
			{
				System.out.print( " (" + entry.getSize()  + ")" ) ;
				System.out.print( " (T:" + entry.getTime()  + ")" ) ;
				System.out.println( " (" + ( entry.isDirectory() ? "Dir" : "File" ) + ")" ) ;
			}
		}
		
		return entry ;
	
public java.lang.Exceptionexception()
Returns the last exception that occured while using this locator or null, if no exception was thrown at all.

		return this.getException() ;
	
public booleanexists()
Returns whether or not the file specified by this locator exists.

		return this.getExists() ;
	
protected java.io.FilefileRef()

		InputStream archiveStream ;
		FileOutputStream fileStream ;
		ZipEntry entry ;
		File tempFile ;
		
		if ( this.isInArchive() )
		{
			entry = this.archiveEntry() ;
			archiveStream = this.container().getInputStream( entry ) ;
			tempFile = File.createTempFile( "FLOC_", ".xtr" ) ;
			tempFile.deleteOnExit() ;
			fileStream = new FileOutputStream( tempFile ) ;
			fileUtil().copyStream( archiveStream, fileStream ) ;
			return tempFile ;
		}
		else
		{
			return this.getFile() ;
		}
	
protected FileUtilfileUtil()

		return FileUtil.current() ;
	
protected java.io.FilefullFilePath(boolean absolute)

		File full ;
		
		if ( this.isInArchive() )
		{
			full = new File( 	this.getParent().fullFilePath( absolute ), 
												this.getFile().getPath() ) ;
		}
		else
		{
			if ( absolute )
				full = this.getFile().getAbsoluteFile() ;
			else
				full = this.getFile() ;
		}
		
		return full ;
	
public java.lang.StringgetAbsolutePath()
Returns the full absolute pathname.

		return this.fullFilePath( true ).getPath() ;
	
protected java.lang.ExceptiongetException()

      return exception ; 
protected booleangetExists()

      return exists ; 
protected java.io.FilegetFile()

      return file ; 
public java.io.InputStreamgetInputStream()
Returns an opened input stream on the file defined by this locator.

		ZipEntry entry ;
		
		if ( this.isInArchive() )
		{
			entry = this.archiveEntry() ;
			return this.container().getInputStream( entry ) ;
		}
		else
		{
			return new FileInputStream( this.getFile() ) ;
		} 
	
protected org.pf.file.FileLocatorgetParent()

      return parent ; 
public java.lang.StringgetPath()
Returns the full pathname.

		return this.fullFilePath( false ).getPath() ;
	
public java.lang.StringgetStandardizedAbsolutePath()
Returns the full absolute pathname in a standardized form. That is all ".." and "." elements are removed and forward slashes are used as separators of the remaining elements.

		return this.fileUtil().standardize( this.getAbsolutePath() ) ;
	
public java.lang.StringgetStandardizedPath()
Returns the full pathname in a standardized for. That is all ".." and "." elements are removed and forward slashes are used as separators of the remaining elements.

		return this.fileUtil().standardize( this.getPath() ) ;
	
protected java.util.zip.ZipFilegetZipFile()

      return zipFile ; 
protected org.pf.file.FileLocatorinitFromPath(java.lang.String[] parts, boolean startsFromRoot)

		FileLocator locator			= this ;
		File pathElement 				= null ;
		String[] rest						= null ;
		boolean elementExists		= false ;
		
		if ( startsFromRoot )
			pathElement = new File( File.separator ) ;
		
		for ( int i = 0 ; i < parts.length ; i++ )
		{
			if ( pathElement == null )
				pathElement = new File( parts[i] ) ;
			else
				pathElement = new File( pathElement, parts[i] ) ;

			elementExists = this.doesElementExist( pathElement ) ;
			
			if ( elementExists )
			{	
				this.setFile( pathElement ) ;
				if ( this.isFileElement( pathElement ) )
				{
					if ( DEBUG ) System.out.println( "Locator(" + pathElement + ")" ) ;
					if ( i < ( parts.length - 1 ) )  // Is not last element ? 
					{
						rest = str().copyFrom( parts, i + 1 ) ;
						// if (DEBUG) com.mdcs.joi.Inspector.inspect( "SubLocator", rest ) ;
						locator = FileLocator.newWith( this, rest ) ;
					}
					break ;
				}
			}
			else
			{
				if ( this.isInArchive() )
				{
					if ( i < ( parts.length - 1 ) )  // Is not last element ? 
					{
						// Directories are not always identifiable individually in zip archives.
						// Therefore it must be accepted that they are not found.
						// So in such case no exception will be thrown.
					}
					else
					{
						throw new Exception( "\"" + pathElement.getPath() + "\" does not exist" );
					}
				}
				else
				{
					throw new Exception( "\"" + pathElement.getPath() + "\" does not exist" );
				}
			}
		}
		return locator ;
	
public booleanisDirectory()
Returns whether or not the name specified by this locator points to a directory.

		try 
		{
			if ( this.exists() )
				return ! this.isFileElement( this.getFile() ) ;
			else
				return false ;
		} 
		catch(Exception e) 
		{
			return false ;
		}
	
public booleanisFile()
Returns whether or not the name specified by this locator points to a file.

		try 
		{
			if ( this.exists() )
				return this.isFileElement( this.getFile() ) ;
			else
				return false ;
		} 
		catch(Exception e) 
		{
			return false ;
		}
	
protected booleanisFileElement(java.io.File element)

		if ( this.isInArchive() )
		{
			return isFileInArchive( element.getPath() ) ;			
		}
		else
		{
			return element.isFile() ;
		}
	
protected booleanisFileInArchive(java.lang.String elementName)

		ZipEntry entry ;
		entry = this.entryFromArchive( elementName ) ;
		
		// Unfortunately entry.isDirectory() returns false even for
		// pure directory entries inside a zip archive, so it can't be used here.
		// The trick below is problematic, because apart from 
		// directories it will also not recognize files with size 0.
		
		return ( entry != null ) && ( entry.getSize() > 0 ) ;
	
public booleanisInArchive()
Returns whether or not the file specified by this locator is inside an archive.

		return this.getParent() != null ;
	
public longlastModified()
Returns the timestamp of when the file was last modified or 0 in any case of error.

		ZipEntry entry ;
		
		try 
		{
			if ( this.isInArchive() )
			{
				entry = this.archiveEntry() ;
				return entry.getTime() ;
			}
			else
			{
				return this.getFile().lastModified() ;
			} 
		}
		catch(Exception ex) 
		{
			if ( DEBUG ) ex.printStackTrace() ;
			return 0L ;
		} 
	
private static org.pf.file.FileLocatornewWith(org.pf.file.FileLocator aParent, java.lang.String[] pathElements)

  	FileLocator locator = new FileLocator() ;
		
		return locator.createFrom( aParent, pathElements ) ;
  
public java.io.FilerealFile()
Returns the file that contains the data the locator points to. If the locator points to a normal file in a directory, than this file will be returned. If the locator points to a file inside an archive, the file will be unzipped into the temp directory and this temp file will be returned. If the locator points to a none existing file, this method returns false.

		File aFile ;
		try
		{
			aFile = this.fileRef() ;
		}
		catch (Exception e)
		{
			aFile = null ;
		}
		return aFile ;
	
protected voidsetException(java.lang.Exception newValue)

 exception = newValue ; 
protected voidsetExists(boolean newValue)

 exists = newValue ; 
protected voidsetFile(java.io.File newValue)

 file = newValue ; 
protected voidsetParent(org.pf.file.FileLocator newValue)

 parent = newValue ; 
protected voidsetZipFile(java.util.zip.ZipFile newValue)

 zipFile = newValue ; 
public longsize()
Returns the size of the file or 0 if it does not exist.

		ZipEntry entry ;
		
		try 
		{
			if ( this.isInArchive() )
			{
				entry = this.archiveEntry() ;
				// if ( DEBUG ) com.mdcs.joi.Inspector.inspectWait( entry ) ;
				return entry.getSize() ;
			}
			else
			{
				return this.getFile().length() ;
			} 
		}
		catch(Exception ex) 
		{
			if ( DEBUG ) ex.printStackTrace() ;
			return 0L ;
		} 
	
protected org.pf.text.StringUtilstr()

		return StringUtil.current() ;
	
public java.net.URLtoURL()
Returns the name of the file as an URL.

		StringBuffer buffer = new StringBuffer( 128 ) ;
		
		this.urlPath( buffer ) ;
		return new URL( buffer.toString() ) ;
	
protected voidurlPath(java.lang.StringBuffer buffer)

		if ( this.isInArchive() )
		{
			this.getParent().urlPath( buffer ) ; 
			buffer.append( ARCHIVE_INDICATOR ) ;
		}
		else
		{
			buffer.append( FILE_PROTOCOL_INDICATOR ) ;
		}		
		buffer.append( this.getFile().getPath() ) ;