📄 filelocator.java
字号:
} // exception()
// -------------------------------------------------------------------------
/**
* Returns the name of the file as an URL.
*/
public URL toURL()
throws MalformedURLException
{
StringBuffer buffer = new StringBuffer( 128 ) ;
this.urlPath( buffer ) ;
return new URL( buffer.toString() ) ;
} // toURL()
// -------------------------------------------------------------------------
// =========================================================================
// PROTECTED INSTANCE METHODS
// =========================================================================
protected FileLocator createFrom( 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 ;
} // createFrom()
// -------------------------------------------------------------------------
private FileLocator createFrom( FileLocator aParent, String[] pathElements )
throws Exception
{
this.setParent( aParent ) ;
return this.initFromPath( pathElements, false ) ;
} // createFrom()
// -------------------------------------------------------------------------
protected FileLocator initFromPath( String[] parts, boolean startsFromRoot )
throws Exception
{
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 ;
} // initFromPath()
// -------------------------------------------------------------------------
protected boolean doesElementExist( File element )
throws Exception
{
if ( this.isInArchive() )
{
return doesElementExistInArchive( element.getPath() ) ;
}
else
{
return element.exists() ;
}
} // doesElementExist()
// -------------------------------------------------------------------------
protected boolean isFileElement( File element )
throws Exception
{
if ( this.isInArchive() )
{
return isFileInArchive( element.getPath() ) ;
}
else
{
return element.isFile() ;
}
} // isFileElement()
// -------------------------------------------------------------------------
protected boolean doesElementExistInArchive( String elementName )
throws Exception
{
ZipEntry entry ;
entry = this.entryFromArchive( elementName ) ;
return ( entry != null ) ;
} // doesElementExistInArchive()
// -------------------------------------------------------------------------
protected boolean isFileInArchive( String elementName )
throws Exception
{
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 ) ;
} // isFileInArchive()
// -------------------------------------------------------------------------
protected ZipEntry entryFromArchive( String elementName )
throws Exception
{
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 ;
} // entryFromArchive()
// -------------------------------------------------------------------------
protected ZipEntry archiveEntry()
throws Exception
{
return this.entryFromArchive( this.getFile().getPath() ) ;
} // archiveEntry()
// -------------------------------------------------------------------------
protected void doesNotExist( File file )
{
this.setExists( false ) ;
this.setFile( file ) ;
} // doesNoTExist()
// -------------------------------------------------------------------------
protected File fullFilePath( 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 ;
} // fullFilePath()
// -------------------------------------------------------------------------
protected void urlPath( StringBuffer buffer )
{
if ( this.isInArchive() )
{
this.getParent().urlPath( buffer ) ;
buffer.append( ARCHIVE_INDICATOR ) ;
}
else
{
buffer.append( FILE_PROTOCOL_INDICATOR ) ;
}
buffer.append( this.getFile().getPath() ) ;
} // urlPath()
// -------------------------------------------------------------------------
protected File fileRef()
throws Exception
{
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() ;
}
} // fileRef()
// -------------------------------------------------------------------------
/**
* Returns the file this locator presents as opened zip file or
* null in any case of error.
*/
protected ZipFile archive()
throws Exception
{
if ( this.getZipFile() == null )
{
this.setZipFile( new ZipFile( this.fileRef() ) ) ;
}
return this.getZipFile() ;
} // archive()
// -------------------------------------------------------------------------
/**
* Returns the zip file which is presented by the parent container
* or null in any case of error.
*/
protected ZipFile container()
throws Exception
{
if ( this.isInArchive() )
return this.getParent().archive() ;
else
return null ;
} // container()
// -------------------------------------------------------------------------
protected File convertFromURLSyntax( File file)
{
String newStr ;
newStr = file.getPath().substring( FILE_PROTOCOL_INDICATOR.length() ) ;
newStr = str().replaceAll( newStr, ARCHIVE_INDICATOR, File.separator ) ;
return new File( newStr ) ;
} // convertFromURLSyntax()
// -------------------------------------------------------------------------
protected StringUtil str()
{
return StringUtil.current() ;
} // str()
// -------------------------------------------------------------------------
protected FileUtil fileUtil()
{
return FileUtil.current() ;
} // fileUtil()
// -------------------------------------------------------------------------
} // class FileLocator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -