From: Taeyoung Son Date: Wed, 15 May 2013 11:16:59 +0000 (+0900) Subject: [Title] Modify FilenameUtil.getRelativePath() X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6f76f3428d9e28fc937e57f201ff321d94499bb5;p=sdk%2Fide%2Fcommon-eplugin.git [Title] Modify FilenameUtil.getRelativePath() [Desc.] [Issue] Change-Id: I81fbd55441b4a05cdfdf624b6fcdba36ddd73773 --- diff --git a/org.tizen.common/src/org/tizen/common/core/command/zip/ZipCommand.java b/org.tizen.common/src/org/tizen/common/core/command/zip/ZipCommand.java index 8318223..497137b 100755 --- a/org.tizen.common/src/org/tizen/common/core/command/zip/ZipCommand.java +++ b/org.tizen.common/src/org/tizen/common/core/command/zip/ZipCommand.java @@ -190,7 +190,7 @@ extends FileHandlingCommand * * @param zipOut {@link ZipOutputStream} to add entry * @param handler {@link FileHandler} to use to read contents - * @param filePath file paht + * @param filePath file path */ protected void diff --git a/org.tizen.common/src/org/tizen/common/util/FilenameUtil.java b/org.tizen.common/src/org/tizen/common/util/FilenameUtil.java index ebaeef5..a714eb9 100755 --- a/org.tizen.common/src/org/tizen/common/util/FilenameUtil.java +++ b/org.tizen.common/src/org/tizen/common/util/FilenameUtil.java @@ -201,49 +201,55 @@ public class FilenameUtil return true; } - /** - * return relative path of filePath based root - * - * @param root base path - * @param filePath file path - * - * @return relative path - */ - public static - String - getRelativePath( - final String root, - final String filePath - ) - { - final String[] rootFragments = FilenameUtil.getCanonicalFragments( root ); - final String[] fileFragments = FilenameUtil.getCanonicalFragments( filePath ); - - Assert.isTrue( - rootFragments.length < fileFragments.length, - "root must be ancestor[" + root + "] of path[" + filePath + "]" - ); - - for ( int i = 0, n = rootFragments.length ; i < n ; ++i ) - { - Assert.isTrue( ObjectUtil.equals( rootFragments[i], fileFragments[i] ), i + "th [" + rootFragments[i] + "] != [" + fileFragments[i] + "]" ); - } - - final StringBuilder buffer = new StringBuilder(); - - for ( int i = rootFragments.length, n = fileFragments.length ; i < n ; ++i ) - { - if ( rootFragments.length < i ) - { - buffer.append( SEPARATOR_DIRECTORY ); - } - buffer.append( fileFragments[i] ); - } - - return buffer.toString(); - } + /** + * return relative path of filePath based root + * + * @param root absolute base path + * @param filePath absolute file path + * + * @return relative path + */ + public static + String + getRelativePath( + final String root, + final String filePath + ) + { + final String[] rootFragments = FilenameUtil.getCanonicalFragments( root ); + final String[] fileFragments = FilenameUtil.getCanonicalFragments( filePath ); + int nRoot = rootFragments.length; + int nFile = fileFragments.length; + final StringBuilder buffer = new StringBuilder(); - + int nLoop = (nRoot < nFile) + ? nRoot : nFile; + + int nStartDiffer = nLoop; + + for (int i=0;i0) { + buffer.append( SEPARATOR_DIRECTORY ); + } + buffer.append(".."); + } + + for (int i=nStartDiffer;i0) { + buffer.append(SEPARATOR_DIRECTORY); + } + buffer.append(fileFragments[i]); + } + + return buffer.toString(); + } /** * Create canonical form for file path * @@ -321,6 +327,9 @@ public class FilenameUtil } else if ( "..".equals( fragment ) ) { + if (stack.isEmpty()) { + continue; + } stack.pop(); } else diff --git a/org.tizen.common/test/src/org/tizen/common/util/FilenameUtilTest.java b/org.tizen.common/test/src/org/tizen/common/util/FilenameUtilTest.java index 87fde7b..994020e 100755 --- a/org.tizen.common/test/src/org/tizen/common/util/FilenameUtilTest.java +++ b/org.tizen.common/test/src/org/tizen/common/util/FilenameUtilTest.java @@ -285,6 +285,29 @@ FilenameUtilTest ); } } - + + /** + * + */ + @Test + public void test_getRelativePath() { + String cwd = "/a/b/c/d"; + String[][] test_list = { + //input expected + {"/a/b/c/d", ""}, + {"/a/b/c/d/e", "e"}, + {"/a/b/d/e/f", "../../d/e/f"}, + {"/a/b/d", "../../d"}, + {"/b/d/e", "../../../../b/d/e"} + }; + for (int i = 0 ; i