Fix node shorthand parsing - closes #143

This commit is contained in:
Luck 2017-01-22 16:29:52 +00:00
parent c03585aeca
commit 0be7300677
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 43 additions and 11 deletions

View File

@ -61,28 +61,53 @@ public class ImmutableNode implements Node {
private static final Pattern META_PATTERN = Pattern.compile("meta\\..*\\..*"); private static final Pattern META_PATTERN = Pattern.compile("meta\\..*\\..*");
private static boolean shouldApply(String str, boolean applyRegex, String thisStr) { private static boolean shouldApply(String str, boolean applyRegex, String thisStr) {
if (str.equalsIgnoreCase(thisStr)) {
return true;
}
Set<String> expandedStr = ShorthandParser.parseShorthand(str, false);
Set<String> expandedThisStr = ShorthandParser.parseShorthand(thisStr, false);
if (str.toLowerCase().startsWith("r=") && applyRegex) { if (str.toLowerCase().startsWith("r=") && applyRegex) {
Pattern p = Patterns.compile(str.substring(2)); Pattern p = Patterns.compile(str.substring(2));
if (p == null) { if (p == null) {
return false; return false;
} }
return p.matcher(thisStr).matches();
}
if (str.startsWith("(") && str.endsWith(")") && str.contains("|")) { for (String s : expandedThisStr) {
final String bits = str.substring(1, str.length() - 1); if (p.matcher(s).matches()) {
Iterable<String> parts = Splitter.on('|').split(bits);
for (String s : parts) {
if (s.equalsIgnoreCase(thisStr)) {
return true; return true;
} }
} }
return false; return false;
} }
return thisStr.equalsIgnoreCase(str); if (thisStr.toLowerCase().startsWith("r=") && applyRegex) {
Pattern p = Patterns.compile(thisStr.substring(2));
if (p == null) {
return false;
}
for (String s : expandedStr) {
if (p.matcher(s).matches()) {
return true;
}
}
return false;
}
if (expandedStr.size() <= 1 && expandedThisStr.size() <= 1) {
return false;
}
for (String t : expandedThisStr) {
for (String s : expandedStr) {
if (t.equalsIgnoreCase(s)) {
return true;
}
}
}
return false;
} }
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")

View File

@ -44,6 +44,10 @@ public class ShorthandParser {
.build(); .build();
public static Set<String> parseShorthand(String s) { public static Set<String> parseShorthand(String s) {
return parseShorthand(s, true);
}
public static Set<String> parseShorthand(String s, boolean removeSelf) {
Set<String> results = new HashSet<>(); Set<String> results = new HashSet<>();
results.add(s); results.add(s);
@ -68,7 +72,10 @@ public class ShorthandParser {
break; break;
} }
results.remove(s); if (removeSelf) {
results.remove(s);
}
return results; return results;
} }