[Title] FilenameUtil permit to use '\' in filename
authorBonyong.lee <bonyong.lee@samsung.com>
Fri, 28 Jun 2013 00:37:03 +0000 (09:37 +0900)
committerBonyong.lee <bonyong.lee@samsung.com>
Fri, 28 Jun 2013 00:37:03 +0000 (09:37 +0900)
[Desc.] You can use escapse character

org.tizen.common/src/org/tizen/common/util/FilenameUtil.java
org.tizen.common/test/src/org/tizen/common/util/FilenameUtilTest.java

index a714eb9..903eb29 100755 (executable)
@@ -25,6 +25,7 @@
 package org.tizen.common.util;
 
 import static org.tizen.common.util.ArrayUtil.size;
+import static org.tizen.common.util.StringUtil.isEmpty;
 import static org.tizen.common.util.StringUtil.trimLeadingCharacter;
 import static org.tizen.common.util.StringUtil.trimTrailingCharacter;
 
@@ -63,25 +64,92 @@ public class FilenameUtil
        /**
         * Separate path to directory name fragements
         * 
+        * If you want to use '\' in file name for windows, use quote mark( ' ).
+        * 
         * @param path file path
         * 
         * @return canonical path's fragements
         */
        public static
        String[]
-       getCanonicalFragments( final String path )
+       getCanonicalFragments(
+           final String path
+       )
        {
-               final String safe = path.replace( File.separatorChar, SEPARATOR_DIRECTORY);
-               final ArrayList<String> ret = new ArrayList<String>();
-               for ( final String fragment : safe.split( "" + SEPARATOR_DIRECTORY ) )
-               {
-                       if ( StringUtil.isEmpty( fragment ) )
-                       {
-                               continue;
-                       }
-                       
-                       ret.add( fragment );
-               }
+           final char CH_ESCAPE = '\\';
+           final char CH_QUOTE = '\'';
+           
+           final int ST_NORMAL = 0;
+           final int ST_READY_TO_ESCAPE =2;
+           final int ST_QUOTE = 3;
+           
+           final ArrayList<String> ret = new ArrayList<String>();
+           char[] characters = path.toCharArray();
+           
+           StringBuilder buffer = new StringBuilder();
+           int status = ST_NORMAL;
+           for ( int i = 0, n = characters.length ; i < n ; ++i )
+           {
+               final char ch = characters[i];
+               switch ( status )
+            {
+            case ST_NORMAL:
+                if ( SEPARATOR_DIRECTORY == ch || File.separatorChar == ch )
+                {
+                    final String fragment = buffer.toString();
+                    if ( !isEmpty( fragment ) )
+                    {
+                        ret.add( fragment );
+                    }
+                    buffer.delete( 0, buffer.length() );
+                }
+                else if ( CH_QUOTE == ch )
+                {
+                    status = ST_QUOTE;
+                }
+                else
+                {
+                    buffer.append( ch );
+                }
+                break;
+            case ST_READY_TO_ESCAPE:
+                if ( CH_QUOTE != ch )
+                {
+                    buffer.append( CH_ESCAPE );
+                    buffer.append( ch );
+                }
+                status = ST_QUOTE;
+                break;
+            case ST_QUOTE:
+                if ( CH_QUOTE == ch )
+                {
+                    status = ST_NORMAL;
+                }
+                else if ( CH_ESCAPE == ch )
+                {
+                    status = ST_READY_TO_ESCAPE;
+                }
+                else
+                {
+                    buffer.append( ch );
+                }
+                break;
+
+            default:
+                break;
+            }
+           }
+           
+           if ( 0 < buffer.length() ) 
+           {
+            final String fragment = buffer.toString();
+            if ( !isEmpty( fragment ) )
+            {
+                ret.add( fragment );
+            }
+            buffer.delete( 0, buffer.length() );
+           }
+           
                return ret.toArray( new String[0] );
        }
 
index 994020e..ab065b1 100755 (executable)
@@ -59,18 +59,18 @@ FilenameUtilTest
        {
                final Object[][] TEST_CASES = new Object[][] {
                        new Object[] { "/aaa/bbb/ccc", new String[] { "aaa", "bbb", "ccc" } },
+                       new Object[] { "/aaa/'\\bbb'/ccc", new String[] { "aaa", "\\bbb", "ccc" } },
                        new Object[] { File.separator + "aaa" + File.separator + "bbb" +  File.separator + "ccc", new String[] { "aaa", "bbb", "ccc" } },
                };
                
                int iTest = 0;
                for ( final Object[] TEST_CASE : TEST_CASES )
                {
-                       ++iTest;
                        final String input = (String) TEST_CASE[0];
                        final String[] expected = (String[]) TEST_CASE[1];
                        
                        assertArrayEquals(
-                               iTest + " th test failed. input :<" + input + ">",
+                               ++iTest + " th test failed. input :<" + input + ">",
                                expected,
                                FilenameUtil.getCanonicalFragments( input )
                        );