// 2012-08-03 johnpfeiffer parsing filesystem path and filename
// TODO: better handling of path "/"
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class FileSystemPath
{
private static final String CLASSVERSION = "0.2";
private ArrayList <String> pathAndFileArrayList;
private String pathAndFile;
private String firstDirectory;
private String path;
private int pathDepth = -1;
private String filename;
FileSystemPath( String pathAndFilename )
{
if( pathAndFilename == null )
{
throw new IllegalArgumentException( "ERROR: pathAndFilename cannot be null" );
}
if( pathAndFilename.isEmpty() )
{
throw new IllegalArgumentException( "ERROR: pathAndFilename cannot be empty" );
}
String sourceSplit[] = pathAndFilename.split( "/" );
pathAndFileArrayList = new ArrayList <String>( Arrays.asList( sourceSplit ) );
if( pathAndFileArrayList.size() == 0 )
{
throw new IllegalArgumentException( "ERROR: Filename cannot be empty" );
}
// TODO: untangle the initialization dependency chain below
sanitizePathAndFilename();
initializePathDepth();
initializePathAndFile();
initializeFirstDirectory();
initializePath();
initializeFilename();
}
private void sanitizePathAndFilename()
{
ArrayList <String> sanitized = new ArrayList <String>();
for( String element : pathAndFileArrayList )
{
if( !element.isEmpty() )
{
sanitized.add( element );
}
}
pathAndFileArrayList = sanitized;
}
private void initializePathDepth()
{
pathDepth = pathAndFileArrayList.size();
}
private void initializePathAndFile()
{
pathAndFile = join( pathAndFileArrayList , "/" );
}
private static String join( final Collection <String> s , final String delimiter )
{
StringBuffer buffer = new StringBuffer();
Iterator <String> iter = s.iterator();
while( iter.hasNext() )
{
buffer.append( iter.next() );
if( iter.hasNext() )
{
buffer.append( delimiter );
}
}
return buffer.toString();
}
private void initializeFirstDirectory()
{
int firstSlashLocation = pathAndFile.indexOf( '/' );
if( firstSlashLocation == -1 ) // no slashes found means just the filename is left
{
firstDirectory = "";
}else
{
firstDirectory = pathAndFile.substring( 0 , firstSlashLocation );
}
}
private void initializePath()
{
int lastSlashLocation = pathAndFile.lastIndexOf( '/' );
if( lastSlashLocation == -1 ) // no slashes found means just the filename is left
{
path = "";
}else
{
path = pathAndFile.substring( 0 , lastSlashLocation );
}
}
private void initializeFilename()
{
int lastSlashLocation = pathAndFile.lastIndexOf( '/' );
if( lastSlashLocation == -1 ) // no slashes means just the filename was left
{
filename = pathAndFile;
}else
{
filename = pathAndFile.substring( lastSlashLocation );
}
}
protected String getVersion()
{
return CLASSVERSION;
}
protected String get()
{
return pathAndFile;
}
protected String getFirstDirectory()
{
return firstDirectory;
}
protected String getPath()
{
return path;
}
protected int getPathDepth()
{
return pathDepth;
}
protected String getFilename()
{
return filename;
}
} // end class