From bf12dff243f53f989d20f1926bbd3b9c3ba14092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Lenhard?= Date: Tue, 28 Feb 2017 21:43:10 +0100 Subject: [PATCH] Improve braces checking (#2593) * Do not count escaped braces for brace calculation * Add changelog entry --- CHANGELOG.md | 3 ++- .../logic/bibtex/LatexFieldFormatter.java | 18 +++++++++++++----- .../logic/bibtex/LatexFieldFormatterTests.java | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d073a77117..ba31c920e6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,7 +47,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - Sciencedirect/Elsevier fetcher is now able to scrape new HTML structure [#2576](https://github.com/JabRef/jabref/issues/2576) - Fixed the synchronization logic of keywords and special fields and vice versa [#2580](https://github.com/JabRef/jabref/issues/2580) - We fixed an issue where the "find unlinked files" functionality threw an error when only one PDF was imported but not assigned to an entry [#2577](https://github.com/JabRef/jabref/issues/2577) - + - We fixed issue where escaped braces were incorrectly counted when calculating brace balance in a field [#2561](https://github.com/JabRef/jabref/issues/2561) + ### Removed diff --git a/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java b/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java index 74c896d0ebd..74ca79d9014 100644 --- a/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java +++ b/src/main/java/org/jabref/logic/bibtex/LatexFieldFormatter.java @@ -259,11 +259,19 @@ private static void checkBraces(String text) throws IllegalArgumentException { int current = -1; // First we collect all occurrences: - while ((current = text.indexOf('{', current + 1)) != -1) { - left.add(current); - } - while ((current = text.indexOf('}', current + 1)) != -1) { - right.add(current); + for (int i = 0; i < text.length(); i++) { + char item = text.charAt(i); + + boolean charBeforeIsEscape = false; + if(i > 0 && text.charAt(i - 1) == '\\') { + charBeforeIsEscape = true; + } + + if(!charBeforeIsEscape && item == '{') { + left.add(current); + } else if (!charBeforeIsEscape && item == '{') { + right.add(current); + } } // Then we throw an exception if the error criteria are met. diff --git a/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java b/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java index a8422abfd34..541604a68ca 100644 --- a/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java +++ b/src/test/java/org/jabref/logic/bibtex/LatexFieldFormatterTests.java @@ -81,4 +81,18 @@ public void removeWhitespaceFromNonMultiLineFields() throws Exception { assertEquals(expected, title); assertEquals(expected, any); } + + @Test(expected = IllegalArgumentException.class) + public void reportUnbalancedBracing() { + String unbalanced = "{"; + + formatter.format(unbalanced, "anyfield"); + } + + @Test(expected = IllegalArgumentException.class) + public void reportUnbalancedBracingWithEscapedBraces() { + String unbalanced = "{\\}"; + + formatter.format(unbalanced, "anyfield"); + } }