From: ho.namkoong Date: Wed, 9 Jan 2013 06:06:23 +0000 (+0900) Subject: [Title] Insert appendPath and converPath method X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=890e7daeeb555a6e4cd70852a4b865061dc9eb7a;p=sdk%2Fide%2Fcommon-eplugin.git [Title] Insert appendPath and converPath method [Type] [Module] [Priority] [Jira#] [Redmine#] 7053 [Problem] [Cause] [Solution] [TestCase] Change-Id: Ibe2898a00985db0d4b6c5129a53dbf4b4b7c6df5 --- diff --git a/org.tizen.common/src/org/tizen/common/util/FileUtil.java b/org.tizen.common/src/org/tizen/common/util/FileUtil.java index b3a0b35..9e49fd4 100755 --- a/org.tizen.common/src/org/tizen/common/util/FileUtil.java +++ b/org.tizen.common/src/org/tizen/common/util/FileUtil.java @@ -618,41 +618,84 @@ public class FileUtil { * @return appended path */ public static String appendPath(String originalPath, String appendPath) { + if(OSChecker.isWindows()) { + return appendPath(originalPath, appendPath, true); + } + return appendPath(originalPath, appendPath, false); + } + + /** + * return appended path with given OS type.. + * @param originalPath original path + * @param appendPath path which will be appended to original path. + * @param iwWindow if you want to get Windows path, true. Else, false. + * @return appended path + */ + public static String appendPath(String originalPath, String appendPath, boolean isWindows) { originalPath = originalPath.trim(); appendPath = appendPath.trim(); - if(OSChecker.isWindows()) { - originalPath = originalPath.replace('/', File.separatorChar); - appendPath = appendPath.replace('/', File.separatorChar); + char separatorChar = isWindows ? '\\' : '/'; + char targetChar = isWindows ? '/' : '\\'; + + if(isWindows) { + originalPath = originalPath.replace(targetChar, separatorChar); + appendPath = appendPath.replace(targetChar, separatorChar); } else { - originalPath = originalPath.replace('\\', File.separatorChar); - appendPath = appendPath.replace('\\', File.separatorChar); + originalPath = originalPath.replace(targetChar, separatorChar); + appendPath = appendPath.replace(targetChar, separatorChar); } - return trimLastPath(originalPath).concat(trimFirstPath(appendPath)); + return trimLastPath(originalPath, separatorChar).concat(trimFirstPath(appendPath, separatorChar)); + } + + /** + * Convert path to given OS path. + * + * @param input path which will be converted. + * @param isWindows If windows, true. Else, false. + * @return converted path. + */ + public static String convertToOSPath(String input, boolean isWindows) { + return appendPath(input, StringUtil.EMPTY_STRING, isWindows); + } + + /** + * Convert path to current OS path. + * + * @param input path which will be converted. + * @return converted path. + */ + public static String convertToOSPath(String input) { + if(OSChecker.isWindows()) { + return convertToOSPath(input, true); + } + else { + return convertToOSPath(input, false); + } } - private static String trimLastPath(String originalPath) { + private static String trimLastPath(String originalPath, char targetChar) { if(StringUtil.isEmpty(originalPath)) { return StringUtil.EMPTY_STRING; } char lastChar = originalPath.charAt(originalPath.length() - 1); - if(lastChar == File.separatorChar) { + if(lastChar == targetChar) { return originalPath.substring(0, originalPath.length() - 1); } return originalPath; } - private static String trimFirstPath(String originalPath) { + private static String trimFirstPath(String originalPath, char targetChar) { if(StringUtil.isEmpty(originalPath)) { return StringUtil.EMPTY_STRING; } char firstChar = originalPath.charAt(0); - if(firstChar != File.separatorChar) { - return ("" + File.separatorChar).concat(originalPath); + if(firstChar != targetChar) { + return ("" + targetChar).concat(originalPath); } return originalPath; } diff --git a/org.tizen.common/test/src/org/tizen/common/util/FileUtilTest.java b/org.tizen.common/test/src/org/tizen/common/util/FileUtilTest.java index d01c807..7d559a4 100644 --- a/org.tizen.common/test/src/org/tizen/common/util/FileUtilTest.java +++ b/org.tizen.common/test/src/org/tizen/common/util/FileUtilTest.java @@ -309,13 +309,15 @@ public class FileUtilTest { assertTrue(result.equals(FileUtil.readFromFile(url))); } } + /** * Test {@link FileUtil#appendPath(String, String)} + * Test {@link FileUtil#appendPath(String, String, boolean)} * * @throws Exception in case of failure in test * * @see {@link FileUtil#appendPath(String, String)} - * @throws Exception + * @see {@link FileUtil#appendPath(String, String, boolean))} */ @Test public void test_appendPath() throws Exception { @@ -329,10 +331,43 @@ public class FileUtilTest { File.separatorChar + "d" + File.separatorChar + "e" + File.separatorChar + "f" + File.separatorChar + "g" + File.separatorChar + "h" + File.separatorChar; - assertTrue(result.equals(FileUtil.appendPath(windowFirst, linuxLast))); - assertTrue(result.equals(FileUtil.appendPath(windowFirst, windowLast))); - assertTrue(result.equals(FileUtil.appendPath(linuxFirst, linuxLast))); - assertTrue(result.equals(FileUtil.appendPath(linuxFirst, windowLast))); + assertEquals(result, FileUtil.appendPath(windowFirst, linuxLast)); + assertEquals(result, FileUtil.appendPath(windowFirst, windowLast)); + assertEquals(result, FileUtil.appendPath(linuxFirst, linuxLast)); + assertEquals(result, FileUtil.appendPath(linuxFirst, windowLast)); + + result = result.replace('\\', '/'); + assertEquals(result, FileUtil.appendPath(windowFirst, linuxLast, false)); + assertEquals(result, FileUtil.appendPath(windowFirst, windowLast, false)); + assertEquals(result, FileUtil.appendPath(linuxFirst, linuxLast, false)); + assertEquals(result, FileUtil.appendPath(linuxFirst, windowLast, false)); + + result = result.replace('/', '\\'); + assertEquals(result, FileUtil.appendPath(windowFirst, linuxLast, true)); + assertEquals(result, FileUtil.appendPath(windowFirst, windowLast, true)); + assertEquals(result, FileUtil.appendPath(linuxFirst, linuxLast, true)); + assertEquals(result, FileUtil.appendPath(linuxFirst, windowLast, true)); + } + + /** + * Test {@link FileUtil#convertToOSPath(String, boolean)} + * Test {@link FileUtil#convertToOSPath(String)} + * + * @throws Exception + */ + public void test_convertToOSPath() throws Exception { + String windowPath = "\\a\\b\\c\\d\\"; + String linuxPath = "/a/b/c/d/"; + + assertEquals(linuxPath, FileUtil.convertToOSPath(windowPath, false)); + assertEquals(linuxPath, FileUtil.convertToOSPath(linuxPath, false)); + assertEquals(windowPath, FileUtil.convertToOSPath(windowPath, true)); + assertEquals(windowPath, FileUtil.convertToOSPath(linuxPath, true)); + + String result = File.separatorChar + "a" + File.separatorChar + "b" + File.separatorChar + "c" + File.separatorChar + "d"; + + assertEquals(result, FileUtil.convertToOSPath(windowPath)); + assertEquals(result, FileUtil.convertToOSPath(linuxPath)); } /**