Skip to content

Commit

Permalink
Implemented #1345 - cleanup ISSN (#1590)
Browse files Browse the repository at this point in the history
* Implemented #1345 - cleanup ISSN

* Fixed comments

* Extracted ISSN class

* Added tests for ISSN and ISBN
  • Loading branch information
oscargus authored Jul 23, 2016
1 parent 0684f55 commit 458490b
Show file tree
Hide file tree
Showing 29 changed files with 352 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Added filter to not show selected integrity checks
- Enhance the entry customization dialog to give better visual feedback
- The arXiv fetcher now also supports free-text search queries
- [#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
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 @@ -22,6 +22,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 @@ -44,6 +45,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("Reformat 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 @@ -71,7 +73,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 @@ -84,7 +86,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 @@ -99,6 +102,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 @@ -117,6 +121,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 @@ -68,6 +68,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
32 changes: 32 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,32 @@
package net.sf.jabref.logic.cleanup;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import net.sf.jabref.logic.util.ISSN;
import net.sf.jabref.model.FieldChange;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;


public class ISSNCleanup implements CleanupJob {

@Override
public List<FieldChange> cleanup(BibEntry entry) {
Optional<String> issnString = entry.getFieldOptional(FieldName.ISSN);
if (!issnString.isPresent()) {
return Collections.emptyList();
}

ISSN issn = new ISSN(issnString.get());
if (issn.isCanBeCleaned()) {
String newValue = issn.getCleanedISSN();
FieldChange change = new FieldChange(entry, FieldName.ISSN, issnString.get(), newValue);
entry.setField(FieldName.ISSN, newValue);
return Collections.singletonList(change);
}
return Collections.emptyList();
}

}
25 changes: 6 additions & 19 deletions src/main/java/net/sf/jabref/logic/integrity/ISSNChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

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

import net.sf.jabref.logic.integrity.IntegrityCheck.Checker;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.ISSN;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;


public class ISSNChecker implements Checker {

private static final Pattern ISSN_PATTERN = Pattern.compile("^\\d{4}-\\d{3}[\\dxX]$");

@Override
public List<IntegrityMessage> check(BibEntry entry) {
Expand All @@ -22,26 +20,15 @@ public List<IntegrityMessage> check(BibEntry entry) {
}

// Check that the ISSN is on the correct form
String issn = entry.getFieldOptional(FieldName.ISSN).get().trim();
Matcher issnMatcher = ISSN_PATTERN.matcher(issn);
if (!issnMatcher.matches()) {
String issnString = entry.getFieldOptional(FieldName.ISSN).get().trim();

ISSN issn = new ISSN(issnString);
if (!issn.isValidFormat()) {
return Collections.singletonList(
new IntegrityMessage(Localization.lang("incorrect format"), entry, FieldName.ISSN));
}

// Check that the control digit is correct, see e.g. https://en.wikipedia.org/wiki/International_Standard_Serial_Number
int sum = 0;
for (int pos = 0; pos <= 7; pos++) {
char c = issn.charAt(pos);
if (pos != 4) {
sum += (c - '0') * ((8 - pos) + (pos > 4 ? 1 : 0));
}
}
char control = issn.charAt(8);
if ((control == 'x') || (control == 'X')) {
control = '9' + 1;
}
if (((((sum % 11) + control) - '0') == 11) || ((sum % 11) == 0)) {
if (issn.isValidChecksum()) {
return Collections.emptyList();
} else {
return Collections
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/logic/util/ISBN.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public boolean isIsbn13() {

// Check that the control digit is correct, see e.g. https://en.wikipedia.org/wiki/International_Standard_Book_Number#Check_digits
private boolean isbn10check() {
if ((isbnString == null) || (isbnString.length() != 10)) {
if (isbnString.length() != 10) {
return false;
}

Expand All @@ -62,7 +62,7 @@ private boolean isbn10check() {

// Check that the control digit is correct, see e.g. https://en.wikipedia.org/wiki/International_Standard_Book_Number#Check_digits
private boolean isbn13check() {
if ((isbnString == null) || (isbnString.length() != 13)) {
if (isbnString.length() != 13) {
return false;
}

Expand Down
52 changes: 52 additions & 0 deletions src/main/java/net/sf/jabref/logic/util/ISSN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.sf.jabref.logic.util;

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

public class ISSN {

private static final Pattern ISSN_PATTERN = Pattern.compile("^\\d{4}-\\d{3}[\\dxX]$");
private static final Pattern ISSN_PATTERN_NODASH = Pattern.compile("^(\\d{4})(\\d{3}[\\dxX])$");

private final String issnString;


public ISSN(String issnString) {
this.issnString = Objects.requireNonNull(issnString).trim();
}

public boolean isValidFormat() {
Matcher issnMatcher = ISSN_PATTERN.matcher(issnString);
return (issnMatcher.matches());
}

public boolean isCanBeCleaned() {
Matcher issnNoDashMatcher = ISSN_PATTERN_NODASH.matcher(issnString);
return (issnNoDashMatcher.matches());
}

public String getCleanedISSN() {
Matcher issnNoDashMatcher = ISSN_PATTERN_NODASH.matcher(issnString);
if (issnNoDashMatcher.find()) {
return issnNoDashMatcher.replaceFirst("$1-$2");
}
return issnString;
}

public boolean isValidChecksum() {
// Check that the control digit is correct, see e.g. https://en.wikipedia.org/wiki/International_Standard_Serial_Number
int sum = 0;
for (int pos = 0; pos <= 7; pos++) {
char c = issnString.charAt(pos);
if (pos != 4) {
sum += (c - '0') * ((8 - pos) + (pos > 4 ? 1 : 0));
}
}
char control = issnString.charAt(8);
if ((control == 'x') || (control == 'X')) {
control = '9' + 1;
}
return (((((sum % 11) + control) - '0') == 11) || ((sum % 11) == 0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,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 @@ -1386,6 +1387,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
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 @@ -1712,3 +1712,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_ISSN=
5 changes: 5 additions & 0 deletions src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2428,3 +2428,8 @@ Execute_command=Befehl_ausführen
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=Hinweis\:_%0_als_Platzhalter_für_den_Speicherort_der_Datenbank_benutzen.
Executing_command_\"%0\"...=Ausführung_des_Kommandos_\"%0\"...
Error_occured_while_executing_the_command_\"%0\".=Während_der_Ausführung_des_Befehls_\"%0\"_ist_ein_Fehler_aufgetreten.



Reformat_ISSN=

1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2279,3 +2279,4 @@ Execute_command=Execute_command
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.
Executing_command_\"%0\"...=Executing_command_\"%0\"...
Error_occured_while_executing_the_command_\"%0\".=Error_occured_while_executing_the_command_\"%0\".
Reformat_ISSN=Reformat_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 @@ -1614,3 +1614,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -2400,3 +2400,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -1658,3 +1658,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -1633,3 +1633,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -1733,3 +1733,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -2378,3 +2378,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -2409,3 +2409,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -2805,3 +2805,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -1627,3 +1627,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -2377,3 +2377,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -1570,3 +1570,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -1646,3 +1646,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -2401,3 +2401,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_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 @@ -1640,3 +1640,5 @@ Execute_command=
Note\:_Use_the_placeholder_%0_for_the_location_of_the_opened_database_file.=
Executing_command_\"%0\"...=
Error_occured_while_executing_the_command_\"%0\".=

Reformat_ISSN=
Loading

0 comments on commit 458490b

Please sign in to comment.