-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #2866 handle relative path and including 'pom.xml' in modules declarations Signed-off-by: Evgen Vidolob <[email protected]>
- Loading branch information
Evgen Vidolob
authored
Dec 22, 2016
1 parent
382217c
commit 69276bb
Showing
8 changed files
with
443 additions
and
32 deletions.
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
core/commons/che-core-commons-lang/src/main/java/org/eclipse/che/commons/lang/PathUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.commons.lang; | ||
|
||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Set of util methods for path strings. | ||
*/ | ||
public class PathUtil { | ||
|
||
/** | ||
* Converts path to canonical form via traversing '..' and eliminating '.' and removing duplicate separators. | ||
* <b>Note:</b> This method works for Unix paths only. | ||
*/ | ||
public static String toCanonicalPath(String path, boolean removeLastSlash) { | ||
if (path == null || path.isEmpty()) { | ||
return path; | ||
} | ||
if (path.charAt(0) == '.') { | ||
if (path.length() == 1) { | ||
return ""; | ||
} | ||
|
||
char c = path.charAt(1); | ||
if (c == '/') { | ||
path = path.substring(2); | ||
} | ||
} | ||
|
||
int index = -1; | ||
do { | ||
index = path.indexOf('/', index + 1); | ||
char next = index == path.length() - 1 ? 0 : path.charAt(index + 1); | ||
if (next == '.' || next == '/') { | ||
break; | ||
} | ||
|
||
} while (index != -1); | ||
|
||
if (index == -1) { | ||
if (removeLastSlash) { | ||
int start = processRoot(path, NullWriter.INSTANCE); | ||
int slashIndex = path.lastIndexOf('/'); | ||
return slashIndex != -1 && slashIndex > start ? StringUtils.trimEnd(path, '/') : path; | ||
} | ||
return path; | ||
} | ||
String finalPath = path; | ||
Supplier<String> canonicalPath = () -> { | ||
try { | ||
return new File(finalPath).getCanonicalPath(); | ||
} catch (IOException ignore) { | ||
return toCanonicalPath(finalPath, removeLastSlash); | ||
} | ||
}; | ||
|
||
StringBuilder result = new StringBuilder(path.length()); | ||
int start = processRoot(path, result); | ||
int dots = 0; | ||
boolean separator = true; | ||
|
||
for (int i = start; i < path.length(); ++i) { | ||
char c = path.charAt(i); | ||
if (c == '/') { | ||
if (!separator) { | ||
if (!processDots(result, dots, start)) { | ||
return canonicalPath.get(); | ||
} | ||
dots = 0; | ||
} | ||
separator = true; | ||
} else if (c == '.') { | ||
if (separator || dots > 0) { | ||
++dots; | ||
} else { | ||
result.append('.'); | ||
} | ||
separator = false; | ||
} else { | ||
if (dots > 0) { | ||
StringUtils.repeatSymbol(result, '.', dots); | ||
dots = 0; | ||
} | ||
result.append(c); | ||
separator = false; | ||
} | ||
} | ||
|
||
if (dots > 0) { | ||
if (!processDots(result, dots, start)) { | ||
return canonicalPath.get(); | ||
} | ||
} | ||
|
||
int lastChar = result.length() - 1; | ||
if (removeLastSlash && lastChar >= 0 && result.charAt(lastChar) == '/' && lastChar > start) { | ||
result.deleteCharAt(lastChar); | ||
} | ||
return result.toString(); | ||
} | ||
|
||
private static boolean processDots(StringBuilder result, int dots, int start) { | ||
if (dots == 2) { | ||
int pos = -1; | ||
if (!StringUtils.endWith(result, "/../") && !StringUtils.equals(result, "../")) { | ||
pos = StringUtils.lastIndexOf(result, '/', start, result.length() - 1); | ||
if (pos >= 0) { | ||
++pos; | ||
} else if (start > 0) { | ||
pos = start; | ||
} else if (result.length() > 0) { | ||
pos = 0; | ||
} | ||
} | ||
if (pos >= 0) { | ||
result.delete(pos, result.length()); | ||
} else { | ||
result.append("../"); | ||
} | ||
} else if (dots != 1) { | ||
StringUtils.repeatSymbol(result, '.', dots); | ||
result.append('/'); | ||
} | ||
return true; | ||
} | ||
|
||
private static int processRoot(String path, Appendable appendable) { | ||
try { | ||
if (!path.isEmpty() && path.charAt(0) == '/') { | ||
appendable.append('/'); | ||
return 1; | ||
} | ||
if (path.length() > 2 && path.charAt(1) == ':' && path.charAt(2) == '/') { | ||
appendable.append(path, 0, 3); | ||
return 3; | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return 0; | ||
} | ||
|
||
private static final class NullWriter implements Appendable { | ||
|
||
public static final NullWriter INSTANCE = new NullWriter(); | ||
|
||
@Override | ||
public Appendable append(CharSequence csq) throws IOException { | ||
return this; | ||
} | ||
|
||
@Override | ||
public Appendable append(CharSequence csq, int start, int end) throws IOException { | ||
return this; | ||
} | ||
|
||
@Override | ||
public Appendable append(char c) throws IOException { | ||
return this; | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...commons/che-core-commons-lang/src/main/java/org/eclipse/che/commons/lang/StringUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.commons.lang; | ||
|
||
/** | ||
* Set of useful String methods | ||
*/ | ||
public class StringUtils { | ||
|
||
/** | ||
* Trim last char of the string if string ends with that char. | ||
* | ||
* @param s the string to trim | ||
* @param suffix the suffix | ||
* @return trimmed string | ||
*/ | ||
public static String trimEnd(String s, char suffix) { | ||
if (endWithChar(s, suffix)) { | ||
return s.substring(0, s.length() - 1); | ||
} | ||
return s; | ||
} | ||
|
||
/** | ||
* Check if string ends with char. | ||
*/ | ||
public static boolean endWithChar(String s, char suffix) { | ||
return s != null && !s.isEmpty() && s.charAt(s.length() - 1) == suffix; | ||
} | ||
|
||
/** | ||
* Add to builder 'times' symbol 'symbol' | ||
*/ | ||
public static void repeatSymbol(StringBuilder builder, char symbol, int times) { | ||
for (int i = 0; i < times; i++) { | ||
builder.append(symbol); | ||
} | ||
} | ||
|
||
/** | ||
* Check if CharSequence end with suffix | ||
*/ | ||
public static boolean endWith(CharSequence builder, CharSequence suffix) { | ||
int bl = builder.length(); | ||
int sl = suffix.length(); | ||
if (bl < sl) { | ||
return false; | ||
} | ||
|
||
for (int i = bl - 1; i >= bl - sl; i--) { | ||
if (builder.charAt(i) != suffix.charAt(i + sl - bl)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Check that char sequences are equal | ||
*/ | ||
public static boolean equals(CharSequence c1, CharSequence c2) { | ||
if (c1 == null ^ c2 == null) { | ||
return false; | ||
} | ||
|
||
if (c1 == null) { | ||
return true; | ||
} | ||
if (c1.length() != c2.length()) { | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < c1.length(); i++) { | ||
if (c1.charAt(i) != c2.charAt(i)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns the index within this string of the last occurrence of the | ||
* specified char, searching in specified range | ||
*/ | ||
public static int lastIndexOf(CharSequence s, char c, int start, int end) { | ||
start = Math.max(start, 0); | ||
for (int i = Math.min(end, s.length()) - 1; i >= start; i--) { | ||
if (s.charAt(i) == c) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...ommons/che-core-commons-lang/src/test/java/org/eclipse/che/commons/lang/PathUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.commons.lang; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertNotNull; | ||
|
||
/** | ||
* Tests for {@link PathUtil} | ||
*/ | ||
public class PathUtilTest { | ||
|
||
@Test | ||
public void testCanonicalPath() throws Exception { | ||
String path = PathUtil.toCanonicalPath("/foo/../bar", false); | ||
assertNotNull(path); | ||
assertFalse(path.isEmpty()); | ||
assertEquals(path, "/bar"); | ||
} | ||
|
||
@Test | ||
public void testRemoveLastSlash() throws Exception { | ||
String path = PathUtil.toCanonicalPath("/foo/bar/", true); | ||
assertNotNull(path); | ||
assertFalse(path.isEmpty()); | ||
assertEquals(path, "/foo/bar"); | ||
} | ||
|
||
@Test | ||
public void testEliminationDot() throws Exception { | ||
String path = PathUtil.toCanonicalPath("./bar", false); | ||
assertNotNull(path); | ||
assertFalse(path.isEmpty()); | ||
assertEquals(path, "bar"); | ||
} | ||
|
||
@Test | ||
public void testCanonicalPathWithFile() throws Exception { | ||
String path = PathUtil.toCanonicalPath("/foo/../bar/pom.xml", false); | ||
assertNotNull(path); | ||
assertFalse(path.isEmpty()); | ||
assertEquals(path, "/bar/pom.xml"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.