Skip to content

Commit

Permalink
Add formatter removing enclosing brackets
Browse files Browse the repository at this point in the history
Readd functionality "Remove double braces around BibTeX fields when
loading" (which was removed in c2928d2)
as a formatter.
  • Loading branch information
tobiasdiez committed Feb 21, 2016
1 parent effb690 commit 3803323
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public static Collection<Object[]> instancesToTest() {
new Object[]{new TrimFormatter()},
new Object[]{new HTMLConverter()},
new Object[]{new SuperscriptFormatter()},
new Object[]{new UnitFormatter()}
new Object[]{new UnitFormatter()},
new Object[]{new RemoveBracesFormatter()}
);
}
}
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}}"));
}
}

0 comments on commit 3803323

Please sign in to comment.