commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adammurd...@apache.org
Subject cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs FileName.java
Date Sun, 27 Oct 2002 08:06:37 GMT
adammurdoch    2002/10/27 01:06:37

  Modified:    vfs/src/java/org/apache/commons/vfs/provider
                        DefaultFileName.java UriParser.java
               vfs/src/java/org/apache/commons/vfs FileName.java
  Log:
  Added FileName.getDepth(), isAncestor() and isDescendent().
  
  Revision  Changes    Path
  1.4       +40 -0     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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultFileName.java	23 Oct 2002 11:59:40 -0000	1.3
  +++ DefaultFileName.java	27 Oct 2002 08:06:37 -0000	1.4
  @@ -185,4 +185,44 @@
       {
           return parser.makeRelative( absPath, name.getPath() );
       }
  +
  +    /**
  +     * Returns the root URI of the file system this file belongs to.
  +     */
  +    public String getRootURI()
  +    {
  +        return rootPrefix;
  +    }
  +
  +    /**
  +     * Returns the depth of this file name, within its file system.
  +     */
  +    public int getDepth()
  +    {
  +        return parser.getDepth( absPath );
  +    }
  +
  +    /**
  +     * Determines if another file name is an ancestor of this file name.
  +     */
  +    public boolean isAncestor( final FileName ancestor )
  +    {
  +        if ( !ancestor.getRootURI().equals( rootPrefix ) )
  +        {
  +            return false;
  +        }
  +        return parser.checkName( ancestor.getPath(), absPath, NameScope.DESCENDENT );
  +    }
  +
  +    /**
  +     * Determines if another file name is a descendent of this file name.
  +     */
  +    public boolean isDescendent( FileName descendent )
  +    {
  +        if ( !descendent.getRootURI().equals( rootPrefix ) )
  +        {
  +            return false;
  +        }
  +        return parser.checkName( absPath, descendent.getPath(), NameScope.DESCENDENT );
  +    }
   }
  
  
  
  1.7       +55 -17    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- UriParser.java	23 Oct 2002 11:59:40 -0000	1.6
  +++ UriParser.java	27 Oct 2002 08:06:37 -0000	1.7
  @@ -469,35 +469,53 @@
           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.CHILD )
           {
  -            final int baseLen = baseFile.length();
  -            if ( !resolvedPath.startsWith( baseFile )
  -                || resolvedPath.length() == baseLen
  -                || ( baseLen > 1 && resolvedPath.charAt( baseLen ) != separatorChar
)
  -                || resolvedPath.indexOf( separatorChar, baseLen + 1 ) != -1 )
  +            final int baseLen = basePath.length();
  +            if ( !path.startsWith( basePath )
  +                || path.length() == baseLen
  +                || ( baseLen > 1 && path.charAt( baseLen ) != separatorChar
)
  +                || path.indexOf( separatorChar, baseLen + 1 ) != -1 )
               {
  -                throw new FileSystemException( "vfs.provider/invalid-childname.error",
path );
  +                return false;
               }
           }
           else if ( scope == NameScope.DESCENDENT )
           {
  -            final int baseLen = baseFile.length();
  -            if ( !resolvedPath.startsWith( baseFile )
  -                || resolvedPath.length() == baseLen
  -                || ( baseLen > 1 && resolvedPath.charAt( baseLen ) != separatorChar
) )
  +            final int baseLen = basePath.length();
  +            if ( !path.startsWith( basePath )
  +                || path.length() == baseLen
  +                || ( baseLen > 1 && path.charAt( baseLen ) != separatorChar
) )
               {
  -                throw new FileSystemException( "vfs.provider/invalid-descendent-name.error",
path );
  +                return false;
               }
           }
           else if ( scope == NameScope.DESCENDENT_OR_SELF )
           {
  -            final int baseLen = baseFile.length();
  -            if ( !resolvedPath.startsWith( baseFile )
  -                || ( resolvedPath.length() != baseLen
  -                && resolvedPath.charAt( baseLen ) != separatorChar ) )
  +            final int baseLen = basePath.length();
  +            if ( !path.startsWith( basePath )
  +                || ( path.length() != baseLen
  +                && path.charAt( baseLen ) != separatorChar ) )
               {
  -                throw new FileSystemException( "vfs.provider/invalid-descendent-name.error",
path );
  +                return false;
               }
           }
           else if ( scope != NameScope.FILE_SYSTEM )
  @@ -505,7 +523,27 @@
               throw new IllegalArgumentException();
           }
   
  -        return resolvedPath;
  +        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;
       }
   
       /**
  
  
  
  1.5       +26 -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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FileName.java	23 Oct 2002 13:09:45 -0000	1.4
  +++ FileName.java	27 Oct 2002 08:06:37 -0000	1.5
  @@ -72,6 +72,8 @@
        * <code>/somefolder/somefile</code> is <code>somefile</code>.
        *
        * <p>The root file of a file system has an empty base name.
  +     *
  +     * @return The base name.  Never returns null.
        */
       String getBaseName();
   
  @@ -82,15 +84,29 @@
        * separator character.  The path always starts with <code>/</code>
        *
        * <p>The root of a file system has <code>/</code> as its absolute
path.
  +     *
  +     * @return The path.  Never returns null.
        */
       String getPath();
   
       /**
  +     * Returns the depth of this file name, within its file system.  The depth
  +     * of the root of a file system is 0.  The depth of any other file is
  +     * 1 + the depth of its parent.
  +     */
  +    int getDepth();
  +
  +    /**
        * Returns the absolute URI of this file.
        */
       String getURI();
   
       /**
  +     * Returns the root URI of the file system this file belongs to.
  +     */
  +    String getRootURI();
  +
  +    /**
        * Returns the file name of the parent of this file.  The root of a
        * file system has no parent.
        *
  @@ -147,4 +163,14 @@
        *      On error.
        */
       String getRelativeName( FileName name ) throws FileSystemException;
  +
  +    /**
  +     * Determines if another file name is an ancestor of this file name.
  +     */
  +    boolean isAncestor( FileName ancestor );
  +
  +    /**
  +     * Determines if another file name is a descendent of this file name.
  +     */
  +    boolean isDescendent( FileName descendent );
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message