Skip to content

Commit

Permalink
Extracted ISBN class
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Jul 18, 2016
1 parent db7e213 commit 6900dff
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 39 deletions.
45 changes: 6 additions & 39 deletions src/main/java/net/sf/jabref/logic/integrity/ISBNChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,35 @@

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.ISBN;
import net.sf.jabref.model.entry.BibEntry;


public class ISBNChecker implements Checker {

private static final Pattern ISBN_PATTERN = Pattern.compile("^(\\d{9}[\\dxX]|\\d{13})$");

@Override
public List<IntegrityMessage> check(BibEntry entry) {
if (!entry.hasField("isbn")) {
return Collections.emptyList();
}

// Check that the ISSN is on the correct form
String isbn = entry.getFieldOptional("isbn").get().trim().replace("-", "");
Matcher isbnMatcher = ISBN_PATTERN.matcher(isbn);
// Check that the ISBN is on the correct form
ISBN isbn = new ISBN(entry.getFieldOptional("isbn").get());

if (!isbnMatcher.matches()) {
if (!isbn.isValidFormat()) {
return Collections.singletonList(
new IntegrityMessage(Localization.lang("incorrect format"), entry, "isbn"));
}

boolean valid;
if (isbn.length() == 10) {
valid = isbn10check(isbn);
} else {
// length is either 10 or 13 based on regexp so will be 13 here
valid = isbn13check(isbn);
}
if (valid) {
return Collections.emptyList();
} else {
if (!isbn.isValidChecksum()) {
return Collections
.singletonList(new IntegrityMessage(Localization.lang("incorrect control digit"), entry, "isbn"));
}
}

// Check that the control digit is correct, see e.g. https://en.wikipedia.org/wiki/International_Standard_Book_Number#Check_digits
private boolean isbn10check(String isbn) {
int sum = 0;
for (int pos = 0; pos <= 8; pos++) {
sum += (isbn.charAt(pos) - '0') * ((10 - pos));
}
char control = isbn.charAt(9);
if ((control == 'x') || (control == 'X')) {
control = '9' + 1;
}
sum += (control - '0');
return (sum % 11) == 0;
return Collections.emptyList();
}

// Check that the control digit is correct, see e.g. https://en.wikipedia.org/wiki/International_Standard_Book_Number#Check_digits
private boolean isbn13check(String isbn) {
int sum = 0;
for (int pos = 0; pos <= 12; pos++) {
sum += (isbn.charAt(pos) - '0') * ((pos % 2) == 0 ? 1 : 3);
}
return (sum % 10) == 0;
}
}
76 changes: 76 additions & 0 deletions src/main/java/net/sf/jabref/logic/util/ISBN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package net.sf.jabref.logic.util;

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

public class ISBN {

private static final Pattern ISBN_PATTERN = Pattern.compile("^(\\d{9}[\\dxX]|\\d{13})$");

private final String isbn;


public ISBN(String isbnString) {
this.isbn = Objects.requireNonNull(isbnString).trim().replace("-", "");
}

public boolean isValidFormat() {
Matcher isbnMatcher = ISBN_PATTERN.matcher(isbn);
if (isbnMatcher.matches()) {
return true;
}
return false;
}

public boolean isValidChecksum() {
boolean valid;
if (isbn.length() == 10) {
valid = isbn10check();
} else {
// length is either 10 or 13 based on regexp so will be 13 here
valid = isbn13check();
}
return valid;
}

public boolean isIsbn10() {
return isbn10check();
}

public boolean isIsbn13() {
return isbn13check();
}

// 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 ((isbn == null) || (isbn.length() != 10)) {
return false;
}

int sum = 0;
for (int pos = 0; pos <= 8; pos++) {
sum += (isbn.charAt(pos) - '0') * ((10 - pos));
}
char control = isbn.charAt(9);
if ((control == 'x') || (control == 'X')) {
control = '9' + 1;
}
sum += (control - '0');
return (sum % 11) == 0;
}

// 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 ((isbn == null) || (isbn.length() != 13)) {
return false;
}

int sum = 0;
for (int pos = 0; pos <= 12; pos++) {
sum += (isbn.charAt(pos) - '0') * ((pos % 2) == 0 ? 1 : 3);
}
return (sum % 10) == 0;
}

}

0 comments on commit 6900dff

Please sign in to comment.