-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from JabRef/pagenumbers
Pagenumbers CleanupAction
- Loading branch information
Showing
7 changed files
with
221 additions
and
6 deletions.
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
30 changes: 30 additions & 0 deletions
30
src/main/java/net/sf/jabref/logic/cleanup/PageNumbersCleanup.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,30 @@ | ||
package net.sf.jabref.logic.cleanup; | ||
|
||
import net.sf.jabref.logic.formatter.FieldFormatters; | ||
import net.sf.jabref.logic.formatter.PageNumbersFormatter; | ||
import net.sf.jabref.model.entry.BibtexEntry; | ||
|
||
/** | ||
* This class includes sensible defaults for consistent formatting of BibTex page numbers. | ||
*/ | ||
public class PageNumbersCleanup { | ||
private BibtexEntry entry; | ||
|
||
public PageNumbersCleanup(BibtexEntry entry) { | ||
this.entry = entry; | ||
} | ||
|
||
/** | ||
* Format page numbers, separated either by commas or double-hyphens. | ||
* Converts the range number format of the <code>pages</code> field to page_number--page_number. | ||
* | ||
* @see{PageNumbersFormatter} | ||
*/ | ||
public void cleanup() { | ||
final String field = "pages"; | ||
|
||
String value = entry.getField(field); | ||
String newValue = FieldFormatters.PAGE_NUMBERS.format(value); | ||
entry.setField(field, newValue); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/net/sf/jabref/logic/formatter/FieldFormatters.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,10 @@ | ||
package net.sf.jabref.logic.formatter; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class FieldFormatters { | ||
public static final PageNumbersFormatter PAGE_NUMBERS = new PageNumbersFormatter(); | ||
|
||
public static final List<Formatter> ALL = Arrays.asList(PAGE_NUMBERS); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/net/sf/jabref/logic/formatter/Formatter.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,21 @@ | ||
package net.sf.jabref.logic.formatter; | ||
|
||
/** | ||
* Formatter Interface | ||
*/ | ||
public interface Formatter { | ||
/** | ||
* Returns a human readable name of the formatter usable for e.g. in the GUI | ||
* | ||
* @return the name of the formatter | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Formats a field value by with a particular formatter transformation. | ||
* | ||
* @param value the input String | ||
* @return the formatted output String | ||
*/ | ||
String format(String value); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/net/sf/jabref/logic/formatter/PageNumbersFormatter.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,51 @@ | ||
package net.sf.jabref.logic.formatter; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* This class includes sensible defaults for consistent formatting of BibTex page numbers. | ||
*/ | ||
public class PageNumbersFormatter implements Formatter { | ||
@Override | ||
public String getName() { | ||
return "Page numbers"; | ||
} | ||
|
||
/** | ||
* Format page numbers, separated either by commas or double-hyphens. | ||
* Converts the range number format of the <code>pages</code> field to page_number--page_number. | ||
* Removes all literals except [0-9,-]. | ||
* Keeps the existing String if the resulting field does not match the expected Regex. | ||
* | ||
* <example> | ||
* 1-2 -> 1--2 | ||
* 1,2,3 -> 1,2,3 | ||
* {1}-{2} -> 1--2 | ||
* Invalid -> Invalid | ||
* </example> | ||
*/ | ||
public String format(String value) { | ||
final String rejectLiterals = "[^0-9,-]"; | ||
final Pattern pagesPattern = Pattern.compile("\\A(\\d+)-{1,2}(\\d+)\\Z"); | ||
final String replace = "$1--$2"; | ||
|
||
// nothing to do | ||
if (value == null || value.isEmpty()) { | ||
return value; | ||
} | ||
|
||
// remove unwanted literals incl. whitespace | ||
String cleanValue = value.replaceAll(rejectLiterals, ""); | ||
// try to find pages pattern | ||
Matcher matcher = pagesPattern.matcher(cleanValue); | ||
// replace | ||
String newValue = matcher.replaceFirst(replace); | ||
// replacement? | ||
if(!newValue.equals(cleanValue)) { | ||
// write field | ||
return newValue; | ||
} | ||
return value; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/net/sf/jabref/logic/cleanup/PageNumbersCleanupTest.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,39 @@ | ||
package net.sf.jabref.logic.cleanup; | ||
|
||
import junit.framework.Assert; | ||
import net.sf.jabref.model.entry.BibtexEntry; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class PageNumbersCleanupTest { | ||
private BibtexEntry entry; | ||
|
||
@Before | ||
public void setUp() { | ||
entry = new BibtexEntry(); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
entry = null; | ||
} | ||
|
||
@Test | ||
public void formatPageNumbers() { | ||
entry.setField("pages", "1-2"); | ||
new PageNumbersCleanup(entry).cleanup(); | ||
|
||
Assert.assertEquals("1--2", entry.getField("pages")); | ||
} | ||
|
||
@Test | ||
public void onlyFormatPageNumbersField() { | ||
entry.setField("otherfield", "1-2"); | ||
new PageNumbersCleanup(entry).cleanup(); | ||
|
||
Assert.assertEquals("1-2", entry.getField("otherfield")); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/net/sf/jabref/logic/formatter/PageNumbersFormatterTest.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; | ||
|
||
import junit.framework.Assert; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class PageNumbersFormatterTest { | ||
private PageNumbersFormatter formatter; | ||
|
||
@Before | ||
public void setUp() { | ||
formatter = new PageNumbersFormatter(); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
formatter = null; | ||
} | ||
|
||
@Test | ||
public void formatPageNumbers() { | ||
expectCorrect("1-2", "1--2"); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersCommaSeparated() { | ||
expectCorrect("1,2,3", "1,2,3"); | ||
} | ||
|
||
@Test | ||
public void ignoreWhitespaceInPageNumbers() { | ||
expectCorrect(" 1 - 2 ", "1--2"); | ||
} | ||
|
||
@Test | ||
public void keepCorrectlyFormattedPageNumbers() { | ||
expectCorrect("1--2", "1--2"); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersEmptyFields() { | ||
expectCorrect("", ""); | ||
expectCorrect(null, null); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersRemoveUnexpectedLiterals() { | ||
expectCorrect("{1}-{2}", "1--2"); | ||
} | ||
|
||
@Test | ||
public void formatPageNumbersRegexNotMatching() { | ||
expectCorrect("12", "12"); | ||
} | ||
|
||
private void expectCorrect(String input, String expected) { | ||
Assert.assertEquals(expected, formatter.format(input)); | ||
} | ||
} |