ZipCommand
extends FileHandlingCommand<Object>
{
- /**
- * Directory to zip
- */
- protected final String baseDir;
-
- /**
- * Constructor with base dir and target file name
- *
- * @param baseDir base directory path
- * @param target target file name
- */
- public
- ZipCommand(
- final String baseDir,
- final String target
- )
- {
- this.baseDir = baseDir;
- setPath( target );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.core.command.file.FileHandlingCommand#execute(org.tizen.common.core.command.Executor, org.tizen.common.core.command.ExecutionContext)
- */
- @Override
- public
- void
- run(
- final Executor executor,
- final ExecutionContext context
- )
- throws IOException
- {
- final FileHandler handler = context.getFileHandler();
- Assert.notNull( handler );
-
- if ( !handler.is( baseDir, Attribute.EXISTS ) )
- {
- final Policy policy = context.getPolicy( NONEXIST_IN_DIRECTORY );
-
- final MessagePolicy messagePolicy = policy.adapt( MessagePolicy.class );
-
- messagePolicy.print( context.getPrompter(), "{0} doesn't exist.", baseDir );
- return ;
- }
-
- if ( handler.is( path, Attribute.EXISTS ) )
- {
- final Policy policy = context.getPolicy( EXIST_OUT_WGT );
-
- final MessagePolicy messagePolicy = policy.adapt( MessagePolicy.class );
- messagePolicy.print( context.getPrompter(), "Widget already exist." );
-
- final FilePolicy filePolicy = policy.adapt( FilePolicy.class );
-
- if ( FilePolicy.OVERWRITE.equals( filePolicy ) )
- {
- logger.debug( "No operation" );
- }
- else
- { // It is default to stop process
- return ;
- }
-
- }
-
- final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
-
- final ZipOutputStream zipOut = createZipOutputStream( byteOut );
-
- try
- {
- addEntry( zipOut, handler, baseDir );
- }
- finally
- {
- tryClose( zipOut );
- }
-
+ /**
+ * Directory to zip
+ */
+ protected final String baseDir;
+
+ /**
+ * Constructor with base dir and target file name
+ *
+ * @param baseDir base directory path
+ * @param target target file name
+ */
+ public
+ ZipCommand(
+ final String baseDir,
+ final String target
+ )
+ {
+ this.baseDir = baseDir;
+ setPath( target );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.core.command.file.FileHandlingCommand#execute(org.tizen.common.core.command.Executor, org.tizen.common.core.command.ExecutionContext)
+ */
+ @Override
+ public
+ void
+ run(
+ final Executor executor,
+ final ExecutionContext context
+ )
+ throws IOException
+ {
+ final FileHandler handler = context.getFileHandler();
+ Assert.notNull( handler );
+
+ if ( !handler.is( baseDir, Attribute.EXISTS ) )
+ {
+ final Policy policy = context.getPolicy( NONEXIST_IN_DIRECTORY );
+
+ final MessagePolicy messagePolicy = policy.adapt( MessagePolicy.class );
+
+ messagePolicy.print( context.getPrompter(), "{0} doesn't exist.", baseDir );
+ return ;
+ }
+
+ if ( handler.is( path, Attribute.EXISTS ) )
+ {
+ final Policy policy = context.getPolicy( EXIST_OUT_WGT );
+
+ final MessagePolicy messagePolicy = policy.adapt( MessagePolicy.class );
+ messagePolicy.print( context.getPrompter(), "Widget already exist." );
+
+ final FilePolicy filePolicy = policy.adapt( FilePolicy.class );
+
+ if ( FilePolicy.OVERWRITE.equals( filePolicy ) )
+ {
+ logger.debug( "No operation" );
+ }
+ else
+ { // It is default to stop process
+ return ;
+ }
+
+ }
+
+ final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
+
+ final ZipOutputStream zipOut = createZipOutputStream( byteOut );
+
+ try
+ {
+ addEntry( zipOut, handler, baseDir );
+ }
+ finally
+ {
+ tryClose( zipOut );
+ }
+
try {
handler.write( path, new ByteArrayInputStream( byteOut.toByteArray() ) );
context.getPrompter().notify( MessageFormat.format( "Widget( {0} ) is created successfully.", path ) );
throw new IllegalStateException( e );
// TODO Handle
}
-
- }
-
- /**
- * Add file entry with <code>filePath</code> into <code>zipOut</code>
- *
- * @param zipOut {@link ZipOutputStream} to add entry
- * @param handler {@link FileHandler} to use to read contents
- * @param filePath file paht
- */
- protected
- void
- addEntry(
- final ZipOutputStream zipOut,
- final FileHandler handler,
- final String filePath
- )
- {
- try
- {
- final Object type = handler.get( filePath, Attribute.TYPE );
-
- logger.trace( "File path :{}", filePath );
-
- if (
- !FilenameUtil.equals( filePath, baseDir ) &&
- !filter.accept( baseDir, getRelativePath( baseDir, filePath ) )
- )
- {
- logger.debug( "Ignore {}", filePath );
- return ;
- }
- logger.trace( "file [{}]'s type :{}", filePath, type );
- if ( Type.DIRECTORY.equals( type ) )
- {
- Collection<String> children = handler.list( filePath );
+
+ }
+
+ /**
+ * Add file entry with <code>filePath</code> into <code>zipOut</code>
+ *
+ * @param zipOut {@link ZipOutputStream} to add entry
+ * @param handler {@link FileHandler} to use to read contents
+ * @param filePath file paht
+ */
+ protected
+ void
+ addEntry(
+ final ZipOutputStream zipOut,
+ final FileHandler handler,
+ final String filePath
+ )
+ {
+ try
+ {
+ final Object type = handler.get( filePath, Attribute.TYPE );
+
+ logger.trace( "File path :{}", filePath );
+
+ if (
+ !FilenameUtil.equals( filePath, baseDir ) &&
+ !filter.accept( baseDir, getRelativePath( baseDir, filePath ) )
+ )
+ {
+ logger.debug( "Ignore {}", filePath );
+ return ;
+ }
+ logger.trace( "file [{}]'s type :{}", filePath, type );
+ if ( Type.DIRECTORY.equals( type ) )
+ {
+ Collection<String> children = handler.list( filePath );
final TreeSet<String> safe = new TreeSet<String>( new Comparator<String>()
- {
+ {
@Override
public
int
compare(
- final String o1,
- final String o2
- )
+ final String o1,
+ final String o2
+ )
{
if ( o1.equals( o2 ) )
{
{
return 1;
}
-
+
return o1.compareTo( o2 );
}
- } );
-
+ } );
+
safe.addAll( children );
children = safe;
logger.debug( "Files :{}", children );
- if ( !FilenameUtil.equals( baseDir, filePath ) )
- {
- final String relative = getRelativePath( baseDir, filePath );
- logger.trace( "Relative path :{}", relative );
-
- final ZipEntry entry = createEntry( relative + "/" );
- zipOut.putNextEntry( entry );
- zipOut.closeEntry();
- }
-
-
- for ( final String child : children )
- {
- addEntry( zipOut, handler, child );
- }
-
- }
- else if ( Type.FILE.equals( type ) )
- {
- final String relative = getRelativePath( baseDir, filePath );
- logger.trace( "Relative path :{}", relative );
-
- final ZipEntry entry = createEntry( relative );
- zipOut.putNextEntry( entry );
- IOUtil.redirect( handler.read( filePath ), zipOut );
- zipOut.closeEntry();
- logger.debug( "Zip {}", relative );
- }
- }
- catch( IOException e )
- {
- throw new IllegalStateException( e );
- // TODO Handle
- }
- }
+ if ( !FilenameUtil.equals( baseDir, filePath ) )
+ {
+ final String relative = getRelativePath( baseDir, filePath );
+ logger.trace( "Relative path :{}", relative );
+
+ final ZipEntry entry = createEntry( relative + "/" );
+ zipOut.putNextEntry( entry );
+ zipOut.closeEntry();
+ }
+
+
+ for ( final String child : children )
+ {
+ addEntry( zipOut, handler, child );
+ }
+
+ }
+ else if ( Type.FILE.equals( type ) )
+ {
+ final String relative = getRelativePath( baseDir, filePath );
+ logger.trace( "Relative path :{}", relative );
+
+ final ZipEntry entry = createEntry( relative );
+ zipOut.putNextEntry( entry );
+ IOUtil.redirect( handler.read( filePath ), zipOut );
+ zipOut.closeEntry();
+ logger.debug( "Zip {}", relative );
+ }
+ }
+ catch( IOException e )
+ {
+ throw new IllegalStateException( e );
+ // TODO Handle
+ }
+ }
/**
* Create {@link ZipOutputStream} to delegate <code>out</code>
protected
ZipOutputStream
createZipOutputStream(
- final OutputStream out
- )
- throws IOException
- {
+ final OutputStream out
+ )
+ throws IOException
+ {
final ZipOutputStream zipOut = new ZipOutputStream( out );
zipOut.setMethod( ZipOutputStream.DEFLATED );
zipOut.setLevel( 9 );
-
+
return zipOut;
- }
-
+ }
+
/**
* Create {@link ZipEntry} with <code>name</code>
*
protected
ZipEntry
createEntry(
- final String name
- )
+ final String name
+ )
{
return new ZipEntry( name );
}
public interface
FileHandler
{
- /**
- * <p>
- * Attribute.
- *
- * File attribute
- * </p>
- *
- * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
- */
- enum Attribute
- {
- CWD, // Current Working Directory
- PATH, // Abstract canonical path
- NAME, // file name
- TYPE, // Type( FILE / DIRECTORY / UNKNOWN )
- EXISTS, // true / false
- READABLE, // true / false
- WRITABLE, // true / false
- HIDDEN, // true / false
- READONLY, // true / false
- MODIFIED, // long
- SIZE, // long
- URI, // String
- URL, // String
- QUALIFIED,
- CUSTOM
- }
-
- /**
- * <p>
- * Type.
- *
- * Value of {@link Attribute#TYPE}
- * </p>
- *
- * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
- */
- enum Type
- {
- FILE,
- DIRECTORY,
- UNKNOWN
- }
-
- /**
- * Return cwd, abbreviating current working directory
- *
- * @return current working directory
- */
- String getCurrentWorkingDirectory();
-
- /**
- * Change current working directory to <code>cwd</code>
- *
- * @param cwd new current working directory
- */
- void setCurrentWorkingDirectory( final String cwd );
-
- /* Directory */
- /**
- * Make new directory with <code>path</code>
- *
- * @param path path to directory
- *
- * @throws IOException If exception occured in making a directory
- */
- void makeDirectory( String path ) throws IOException;
-
- /**
- * Move <code>source</code> directory to <code>target</code>
- *
- * @param source directory path to move
- * @param target path to move to
- *
- * @throws IOException If exception occured in moving a directory
- */
- void moveDirectory( String source, String target ) throws IOException;
-
- /**
- * Copy <code>source</code> directory to <code>target</code>
- *
- * @param source directory path to copy
- * @param target path to copy to
- *
- * @throws IOException If exception occured in copying a directory
- */
- void copyDirectory( String source, String target ) throws IOException;
-
- /**
- * Delete <code>path</code> directory
- *
- * @param path directory path to delete
- *
- * @throws IOException If exception occured in deleting a directory
- */
- void removeDirectory( String path ) throws IOException;
-
- /**
- * List child directory and file in <code>path</code>
- *
- * @param path directory path
- * @return child directory and file's path
- *
- * @throws IOException If exception occured in list child in directory
- */
- Collection<String> list( String path ) throws IOException;
-
- /* File */
- /**
- * Save file in <code>path</code> using <code>out</code>
- *
- * @param path file path to write
- * @param out {@link InputStream} to provide contents
- *
- * @throws IOException If exception occured in saving
- */
- void write( String path, InputStream out ) throws IOException;
-
- /**
- * Return {@link InputStream} to provice file contents
- *
- * @param path file path to read
- * @return
- * @throws IOException
- */
- InputStream read( String path ) throws IOException;
-
- /**
- * Move <code>source</code> file to <code>target</code>
- *
- * @param source file path to move
- * @param target path to move to
- *
- * @throws IOException If exception occured in moving a file
- */
- void moveFile( String source, String target ) throws IOException;
-
- /**
- * Copy <code>source</code> file to <code>target</code>
- *
- * @param source file path to copy
- * @param target path to copy to
- *
- * @throws IOException If exception occured in copying a file
- */
- void copyFile( String source, String target ) throws IOException;
-
- /**
- * Delete file in <code>path</code>
- *
- * @param path file path
- *
- * @throws IOException If exception occured in deletion
- */
- void removeFile( String path ) throws IOException;
-
- /**
- * Return <code>path</code>'s attribute value having <code>name</code> as attribute name
- *
- * @param path file path
- * @param name attribute name
- *
- * @return attribute value
- *
- * @throws IOException If exception occured in reading attribute
- */
- Object get( final String path, Attribute name ) throws IOException;
-
- /**
- * Check if <code>path</code> file's attribute <code>name</code> is <code>true</code>
- *
- * @param path file path to check
- * @param name attribute name
- *
- * @return boolean value if thread is <code>name</code> attribute
- *
- * @throws IOException If exception occured in reading attribute
- */
- boolean is( final String path, Attribute name ) throws IOException;
-
- /**
- * Set <code>path</code> file's attribute <code>name</code> as <code>value</code>
- *
- * @param path file path
- * @param name attribute anme
- * @param value attribute value
- *
- * @throws IOException If exception occured in change file attribute
- *
- * @see {@link Attribute}
- */
- void set( final String path, Attribute name, Object value ) throws IOException;
+ /**
+ * <p>
+ * Attribute.
+ *
+ * File attribute
+ * </p>
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+ enum Attribute
+ {
+ CWD, // Current Working Directory
+ PATH, // Abstract canonical path
+ NAME, // file name
+ TYPE, // Type( FILE / DIRECTORY / UNKNOWN )
+ EXISTS, // true / false
+ READABLE, // true / false
+ WRITABLE, // true / false
+ HIDDEN, // true / false
+ READONLY, // true / false
+ MODIFIED, // long
+ SIZE, // long
+ URI, // String
+ URL, // String
+ QUALIFIED,
+ CUSTOM
+ }
+
+ /**
+ * <p>
+ * Type.
+ *
+ * Value of {@link Attribute#TYPE}
+ * </p>
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+ enum Type
+ {
+ FILE,
+ DIRECTORY,
+ UNKNOWN
+ }
+
+ /**
+ * Return cwd, abbreviating current working directory
+ *
+ * @return current working directory
+ */
+ String getCurrentWorkingDirectory();
+
+ /**
+ * Change current working directory to <code>cwd</code>
+ *
+ * @param cwd new current working directory
+ */
+ void setCurrentWorkingDirectory( final String cwd );
+
+ /* Directory */
+ /**
+ * Make new directory with <code>path</code>
+ *
+ * @param path path to directory
+ *
+ * @throws IOException If exception occured in making a directory
+ */
+ void makeDirectory( String path ) throws IOException;
+
+ /**
+ * Move <code>source</code> directory to <code>target</code>
+ *
+ * @param source directory path to move
+ * @param target path to move to
+ *
+ * @throws IOException If exception occured in moving a directory
+ */
+ void moveDirectory( String source, String target ) throws IOException;
+
+ /**
+ * Copy <code>source</code> directory to <code>target</code>
+ *
+ * @param source directory path to copy
+ * @param target path to copy to
+ *
+ * @throws IOException If exception occured in copying a directory
+ */
+ void copyDirectory( String source, String target ) throws IOException;
+
+ /**
+ * Delete <code>path</code> directory
+ *
+ * @param path directory path to delete
+ *
+ * @throws IOException If exception occured in deleting a directory
+ */
+ void removeDirectory( String path ) throws IOException;
+
+ /**
+ * List child directory and file in <code>path</code>
+ *
+ * @param path directory path
+ * @return child directory and file's path
+ *
+ * @throws IOException If exception occured in list child in directory
+ */
+ Collection<String> list( String path ) throws IOException;
+
+ /* File */
+ /**
+ * Save file in <code>path</code> using <code>out</code>
+ *
+ * @param path file path to write
+ * @param out {@link InputStream} to provide contents
+ *
+ * @throws IOException If exception occured in saving
+ */
+ void write( String path, InputStream out ) throws IOException;
+
+ /**
+ * Return {@link InputStream} to provice file contents
+ *
+ * @param path file path to read
+ * @return
+ * @throws IOException
+ */
+ InputStream read( String path ) throws IOException;
+
+ /**
+ * Move <code>source</code> file to <code>target</code>
+ *
+ * @param source file path to move
+ * @param target path to move to
+ *
+ * @throws IOException If exception occured in moving a file
+ */
+ void moveFile( String source, String target ) throws IOException;
+
+ /**
+ * Copy <code>source</code> file to <code>target</code>
+ *
+ * @param source file path to copy
+ * @param target path to copy to
+ *
+ * @throws IOException If exception occured in copying a file
+ */
+ void copyFile( String source, String target ) throws IOException;
+
+ /**
+ * Delete file in <code>path</code>
+ *
+ * @param path file path
+ *
+ * @throws IOException If exception occured in deletion
+ */
+ void removeFile( String path ) throws IOException;
+
+ /**
+ * Return <code>path</code>'s attribute value having <code>name</code> as attribute name
+ *
+ * @param path file path
+ * @param name attribute name
+ *
+ * @return attribute value
+ *
+ * @throws IOException If exception occured in reading attribute
+ */
+ Object get( final String path, Attribute name ) throws IOException;
+
+ /**
+ * Check if <code>path</code> file's attribute <code>name</code> is <code>true</code>
+ *
+ * @param path file path to check
+ * @param name attribute name
+ *
+ * @return boolean value if thread is <code>name</code> attribute
+ *
+ * @throws IOException If exception occured in reading attribute
+ */
+ boolean is( final String path, Attribute name ) throws IOException;
+
+ /**
+ * Set <code>path</code> file's attribute <code>name</code> as <code>value</code>
+ *
+ * @param path file path
+ * @param name attribute anme
+ * @param value attribute value
+ *
+ * @throws IOException If exception occured in change file attribute
+ *
+ * @see {@link Attribute}
+ */
+ void set( final String path, Attribute name, Object value ) throws IOException;
}
extends AbstractFileHandler
{
- /**
- * <p>
- * File.
- *
- * File contents container
- *
- * </p>
- *
- * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
- */
- private class
- File
- implements Cloneable
- {
- /**
- * File name
- */
- String name;
-
- /**
- * File contents
- */
- byte[] contents;
-
- /**
- * File attributes
- */
- HashMap<Attribute, Object> attr = new HashMap<Attribute, Object>();
-
- /**
- * Default constructor
- */
- public File()
- {
- }
-
- /**
- * Constructor with name and contents
- *
- * @param name file name
- * @param contents file contents
- */
- public
- File(
- final String name,
- final byte[] contents
- )
- {
- this.name = name;
- this.contents = contents;
- }
-
- /**
- * Return file name
- *
- * @return file name
- */
- public
- String
- getName()
- {
- return this.name;
- }
-
- /**
- * Return file contents
- *
- * @return file contents
- */
- public
- byte[] getContents()
- {
- return this.contents;
- }
-
- /**
- * Set <code>contents</code> as file contents
- *
- * @param contents new file contents
- */
- public
- void
- setContents(
- final byte[] contents
- )
- {
- this.contents = contents;
- }
-
- /**
- * Set file attribute value whose name is <code>name</code> as <code>value</code>
- *
- * @param name attribute name
- * @param value attribute value
- */
- public
- void
- setAttribute(
- final Attribute name,
- final Object value
- )
- {
- attr.put( name, value );
- }
- /**
- * Return file attribute value whose name is <code>name</code>
- *
- * @param name attribute name
- *
- * @return attribute value
- */
- public
- Object
- getAttribute(
- final Attribute name
- )
- {
- return attr.get( name );
- }
-
- /* (non-Javadoc)
- * @see java.lang.Object#clone()
- */
- @Override
- public
- Object
- clone()
- {
- final File file = new File();
- file.name = this.name;
- file.contents = this.contents;
- return file;
- }
-
- }
-
- /**
- * <p>
- * Directory.
- *
- * Directory in memory
- *
- * </p>
- *
- * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
- */
- class Directory
- extends File
- implements Cloneable
- {
- /**
- * Child files & directories
- */
- protected final HashMap<String, File> name2file = new HashMap<String, File>();
-
- /**
- * Default constructor
- */
- public Directory()
- {
- }
-
- /**
- * Constructor with directory name
- *
- * @param name directory name
- */
- public
- Directory(
- final String name
- )
- {
- super( name, null );
- }
-
- /**
- * Return child file whose name is <code>name</code>
- *
- * @param name file name
- *
- * @return {@link File}
- */
- public
- File
- get(
- final String name
- )
- {
- return name2file.get( name );
- }
-
- /**
- * Delete child file whose name is <code>name</code>
- *
- * @param name file name
- *
- * @return file to be deleted
- */
- public
- File
- remove(
- final String name
- )
- {
- return name2file.remove( name );
- }
-
- /**
- * Add new file whose name is <code>contents<code> and whose contents is <code>contents</code>
- *
- * @param name file name
- * @param contents file contents
- */
- public
- void
- add(
- final String name,
- final byte[] contents
- )
- {
- name2file.put( name, new File( name, contents ) );
- }
-
- /**
- * Add directory whose name is <code>name</code>
- *
- * @param name directory name
- */
- public
- void
- add(
- final String name
- )
- {
- name2file.put( name, new Directory( name ) );
- }
-
- /**
- * Add <code>file</code>
- *
- * @param file file or directory to add
- */
- public
- void
- add(
- final File file
- )
- {
- name2file.put( file.getName(), file );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.VirtualFileHandler.File#clone()
- */
- @Override
- public
- Object
- clone()
- {
- final Directory dir = new Directory();
- dir.contents = this.contents;
- dir.name = this.name;
- for ( final String name : name2file.keySet() )
- {
- name2file.put( name, (File) name2file.get( name ).clone() );
- }
-
- return dir;
- }
-
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- @Override
- public
- String
- toString()
- {
- return name2file.keySet().toString();
- }
- }
-
- /**
- * Prefix abstract path
- */
- protected static final String ROOT_PREFIX = "/";
-
- /**
- * Logger for this instance
- */
- protected final Logger logger = LoggerFactory.getLogger( getClass() );
-
- /**
- * Root directory
- */
- protected final Directory root = new Directory();
-
- /**
- * Current working directory
- */
- protected String cwd = "/";
-
- /**
- * Return {@link File} matching with <code>path</code>
- *
- * @param path file path
- * @return {@link File} matching with <code>path</code>
- *
- * @throws IOException If file is not accessible
- */
- protected
- File
- getFile(
- final String path
- )
- {
- logger.trace( "Path :{}", path );
-
- if ( null == path )
- {
- return null;
- }
-
- final String[] fragments = getCanonicalFragments( path );
-
- File iter = root;
- for ( int i = 0, n = fragments.length ; i < n ; ++i )
- {
- Directory dir = (Directory) iter;
- iter = dir.get( fragments[i] );
- if ( null == iter )
- {
- logger.debug( "fragment :{}", fragments[i] );
- logger.debug( "in-processing directory :{}", dir );
-
- return null;
- }
- }
-
- return iter;
- }
-
- /**
- * Return parent directory of file matching with <code>path</code>
- * @param path file path
- *
- * @return parent directory
- *
- * @throws IOException If file or directory is not accessible
- */
- public
- Directory
- getParent(
- final String path
- )
- throws IOException
- {
- final File parent = getFile( removeTailingPath( path, 1 ) );
- if ( !( parent instanceof Directory ) )
- {
- throw new IOException();
- }
-
- return (Directory) parent;
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#makeDirectory(java.lang.String)
- */
- @Override
- public
- void
- makeDirectory(
- final String path
- )
- throws IOException
- {
- final Directory parent = getParent( path );
- final String dirName = getFilename( path );
- if ( parent.name2file.containsKey( dirName ) )
- {
- throw new IOException();
- }
- parent.add( dirName );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#moveDirectory(java.lang.String, java.lang.String)
- */
- @Override
- public
- void
- moveDirectory(
- final String source,
- final String target
- )
- throws IOException
- {
- final String filename = getFilename( source );
-
- final Directory parent = getParent( source );
- final File file = parent.get( filename );
- if ( !( file instanceof Directory ) )
- {
- throw new IOException();
- }
- final Directory newParent = (Directory) getFile( target );
- newParent.add( newParent.remove( filename ) );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#copyDirectory(java.lang.String, java.lang.String)
- */
- @Override
- public
- void
- copyDirectory(
- final String source,
- final String target
- )
- throws IOException
- {
- final Directory dir = (Directory) getFile( source );
- final Directory targetDir = (Directory) getFile( target );
-
- targetDir.add( (File) dir.clone() );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#removeDirectory(java.lang.String)
- */
- @Override
- public
- void
- removeDirectory(
- final String path
- )
- throws IOException
- {
- final Directory dir = getParent( path );
- final String name = getFilename( path );
- dir.remove( name );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#list(java.lang.String)
- */
- @Override
- public
- Collection<String>
- list(
- final String path
- )
- throws IOException
- {
- final Directory dir = (Directory) getFile( path );
- final Collection<String> childNames = dir.name2file.keySet();
- logger.trace( "Directory[{}]'s child :{}", path, childNames );
-
- final ArrayList<String> fullNames = new ArrayList<String>();
-
- for ( final String name : dir.name2file.keySet() )
- {
- fullNames.add( addTailingPath( path, name ) );
- }
-
- logger.debug( "Fullnames :\n{}", fullNames );
- return fullNames;
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#write(java.lang.String, java.io.InputStream)
- */
- @Override
- public
- void
- write(
- final String path,
- final InputStream in
- )
- throws IOException
- {
- final Directory parent = getParent( path );
- if ( !(parent instanceof Directory ) )
- {
- throw new IOException();
- }
-
- final String filename = getFilename( path );
- logger.debug( "Filename :{}", filename );
-
- File file = parent.get( filename );
-
- if ( file instanceof Directory )
- {
- throw new IOException();
- }
- if ( null == file )
- {
- file = new File( getFilename( path ), null );
- }
- getParent( path ).add( file );
-
- final byte[] contents = IOUtil.getBytes( in );
-
- if ( logger.isTraceEnabled() )
- {
- int nSize = size( contents );
- logger.trace(
- "Save file[{}] :{} byte(s)\n{}",
- new Object[] {
- path,
- nSize,
- StringUtil.text2hexa( contents, 0, Math.min( 64, size( contents ) ) ) + ( (64 < size( contents ) )?"...":"" )
- }
- );
- }
-
- file.setContents( contents );
- }
-
- /**
- * write <code>contents</code> to file with <code>path</code>
- *
- * @param path file path
- * @param contents file contents
- *
- * @throws IOException If file can't be writable
- *
- * @see {@link #write(String, InputStream)}
- */
- public
- void
- write(
- final String path,
- final String contents
- )
- throws IOException
- {
- logger.trace( "Save file :{}\n{}", path, contents );
- final InputStream in = new ByteArrayInputStream( (null==contents)?(new byte[0]):contents.getBytes() );
- try
- {
- write( path, in );
- }
- finally
- {
- tryClose( in );
- }
-
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#read(java.lang.String)
- */
- @Override
- public
- InputStream
- read(
- final String path
- )
- throws IOException
- {
- logger.trace( "Read {}", path );
- final File file = getFile( path );
- if ( file instanceof Directory )
- {
- throw new IOException();
- }
-
- return new ByteArrayInputStream( file.contents );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#moveFile(java.lang.String, java.lang.String)
- */
- @Override
- public
- void
- moveFile(
- final String source,
- final String target
- )
- throws IOException
- {
- final Directory directory = getParent( source );
- final String fileName = getFilename( source );
- final File file = directory.remove( fileName );
- if ( null == file )
- {
- throw new IOException();
- }
-
- final Directory targetDirectory = (Directory) getFile( target );
- targetDirectory.add( file );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#copyFile(java.lang.String, java.lang.String)
- */
- @Override
- public
- void
- copyFile(
- final String source,
- final String target
- )
- throws IOException
- {
- final Directory directory = getParent( source );
- final String fileName = getFilename( source );
- final File file = directory.get( fileName );
- if ( null == file )
- {
- throw new IOException();
- }
-
- final Directory targetDirectory = (Directory) getFile( target );
- targetDirectory.add( (File) file.clone() );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#removeFile(java.lang.String)
- */
- @Override
- public
- void
- removeFile(
- final String path
- )
- throws IOException
- {
- final File file = getFile( path );
- if ( file instanceof Directory )
- {
- throw new IOException();
- }
-
- final Directory directory = getParent( path );
- final String fileName = getFilename( path );
- directory.remove( fileName );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#get(java.lang.String, org.tizen.common.file.FileHandler.Attribute)
- */
- @Override
- public
- Object
- get(
- final String path,
- final Attribute name
- )
- throws IOException
- {
- logger.trace( "Path :{}, Name :{}", path, name );
- File file = getFile( path );
- if ( Attribute.TYPE.equals( name ) )
- {
- if ( file instanceof Directory )
- {
- return Type.DIRECTORY;
- }
- else if ( file instanceof File )
- {
- return Type.FILE;
- }
- }
- else if ( Attribute.EXISTS.equals( name ) )
- {
- return ( null != file );
-
- }
- else if ( Attribute.SIZE.equals( name ) )
- {
- if ( null == file || null == file.getContents() )
- {
- return (long) 0;
- }
- return (long) file.getContents().length;
- }
- else if ( Attribute.PATH.equals( name ) )
- {
- if ( path.startsWith( ROOT_PREFIX ) )
- {
- return getCanonicalForm( path );
- }
-
- return getCanonicalForm ( addTailingPath( cwd, path ) );
- }
- else if ( Attribute.NAME.equals( name ) )
- {
- return getFilename( path );
- }
- else if ( Attribute.URI.equals( name ) || Attribute.URL.equals( name ) )
- {
- return "vf://" + path;
- }
- else
- {
- file.getAttribute( name );
- return false;
- }
- throw new IOException( path + "'s Unknown attribute :" + name );
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#set(java.lang.String, org.tizen.common.file.FileHandler.Attribute, java.lang.Object)
- */
- @Override
- public
- void
- set(
- final String path,
- final Attribute name,
- final Object value
- )
- throws IOException
- {
- final File file = getFile( path );
- if ( null != file )
- {
- file.setAttribute( name, value );
- }
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#getCurrentWorkingDirectory()
- */
- @Override
- public
- String
- getCurrentWorkingDirectory() {
- return this.cwd;
- }
-
- /* (non-Javadoc)
- * @see org.tizen.common.file.FileHandler#setCurrentWorkingDirectory(java.lang.String)
- */
- @Override
- public
- void
- setCurrentWorkingDirectory(
- final String cwd
- )
- {
- this.cwd = cwd;
- logger.info( "CWD changed :{}", this.cwd );
- }
+ /**
+ * <p>
+ * File.
+ *
+ * File contents container
+ *
+ * </p>
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+ private class
+ File
+ implements Cloneable
+ {
+ /**
+ * File name
+ */
+ String name;
+
+ /**
+ * File contents
+ */
+ byte[] contents;
+
+ /**
+ * File attributes
+ */
+ HashMap<Attribute, Object> attr = new HashMap<Attribute, Object>();
+
+ /**
+ * Default constructor
+ */
+ public File()
+ {
+ }
+
+ /**
+ * Constructor with name and contents
+ *
+ * @param name file name
+ * @param contents file contents
+ */
+ public
+ File(
+ final String name,
+ final byte[] contents
+ )
+ {
+ this.name = name;
+ this.contents = contents;
+ }
+
+ /**
+ * Return file name
+ *
+ * @return file name
+ */
+ public
+ String
+ getName()
+ {
+ return this.name;
+ }
+
+ /**
+ * Return file contents
+ *
+ * @return file contents
+ */
+ public
+ byte[] getContents()
+ {
+ return this.contents;
+ }
+
+ /**
+ * Set <code>contents</code> as file contents
+ *
+ * @param contents new file contents
+ */
+ public
+ void
+ setContents(
+ final byte[] contents
+ )
+ {
+ this.contents = contents;
+ }
+
+ /**
+ * Set file attribute value whose name is <code>name</code> as <code>value</code>
+ *
+ * @param name attribute name
+ * @param value attribute value
+ */
+ public
+ void
+ setAttribute(
+ final Attribute name,
+ final Object value
+ )
+ {
+ attr.put( name, value );
+ }
+ /**
+ * Return file attribute value whose name is <code>name</code>
+ *
+ * @param name attribute name
+ *
+ * @return attribute value
+ */
+ public
+ Object
+ getAttribute(
+ final Attribute name
+ )
+ {
+ return attr.get( name );
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#clone()
+ */
+ @Override
+ public
+ Object
+ clone()
+ {
+ final File file = new File();
+ file.name = this.name;
+ file.contents = this.contents;
+ return file;
+ }
+
+ }
+
+ /**
+ * <p>
+ * Directory.
+ *
+ * Directory in memory
+ *
+ * </p>
+ *
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+ class Directory
+ extends File
+ implements Cloneable
+ {
+ /**
+ * Child files & directories
+ */
+ protected final HashMap<String, File> name2file = new HashMap<String, File>();
+
+ /**
+ * Default constructor
+ */
+ public Directory()
+ {
+ }
+
+ /**
+ * Constructor with directory name
+ *
+ * @param name directory name
+ */
+ public
+ Directory(
+ final String name
+ )
+ {
+ super( name, null );
+ }
+
+ /**
+ * Return child file whose name is <code>name</code>
+ *
+ * @param name file name
+ *
+ * @return {@link File}
+ */
+ public
+ File
+ get(
+ final String name
+ )
+ {
+ return name2file.get( name );
+ }
+
+ /**
+ * Delete child file whose name is <code>name</code>
+ *
+ * @param name file name
+ *
+ * @return file to be deleted
+ */
+ public
+ File
+ remove(
+ final String name
+ )
+ {
+ return name2file.remove( name );
+ }
+
+ /**
+ * Add new file whose name is <code>contents<code> and whose contents is <code>contents</code>
+ *
+ * @param name file name
+ * @param contents file contents
+ */
+ public
+ void
+ add(
+ final String name,
+ final byte[] contents
+ )
+ {
+ name2file.put( name, new File( name, contents ) );
+ }
+
+ /**
+ * Add directory whose name is <code>name</code>
+ *
+ * @param name directory name
+ */
+ public
+ void
+ add(
+ final String name
+ )
+ {
+ name2file.put( name, new Directory( name ) );
+ }
+
+ /**
+ * Add <code>file</code>
+ *
+ * @param file file or directory to add
+ */
+ public
+ void
+ add(
+ final File file
+ )
+ {
+ name2file.put( file.getName(), file );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.VirtualFileHandler.File#clone()
+ */
+ @Override
+ public
+ Object
+ clone()
+ {
+ final Directory dir = new Directory();
+ dir.contents = this.contents;
+ dir.name = this.name;
+ for ( final String name : name2file.keySet() )
+ {
+ name2file.put( name, (File) name2file.get( name ).clone() );
+ }
+
+ return dir;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public
+ String
+ toString()
+ {
+ return name2file.keySet().toString();
+ }
+ }
+
+ /**
+ * Prefix abstract path
+ */
+ protected static final String ROOT_PREFIX = "/";
+
+ /**
+ * Logger for this instance
+ */
+ protected final Logger logger = LoggerFactory.getLogger( getClass() );
+
+ /**
+ * Root directory
+ */
+ protected final Directory root = new Directory();
+
+ /**
+ * Current working directory
+ */
+ protected String cwd = "/";
+
+ /**
+ * Return {@link File} matching with <code>path</code>
+ *
+ * @param path file path
+ * @return {@link File} matching with <code>path</code>
+ *
+ * @throws IOException If file is not accessible
+ */
+ protected
+ File
+ getFile(
+ final String path
+ )
+ {
+ logger.trace( "Path :{}", path );
+
+ if ( null == path )
+ {
+ return null;
+ }
+
+ final String[] fragments = getCanonicalFragments( path );
+
+ File iter = root;
+ for ( int i = 0, n = fragments.length ; i < n ; ++i )
+ {
+ Directory dir = (Directory) iter;
+ iter = dir.get( fragments[i] );
+ if ( null == iter )
+ {
+ logger.debug( "fragment :{}", fragments[i] );
+ logger.debug( "in-processing directory :{}", dir );
+
+ return null;
+ }
+ }
+
+ return iter;
+ }
+
+ /**
+ * Return parent directory of file matching with <code>path</code>
+ * @param path file path
+ *
+ * @return parent directory
+ *
+ * @throws IOException If file or directory is not accessible
+ */
+ public
+ Directory
+ getParent(
+ final String path
+ )
+ throws IOException
+ {
+ final File parent = getFile( removeTailingPath( path, 1 ) );
+ if ( !( parent instanceof Directory ) )
+ {
+ throw new IOException();
+ }
+
+ return (Directory) parent;
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#makeDirectory(java.lang.String)
+ */
+ @Override
+ public
+ void
+ makeDirectory(
+ final String path
+ )
+ throws IOException
+ {
+ final Directory parent = getParent( path );
+ final String dirName = getFilename( path );
+ if ( parent.name2file.containsKey( dirName ) )
+ {
+ throw new IOException();
+ }
+ parent.add( dirName );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#moveDirectory(java.lang.String, java.lang.String)
+ */
+ @Override
+ public
+ void
+ moveDirectory(
+ final String source,
+ final String target
+ )
+ throws IOException
+ {
+ final String filename = getFilename( source );
+
+ final Directory parent = getParent( source );
+ final File file = parent.get( filename );
+ if ( !( file instanceof Directory ) )
+ {
+ throw new IOException();
+ }
+ final Directory newParent = (Directory) getFile( target );
+ newParent.add( newParent.remove( filename ) );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#copyDirectory(java.lang.String, java.lang.String)
+ */
+ @Override
+ public
+ void
+ copyDirectory(
+ final String source,
+ final String target
+ )
+ throws IOException
+ {
+ final Directory dir = (Directory) getFile( source );
+ final Directory targetDir = (Directory) getFile( target );
+
+ targetDir.add( (File) dir.clone() );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#removeDirectory(java.lang.String)
+ */
+ @Override
+ public
+ void
+ removeDirectory(
+ final String path
+ )
+ throws IOException
+ {
+ final Directory dir = getParent( path );
+ final String name = getFilename( path );
+ dir.remove( name );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#list(java.lang.String)
+ */
+ @Override
+ public
+ Collection<String>
+ list(
+ final String path
+ )
+ throws IOException
+ {
+ final Directory dir = (Directory) getFile( path );
+ final Collection<String> childNames = dir.name2file.keySet();
+ logger.trace( "Directory[{}]'s child :{}", path, childNames );
+
+ final ArrayList<String> fullNames = new ArrayList<String>();
+
+ for ( final String name : dir.name2file.keySet() )
+ {
+ fullNames.add( addTailingPath( path, name ) );
+ }
+
+ logger.debug( "Fullnames :\n{}", fullNames );
+ return fullNames;
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#write(java.lang.String, java.io.InputStream)
+ */
+ @Override
+ public
+ void
+ write(
+ final String path,
+ final InputStream in
+ )
+ throws IOException
+ {
+ final Directory parent = getParent( path );
+ if ( !(parent instanceof Directory ) )
+ {
+ throw new IOException();
+ }
+
+ final String filename = getFilename( path );
+ logger.debug( "Filename :{}", filename );
+
+ File file = parent.get( filename );
+
+ if ( file instanceof Directory )
+ {
+ throw new IOException();
+ }
+ if ( null == file )
+ {
+ file = new File( getFilename( path ), null );
+ }
+ getParent( path ).add( file );
+
+ final byte[] contents = IOUtil.getBytes( in );
+
+ if ( logger.isTraceEnabled() )
+ {
+ int nSize = size( contents );
+ logger.trace(
+ "Save file[{}] :{} byte(s)\n{}",
+ new Object[] {
+ path,
+ nSize,
+ StringUtil.text2hexa( contents, 0, Math.min( 64, size( contents ) ) ) + ( (64 < size( contents ) )?"...":"" )
+ }
+ );
+ }
+
+ file.setContents( contents );
+ }
+
+ /**
+ * write <code>contents</code> to file with <code>path</code>
+ *
+ * @param path file path
+ * @param contents file contents
+ *
+ * @throws IOException If file can't be writable
+ *
+ * @see {@link #write(String, InputStream)}
+ */
+ public
+ void
+ write(
+ final String path,
+ final String contents
+ )
+ throws IOException
+ {
+ logger.trace( "Save file :{}\n{}", path, contents );
+ final InputStream in = new ByteArrayInputStream( (null==contents)?(new byte[0]):contents.getBytes() );
+ try
+ {
+ write( path, in );
+ }
+ finally
+ {
+ tryClose( in );
+ }
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#read(java.lang.String)
+ */
+ @Override
+ public
+ InputStream
+ read(
+ final String path
+ )
+ throws IOException
+ {
+ logger.trace( "Read {}", path );
+ final File file = getFile( path );
+ if ( file instanceof Directory )
+ {
+ throw new IOException();
+ }
+
+ return new ByteArrayInputStream( file.contents );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#moveFile(java.lang.String, java.lang.String)
+ */
+ @Override
+ public
+ void
+ moveFile(
+ final String source,
+ final String target
+ )
+ throws IOException
+ {
+ final Directory directory = getParent( source );
+ final String fileName = getFilename( source );
+ final File file = directory.remove( fileName );
+ if ( null == file )
+ {
+ throw new IOException();
+ }
+
+ final Directory targetDirectory = (Directory) getFile( target );
+ targetDirectory.add( file );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#copyFile(java.lang.String, java.lang.String)
+ */
+ @Override
+ public
+ void
+ copyFile(
+ final String source,
+ final String target
+ )
+ throws IOException
+ {
+ final Directory directory = getParent( source );
+ final String fileName = getFilename( source );
+ final File file = directory.get( fileName );
+ if ( null == file )
+ {
+ throw new IOException();
+ }
+
+ final Directory targetDirectory = (Directory) getFile( target );
+ targetDirectory.add( (File) file.clone() );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#removeFile(java.lang.String)
+ */
+ @Override
+ public
+ void
+ removeFile(
+ final String path
+ )
+ throws IOException
+ {
+ final File file = getFile( path );
+ if ( file instanceof Directory )
+ {
+ throw new IOException();
+ }
+
+ final Directory directory = getParent( path );
+ final String fileName = getFilename( path );
+ directory.remove( fileName );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#get(java.lang.String, org.tizen.common.file.FileHandler.Attribute)
+ */
+ @Override
+ public
+ Object
+ get(
+ final String path,
+ final Attribute name
+ )
+ throws IOException
+ {
+ logger.trace( "Path :{}, Name :{}", path, name );
+ File file = getFile( path );
+ if ( Attribute.TYPE.equals( name ) )
+ {
+ if ( file instanceof Directory )
+ {
+ return Type.DIRECTORY;
+ }
+ else if ( file instanceof File )
+ {
+ return Type.FILE;
+ }
+ }
+ else if ( Attribute.EXISTS.equals( name ) )
+ {
+ return ( null != file );
+
+ }
+ else if ( Attribute.SIZE.equals( name ) )
+ {
+ if ( null == file || null == file.getContents() )
+ {
+ return (long) 0;
+ }
+ return (long) file.getContents().length;
+ }
+ else if ( Attribute.PATH.equals( name ) )
+ {
+ if ( path.startsWith( ROOT_PREFIX ) )
+ {
+ return getCanonicalForm( path );
+ }
+
+ return getCanonicalForm ( addTailingPath( cwd, path ) );
+ }
+ else if ( Attribute.NAME.equals( name ) )
+ {
+ return getFilename( path );
+ }
+ else if ( Attribute.URI.equals( name ) || Attribute.URL.equals( name ) )
+ {
+ return "vf://" + path;
+ }
+ else
+ {
+ file.getAttribute( name );
+ return false;
+ }
+ throw new IOException( path + "'s Unknown attribute :" + name );
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#set(java.lang.String, org.tizen.common.file.FileHandler.Attribute, java.lang.Object)
+ */
+ @Override
+ public
+ void
+ set(
+ final String path,
+ final Attribute name,
+ final Object value
+ )
+ throws IOException
+ {
+ final File file = getFile( path );
+ if ( null != file )
+ {
+ file.setAttribute( name, value );
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#getCurrentWorkingDirectory()
+ */
+ @Override
+ public
+ String
+ getCurrentWorkingDirectory() {
+ return this.cwd;
+ }
+
+ /* (non-Javadoc)
+ * @see org.tizen.common.file.FileHandler#setCurrentWorkingDirectory(java.lang.String)
+ */
+ @Override
+ public
+ void
+ setCurrentWorkingDirectory(
+ final String cwd
+ )
+ {
+ this.cwd = cwd;
+ logger.info( "CWD changed :{}", this.cwd );
+ }
}