Skip to content
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 3 commits into from
Feb 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/main/java/net/sf/jabref/importer/HTMLConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package net.sf.jabref.importer;

import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -45,9 +46,7 @@ public HTMLConverter() {
}

public String formatUnicode(String text) {
if (text == null) {
return null;
}
Objects.requireNonNull(text);

if (text.isEmpty()) {
return text;
Expand Down Expand Up @@ -94,9 +93,7 @@ public String formatUnicode(String text) {

@Override
public String format(String text) {
if (text == null) {
return null;
}
Objects.requireNonNull(text);

if (text.isEmpty()) {
return text;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/sf/jabref/logic/formatter/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ public interface Formatter {
/**
* Returns a human readable name of the formatter usable for e.g. in the GUI
*
* @return the name of the formatter
* @return the name of the formatter, always not null
*/
String getName();


/**
* Returns a unique key for the formatter that can be used for its identification
* @return
* @return the key of the formatter, always not null
*/
String getKey();

/**
* Formats a field value by with a particular formatter transformation.
*
* Calling this method with a null argument results in a NullPointerException.
*
* @param value the input String
* @return the formatted output String
* @return the formatted output String, always not null
*/
String format(String value);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.sf.jabref.logic.formatter;

import java.util.Objects;

/**
* It may seem useless, but is needed as a fallback option
*/
Expand All @@ -17,6 +19,7 @@ public String getKey() {

@Override
public String format(String value) {
Objects.requireNonNull(value);
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.sf.jabref.logic.formatter.Formatter;
import net.sf.jabref.model.entry.MonthUtil;

import java.util.Objects;

public class MonthFormatter implements Formatter {

@Override
Expand All @@ -17,6 +19,7 @@ public String getKey() {

@Override
public String format(String value) {
Objects.requireNonNull(value);
MonthUtil.Month month = MonthUtil.getMonth(value);
if (month.isValid()) {
return month.bibtexFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.sf.jabref.logic.formatter.Formatter;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -48,9 +49,10 @@ public String getKey() {
*/
@Override
public String format(String value) {
Objects.requireNonNull(value);

// nothing to do
if ((value == null) || value.isEmpty()) {
if (value.isEmpty()) {
// nothing to do
return value;
}

Expand Down
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 @@ -16,6 +16,8 @@
package net.sf.jabref.logic.formatter.bibtexfields;

import net.sf.jabref.logic.formatter.Formatter;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -50,9 +52,10 @@ public String getKey() {
*/
@Override
public String format(String value) {
Objects.requireNonNull(value);

// nothing to do
if ((value == null) || value.isEmpty()) {
if (value.isEmpty()) {
// nothing to do
return value;
}

Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package net.sf.jabref.logic.formatter.bibtexfields;

import java.util.Arrays;
import java.util.Objects;

import net.sf.jabref.logic.formatter.Formatter;
import net.sf.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -129,7 +130,8 @@ private static String format(String text, String[] listOfWords) {

@Override
public String format(String text) {
if ((text == null) || text.isEmpty()) {
Objects.requireNonNull(text);
if (text.isEmpty()) {
return text;
}
return format(text, UnitFormatter.UNIT_COMBINATIONS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package net.sf.jabref.logic.formatter.casechanger;

import java.util.Arrays;
import java.util.Objects;

import net.sf.jabref.logic.formatter.Formatter;
import net.sf.jabref.logic.l10n.Localization;
Expand All @@ -35,7 +36,9 @@ private String format(String text, String[] listOfWords) {

@Override
public String format(String text) {
if ((text == null) || text.isEmpty()) {
Objects.requireNonNull(text);

if (text.isEmpty()) {
return text;
}
final CaseKeeperList list = new CaseKeeperList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.sf.jabref.logic.formatter.Formatter;

import java.util.Objects;

/**
* Replaces three or more authors with and others
*/
Expand All @@ -24,8 +26,10 @@ public String getKey() {
*/
@Override
public String format(String value) {
// nothing to do
if ((value == null) || value.isEmpty()) {
Objects.requireNonNull(value);

if (value.isEmpty()) {
// nothing to do
return value;
}

Expand Down
9 changes: 2 additions & 7 deletions src/test/java/net/sf/jabref/importer/HTMLConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public void testBasic() {
assertEquals("aaa", conv.formatUnicode("aaa"));
}

@Test
public void testHTMLNull() {
assertEquals(null, conv.format(null));
}

@Test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep and add (expected...)?

Copy link
Contributor

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... ;-)

public void testHTMLEmpty() {
assertEquals("", conv.format(""));
Expand Down Expand Up @@ -72,9 +67,9 @@ public void testUnicodeSingle() {
assertEquals("a", conv.formatUnicode("a"));
}

@Test
@Test(expected = NullPointerException.class)
public void testUnicodeNull() {
assertEquals(null, conv.formatUnicode(null));
conv.formatUnicode(null);
}

@Test
Expand Down
91 changes: 91 additions & 0 deletions src/test/java/net/sf/jabref/logic/formatter/FormatterTest.java
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()}
);
}
}
Loading