-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add formatter removing enclosing brackets
Readd functionality "Remove double braces around BibTeX fields when loading" (which was removed in c2928d2) as a formatter.
- Loading branch information
1 parent
effb690
commit 3803323
Showing
3 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/net/sf/jabref/logic/formatter/bibtexfields/RemoveBracesFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.sf.jabref.logic.formatter.bibtexfields; | ||
|
||
import net.sf.jabref.logic.formatter.Formatter; | ||
|
||
import java.util.Objects; | ||
|
||
public class RemoveBracesFormatter implements Formatter { | ||
|
||
@Override | ||
public String getName() { | ||
return "Remove enclosing double braces"; | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "RemoveBracesFormatter"; | ||
} | ||
|
||
@Override | ||
public String format(String value) { | ||
Objects.requireNonNull(value); | ||
|
||
String formatted = value; | ||
while (formatted.length() >= 2 && formatted.charAt(0) == '{' && formatted.charAt(formatted.length() - 1) | ||
== '}') { | ||
String trimmed = formatted.substring(1, formatted.length() - 1); | ||
|
||
// It could be that the removed braces were not matching | ||
// For example, "{A} test {B}" results in "A} test {B" | ||
// In this case, trimmed has a closing } without an opening { before that | ||
if(hasNegativeBraceCount(trimmed)) { | ||
return formatted; | ||
} else { | ||
formatted = trimmed; | ||
} | ||
} | ||
return formatted; | ||
} | ||
|
||
/** | ||
* Check if a string at any point has had more ending } braces than opening { ones. | ||
* Will e.g. return true for the string "DNA} blahblal {EPA" | ||
* | ||
* @param value The string to check. | ||
* @return true if at any index the brace count is negative. | ||
*/ | ||
private boolean hasNegativeBraceCount(String value) { | ||
int braceCount = 0; | ||
for (int index = 0; index < value.length(); index++) { | ||
char charAtIndex = value.charAt(index); | ||
if (charAtIndex == '{') { | ||
braceCount++; | ||
} else if (charAtIndex == '}') { | ||
braceCount--; | ||
} | ||
if (braceCount < 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/net/sf/jabref/logic/formatter/bibtexfields/RemoveBracesFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package net.sf.jabref.logic.formatter.bibtexfields; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
|
||
public class RemoveBracesFormatterTest { | ||
|
||
@Test | ||
public void formatRemovesSingleEnclosingBraces() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
assertEquals("test", formatter.format("{test}")); | ||
} | ||
|
||
@Test | ||
public void formatKeepsUnmatchedBracesAtBeginning() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
assertEquals("{test", formatter.format("{test")); | ||
} | ||
|
||
@Test | ||
public void formatKeepsUnmatchedBracesAtEnd() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
assertEquals("test}", formatter.format("test}")); | ||
} | ||
|
||
@Test | ||
public void formatKeepsShortString() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
assertEquals("t", formatter.format("t")); | ||
} | ||
|
||
@Test | ||
public void formatKeepsEmptyString() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
assertEquals("", formatter.format("")); | ||
} | ||
|
||
@Test | ||
public void formatRemovesDoubleEnclosingBraces() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
assertEquals("test", formatter.format("{{test}}")); | ||
} | ||
|
||
@Test | ||
public void formatRemovesTripleEnclosingBraces() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
assertEquals("test", formatter.format("{{{test}}}")); | ||
} | ||
|
||
@Test | ||
public void formatKeepsNonMatchingBraces() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
assertEquals("{A} and {B}", formatter.format("{A} and {B}")); | ||
} | ||
|
||
@Test | ||
public void formatRemovesOnlyMatchingBraces() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
assertEquals("{A} and {B}", formatter.format("{{A} and {B}}")); | ||
} | ||
|
||
@Test | ||
public void formatDoesNotRemoveBracesInBrokenString() { | ||
RemoveBracesFormatter formatter = new RemoveBracesFormatter(); | ||
// We opt here for a conservative approach although one could argue that "A} and {B}" is also a valid return | ||
assertEquals("{A} and {B}}", formatter.format("{A} and {B}}")); | ||
} | ||
} |