adammurdoch 2003/01/23 04:27:28 Modified: vfs/src/java/org/apache/commons/vfs FileName.java vfs/src/java/org/apache/commons/vfs/impl VirtualFileProvider.java vfs/src/java/org/apache/commons/vfs/provider AbstractLayeredFileProvider.java AbstractOriginatingFileProvider.java DefaultFileName.java GenericUri.java UriParser.java vfs/src/java/org/apache/commons/vfs/provider/ftp FtpFileSystem.java FtpFileSystemProvider.java FtpUri.java vfs/src/java/org/apache/commons/vfs/provider/jar JarFileSystem.java JarFileSystemProvider.java vfs/src/java/org/apache/commons/vfs/provider/local DefaultLocalFileSystemProvider.java LocalFileNameParser.java LocalFileUri.java vfs/src/java/org/apache/commons/vfs/provider/smb SmbFileSystemProvider.java SmbUri.java vfs/src/java/org/apache/commons/vfs/provider/temp TemporaryFileProvider.java vfs/src/java/org/apache/commons/vfs/provider/url UrlFileProvider.java vfs/src/java/org/apache/commons/vfs/provider/zip ZipFileNameParser.java ZipFileSystem.java ZipFileSystemProvider.java Added: vfs/src/java/org/apache/commons/vfs/provider LayeredFileName.java Removed: vfs/src/java/org/apache/commons/vfs/provider Uri.java vfs/src/java/org/apache/commons/vfs/provider/ftp FtpFileNameParser.java vfs/src/java/org/apache/commons/vfs/provider/smb SmbFileNameParser.java Log: First pass at combining the FileName, Uri, and UriParser hierarchies. Suggested by Jeff Barrett. - DefaultFileName now extends UriParser (temporarily). Moved some methods down from UriParser. - Merged Uri into DefaultFileName. - GenericUri and LocalFileUri now extend DefaultFileName. - Merged FtpFileNameParser into FtpUri. - Merged SmbFileNameParser into SmbUri. Revision Changes Path 1.9 +5 -0 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileName.java Index: FileName.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileName.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- FileName.java 23 Jan 2003 04:33:34 -0000 1.8 +++ FileName.java 23 Jan 2003 12:27:23 -0000 1.9 @@ -110,6 +110,11 @@ int getDepth(); /** + * Returns the URI scheme of this file. + */ + String getScheme(); + + /** * Returns the absolute URI of this file. */ String getURI(); 1.3 +7 -6 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/VirtualFileProvider.java Index: VirtualFileProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/VirtualFileProvider.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- VirtualFileProvider.java 23 Nov 2002 00:05:26 -0000 1.2 +++ VirtualFileProvider.java 23 Jan 2003 12:27:23 -0000 1.3 @@ -60,7 +60,6 @@ import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.provider.AbstractVfsContainer; import org.apache.commons.vfs.provider.DefaultFileName; -import org.apache.commons.vfs.provider.UriParser; /** @@ -72,15 +71,16 @@ public class VirtualFileProvider extends AbstractVfsContainer { - private final UriParser parser = new UriParser(); - /** * Creates a virtual file system, with the supplied file as its root. */ public FileObject createFileSystem( final FileObject rootFile ) throws FileSystemException { - final FileName rootName = new DefaultFileName( parser, rootFile.getName().getURI(), FileName.ROOT_PATH ); + final FileName rootName = + new DefaultFileName( rootFile.getName().getScheme(), + rootFile.getName().getURI(), + FileName.ROOT_PATH ); final VirtualFileSystem fs = new VirtualFileSystem( rootName ); addComponent( fs ); fs.addJunction( FileName.ROOT_PATH, rootFile ); @@ -92,7 +92,8 @@ */ public FileObject createFileSystem( final String rootUri ) throws FileSystemException { - final FileName rootName = new DefaultFileName( parser, rootUri, FileName.ROOT_PATH ); + final FileName rootName = + new DefaultFileName( rootUri, FileName.ROOT_PATH ); final VirtualFileSystem fs = new VirtualFileSystem( rootName ); addComponent( fs ); return fs.getRoot(); 1.7 +6 -6 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractLayeredFileProvider.java Index: AbstractLayeredFileProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractLayeredFileProvider.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- AbstractLayeredFileProvider.java 23 Nov 2002 00:41:09 -0000 1.6 +++ AbstractLayeredFileProvider.java 23 Jan 2003 12:27:23 -0000 1.7 @@ -78,19 +78,19 @@ final String uri ) throws FileSystemException { // Split the URI up into its parts - final Uri parsedUri = parseUri( uri ); + final LayeredFileName name = (LayeredFileName)parseUri( uri ); // Make the URI canonical // Resolve the outer file name - final String fileName = parsedUri.getContainerUri(); + final String fileName = name.getOuterUri(); final FileObject file = getContext().resolveFile( baseFile, fileName ); // Create the file system - final FileObject rootFile = createFileSystem( parsedUri.getScheme(), file ); + final FileObject rootFile = createFileSystem( name.getScheme(), file ); // Resolve the file - return rootFile.resolveFile( parsedUri.getPath() ); + return rootFile.resolveFile( name.getPath() ); } /** @@ -128,6 +128,6 @@ * Parses an absolute URI. * @param uri The URI to parse. */ - protected abstract Uri parseUri( String uri ) + protected abstract FileName parseUri( String uri ) throws FileSystemException; } 1.9 +19 -16 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractOriginatingFileProvider.java Index: AbstractOriginatingFileProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractOriginatingFileProvider.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- AbstractOriginatingFileProvider.java 23 Nov 2002 00:33:53 -0000 1.8 +++ AbstractOriginatingFileProvider.java 23 Jan 2003 12:27:23 -0000 1.9 @@ -58,6 +58,7 @@ import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; +import org.apache.commons.vfs.FileName; /** * A {@link FileProvider} that handles physical files, such as the files in a @@ -80,54 +81,56 @@ final String uri ) throws FileSystemException { // Parse the URI - final Uri parsedUri; + final FileName name; try { - parsedUri = parseUri( uri ); + name = parseUri( uri ); } catch ( FileSystemException exc ) { - throw new FileSystemException( "vfs.provider/invalid-absolute-uri.error", new Object[]{uri}, exc ); + throw new FileSystemException( "vfs.provider/invalid-absolute-uri.error", uri, exc ); } // Locate the file - return findFile( parsedUri ); + return findFile( name ); } /** * Locates a file from its parsed URI. */ - private FileObject findFile( final Uri parsedUri ) + private FileObject findFile( final FileName name ) throws FileSystemException { // Check in the cache for the file system - final String rootUri = parsedUri.getContainerUri(); + final String rootUri = name.getRootURI(); FileSystem fs = findFileSystem( rootUri ); if ( fs == null ) { // Need to create the file system, and cache it - fs = doCreateFileSystem( parsedUri ); + fs = doCreateFileSystem( name ); addFileSystem( rootUri, fs ); } // Locate the file - return fs.resolveFile( parsedUri.getPath() ); + return fs.resolveFile( name.getPath() ); } /** - * Parses a URI into its components. The returned value is used to - * locate the file system in the cache (using the root prefix). + * Parses a URI. * - * <p>The provider can annotate this object with any additional - * information it requires to create a file system from the URI. + * @return The name of the file. This name is used to locate the file + * system in the cache, using the root URI. This name is also + * passed to {@link #doCreateFileSystem} to create the file system. */ - protected abstract Uri parseUri( final String uri ) + protected abstract FileName parseUri( final String uri ) throws FileSystemException; /** - * Creates the filesystem. The file system may implement {@link VfsComponent}. + * Creates a filesystem. The file system may implement {@link VfsComponent}. + * + * @param name The name of the file to create the file system for. */ - protected abstract FileSystem doCreateFileSystem( final Uri uri ) + protected abstract FileSystem doCreateFileSystem( final FileName name ) throws FileSystemException; } 1.7 +233 -23 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultFileName.java Index: DefaultFileName.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultFileName.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- DefaultFileName.java 23 Jan 2003 04:33:34 -0000 1.6 +++ DefaultFileName.java 23 Jan 2003 12:27:23 -0000 1.7 @@ -65,32 +65,81 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> * @version $Revision$ $Date$ */ -public final class DefaultFileName +public class DefaultFileName + extends UriParser implements FileName { - private final UriParser parser; - private final String rootPrefix; - private final String absPath; + private static final char separatorChar = '/'; + private static final String separator = "/"; + + private String scheme; + private String rootUri; + private String absPath; // Cached stuff private String uri; private String baseName; - public DefaultFileName( final UriParser parser, + public DefaultFileName( final String scheme, final String rootPrefix, final String absPath ) { - this.parser = parser; - this.rootPrefix = rootPrefix; + setScheme( scheme ); + setRootURI( rootPrefix ); + setPath( absPath ); + } + + public DefaultFileName( final String rootUri, + final String absPath ) + { + this( extractScheme( rootUri ), rootUri, absPath ); + } + + /** + * @todo Get rid of this and make fields final again. + */ + protected DefaultFileName() + { + } + + /** + * Sets the scheme for this filename. + */ + protected void setScheme( final String scheme ) + { + this.scheme = scheme; + } + + /** + * Sets the path for this filename. + */ + protected void setPath( final String absPath ) + { this.absPath = absPath; } /** + * Sets the root URI for this filename. + */ + protected void setRootURI( final String uri ) + { + // Remove trailing separator, if any + if ( uri.endsWith( separator ) ) + { + this.rootUri = uri.substring( 0, uri.length() - 1 ); + } + else + { + this.rootUri = uri; + } + } + + /** * Returns the hashcode for this name. */ public int hashCode() { - return ( rootPrefix.hashCode() ^ absPath.hashCode() ); + return ( rootUri.hashCode() ^ absPath.hashCode() ); } /** @@ -99,7 +148,7 @@ public boolean equals( final Object obj ) { final DefaultFileName name = (DefaultFileName)obj; - return ( rootPrefix.equals( name.rootPrefix ) && absPath.equals( absPath ) ); + return ( rootUri.equals( name.rootUri ) && absPath.equals( absPath ) ); } /** @@ -111,14 +160,32 @@ } /** + * Factory method for creating name instances. Can be overridden. + * @param absPath + */ + protected FileName createName( final String absPath ) + { + return new DefaultFileName( scheme, rootUri, absPath ); + } + + /** * Returns the base name of the file. */ public String getBaseName() { if ( baseName == null ) { - baseName = parser.getBaseName( absPath ); + final int idx = absPath.lastIndexOf( separatorChar ); + if ( idx == -1 ) + { + baseName = absPath; + } + else + { + baseName = absPath.substring( idx + 1 ); + } } + return baseName; } @@ -138,8 +205,13 @@ final NameScope scope ) throws FileSystemException { - final String otherAbsPath = parser.resolvePath( absPath, name, scope ); - return new DefaultFileName( parser, rootPrefix, otherAbsPath ); + final String resolvedPath = resolvePath( absPath, name ); + if ( !checkName( absPath, resolvedPath, scope ) ) + { + throw new FileSystemException( "vfs.provider/invalid-descendent-name.error", name ); + } + + return createName( resolvedPath ); } /** @@ -147,12 +219,23 @@ */ public FileName getParent() { - final String parentPath = parser.getParentPath( absPath ); - if ( parentPath == null ) + final String parentPath; + final int idx = absPath.lastIndexOf( separatorChar ); + if ( idx == -1 || idx == absPath.length() - 1 ) { + // No parent return null; } - return new DefaultFileName( parser, rootPrefix, parentPath ); + else if ( idx == 0 ) + { + // Root is the parent + parentPath = separator; + } + else + { + parentPath = absPath.substring( 0, idx ); + } + return createName( parentPath ); } /** @@ -167,13 +250,21 @@ } /** + * Returns the URI scheme of this file. + */ + public String getScheme() + { + return scheme; + } + + /** * Returns the absolute URI of the file. */ public String getURI() { if ( uri == null ) { - uri = parser.getUri( rootPrefix, absPath ); + uri = rootUri + absPath; } return uri; } @@ -183,7 +274,59 @@ */ public String getRelativeName( final FileName name ) throws FileSystemException { - return parser.makeRelative( absPath, name.getPath() ); + final String path = name.getPath(); + + // Calculate the common prefix + final int basePathLen = absPath.length(); + final int pathLen = path.length(); + + // Deal with root + if ( basePathLen == 1 && pathLen == 1 ) + { + return "."; + } + else if ( basePathLen == 1 ) + { + return path.substring( 1 ); + } + + final int maxlen = Math.min( basePathLen, pathLen ); + int pos = 0; + for ( ; pos < maxlen && absPath.charAt( pos ) == path.charAt( pos ); pos++ ) + { + } + + if ( pos == basePathLen && pos == pathLen ) + { + // Same names + return "."; + } + else if ( pos == basePathLen && pos < pathLen && path.charAt( pos ) == separatorChar ) + { + // A descendent of the base path + return path.substring( pos + 1 ); + } + + // Strip the common prefix off the path + final StringBuffer buffer = new StringBuffer(); + if ( pathLen > 1 && ( pos < pathLen || absPath.charAt( pos ) != separatorChar ) ) + { + // Not a direct ancestor, need to back up + pos = absPath.lastIndexOf( separatorChar, pos ); + buffer.append( path.substring( pos ) ); + } + + // Prepend a '../' for each element in the base path past the common + // prefix + buffer.insert( 0, ".." ); + pos = absPath.indexOf( separatorChar, pos + 1 ); + while ( pos != -1 ) + { + buffer.insert( 0, "../" ); + pos = absPath.indexOf( separatorChar, pos + 1 ); + } + + return buffer.toString(); } /** @@ -191,7 +334,7 @@ */ public String getRootURI() { - return rootPrefix; + return rootUri; } /** @@ -199,7 +342,17 @@ */ public int getDepth() { - return parser.getDepth( absPath ); + final int len = absPath.length(); + if ( len == 0 || ( len == 1 && absPath.charAt( 0 ) == separatorChar ) ) + { + return 0; + } + int depth = 1; + for ( int pos = 0; pos > -1 && pos < len; depth++ ) + { + pos = absPath.indexOf( separatorChar, pos + 1 ); + } + return depth; } /** @@ -225,11 +378,11 @@ */ public boolean isAncestor( final FileName ancestor ) { - if ( !ancestor.getRootURI().equals( rootPrefix ) ) + if ( !ancestor.getRootURI().equals( rootUri ) ) { return false; } - return parser.checkName( ancestor.getPath(), absPath, NameScope.DESCENDENT ); + return checkName( ancestor.getPath(), absPath, NameScope.DESCENDENT ); } /** @@ -246,10 +399,67 @@ public boolean isDescendent( final FileName descendent, final NameScope scope ) { - if ( !descendent.getRootURI().equals( rootPrefix ) ) + if ( !descendent.getRootURI().equals( rootUri ) ) + { + return false; + } + return checkName( absPath, descendent.getPath(), scope ); + } + + /** + * Checks whether a path fits in a particular scope of another path. + * + * @param basePath An absolute, normalised path. + * @param path An absolute, normalised path. + */ + private boolean checkName( final String basePath, + final String path, + final NameScope scope ) + { + if ( scope == NameScope.FILE_SYSTEM ) + { + // All good + return true; + } + + if ( !path.startsWith( basePath ) ) { return false; } - return parser.checkName( absPath, descendent.getPath(), scope ); + final int baseLen = basePath.length(); + + if ( scope == NameScope.CHILD ) + { + if ( path.length() == baseLen + || ( baseLen > 1 && path.charAt( baseLen ) != separatorChar ) + || path.indexOf( separatorChar, baseLen + 1 ) != -1 ) + { + return false; + } + } + else if ( scope == NameScope.DESCENDENT ) + { + if ( path.length() == baseLen + || ( baseLen > 1 && path.charAt( baseLen ) != separatorChar ) ) + { + return false; + } + } + else if ( scope == NameScope.DESCENDENT_OR_SELF ) + { + if ( baseLen > 1 + && path.length() > baseLen + && path.charAt( baseLen ) != separatorChar ) + { + return false; + } + } + else if ( scope != NameScope.FILE_SYSTEM ) + { + throw new IllegalArgumentException(); + } + + return true; } + } 1.2 +189 -2 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/GenericUri.java Index: GenericUri.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/GenericUri.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- GenericUri.java 31 Oct 2002 10:40:57 -0000 1.1 +++ GenericUri.java 23 Jan 2003 12:27:23 -0000 1.2 @@ -55,6 +55,8 @@ */ package org.apache.commons.vfs.provider; +import org.apache.commons.vfs.FileSystemException; + /** * A 'generic' URI, as per RFC 2396. Consists of a scheme, userinfo (typically * username and password), hostname, port, and path. @@ -63,7 +65,7 @@ * @version $Revision$ $Date$ */ public class GenericUri - extends Uri + extends DefaultFileName { private String userInfo; private String hostName; @@ -104,4 +106,189 @@ { this.port = port; } + + /** + * Parses a generic URI. Briefly, a generic URI looks like: + * + * <pre> + * <scheme> '://' [ <userinfo> '@' ] <hostname> [ ':' <port> ] '/' <path> + * </pre> + * + * <p>This method differs from the RFC, in that either / or \ is allowed + * as a path separator. + * + * @param uri + * The URI to parse. + */ + protected void parseGenericUri( final String uri ) + throws FileSystemException + { + final StringBuffer name = new StringBuffer(); + + // Extract the scheme and authority parts + extractToPath( uri, name ); + + // Decode and normalise the file name + decode( name, 0, name.length() ); + normalisePath( name ); + setPath( name.toString() ); + } + + /** + * Extracts the scheme, userinfo, hostname and port components of a + * generic URI. + * + * @param uri + * The absolute URI to parse. + * + * @param name + * Used to return the remainder of the URI. + */ + protected void extractToPath( final String uri, + final StringBuffer name ) + throws FileSystemException + { + // Extract the scheme + final String scheme = extractScheme( uri, name ); + setScheme( scheme ); + + // Expecting "//" + if ( name.length() < 2 || name.charAt( 0 ) != '/' || name.charAt( 1 ) != '/' ) + { + throw new FileSystemException( "vfs.provider/missing-double-slashes.error", uri ); + } + name.delete( 0, 2 ); + + // Extract userinfo + final String userInfo = extractUserInfo( name ); + setUserInfo( userInfo ); + + // Extract hostname, and normalise + final String hostName = extractHostName( name ); + if ( hostName == null ) + { + throw new FileSystemException( "vfs.provider/missing-hostname.error", uri ); + } + setHostName( hostName.toLowerCase() ); + + // Extract port + final String port = extractPort( name ); + if ( port != null && port.length() == 0 ) + { + throw new FileSystemException( "vfs.provider/missing-port.error", uri ); + } + setPort( port ); + + // Expecting '/' or empty name + if ( name.length() > 0 && name.charAt( 0 ) != '/' ) + { + throw new FileSystemException( "vfs.provider/missing-hostname-path-sep.error", uri ); + } + } + + /** + * Extracts the user info from a URI. The <scheme>:// part has been removed + * already. + */ + protected String extractUserInfo( final StringBuffer name ) + { + final int maxlen = name.length(); + for ( int pos = 0; pos < maxlen; pos++ ) + { + final char ch = name.charAt( pos ); + if ( ch == '@' ) + { + // Found the end of the user info + String userInfo = name.substring( 0, pos ); + name.delete( 0, pos + 1 ); + return userInfo; + } + if ( ch == '/' || ch == '?' ) + { + // Not allowed in user info + break; + } + } + + // Not found + return null; + } + + /** + * Extracts the hostname from a URI. The <scheme>://<userinfo>@ part has + * been removed. + */ + protected String extractHostName( final StringBuffer name ) + { + final int maxlen = name.length(); + int pos = 0; + for ( ; pos < maxlen; pos++ ) + { + final char ch = name.charAt( pos ); + if ( ch == '/' || ch == ';' || ch == '?' || ch == ':' + || ch == '@' || ch == '&' || ch == '=' || ch == '+' + || ch == '$' || ch == ',' ) + { + break; + } + } + if ( pos == 0 ) + { + return null; + } + + final String hostname = name.substring( 0, pos ); + name.delete( 0, pos ); + return hostname; + } + + /** + * Extracts the port from a URI. The <scheme>://<userinfo>@<hostname> + * part has been removed. + */ + protected String extractPort( final StringBuffer name ) + { + if ( name.length() < 1 || name.charAt( 0 ) != ':' ) + { + return null; + } + + final int maxlen = name.length(); + int pos = 1; + for ( ; pos < maxlen; pos++ ) + { + final char ch = name.charAt( pos ); + if ( ch < '0' || ch > '9' ) + { + break; + } + } + + final String port = name.substring( 1, pos ); + name.delete( 0, pos ); + return port; + } + + /** + * Assembles a generic URI, appending to the supplied StringBuffer. + */ + protected void appendRootUri( final StringBuffer rootUri ) + { + rootUri.append( getScheme() ); + rootUri.append( "://" ); + final String userInfo = getUserInfo(); + if ( userInfo != null && userInfo.length() != 0 ) + { + rootUri.append( userInfo ); + rootUri.append( "@" ); + } + rootUri.append( getHostName() ); + final String port = getPort(); + if ( port != null && port.length() > 0 ) + { + rootUri.append( ":" ); + rootUri.append( port ); + } + } + } 1.11 +0 -448 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/UriParser.java Index: UriParser.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/UriParser.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- UriParser.java 23 Nov 2002 00:41:09 -0000 1.10 +++ UriParser.java 23 Jan 2003 12:27:23 -0000 1.11 @@ -58,7 +58,6 @@ import java.util.HashSet; import java.util.Iterator; import org.apache.commons.vfs.FileSystemException; -import org.apache.commons.vfs.NameScope; /** * A name parser which parses absolute URIs. See RFC 2396 for details. @@ -70,7 +69,6 @@ { /** The normalised separator to use. */ private final char separatorChar; - private final String separator; /** * The set of valid separators. These are all converted to the normalised one. @@ -120,226 +118,6 @@ final Character ch = (Character)iter.next(); this.separators[ i ] = ch.charValue(); } - - separator = String.valueOf( separatorChar ); - } - - /** - * Parses an absolute URI, splitting it into its components. This - * implementation assumes a "generic URI", as defined by RFC 2396. See - * {@link #parseGenericUri} for more info. - */ - public GenericUri parseUri( final String uriStr ) throws FileSystemException - { - // Parse the URI - final GenericUri uri = new GenericUri(); - parseGenericUri( uriStr, uri ); - - // Build the root URI - final StringBuffer rootUri = new StringBuffer(); - appendRootUri( uri, rootUri ); - uri.setContainerUri( rootUri.toString() ); - - return uri; - } - - /** - * Assembles a generic URI, appending to the supplied StringBuffer. - */ - protected void appendRootUri( final GenericUri uri, final StringBuffer rootUri ) - { - rootUri.append( uri.getScheme() ); - rootUri.append( "://" ); - final String userInfo = uri.getUserInfo(); - if ( userInfo != null && userInfo.length() != 0 ) - { - rootUri.append( userInfo ); - rootUri.append( "@" ); - } - rootUri.append( uri.getHostName() ); - final String port = uri.getPort(); - if ( port != null && port.length() > 0 ) - { - rootUri.append( ":" ); - rootUri.append( port ); - } - } - - /** - * Parses a generic URI, as defined by RFC 2396. Briefly, a generic URI - * looks like: - * - * <pre> - * <scheme> '://' [ <userinfo> '@' ] <hostname> [ ':' <port> ] '/' <path> - * </pre> - * - * <p>This method differs from the RFC, in that either / or \ is allowed - * as a path separator. - * - * @param uriStr - * The URI to parse. - * @param uri - * Used to return the parsed components of the URI. - */ - protected void parseGenericUri( final String uriStr, - final GenericUri uri ) - throws FileSystemException - { - final StringBuffer name = new StringBuffer(); - - // Extract the scheme and authority parts - extractToPath( uriStr, name, uri ); - - // Decode and normalise the file name - decode( name, 0, name.length() ); - normalisePath( name ); - uri.setPath( name.toString() ); - - // Build the root uri - final StringBuffer rootUri = new StringBuffer(); - rootUri.append( uri.getScheme() ); - rootUri.append( "://" ); - rootUri.append( uri.getHostName() ); - uri.setContainerUri( rootUri.toString() ); - } - - /** - * Extracts the scheme, userinfo, hostname and port components of an - * absolute "generic URI". - * - * @param uri - * The absolute URI to parse. - * - * @param name - * Used to return the remainder of the URI. - * - * @param genericUri - * Used to return the extracted components. - */ - protected void extractToPath( final String uri, - final StringBuffer name, - final GenericUri genericUri ) - throws FileSystemException - { - // Extract the scheme - final String scheme = extractScheme( uri, name ); - genericUri.setScheme( scheme ); - - // Expecting "//" - if ( name.length() < 2 || name.charAt( 0 ) != '/' || name.charAt( 1 ) != '/' ) - { - throw new FileSystemException( "vfs.provider/missing-double-slashes.error", uri ); - } - name.delete( 0, 2 ); - - // Extract userinfo - final String userInfo = extractUserInfo( name ); - genericUri.setUserInfo( userInfo ); - - // Extract hostname, and normalise - final String hostName = extractHostName( name ); - if ( hostName == null ) - { - throw new FileSystemException( "vfs.provider/missing-hostname.error", uri ); - } - genericUri.setHostName( hostName.toLowerCase() ); - - // Extract port - final String port = extractPort( name ); - if ( port != null && port.length() == 0 ) - { - throw new FileSystemException( "vfs.provider/missing-port.error", uri ); - } - genericUri.setPort( port ); - - // Expecting '/' or empty name - if ( name.length() > 0 && name.charAt( 0 ) != '/' ) - { - throw new FileSystemException( "vfs.provider/missing-hostname-path-sep.error", uri ); - } - } - - /** - * Extracts the user info from a URI. The <scheme>:// part has been removed - * already. - */ - protected String extractUserInfo( final StringBuffer name ) - { - final int maxlen = name.length(); - for ( int pos = 0; pos < maxlen; pos++ ) - { - final char ch = name.charAt( pos ); - if ( ch == '@' ) - { - // Found the end of the user info - String userInfo = name.substring( 0, pos ); - name.delete( 0, pos + 1 ); - return userInfo; - } - if ( ch == '/' || ch == '?' ) - { - // Not allowed in user info - break; - } - } - - // Not found - return null; - } - - /** - * Extracts the hostname from a URI. The <scheme>://<userinfo>@ part has - * been removed. - */ - protected String extractHostName( final StringBuffer name ) - { - final int maxlen = name.length(); - int pos = 0; - for ( ; pos < maxlen; pos++ ) - { - final char ch = name.charAt( pos ); - if ( ch == '/' || ch == ';' || ch == '?' || ch == ':' - || ch == '@' || ch == '&' || ch == '=' || ch == '+' - || ch == '$' || ch == ',' ) - { - break; - } - } - if ( pos == 0 ) - { - return null; - } - - final String hostname = name.substring( 0, pos ); - name.delete( 0, pos ); - return hostname; - } - - /** - * Extracts the port from a URI. The <scheme>://<userinfo>@<hostname> - * part has been removed. - */ - protected String extractPort( final StringBuffer name ) - { - if ( name.length() < 1 || name.charAt( 0 ) != ':' ) - { - return null; - } - - final int maxlen = name.length(); - int pos = 1; - for ( ; pos < maxlen; pos++ ) - { - final char ch = name.charAt( pos ); - if ( ch < '0' || ch > '9' ) - { - break; - } - } - - final String port = name.substring( 1, pos ); - name.delete( 0, pos ); - return port; } /** @@ -375,48 +153,6 @@ } /** - * Builds a URI from a root URI and path. - * - * @param rootUri - * The root URI. - * - * @param path - * A <i>normalised</i> path. - */ - public String getUri( final String rootUri, - final String path ) - { - final StringBuffer uri = new StringBuffer( rootUri ); - final int len = uri.length(); - if ( uri.charAt( len - 1 ) == separatorChar ) - { - uri.delete( len - 1, len ); - } - if ( !path.startsWith( separator ) ) - { - uri.append( separatorChar ); - } - uri.append( path ); - return uri.toString(); - } - - /** - * Returns the base name of a path. - * - * @param path - * A <i>normalised</i> path. - */ - public String getBaseName( final String path ) - { - final int idx = path.lastIndexOf( separatorChar ); - if ( idx == -1 ) - { - return path; - } - return path.substring( idx + 1 ); - } - - /** * Resolves a path, relative to a base path. If the supplied path * is an absolute path, it is normalised and returned. If the supplied * path is a relative path, it is resolved relative to the base path. @@ -451,190 +187,6 @@ return buffer.toString(); } - /** - * Resolved a name, relative to a base file. - * - * @param baseFile - * A <i>normalised</i> path. - * - * @param path - * The path to resolve. - * - * @param scope - * The scope to resolve and validate the name in. - */ - public String resolvePath( final String baseFile, - final String path, - final NameScope scope ) - throws FileSystemException - { - final String resolvedPath = resolvePath( baseFile, path ); - if ( !checkName( baseFile, resolvedPath, scope ) ) - { - throw new FileSystemException( "vfs.provider/invalid-descendent-name.error", path ); - } - - return resolvedPath; - } - - /** - * Checks whether a path fits in a particular scope of another path. - * - * @param basePath An absolute, normalised path. - * @param path An absolute, normalised path. - */ - public boolean checkName( final String basePath, - final String path, - final NameScope scope ) - { - if ( scope == NameScope.FILE_SYSTEM ) - { - // All good - return true; - } - - if ( !path.startsWith( basePath ) ) - { - return false; - } - final int baseLen = basePath.length(); - - if ( scope == NameScope.CHILD ) - { - if ( path.length() == baseLen - || ( baseLen > 1 && path.charAt( baseLen ) != separatorChar ) - || path.indexOf( separatorChar, baseLen + 1 ) != -1 ) - { - return false; - } - } - else if ( scope == NameScope.DESCENDENT ) - { - if ( path.length() == baseLen - || ( baseLen > 1 && path.charAt( baseLen ) != separatorChar ) ) - { - return false; - } - } - else if ( scope == NameScope.DESCENDENT_OR_SELF ) - { - if ( baseLen > 1 - && path.length() > baseLen - && path.charAt( baseLen ) != separatorChar ) - { - return false; - } - } - else if ( scope != NameScope.FILE_SYSTEM ) - { - throw new IllegalArgumentException(); - } - - return true; - } - - /** - * Returns the depth of a path. - * - * @param path A normalised path. - */ - public int getDepth( final String path ) - { - final int len = path.length(); - if ( len == 0 || ( len == 1 && path.charAt( 0 ) == separatorChar ) ) - { - return 0; - } - int depth = 1; - for ( int pos = 0; pos > -1 && pos < len; depth++ ) - { - pos = path.indexOf( separatorChar, pos + 1 ); - } - return depth; - } - - /** - * Returns a parent path, or null if the path has no parent. - * - * @param path - * A <i>normalised</i> path. - */ - public String getParentPath( final String path ) - { - final int idx = path.lastIndexOf( separatorChar ); - if ( idx == -1 || idx == path.length() - 1 ) - { - // No parent - return null; - } - if ( idx == 0 ) - { - // Root is the parent - return separator; - } - return path.substring( 0, idx ); - } - - /** - * Converts an absolute path into a relative path. - * - * @param basePath The base path. - * @param path The path to convert. - */ - public String makeRelative( final String basePath, final String path ) - { - // Calculate the common prefix - final int basePathLen = basePath.length(); - final int pathLen = path.length(); - - // Deal with root - if ( basePathLen == 1 && pathLen == 1 ) - { - return "."; - } - else if ( basePathLen == 1 ) - { - return path.substring( 1 ); - } - - final int maxlen = Math.min( basePathLen, pathLen ); - int pos = 0; - for ( ; pos < maxlen && basePath.charAt( pos ) == path.charAt( pos ); pos++ ) - { - } - - if ( pos == basePathLen && pos == pathLen ) - { - // Same names - return "."; - } - else if ( pos == basePathLen && pos < pathLen && path.charAt( pos ) == separatorChar ) - { - // A descendent of the base path - return path.substring( pos + 1 ); - } - - // Strip the common prefix off the path - final StringBuffer buffer = new StringBuffer(); - if ( pathLen > 1 && ( pos < pathLen || basePath.charAt( pos ) != separatorChar ) ) - { - // Not a direct ancestor, need to back up - pos = basePath.lastIndexOf( separatorChar, pos ); - buffer.append( path.substring( pos ) ); - } - - // Prepend a '../' for each element in the base path past the common - // prefix - buffer.insert( 0, ".." ); - pos = basePath.indexOf( separatorChar, pos + 1 ); - while ( pos != -1 ) - { - buffer.insert( 0, "../" ); - pos = basePath.indexOf( separatorChar, pos + 1 ); - } - - return buffer.toString(); - } /** * Normalises a path. Does the following: 1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/LayeredFileName.java Index: LayeredFileName.java =================================================================== /* ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.vfs.provider; /** * A file name for layered files. * * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> * @version $Revision: 1.1 $ $Date: 2003/01/23 12:27:23 $ */ public class LayeredFileName extends DefaultFileName { private String outerUri; /** * Returns the URI of the outer file. */ public String getOuterUri() { return outerUri; } public void setOuterUri( final String outerUri ) { this.outerUri = outerUri; } } 1.16 +0 -1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystem.java Index: FtpFileSystem.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystem.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- FtpFileSystem.java 23 Jan 2003 04:37:22 -0000 1.15 +++ FtpFileSystem.java 23 Jan 2003 12:27:23 -0000 1.16 @@ -143,7 +143,6 @@ */ public FTPClient getClient() throws FileSystemException { - // TODO - connect on demand, and garbage collect connections if ( idleClient == null ) { return createConnection( hostname, username, password ); 1.9 +6 -10 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemProvider.java Index: FtpFileSystemProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemProvider.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- FtpFileSystemProvider.java 23 Nov 2002 00:05:26 -0000 1.8 +++ FtpFileSystemProvider.java 23 Jan 2003 12:27:23 -0000 1.9 @@ -59,8 +59,6 @@ import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider; -import org.apache.commons.vfs.provider.DefaultFileName; -import org.apache.commons.vfs.provider.Uri; /** * A provider for FTP file systems. @@ -71,27 +69,25 @@ public final class FtpFileSystemProvider extends AbstractOriginatingFileProvider { - private final FtpFileNameParser parser = new FtpFileNameParser(); - /** - * Parses a URI into its components. + * Parses a URI. */ - protected Uri parseUri( final String uri ) + protected FileName parseUri( final String uri ) throws FileSystemException { - return parser.parseFtpUri( uri ); + return new FtpUri( uri ); } /** * Creates the filesystem. */ - protected FileSystem doCreateFileSystem( final Uri uri ) + protected FileSystem doCreateFileSystem( final FileName name ) throws FileSystemException { - final FtpUri ftpUri = (FtpUri)uri; + final FtpUri ftpUri = (FtpUri)name; // Build the root name - final FileName rootName = new DefaultFileName( parser, ftpUri.getContainerUri(), FileName.ROOT_PATH ); + final FileName rootName = ftpUri.resolveName( FileName.ROOT_PATH ); // Determine the username and password to use String username = ftpUri.getUserName(); 1.2 +41 -2 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpUri.java Index: FtpUri.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpUri.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- FtpUri.java 31 Oct 2002 10:40:57 -0000 1.1 +++ FtpUri.java 23 Jan 2003 12:27:23 -0000 1.2 @@ -56,6 +56,7 @@ package org.apache.commons.vfs.provider.ftp; import org.apache.commons.vfs.provider.GenericUri; +import org.apache.commons.vfs.FileSystemException; /** * An FTP URI. Splits userinfo (see {@link #getUserInfo}) into username and @@ -64,11 +65,49 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> * @version $Revision$ $Date$ */ -final class FtpUri +class FtpUri extends GenericUri { private String userName; private String password; + + public FtpUri( final String uri ) + throws FileSystemException + { + // FTP URI are generic URI (as per RFC 2396) + parseGenericUri( uri ); + + // Drop the port if it is 21 + final String port = getPort(); + if ( port != null && port.equals( "21" ) ) + { + setPort( null ); + } + + // Split up the userinfo into a username and password + // TODO - push this into GenericUri + final String userInfo = getUserInfo(); + if ( userInfo != null ) + { + int idx = userInfo.indexOf( ':' ); + if ( idx == -1 ) + { + setUserName( userInfo ); + } + else + { + String userName = userInfo.substring( 0, idx ); + String password = userInfo.substring( idx + 1 ); + setUserName( userName ); + setPassword( password ); + } + } + + // Now build the root URI + final StringBuffer rootUri = new StringBuffer(); + appendRootUri( rootUri ); + setRootURI( rootUri.toString() ); + } public String getUserName() { 1.10 +2 -3 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/jar/JarFileSystem.java Index: JarFileSystem.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/jar/JarFileSystem.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- JarFileSystem.java 25 Nov 2002 05:37:13 -0000 1.9 +++ JarFileSystem.java 23 Jan 2003 12:27:23 -0000 1.10 @@ -68,7 +68,6 @@ import org.apache.commons.vfs.FileName; import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystemException; -import org.apache.commons.vfs.provider.DefaultFileName; import org.apache.commons.vfs.provider.zip.ZipFileObject; import org.apache.commons.vfs.provider.zip.ZipFileSystem; @@ -83,7 +82,7 @@ { private Attributes attributes; - public JarFileSystem( final DefaultFileName rootName, + public JarFileSystem( final FileName rootName, final FileObject file ) throws FileSystemException { super( rootName, file ); 1.8 +4 -4 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/jar/JarFileSystemProvider.java Index: JarFileSystemProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/jar/JarFileSystemProvider.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- JarFileSystemProvider.java 23 Nov 2002 00:33:53 -0000 1.7 +++ JarFileSystemProvider.java 23 Jan 2003 12:27:24 -0000 1.8 @@ -59,7 +59,7 @@ import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; -import org.apache.commons.vfs.provider.DefaultFileName; +import org.apache.commons.vfs.provider.zip.ZipFileNameParser; import org.apache.commons.vfs.provider.zip.ZipFileSystemProvider; /** @@ -84,8 +84,8 @@ FileObject file ) throws FileSystemException { - final String rootUri = getParser().buildRootUri( scheme, file.getName().getURI() ); - final DefaultFileName name = new DefaultFileName( getParser(), rootUri, FileName.ROOT_PATH ); + final FileName name = + new ZipFileNameParser( scheme, file.getName().getURI(), FileName.ROOT_PATH ); return new JarFileSystem( name, file ); } } 1.12 +6 -13 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/DefaultLocalFileSystemProvider.java Index: DefaultLocalFileSystemProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/DefaultLocalFileSystemProvider.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- DefaultLocalFileSystemProvider.java 23 Nov 2002 00:33:53 -0000 1.11 +++ DefaultLocalFileSystemProvider.java 23 Jan 2003 12:27:24 -0000 1.12 @@ -61,9 +61,7 @@ import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider; -import org.apache.commons.vfs.provider.DefaultFileName; import org.apache.commons.vfs.provider.LocalFileProvider; -import org.apache.commons.vfs.provider.Uri; import org.apache.commons.vfs.util.Os; /** @@ -120,31 +118,26 @@ } /** - * Parses a URI into its components. The returned value is used to - * locate the file system in the cache (using the root prefix), and is - * passed to {@link #doCreateFileSystem} to create the file system. - * - * <p>The provider can annotate this object with any additional - * information it requires to create a file system from the URI. + * Parses a URI. */ - protected Uri parseUri( final String uri ) + protected FileName parseUri( final String uri ) throws FileSystemException { - return parser.parseFileUri( uri ); + return new LocalFileUri( uri, parser ); } /** * Creates the filesystem. */ - protected FileSystem doCreateFileSystem( final Uri uri ) + protected FileSystem doCreateFileSystem( final FileName name ) throws FileSystemException { // Build the name of the root file. - final LocalFileUri fileUri = (LocalFileUri)uri; + final LocalFileUri fileUri = (LocalFileUri)name; final String rootFile = fileUri.getRootFile(); // Create the file system - final DefaultFileName rootName = new DefaultFileName( parser, fileUri.getContainerUri(), FileName.ROOT_PATH ); + final FileName rootName = fileUri.resolveName( FileName.ROOT_PATH ); return new LocalFileSystem( rootName, rootFile ); } } 1.6 +0 -38 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/LocalFileNameParser.java Index: LocalFileNameParser.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/LocalFileNameParser.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- LocalFileNameParser.java 23 Nov 2002 00:33:53 -0000 1.5 +++ LocalFileNameParser.java 23 Jan 2003 12:27:24 -0000 1.6 @@ -57,7 +57,6 @@ import java.io.File; import org.apache.commons.vfs.FileSystemException; -import org.apache.commons.vfs.provider.Uri; import org.apache.commons.vfs.provider.UriParser; /** @@ -91,43 +90,6 @@ { return false; } - } - - /** - * Parses an absolute URI, splitting it into its components. - * - * @param uriStr The URI. - */ - public Uri parseFileUri( final String uriStr ) - throws FileSystemException - { - final StringBuffer name = new StringBuffer(); - final LocalFileUri uri = new LocalFileUri(); - - // Extract the scheme - final String scheme = extractScheme( uriStr, name ); - uri.setScheme( scheme ); - - // Remove encoding, and adjust the separators - decode( name, 0, name.length() ); - fixSeparators( name ); - - // Extract the root prefix - final String rootFile = extractRootPrefix( uriStr, name ); - uri.setRootFile( rootFile ); - - // Normalise the path - normalisePath( name ); - uri.setPath( name.toString() ); - - // Build the root URI - final StringBuffer rootUri = new StringBuffer(); - rootUri.append( scheme ); - rootUri.append( "://" ); - rootUri.append( rootFile ); - uri.setContainerUri( rootUri.toString() ); - - return uri; } /** 1.2 +34 -4 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/LocalFileUri.java Index: LocalFileUri.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/LocalFileUri.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- LocalFileUri.java 31 Oct 2002 10:40:57 -0000 1.1 +++ LocalFileUri.java 23 Jan 2003 12:27:24 -0000 1.2 @@ -55,7 +55,8 @@ */ package org.apache.commons.vfs.provider.local; -import org.apache.commons.vfs.provider.Uri; +import org.apache.commons.vfs.FileSystemException; +import org.apache.commons.vfs.provider.DefaultFileName; /** * A local file URI. @@ -63,10 +64,39 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> * @version $Revision$ $Date$ */ -final class LocalFileUri - extends Uri +class LocalFileUri + extends DefaultFileName { private String rootFile; + + public LocalFileUri( final String uri, final LocalFileNameParser parser ) + throws FileSystemException + { + final StringBuffer name = new StringBuffer(); + + // Extract the scheme + final String scheme = extractScheme( uri, name ); + setScheme( scheme ); + + // Remove encoding, and adjust the separators + decode( name, 0, name.length() ); + fixSeparators( name ); + + // Extract the root prefix + final String rootFile = parser.extractRootPrefix( uri, name ); + setRootFile( rootFile ); + + // Normalise the path + normalisePath( name ); + setPath( name.toString() ); + + // Build the root URI + final StringBuffer rootUri = new StringBuffer(); + rootUri.append( scheme ); + rootUri.append( "://" ); + rootUri.append( rootFile ); + setRootURI( rootUri.toString() ); + } public String getRootFile() { 1.9 +6 -10 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb/SmbFileSystemProvider.java Index: SmbFileSystemProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb/SmbFileSystemProvider.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- SmbFileSystemProvider.java 23 Nov 2002 00:05:26 -0000 1.8 +++ SmbFileSystemProvider.java 23 Jan 2003 12:27:24 -0000 1.9 @@ -59,9 +59,7 @@ import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider; -import org.apache.commons.vfs.provider.DefaultFileName; import org.apache.commons.vfs.provider.FileProvider; -import org.apache.commons.vfs.provider.Uri; /** * A provider for SMB (Samba, Windows share) file systems. @@ -73,25 +71,23 @@ extends AbstractOriginatingFileProvider implements FileProvider { - private final SmbFileNameParser parser = new SmbFileNameParser(); - /** - * Parses a URI into its components. + * Parses a URI. */ - protected Uri parseUri( final String uri ) + protected FileName parseUri( final String uri ) throws FileSystemException { - return parser.parseSmbUri( uri ); + return new SmbUri( uri ); } /** * Creates the filesystem. */ - protected FileSystem doCreateFileSystem( final Uri uri ) + protected FileSystem doCreateFileSystem( final FileName name ) throws FileSystemException { - final SmbUri smbUri = (SmbUri)uri; - final FileName rootName = new DefaultFileName( parser, smbUri.getContainerUri(), FileName.ROOT_PATH ); + final SmbUri smbUri = (SmbUri)name; + final FileName rootName = smbUri.resolveName( FileName.ROOT_PATH ); return new SmbFileSystem( rootName ); } } 1.2 +39 -2 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb/SmbUri.java Index: SmbUri.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb/SmbUri.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SmbUri.java 31 Oct 2002 10:40:57 -0000 1.1 +++ SmbUri.java 23 Jan 2003 12:27:24 -0000 1.2 @@ -56,6 +56,7 @@ package org.apache.commons.vfs.provider.smb; import org.apache.commons.vfs.provider.GenericUri; +import org.apache.commons.vfs.FileSystemException; /** * An SMB URI. Adds a share name to the generic URI. @@ -63,10 +64,46 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> * @version $Revision$ $Date$ */ -final class SmbUri +class SmbUri extends GenericUri { private String share; + + public SmbUri( final String uri ) + throws FileSystemException + { + final StringBuffer name = new StringBuffer(); + + // Extract the scheme and authority parts + extractToPath( uri, name ); + + // TODO - drop the default port + + // Decode and adjust separators + decode( name, 0, name.length() ); + fixSeparators( name ); + + // Extract the share + final String share = extractFirstElement( name ); + if ( share == null ) + { + throw new FileSystemException( "vfs.provider.smb/missing-share-name.error", uri ); + } + setShare( share ); + + // Normalise the path + normalisePath( name ); + + // Set the path + setPath( name.toString() ); + + // Set the root URI + StringBuffer rootUri = new StringBuffer(); + appendRootUri( rootUri ); + rootUri.append( '/' ); + rootUri.append( share ); + setRootURI( rootUri.toString() ); + } public String getShare() { 1.4 +10 -20 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/temp/TemporaryFileProvider.java Index: TemporaryFileProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/temp/TemporaryFileProvider.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TemporaryFileProvider.java 23 Nov 2002 00:05:27 -0000 1.3 +++ TemporaryFileProvider.java 23 Jan 2003 12:27:26 -0000 1.4 @@ -63,7 +63,6 @@ import org.apache.commons.vfs.provider.AbstractFileSystemProvider; import org.apache.commons.vfs.provider.DefaultFileName; import org.apache.commons.vfs.provider.FileProvider; -import org.apache.commons.vfs.provider.Uri; import org.apache.commons.vfs.provider.UriParser; import org.apache.commons.vfs.provider.local.LocalFileSystem; @@ -96,9 +95,13 @@ throws FileSystemException { // Parse the name - final Uri parsedUri = parseUri( uri ); + final StringBuffer buffer = new StringBuffer( uri ); + final String scheme = parser.extractScheme( uri, buffer ); + parser.decode( buffer, 0, buffer.length() ); + parser.normalisePath( buffer ); + final String path = buffer.toString(); - // Create the temp file system + // Create the temp file system if it does not exist FileSystem filesystem = findFileSystem( this ); if ( filesystem == null ) { @@ -106,26 +109,13 @@ { rootFile = getContext().getTemporaryFileStore().allocateFile( "tempfs" ); } - final FileName rootName = new DefaultFileName( parser, parsedUri.getContainerUri(), FileName.ROOT_PATH ); + final FileName rootName = + new DefaultFileName( scheme, scheme + ":", FileName.ROOT_PATH ); filesystem = new LocalFileSystem( rootName, rootFile.getAbsolutePath() ); addFileSystem( this, filesystem ); } // Find the file - return filesystem.resolveFile( parsedUri.getPath() ); - } - - /** Parses an absolute URI into its parts. */ - private Uri parseUri( final String uri ) throws FileSystemException - { - final StringBuffer buffer = new StringBuffer( uri ); - final Uri parsedUri = new Uri(); - final String scheme = parser.extractScheme( uri, buffer ); - parsedUri.setScheme( scheme ); - parser.decode( buffer, 0, buffer.length() ); - parser.normalisePath( buffer ); - parsedUri.setPath( buffer.toString() ); - parsedUri.setContainerUri( scheme + ":" ); - return parsedUri; + return filesystem.resolveFile( path ); } } 1.10 +8 -9 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/url/UrlFileProvider.java Index: UrlFileProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/url/UrlFileProvider.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- UrlFileProvider.java 25 Nov 2002 05:38:45 -0000 1.9 +++ UrlFileProvider.java 23 Jan 2003 12:27:26 -0000 1.10 @@ -55,16 +55,14 @@ */ package org.apache.commons.vfs.provider.url; +import java.net.MalformedURLException; +import java.net.URL; import org.apache.commons.vfs.FileName; +import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; -import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.provider.AbstractFileSystemProvider; import org.apache.commons.vfs.provider.DefaultFileName; -import org.apache.commons.vfs.provider.Uri; -import org.apache.commons.vfs.provider.UriParser; -import java.net.URL; -import java.net.MalformedURLException; /** * A file provider backed by Java's URL API. @@ -75,8 +73,6 @@ public class UrlFileProvider extends AbstractFileSystemProvider { - private final UriParser parser = new UriParser(); - /** * Locates a file object, by absolute URI. */ @@ -91,7 +87,10 @@ FileSystem fs = findFileSystem( rootUrl ); if ( fs == null ) { - final FileName rootName = new DefaultFileName( parser, rootUrl.toExternalForm(), FileName.ROOT_PATH ); + final FileName rootName = + new DefaultFileName( rootUrl.getProtocol(), + rootUrl.toExternalForm(), + FileName.ROOT_PATH ); fs = new UrlFileSystem( rootName ); addFileSystem( rootUrl, fs ); } 1.7 +16 -26 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/zip/ZipFileNameParser.java Index: ZipFileNameParser.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/zip/ZipFileNameParser.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- ZipFileNameParser.java 31 Oct 2002 10:40:58 -0000 1.6 +++ ZipFileNameParser.java 23 Jan 2003 12:27:27 -0000 1.7 @@ -56,8 +56,7 @@ package org.apache.commons.vfs.provider.zip; import org.apache.commons.vfs.FileSystemException; -import org.apache.commons.vfs.provider.Uri; -import org.apache.commons.vfs.provider.UriParser; +import org.apache.commons.vfs.provider.LayeredFileName; /** * A parser for Zip file names. @@ -66,55 +65,46 @@ * @version $Revision$ $Date$ */ public class ZipFileNameParser - extends UriParser + extends LayeredFileName { private static final char[] ZIP_URL_RESERVED_CHARS = {'!'}; - /** - * Parses an absolute URI, splitting it into its components. - * - * @param uriStr - * The URI. - */ - public Uri parseZipUri( final String uriStr ) + public ZipFileNameParser( final String uri ) throws FileSystemException { final StringBuffer name = new StringBuffer(); - final Uri uri = new Uri(); // Extract the scheme - final String scheme = extractScheme( uriStr, name ); - uri.setScheme( scheme ); + final String scheme = extractScheme( uri, name ); + setScheme( scheme ); - // Extract the Zip file name - final String zipName = extractZipName( name ); - uri.setContainerUri( zipName ); + // Extract the Zip file URI + final String zipUri = extractZipName( name ); + setOuterUri( zipUri ); - // Decode and normalise the file name + // Decode and normalise the path decode( name, 0, name.length() ); normalisePath( name ); - uri.setPath( name.toString() ); - - return uri; + setPath( name.toString() ); } - /** - * Assembles a root URI from the components of a parsed URI. - */ - public String buildRootUri( final String scheme, final String outerFileUri ) + public ZipFileNameParser( final String scheme, + final String outerFileUri, + final String path ) { final StringBuffer rootUri = new StringBuffer(); rootUri.append( scheme ); rootUri.append( ":" ); appendEncoded( rootUri, outerFileUri, ZIP_URL_RESERVED_CHARS ); rootUri.append( "!" ); - return rootUri.toString(); + setRootURI( rootUri.toString() ); + setPath( path ); } /** * Pops the root prefix off a URI, which has had the scheme removed. */ - private String extractZipName( final StringBuffer uri ) + private static String extractZipName( final StringBuffer uri ) throws FileSystemException { // Looking for <name>!<abspath> 1.19 +1 -2 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/zip/ZipFileSystem.java Index: ZipFileSystem.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/zip/ZipFileSystem.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- ZipFileSystem.java 23 Jan 2003 04:37:23 -0000 1.18 +++ ZipFileSystem.java 23 Jan 2003 12:27:27 -0000 1.19 @@ -68,7 +68,6 @@ import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.Selectors; import org.apache.commons.vfs.provider.AbstractFileSystem; -import org.apache.commons.vfs.provider.DefaultFileName; /** * A read-only file system for Zip/Jar files. @@ -83,7 +82,7 @@ private final File file; protected final ZipFile zipFile; - public ZipFileSystem( final DefaultFileName rootName, + public ZipFileSystem( final FileName rootName, final FileObject parentLayer ) throws FileSystemException { 1.15 +5 -18 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/zip/ZipFileSystemProvider.java Index: ZipFileSystemProvider.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/zip/ZipFileSystemProvider.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- ZipFileSystemProvider.java 23 Nov 2002 00:33:54 -0000 1.14 +++ ZipFileSystemProvider.java 23 Jan 2003 12:27:28 -0000 1.15 @@ -60,9 +60,7 @@ import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.provider.AbstractLayeredFileProvider; -import org.apache.commons.vfs.provider.DefaultFileName; import org.apache.commons.vfs.provider.FileProvider; -import org.apache.commons.vfs.provider.Uri; /** * A file system provider for Zip files. Provides read-only file systems. @@ -74,16 +72,14 @@ extends AbstractLayeredFileProvider implements FileProvider { - private final ZipFileNameParser parser = new ZipFileNameParser(); - /** * Parses an absolute URI. * @param uri The URI to parse. */ - protected Uri parseUri( final String uri ) + protected FileName parseUri( final String uri ) throws FileSystemException { - return parser.parseZipUri( uri ); + return new ZipFileNameParser( uri ); } /** @@ -97,17 +93,8 @@ final FileObject file ) throws FileSystemException { - final String rootUri = parser.buildRootUri( scheme, file.getName().getURI() ); - final DefaultFileName name = new DefaultFileName( parser, rootUri, FileName.ROOT_PATH ); - return new ZipFileSystem( name, file ); + final FileName rootName = + new ZipFileNameParser( scheme, file.getName().getURI(), FileName.ROOT_PATH ); + return new ZipFileSystem( rootName, file ); } - - /** - * Returns the URI parser. - */ - protected ZipFileNameParser getParser() - { - return parser; - } - }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>