Skip to content

Commit

Permalink
Implemented JabRef#1345 - cleanup ISSN
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Jul 16, 2016
1 parent e8cc789 commit 14a0bdc
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- For developers: Moved the bst package into logic. This requires the regeneration of antlr sources, execute: gradlew generateSource
- [#1026](https://github.com/JabRef/jabref/issues/1026) JabRef does no longer delete user comments outside of BibTeX entries and strings
- [#1249](https://github.com/JabRef/jabref/issues/1249) Date layout formatter added
- [#1345](https://github.com/JabRef/jabref/issues/1345) Cleanup ISSN

### Fixed
- Fixed [#1264](https://github.com/JabRef/jabref/issues/1264): S with caron does not render correctly
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public class JabRefPreferences {

public static final String AKS_AUTO_NAMING_PDFS_AGAIN = "AskAutoNamingPDFsAgain";
public static final String CLEANUP_DOI = "CleanUpDOI";
public static final String CLEANUP_ISSN = "CleanUpISSN";
public static final String CLEANUP_MOVE_PDF = "CleanUpMovePDF";
public static final String CLEANUP_MAKE_PATHS_RELATIVE = "CleanUpMakePathsRelative";
public static final String CLEANUP_RENAME_PDF = "CleanUpRenamePDF";
Expand Down Expand Up @@ -1374,6 +1375,7 @@ public void setDefaultEncoding(Charset encoding) {

private static void insertCleanupPreset(Map<String, Object> storage, CleanupPreset preset) {
storage.put(CLEANUP_DOI, preset.isCleanUpDOI());
storage.put(CLEANUP_ISSN, preset.isCleanUpISSN());
storage.put(CLEANUP_MOVE_PDF, preset.isMovePDF());
storage.put(CLEANUP_MAKE_PATHS_RELATIVE, preset.isMakePathsRelative());
storage.put(CLEANUP_RENAME_PDF, preset.isRenamePDF());
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/net/sf/jabref/gui/cleanup/CleanupPresetPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class CleanupPresetPanel {

private final BibDatabaseContext databaseContext;
private JCheckBox cleanUpDOI;
private JCheckBox cleanUpISSN;
private JCheckBox cleanUpMovePDF;
private JCheckBox cleanUpMakePathsRelative;
private JCheckBox cleanUpRenamePDF;
Expand All @@ -43,6 +44,7 @@ public CleanupPresetPanel(BibDatabaseContext databaseContext, CleanupPreset clea
private void init() {
cleanUpDOI = new JCheckBox(
Localization.lang("Move DOIs from note and URL field to DOI field and remove http prefix"));
cleanUpISSN = new JCheckBox(Localization.lang("Add possibly missing dash to ISSN"));
if (databaseContext.getMetaData().getDefaultFileDirectory().isPresent()) {
cleanUpMovePDF = new JCheckBox(Localization.lang("Move linked files to default file directory %0",
databaseContext.getMetaData().getDefaultFileDirectory().get()));
Expand Down Expand Up @@ -70,7 +72,7 @@ private void init() {
updateDisplay(cleanupPreset);

FormLayout layout = new FormLayout("left:15dlu, pref:grow",
"pref, pref, pref, pref, pref, pref, pref,pref, 190dlu, fill:pref:grow,");
"pref, pref, pref, pref, pref, pref, pref,pref, pref,190dlu, fill:pref:grow,");

FormBuilder builder = FormBuilder.create().layout(layout);
builder.add(cleanUpDOI).xyw(1, 1, 2);
Expand All @@ -83,7 +85,8 @@ private void init() {
builder.add(new JLabel(currentPattern)).xy(2, 6);
builder.add(cleanUpRenamePDFonlyRelativePaths).xy(2, 7);
builder.add(cleanUpBibLatex).xyw(1, 8, 2);
builder.add(cleanUpFormatters).xyw(1, 9, 2);
builder.add(cleanUpISSN).xyw(1, 9, 2);
builder.add(cleanUpFormatters).xyw(1, 10, 2);
panel = builder.build();
}

Expand All @@ -98,6 +101,7 @@ private void updateDisplay(CleanupPreset preset) {
cleanUpRenamePDFonlyRelativePaths.setEnabled(cleanUpRenamePDF.isSelected());
cleanUpUpgradeExternalLinks.setSelected(preset.isCleanUpUpgradeExternalLinks());
cleanUpBibLatex.setSelected(preset.isConvertToBiblatex());
cleanUpBibLatex.setSelected(preset.isCleanUpISSN());
cleanUpFormatters.setValues(preset.getFormatterCleanups());
}

Expand All @@ -116,6 +120,9 @@ public CleanupPreset getCleanupPreset() {
if (cleanUpDOI.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CLEAN_UP_DOI);
}
if (cleanUpISSN.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CLEAN_UP_ISSN);
}
if (cleanUpMakePathsRelative.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.MAKE_PATHS_RELATIVE);
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/net/sf/jabref/logic/cleanup/CleanupPreset.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public static CleanupPreset loadFromPreferences(JabRefPreferences preferences) {
if (preferences.getBoolean(JabRefPreferences.CLEANUP_DOI)) {
activeJobs.add(CleanupStep.CLEAN_UP_DOI);
}
if (preferences.getBoolean(JabRefPreferences.CLEANUP_ISSN)) {
activeJobs.add(CleanupStep.CLEAN_UP_ISSN);
}
if (preferences.getBoolean(JabRefPreferences.CLEANUP_MOVE_PDF)) {
activeJobs.add(CleanupStep.MOVE_PDF);
}
Expand Down Expand Up @@ -88,6 +91,10 @@ public boolean isCleanUpDOI() {
return isActive(CleanupStep.CLEAN_UP_DOI);
}

public boolean isCleanUpISSN() {
return isActive(CleanupStep.CLEAN_UP_ISSN);
}

public boolean isFixFileLinks() {
return isActive(CleanupStep.FIX_FILE_LINKS);
}
Expand All @@ -114,6 +121,7 @@ public boolean isRenamePdfOnlyRelativePaths() {

public void storeInPreferences(JabRefPreferences preferences) {
preferences.putBoolean(JabRefPreferences.CLEANUP_DOI, isActive(CleanupStep.CLEAN_UP_DOI));
preferences.putBoolean(JabRefPreferences.CLEANUP_ISSN, isActive(CleanupStep.CLEAN_UP_ISSN));
preferences.putBoolean(JabRefPreferences.CLEANUP_MOVE_PDF, isActive(CleanupStep.MOVE_PDF));
preferences.putBoolean(JabRefPreferences.CLEANUP_MAKE_PATHS_RELATIVE, isActive(CleanupStep.MAKE_PATHS_RELATIVE));
preferences.putBoolean(JabRefPreferences.CLEANUP_RENAME_PDF, isActive(CleanupStep.RENAME_PDF));
Expand Down Expand Up @@ -152,6 +160,8 @@ public enum CleanupStep {
*/
CONVERT_TO_BIBLATEX,
MOVE_PDF,
FIX_FILE_LINKS
FIX_FILE_LINKS,
CLEAN_UP_ISSN
}

}
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ private List<CleanupJob> determineCleanupActions(CleanupPreset preset) {
if (preset.isCleanUpDOI()) {
jobs.add(new DoiCleanup());
}
if (preset.isCleanUpISSN()) {
jobs.add(new ISSNCleanup());
}
if (preset.isFixFileLinks()) {
jobs.add(new FileLinksCleanup());
}
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/net/sf/jabref/logic/cleanup/ISSNCleanup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.sf.jabref.logic.cleanup;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.jabref.model.FieldChange;
import net.sf.jabref.model.entry.BibEntry;


public class ISSNCleanup implements CleanupJob {

private static final Pattern ISSN_PATTERN_NODASH = Pattern.compile("^(\\d{4})(\\d{3}[\\dxX])$");
@Override
public List<FieldChange> cleanup(BibEntry entry) {
Optional<String> issn = entry.getFieldOptional("issn");
if (!issn.isPresent()) {
return Collections.emptyList();
}

Matcher matcher = ISSN_PATTERN_NODASH.matcher(issn.get());
if (matcher.find()) {
String result = matcher.replaceFirst("$1-$2");
entry.setField("issn", result);
FieldChange change = new FieldChange(entry, "issn", issn.get(), result);
return Collections.singletonList(change);
}
return Collections.emptyList();
}

}
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1706,3 +1706,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2424,3 +2424,5 @@ Online_help_forum=Online-Hilfeforum
Custom=



Add_possibly_missing_dash_to_ISSN=
3 changes: 2 additions & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2272,4 +2272,5 @@ A_new_version_of_JabRef_has_been_released.=A_new_version_of_JabRef_has_been_rele
JabRef_is_up-to-date.=JabRef_is_up-to-date.
Latest_version=Latest_version
Online_help_forum=Online_help_forum
Custom=Custom
Custom=Custom
Add_possibly_missing_dash_to_ISSN=Add_possibly_missing_dash_to_ISSN
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1608,3 +1608,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2394,3 +2394,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1652,3 +1652,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1627,3 +1627,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1727,3 +1727,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2372,3 +2372,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2403,3 +2403,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2799,3 +2799,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1621,3 +1621,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2371,3 +2371,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_sv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1564,3 +1564,5 @@ Online_help_forum=Onlineforum
Custom=Egna



Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_tr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1640,3 +1640,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_vi.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2395,3 +2395,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1634,3 +1634,5 @@ Online_help_forum=
Custom=

Converts_HTML_code_to_Unicode.=

Add_possibly_missing_dash_to_ISSN=
31 changes: 31 additions & 0 deletions src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.Globals;
Expand Down Expand Up @@ -166,6 +167,36 @@ public void cleanupDoiReturnsChangeWhenDoiInURLField() {
Assert.assertEquals(changeList, changes);
}

@Test
public void cleanupISSNReturnsCorrectISSN() {
CleanupPreset preset = new CleanupPreset(CleanupPreset.CleanupStep.CLEAN_UP_ISSN);
BibEntry entry = new BibEntry();
entry.setField("issn", "0123-4567");

worker.cleanup(preset, entry);
Assert.assertEquals(Optional.of("0123-4567"), entry.getFieldOptional("issn"));
}

@Test
public void cleanupISSNAddsMissingDash() {
CleanupPreset preset = new CleanupPreset(CleanupPreset.CleanupStep.CLEAN_UP_ISSN);
BibEntry entry = new BibEntry();
entry.setField("issn", "01234567");

worker.cleanup(preset, entry);
Assert.assertEquals(Optional.of("0123-4567"), entry.getFieldOptional("issn"));
}

@Test
public void cleanupISSNJunkStaysJunk() {
CleanupPreset preset = new CleanupPreset(CleanupPreset.CleanupStep.CLEAN_UP_ISSN);
BibEntry entry = new BibEntry();
entry.setField("issn", "Banana");

worker.cleanup(preset, entry);
Assert.assertEquals(Optional.of("Banana"), entry.getFieldOptional("issn"));
}

@Test
public void cleanupMonthChangesNumberToBibtex() {
CleanupPreset preset = new CleanupPreset(new FieldFormatterCleanups(true,
Expand Down

0 comments on commit 14a0bdc

Please sign in to comment.