Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bracket collisions #6469

Merged
merged 4 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where sort on numeric cases was broken. [#6349](https://github.com/JabRef/jabref/issues/6349)
- We fixed an issue where an "Not on FX thread" exception occured when saving on linux [#6453](https://github.com/JabRef/jabref/issues/6453)
- We fixed an issue where the library sort order was lost. [#6091](https://github.com/JabRef/jabref/issues/6091)
- We fixed an issue where brackets in regular expressions were not working. [6469](https://github.com/JabRef/jabref/pull/6469)

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,40 +106,72 @@ public static String expandBrackets(String pattern, Character keywordDelimiter,
Objects.requireNonNull(pattern);
Objects.requireNonNull(entry);
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(pattern, "\\[]", true);
StringTokenizer st = new StringTokenizer(pattern, "\\[]\"", true);

while (st.hasMoreTokens()) {
String token = st.nextToken();
if ("\\".equals(token)) {
if (st.hasMoreTokens()) {
sb.append(st.nextToken());
}
// FIXME: else -> raise exception or log? (S.G.)
} else {
if ("[".equals(token)) {
// Fetch the next token after the '[':
if ("\"".equals(token)) {
sb.append(token);
while (st.hasMoreTokens()) {
token = st.nextToken();
List<String> fieldParts = parseFieldMarker(token);
// check whether there is a modifier on the end such as
// ":lower":
if (fieldParts.size() <= 1) {
sb.append(getFieldValue(entry, token, keywordDelimiter, database));
} else {
// apply modifiers:
String fieldValue = getFieldValue(entry, fieldParts.get(0), keywordDelimiter, database);
sb.append(applyModifiers(fieldValue, fieldParts, 1));
sb.append(token);
if ("\"".equals(token)) {
break;
}
// Fetch and discard the closing ']'
}
} else {
if ("\\".equals(token)) {
if (st.hasMoreTokens()) {
sb.append(st.nextToken());
}
// FIXME: else -> raise exception or log? (S.G.)
} else {
if ("[".equals(token)) {
Boolean foundClosingBracket = false;
// Fetch the next token after the '[':
token = st.nextToken();
if ("]".equals(token)) {
LOGGER.warn("Found empty brackets \"[]\" in '" + pattern + "'");
foundClosingBracket = true;
}
// make sure to read until the next ']'
while (st.hasMoreTokens() && !foundClosingBracket) {
String subtoken = st.nextToken();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: you fetch the next token above in line 131, and then here the subtoken. So this moves the stream forward twice, right? What happens if the user adds an empty brace, i.e []. Is the closing bracket still found? Can you please add a test for this case as well. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! But what should "[]" expand to? Inside a quote, the parsing should work fine. but what does "[]" mean in terms of JabRef fields? Should it just do nothing and print an appropriate warning?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I would say it should do nothing and log a warning. I don't really see a use case for the empty bracket, it should just not break the parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 230fef2

// I the beginning of a quote is found, include the content in the original token
if ("\"".equals(subtoken)) {
token = token + subtoken;
while (st.hasMoreTokens()) {
subtoken = st.nextToken();
token = token + subtoken;
if ("\"".equals(subtoken)) {
break;
}
}
} else {
if ("]".equals(subtoken)) {
foundClosingBracket = true;
break;
} else {
token = token + subtoken;
}
}
}
if (!foundClosingBracket) {
LOGGER.warn("Missing closing bracket ']' in '" + pattern + "'");
}
List<String> fieldParts = parseFieldMarker(token);
// check whether there is a modifier on the end such as
// ":lower":
if (fieldParts.size() <= 1) {
sb.append(getFieldValue(entry, token, keywordDelimiter, database));
} else {
// apply modifiers:
String fieldValue = getFieldValue(entry, fieldParts.get(0), keywordDelimiter, database);
sb.append(applyModifiers(fieldValue, fieldParts, 1));
}
} else {
token = "";
}
if (!"]".equals(token)) {
LOGGER.warn("Missing closing bracket ']' in '" + pattern + "'");
sb.append(token);
}
} else {
sb.append(token);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/jabref/logic/util/BracketedPatternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,16 @@ void regularExpressionReplace() {
assertEquals("2003-JabRef Science",
BracketedPattern.expandBrackets("[year]-[journal:regex(\"Organization\",\"JabRef\")]", ';', dbentry, database));
}

@Test
void regularExpressionWithBrackets() {
assertEquals("2003-JabRef Science",
BracketedPattern.expandBrackets("[year]-[journal:regex(\"[OX]rganization\",\"JabRef\")]", ';', dbentry, database));
}

@Test
void testEmptyBrackets() {
assertEquals("2003-Organization Science",
BracketedPattern.expandBrackets("[year][]-[journal]", ';', dbentry, database));
}
}