[Title] added more funcion for ZipCommand
authorTaeyoung Son <taeyoung2.son@samsung.com>
Mon, 6 May 2013 05:45:00 +0000 (14:45 +0900)
committerTaeyoung Son <taeyoung2.son@samsung.com>
Thu, 9 May 2013 06:03:17 +0000 (15:03 +0900)
[Desc.]
[Issue]

Change-Id: Ic822135a19f4165611848facedaceed2bba4d8d7

org.tizen.common.builder/src/org/tizen/common/builder/Resource.java
org.tizen.common.builder/test/src/org/tizen/common/builder/ResourceLayerTest.java
org.tizen.common/src/org/tizen/common/core/command/zip/ZipCommand.java
org.tizen.common/src/org/tizen/common/file/IResource.java [new file with mode: 0644]
org.tizen.common/src/org/tizen/common/file/VirtualFileHandler.java

index 584d87f..f0a32b9 100755 (executable)
@@ -34,10 +34,12 @@ import java.io.InputStream;
 \r
 import org.apache.commons.lang3.builder.EqualsBuilder;\r
 import org.apache.commons.lang3.builder.HashCodeBuilder;\r
+import org.tizen.common.file.FileHandler;\r
 import org.tizen.common.file.FileHandler.Attribute;\r
+import org.tizen.common.file.IResource;\r
 \r
 public class\r
-Resource\r
+Resource implements IResource\r
 {\r
     protected String path;\r
     \r
@@ -112,4 +114,13 @@ Resource
     {\r
         return "Resource[" + getPath() + "](" + getLayer() + ")";\r
     }\r
+    \r
+    @Override\r
+    public FileHandler getFileHandler() {\r
+        if (layer!= null) {\r
+            return layer.getFileHandler();\r
+        } else {\r
+            return null;\r
+        }\r
+    }\r
 }\r
index 535620d..0be1d37 100644 (file)
@@ -29,7 +29,6 @@
 package org.tizen.common.builder;\r
 \r
 import org.junit.Test;\r
-import org.tizen.common.builder.ResourceLayer;\r
 import org.tizen.common.file.VirtualFileHandler;\r
 \r
 public class ResourceLayerTest\r
index b421df6..aed38b4 100755 (executable)
@@ -49,6 +49,7 @@ import org.tizen.common.core.command.policy.MessagePolicy;
 import org.tizen.common.file.FileHandler;
 import org.tizen.common.file.FileHandler.Attribute;
 import org.tizen.common.file.FileHandler.Type;
+import org.tizen.common.file.IResource;
 import org.tizen.common.util.Assert;
 import org.tizen.common.util.FilenameUtil;
 import org.tizen.common.util.IOUtil;
@@ -70,6 +71,7 @@ extends FileHandlingCommand<Object>
      * Directory to zip
      */
     protected final String baseDir;
+    private IResource[] resources = null;
 
     /**
      * Constructor with base dir and target file name
@@ -83,7 +85,24 @@ extends FileHandlingCommand<Object>
             final String target
             )
     {
+        this(baseDir, null, target);
+    }
+
+    /**
+     * Constructor with base dir and resources and target file name
+     * @param baseDir base directory path
+     * @param resources resource file list what add to target file. If it is null, add file located in base directory to target file.
+     * @param target target file name
+     */
+    public
+    ZipCommand(
+            final String baseDir,
+            final IResource[] resources,
+            final String target
+            )
+    {
         this.baseDir = baseDir;
+        this.resources = resources;
         setPath( target );
     }
 
@@ -98,7 +117,7 @@ extends FileHandlingCommand<Object>
             final ExecutionContext context
             )
                     throws IOException
-                    {
+    {
         final FileHandler handler = context.getFileHandler();
         Assert.notNull( handler );
 
@@ -138,7 +157,13 @@ extends FileHandlingCommand<Object>
 
         try
         {
-            addEntry( zipOut, handler, baseDir );
+            if (resources != null) {
+                for (IResource resource : resources) {
+                    addEntry(zipOut, resource.getFileHandler(), resource.getPath());
+                }
+            } else {
+                addEntry( zipOut, handler, baseDir );
+            }
         }
         finally
         {
@@ -154,8 +179,7 @@ extends FileHandlingCommand<Object>
             throw new IllegalStateException( e );
             // TODO Handle
         }
-
-                    }
+    }
 
     /**
      * Add file entry with <code>filePath</code> into <code>zipOut</code>
@@ -231,7 +255,6 @@ extends FileHandlingCommand<Object>
                     zipOut.closeEntry();
                 }
 
-
                 for ( final String child : children )
                 {
                     addEntry( zipOut, handler, child );
diff --git a/org.tizen.common/src/org/tizen/common/file/IResource.java b/org.tizen.common/src/org/tizen/common/file/IResource.java
new file mode 100644 (file)
index 0000000..532aa6a
--- /dev/null
@@ -0,0 +1,13 @@
+package org.tizen.common.file;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public interface IResource {
+    public String getName() throws IOException;
+    public String getPath();
+    public FileHandler getFileHandler();
+    public InputStream getContents() throws IOException;
+    public void setContents(InputStream in) throws IOException;
+    public void setContents(byte[] contents) throws IOException;
+}
index 96d266d..57ed4e9 100755 (executable)
@@ -41,6 +41,7 @@ import java.util.HashMap;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.tizen.common.util.FilenameUtil;
 import org.tizen.common.util.IOUtil;
 import org.tizen.common.util.StringUtil;
 
@@ -428,6 +429,29 @@ extends AbstractFileHandler
     /* (non-Javadoc)
      * @see org.tizen.common.file.FileHandler#makeDirectory(java.lang.String)
      */
+    public void makeDirectory(final String path, boolean recursive) throws IOException {
+        if (recursive) {
+            String parentDir = FilenameUtil.removeTailingPath(path, 1);
+            if (!is(parentDir, Attribute.EXISTS)) {
+                makeDirectory(parentDir, recursive);
+            }
+        } 
+        final Directory parent = getParent(path);
+        final String dirName = getFilename(path);
+        
+        if (parent.name2file.containsKey(dirName)) {
+            if (!recursive) {
+                throw new IOException();
+            } else {
+                return;
+            }
+        }
+        parent.add(dirName);
+    }
+
+    /* (non-Javadoc)
+     * @see org.tizen.common.file.FileHandler#makeDirectory(java.lang.String)
+     */
     @Override
     public
     void
@@ -436,13 +460,7 @@ extends AbstractFileHandler
             )
                     throws IOException
                     {
-        final Directory parent = getParent( path );
-        final String dirName = getFilename( path );
-        if ( parent.name2file.containsKey( dirName ) )
-        {
-            throw new IOException();
-        }
-        parent.add( dirName );
+        makeDirectory(path, false);
                     }
 
     /* (non-Javadoc)
@@ -808,5 +826,4 @@ extends AbstractFileHandler
         this.cwd = cwd;
         logger.info( "CWD changed :{}", this.cwd );
     }
-
 }