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

Pagenumbers CleanupAction #273

Merged
merged 7 commits into from
Oct 29, 2015
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
14 changes: 8 additions & 6 deletions src/main/java/net/sf/jabref/gui/actions/CleanUpAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

import com.jgoodies.forms.builder.FormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import net.sf.jabref.logic.cleanup.PageNumbersCleanup;
import net.sf.jabref.logic.formatter.FieldFormatters;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.entry.BibtexEntry;
import net.sf.jabref.logic.util.DOI;
Expand Down Expand Up @@ -445,12 +447,12 @@ private static void doCleanUpMonth(BibtexEntry entry, NamedCompound ce) {

private static void doCleanUpPageNumbers(BibtexEntry entry, NamedCompound ce) {
String oldValue = entry.getField("pages");
if (oldValue == null) {
return;
}
String newValue = oldValue.replaceAll(" *(\\d+) *- *(\\d+) *", "$1--$2");
if (!oldValue.equals(newValue)) {
entry.setField("pages", newValue);
// run formatter
new PageNumbersCleanup(entry).cleanup();
// new value
String newValue = entry.getField("pages");
// undo action
if(!oldValue.equals(newValue)) {
ce.addEdit(new UndoableFieldChange(entry, "pages", oldValue, newValue));
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/net/sf/jabref/logic/cleanup/PageNumbersCleanup.java
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 src/main/java/net/sf/jabref/logic/formatter/FieldFormatters.java
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 src/main/java/net/sf/jabref/logic/formatter/Formatter.java
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);
}
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

just return the new value

Copy link
Member Author

Choose a reason for hiding this comment

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

We want to return the initial value and not the cleaned value if page numbers regex does not match?!

Copy link
Contributor

Choose a reason for hiding this comment

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

ah ok. then leave this untouched.

// write field
return newValue;
}
return value;
}
}
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"));
}
}
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));
}
}