-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add a few cleanup formatters #853
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
25 changes: 25 additions & 0 deletions
25
src/main/java/net/sf/jabref/logic/formatter/bibtexfields/TrimFormatter.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,25 @@ | ||
package net.sf.jabref.logic.formatter.bibtexfields; | ||
|
||
import net.sf.jabref.logic.formatter.Formatter; | ||
import net.sf.jabref.model.entry.MonthUtil; | ||
|
||
import java.util.Objects; | ||
|
||
public class TrimFormatter implements Formatter { | ||
|
||
@Override | ||
public String getName() { | ||
return "Trim whitespace"; | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "TrimFormatter"; | ||
} | ||
|
||
@Override | ||
public String format(String value) { | ||
Objects.requireNonNull(value); | ||
return value.trim(); | ||
} | ||
} |
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
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
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
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
91 changes: 91 additions & 0 deletions
91
src/test/java/net/sf/jabref/logic/formatter/FormatterTest.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,91 @@ | ||
package net.sf.jabref.logic.formatter; | ||
|
||
import net.sf.jabref.Globals; | ||
import net.sf.jabref.JabRefPreferences; | ||
import net.sf.jabref.importer.HTMLConverter; | ||
import net.sf.jabref.logic.formatter.bibtexfields.*; | ||
import net.sf.jabref.logic.formatter.casechanger.*; | ||
import net.sf.jabref.logic.formatter.minifier.AuthorsMinifier; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(Parameterized.class) | ||
public class FormatterTest { | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
Globals.prefs = JabRefPreferences.getInstance(); | ||
} | ||
|
||
@Test | ||
public void getNameReturnsNotNull() { | ||
assertNotNull(formatter.getName()); | ||
} | ||
|
||
@Test | ||
public void getNameReturnsNotEmpty() { | ||
assertNotEquals("", formatter.getName()); | ||
} | ||
|
||
@Test | ||
public void getKeyReturnsNotNull() { | ||
assertNotNull(formatter.getKey()); | ||
} | ||
|
||
@Test | ||
public void getKeyReturnsNotEmpty() { | ||
assertNotEquals("", formatter.getKey()); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void formatOfNullThrowsException() { | ||
formatter.format(null); | ||
} | ||
|
||
@Test | ||
public void formatOfEmptyStringReturnsEmpty() { | ||
assertEquals("", formatter.format("")); | ||
} | ||
|
||
@Test | ||
public void formatNotReturnsNull() { | ||
assertNotNull(formatter.format("string")); | ||
} | ||
|
||
public Formatter formatter; | ||
|
||
public FormatterTest(Formatter formatter) { | ||
this.formatter = formatter; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{index}: {0}") | ||
public static Collection<Object[]> instancesToTest() { | ||
return Arrays.asList( | ||
new Object[]{new AuthorsFormatter()}, | ||
new Object[]{new UpperEachFirstCaseChanger()}, | ||
new Object[]{new UpperCaseChanger()}, | ||
new Object[]{new MonthFormatter()}, | ||
new Object[]{new LatexFormatter()}, | ||
new Object[]{new IdentityFormatter()}, | ||
new Object[]{new UpperFirstCaseChanger()}, | ||
new Object[]{new AuthorsMinifier()}, | ||
new Object[]{new DateFormatter()}, | ||
new Object[]{new TitleCaseChanger()}, | ||
new Object[]{new CaseKeeper()}, | ||
new Object[]{new PageNumbersFormatter()}, | ||
new Object[]{new LowerCaseChanger()}, | ||
new Object[]{new TrimFormatter()}, | ||
new Object[]{new HTMLConverter()}, | ||
new Object[]{new SuperscriptFormatter()}, | ||
new Object[]{new UnitFormatter()}, | ||
new Object[]{new RemoveBracesFormatter()} | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep and add (expected...)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, now I saw the next file... ;-)