From 65d05ed4d2f31092d1034aa725992f004ca3c658 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Tue, 12 May 2020 11:05:53 +0200 Subject: [PATCH 1/4] Fixed brackets in regular expressions --- .../bibtexkeypattern/BracketedPattern.java | 78 +++++++++++++------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java b/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java index c90204eee67..d1c07072d26 100644 --- a/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java +++ b/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java @@ -106,40 +106,68 @@ 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 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)) { + // Fetch the next token after the '[': token = st.nextToken(); + Boolean foundClosingBracket = false; + // make sure to read until the next ']' + while (st.hasMoreTokens()) { + String subtoken = st.nextToken(); + // 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 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); } } } From 0d4c7ce7cd525bea20a0cfd148fad87d9a7586e1 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Tue, 12 May 2020 11:06:22 +0200 Subject: [PATCH 2/4] Added a test --- .../java/org/jabref/logic/util/BracketedPatternTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/java/org/jabref/logic/util/BracketedPatternTest.java b/src/test/java/org/jabref/logic/util/BracketedPatternTest.java index 41bfbad9622..4d8f4d5ef0f 100644 --- a/src/test/java/org/jabref/logic/util/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/util/BracketedPatternTest.java @@ -241,4 +241,10 @@ 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)); + } } From 5861685e11ba8e1539c35e6c40be9ed6ecf80fc7 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Tue, 12 May 2020 11:08:19 +0200 Subject: [PATCH 3/4] Changelog update --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cd9432d455..f84c23f434c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 230fef2a16cd09ec3bd50647b7499ae943608e80 Mon Sep 17 00:00:00 2001 From: Benedikt Tutzer Date: Tue, 12 May 2020 13:43:04 +0200 Subject: [PATCH 4/4] Consider empty brackets --- .../jabref/logic/bibtexkeypattern/BracketedPattern.java | 8 ++++++-- .../java/org/jabref/logic/util/BracketedPatternTest.java | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java b/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java index d1c07072d26..316de3b4dc3 100644 --- a/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java +++ b/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java @@ -127,11 +127,15 @@ public static String expandBrackets(String pattern, Character keywordDelimiter, // FIXME: else -> raise exception or log? (S.G.) } else { if ("[".equals(token)) { + Boolean foundClosingBracket = false; // Fetch the next token after the '[': token = st.nextToken(); - Boolean foundClosingBracket = false; + if ("]".equals(token)) { + LOGGER.warn("Found empty brackets \"[]\" in '" + pattern + "'"); + foundClosingBracket = true; + } // make sure to read until the next ']' - while (st.hasMoreTokens()) { + while (st.hasMoreTokens() && !foundClosingBracket) { String subtoken = st.nextToken(); // I the beginning of a quote is found, include the content in the original token if ("\"".equals(subtoken)) { diff --git a/src/test/java/org/jabref/logic/util/BracketedPatternTest.java b/src/test/java/org/jabref/logic/util/BracketedPatternTest.java index 4d8f4d5ef0f..e142c4fb270 100644 --- a/src/test/java/org/jabref/logic/util/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/util/BracketedPatternTest.java @@ -247,4 +247,10 @@ 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)); + } }