Skip to content
Permalink
Browse files

Resolve #73

  • Loading branch information
mordechaim committed Nov 28, 2019
1 parent 77da68d commit 7279925dcf03e8c12f2d8ee882c6f5760d4cce6b
Showing with 46 additions and 24 deletions.
  1. +10 −0 src/main/java/org/update4j/Configuration.java
  2. +36 −24 src/main/java/org/update4j/util/PropertyManager.java
@@ -716,6 +716,11 @@ public String implyPlaceholders(String str) {
* </pre>
*
* <p>
* Additionally, if {@code isPath} is {@code true}, {@code user.home} and
* {@code user.dir} will only be matched to the beginning of the string or just after
* the {@code file:} URI scheme in the beginning of the string.
*
* <p>
* If the given string is {@code null}, the same value will be returned.
*
* @param str
@@ -771,6 +776,11 @@ public String implyPlaceholders(String str, PlaceholderMatchType matchType) {
* </pre>
*
* <p>
* Additionally, if {@code isPath} is {@code true}, {@code user.home} and
* {@code user.dir} will only be matched to the beginning of the string or just after
* the {@code file:} URI scheme in the beginning of the string.
*
* <p>
* You can specify how matches should be found by passing the
* {@link PlaceholderMatchType}.
* <ul>
@@ -222,11 +222,7 @@ public String implyPlaceholders(String str, PlaceholderMatchType matchType, bool
.stream()
.filter(e -> !e.getValue().isEmpty())
.sorted((e1, e2) -> e2.getValue().length() - e1.getValue().length())
.peek(e -> {
if (isPath) {
e.setValue(e.getValue().replace("\\", "/"));
}
})
.peek(e -> normalizePath(e, isPath))
.collect(Collectors.toList());

for (Map.Entry<String, String> e : resolved) {
@@ -240,25 +236,35 @@ public String implyPlaceholders(String str, PlaceholderMatchType matchType, bool
return str;
}

/*
* https://stackoverflow.com/a/34464459/1751640
*
* This regex will not replace characters inside an existing placeholder.
*/
if (matchType == PlaceholderMatchType.EVERY_OCCURRENCE) {
if (matchType == PlaceholderMatchType.EVERY_OCCURRENCE || matchType == PlaceholderMatchType.WHOLE_WORD) {
for (Map.Entry<String, String> e : resolved) {
String pattern = "(?<!\\$\\{[^{}]{0,500})" + Pattern.quote(e.getValue());

str = str.replaceAll(pattern, Matcher.quoteReplacement(wrap(e.getKey())));
}

return str;
}

if (matchType == PlaceholderMatchType.WHOLE_WORD) {
for (Map.Entry<String, String> e : resolved) {
String pattern = "(?<!\\$\\{[^{}]{0,500})\\b" + Pattern.quote(e.getValue()) + "\\b";
str = str.replaceAll(pattern, Matcher.quoteReplacement(wrap(e.getKey())));
String key = e.getKey();
String wrappedKey = Matcher.quoteReplacement(wrap(key));
String quote = Pattern.quote(e.getValue());

if (matchType == PlaceholderMatchType.WHOLE_WORD)
quote = "\\b" + quote + "\\b";

/*
* Those keys must only match the beginning of a file path or file:// uri
* see GitHub Issue #73
*/
if (isPath && (key.equals("user.home") || key.equals("user.dir"))) {
// if hardcoded into string
if (str.contains(wrap(key)))
continue;

str = str.replaceFirst("^(file:/*)?" + quote, "$1" + wrappedKey);
} else {

/*
* https://stackoverflow.com/a/34464459
* This regex will not replace characters inside an existing placeholder.
*/
String pattern = "(\\$\\{[^{}]*)|" + quote;
Matcher m = Pattern.compile(pattern).matcher(str);
str = m.replaceAll(i -> i.group(1) != null ? Matcher.quoteReplacement(i.group(1)) : wrappedKey);
}
}

return str;
@@ -267,7 +273,13 @@ public String implyPlaceholders(String str, PlaceholderMatchType matchType, bool
// In case we rename this enum, lets stay safe the IDE will automatically fix this
throw new UnsupportedOperationException("Unknown " + PlaceholderMatchType.class.getSimpleName());
}


private static void normalizePath(Map.Entry<String, String> e, boolean isPath) {
if (isPath) {
e.setValue(e.getValue().replace("\\", "/"));
}
}

private static Map<String, String> extractPropertiesForCurrentMachine(Collection<? extends Property> properties,
Collection<String> systemProperties) {

0 comments on commit 7279925

Please sign in to comment.
You can’t perform that action at this time.