From 14a0bdcc9f31502b07499345ee196e4963135dd9 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Fri, 15 Jul 2016 17:15:20 +0200 Subject: [PATCH] Implemented #1345 - cleanup ISSN --- CHANGELOG.md | 1 + .../java/net/sf/jabref/JabRefPreferences.java | 2 ++ .../gui/cleanup/CleanupPresetPanel.java | 11 +++++-- .../jabref/logic/cleanup/CleanupPreset.java | 12 ++++++- .../jabref/logic/cleanup/CleanupWorker.java | 3 ++ .../sf/jabref/logic/cleanup/ISSNCleanup.java | 33 +++++++++++++++++++ src/main/resources/l10n/JabRef_da.properties | 2 ++ src/main/resources/l10n/JabRef_de.properties | 2 ++ src/main/resources/l10n/JabRef_en.properties | 3 +- src/main/resources/l10n/JabRef_es.properties | 2 ++ src/main/resources/l10n/JabRef_fa.properties | 2 ++ src/main/resources/l10n/JabRef_fr.properties | 2 ++ src/main/resources/l10n/JabRef_in.properties | 2 ++ src/main/resources/l10n/JabRef_it.properties | 2 ++ src/main/resources/l10n/JabRef_ja.properties | 2 ++ src/main/resources/l10n/JabRef_nl.properties | 2 ++ src/main/resources/l10n/JabRef_no.properties | 2 ++ .../resources/l10n/JabRef_pt_BR.properties | 2 ++ src/main/resources/l10n/JabRef_ru.properties | 2 ++ src/main/resources/l10n/JabRef_sv.properties | 2 ++ src/main/resources/l10n/JabRef_tr.properties | 2 ++ src/main/resources/l10n/JabRef_vi.properties | 2 ++ src/main/resources/l10n/JabRef_zh.properties | 2 ++ .../logic/cleanup/CleanupWorkerTest.java | 31 +++++++++++++++++ 24 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/sf/jabref/logic/cleanup/ISSNCleanup.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 292f3c65486c..0eb36205a9b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/net/sf/jabref/JabRefPreferences.java b/src/main/java/net/sf/jabref/JabRefPreferences.java index 79cae1b39fcf..aace58f1a372 100644 --- a/src/main/java/net/sf/jabref/JabRefPreferences.java +++ b/src/main/java/net/sf/jabref/JabRefPreferences.java @@ -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"; @@ -1374,6 +1375,7 @@ public void setDefaultEncoding(Charset encoding) { private static void insertCleanupPreset(Map 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()); diff --git a/src/main/java/net/sf/jabref/gui/cleanup/CleanupPresetPanel.java b/src/main/java/net/sf/jabref/gui/cleanup/CleanupPresetPanel.java index 3bb7c8161be1..290b5207af7b 100644 --- a/src/main/java/net/sf/jabref/gui/cleanup/CleanupPresetPanel.java +++ b/src/main/java/net/sf/jabref/gui/cleanup/CleanupPresetPanel.java @@ -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; @@ -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())); @@ -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); @@ -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(); } @@ -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()); } @@ -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); } diff --git a/src/main/java/net/sf/jabref/logic/cleanup/CleanupPreset.java b/src/main/java/net/sf/jabref/logic/cleanup/CleanupPreset.java index e90c3f1c32b3..0a719dae7b54 100644 --- a/src/main/java/net/sf/jabref/logic/cleanup/CleanupPreset.java +++ b/src/main/java/net/sf/jabref/logic/cleanup/CleanupPreset.java @@ -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); } @@ -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); } @@ -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)); @@ -152,6 +160,8 @@ public enum CleanupStep { */ CONVERT_TO_BIBLATEX, MOVE_PDF, - FIX_FILE_LINKS + FIX_FILE_LINKS, + CLEAN_UP_ISSN } + } diff --git a/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java b/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java index 8c6c8edee187..0e589f7aeb0a 100644 --- a/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java +++ b/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java @@ -62,6 +62,9 @@ private List determineCleanupActions(CleanupPreset preset) { if (preset.isCleanUpDOI()) { jobs.add(new DoiCleanup()); } + if (preset.isCleanUpISSN()) { + jobs.add(new ISSNCleanup()); + } if (preset.isFixFileLinks()) { jobs.add(new FileLinksCleanup()); } diff --git a/src/main/java/net/sf/jabref/logic/cleanup/ISSNCleanup.java b/src/main/java/net/sf/jabref/logic/cleanup/ISSNCleanup.java new file mode 100644 index 000000000000..763b9b786a6e --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/cleanup/ISSNCleanup.java @@ -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 cleanup(BibEntry entry) { + Optional 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(); + } + +} diff --git a/src/main/resources/l10n/JabRef_da.properties b/src/main/resources/l10n/JabRef_da.properties index 4dec88144f5f..bcf5a03187d8 100644 --- a/src/main/resources/l10n/JabRef_da.properties +++ b/src/main/resources/l10n/JabRef_da.properties @@ -1706,3 +1706,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_de.properties b/src/main/resources/l10n/JabRef_de.properties index 43e0ce879f46..e4a800be83f5 100644 --- a/src/main/resources/l10n/JabRef_de.properties +++ b/src/main/resources/l10n/JabRef_de.properties @@ -2424,3 +2424,5 @@ Online_help_forum=Online-Hilfeforum Custom= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 12ac8522ff7d..9d17de855cfb 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -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 \ No newline at end of file +Custom=Custom +Add_possibly_missing_dash_to_ISSN=Add_possibly_missing_dash_to_ISSN \ No newline at end of file diff --git a/src/main/resources/l10n/JabRef_es.properties b/src/main/resources/l10n/JabRef_es.properties index 63cab8f118ad..9b74eff96b46 100644 --- a/src/main/resources/l10n/JabRef_es.properties +++ b/src/main/resources/l10n/JabRef_es.properties @@ -1608,3 +1608,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_fa.properties b/src/main/resources/l10n/JabRef_fa.properties index b8e1a33a40ee..1cbe13c01ba1 100644 --- a/src/main/resources/l10n/JabRef_fa.properties +++ b/src/main/resources/l10n/JabRef_fa.properties @@ -2394,3 +2394,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_fr.properties b/src/main/resources/l10n/JabRef_fr.properties index aba12d64823f..3e518a0dc197 100644 --- a/src/main/resources/l10n/JabRef_fr.properties +++ b/src/main/resources/l10n/JabRef_fr.properties @@ -1652,3 +1652,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_in.properties b/src/main/resources/l10n/JabRef_in.properties index 6569ed22c766..46b839c841e0 100644 --- a/src/main/resources/l10n/JabRef_in.properties +++ b/src/main/resources/l10n/JabRef_in.properties @@ -1627,3 +1627,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_it.properties b/src/main/resources/l10n/JabRef_it.properties index 3764196d33fd..ce5c9d7db19f 100644 --- a/src/main/resources/l10n/JabRef_it.properties +++ b/src/main/resources/l10n/JabRef_it.properties @@ -1727,3 +1727,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_ja.properties b/src/main/resources/l10n/JabRef_ja.properties index 02947a65c639..676ba98beec4 100644 --- a/src/main/resources/l10n/JabRef_ja.properties +++ b/src/main/resources/l10n/JabRef_ja.properties @@ -2372,3 +2372,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_nl.properties b/src/main/resources/l10n/JabRef_nl.properties index 774ff9b0a7d8..da8c24628a47 100644 --- a/src/main/resources/l10n/JabRef_nl.properties +++ b/src/main/resources/l10n/JabRef_nl.properties @@ -2403,3 +2403,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_no.properties b/src/main/resources/l10n/JabRef_no.properties index 38a65da7b274..8b174fff7762 100644 --- a/src/main/resources/l10n/JabRef_no.properties +++ b/src/main/resources/l10n/JabRef_no.properties @@ -2799,3 +2799,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_pt_BR.properties b/src/main/resources/l10n/JabRef_pt_BR.properties index 71dd268b4fbd..7edd37f1f412 100644 --- a/src/main/resources/l10n/JabRef_pt_BR.properties +++ b/src/main/resources/l10n/JabRef_pt_BR.properties @@ -1621,3 +1621,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_ru.properties b/src/main/resources/l10n/JabRef_ru.properties index 0a2a26969746..069263e63df7 100644 --- a/src/main/resources/l10n/JabRef_ru.properties +++ b/src/main/resources/l10n/JabRef_ru.properties @@ -2371,3 +2371,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_sv.properties b/src/main/resources/l10n/JabRef_sv.properties index 370b53f700eb..90762cffcefe 100644 --- a/src/main/resources/l10n/JabRef_sv.properties +++ b/src/main/resources/l10n/JabRef_sv.properties @@ -1564,3 +1564,5 @@ Online_help_forum=Onlineforum Custom=Egna + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_tr.properties b/src/main/resources/l10n/JabRef_tr.properties index 6db3d61b7e2f..bed0696d4f68 100644 --- a/src/main/resources/l10n/JabRef_tr.properties +++ b/src/main/resources/l10n/JabRef_tr.properties @@ -1640,3 +1640,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_vi.properties b/src/main/resources/l10n/JabRef_vi.properties index cdd8a3858184..acd319dcdfe5 100644 --- a/src/main/resources/l10n/JabRef_vi.properties +++ b/src/main/resources/l10n/JabRef_vi.properties @@ -2395,3 +2395,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/main/resources/l10n/JabRef_zh.properties b/src/main/resources/l10n/JabRef_zh.properties index 30d7b51bd6a8..6d60ea70c404 100644 --- a/src/main/resources/l10n/JabRef_zh.properties +++ b/src/main/resources/l10n/JabRef_zh.properties @@ -1634,3 +1634,5 @@ Online_help_forum= Custom= Converts_HTML_code_to_Unicode.= + +Add_possibly_missing_dash_to_ISSN= diff --git a/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java b/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java index 30bc897ee927..73d33ff2bb77 100644 --- a/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java +++ b/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java @@ -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; @@ -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,