Skip to content

Commit c2c1133

Browse files
author
mgeipel
committed
fixed #94, fixed #97
1 parent 31f1858 commit c2c1133

File tree

4 files changed

+319
-182
lines changed

4 files changed

+319
-182
lines changed

src/main/java/org/culturegraph/mf/morph/functions/ISBN.java

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@
1818
import java.util.regex.Matcher;
1919
import java.util.regex.Pattern;
2020

21-
import org.culturegraph.mf.exceptions.MorphException;
22-
23-
2421
/**
2522
* Offers ISBN conversions
2623
*
2724
* @author Markus Michael Geipel
2825
*/
2926
public final class ISBN extends AbstractSimpleStatelessFunction {
30-
private static final String CHECK = "0123456789X0";
31-
32-
private static final String APOSTROPH = "'";
27+
3328

29+
private static final String CHECK = "0123456789X0";
30+
3431
private static final String ISBN10 = "isbn10";
3532
private static final String ISBN13 = "isbn13";
3633
private static final Pattern ISBN_PATTERN = Pattern.compile("[\\dX]+");
@@ -42,28 +39,33 @@ public final class ISBN extends AbstractSimpleStatelessFunction {
4239
private static final int ISBN10_MOD = 11;
4340
private static final int ISBN13_MOD = 10;
4441

42+
43+
4544
private boolean to10;
4645
private boolean to13;
4746
private boolean verifyCheckDigit;
47+
private String errorString;
4848

49-
50-
49+
50+
public void setErrorString(final String errorString) {
51+
this.errorString = errorString;
52+
}
53+
5154
@Override
5255
public String process(final String value) {
5356
String result = cleanse(value);
5457
final int size = result.length();
55-
56-
if(verifyCheckDigit && !isValid(result)){
57-
throw new InvalidISBNCheckDigitException(value);
58-
}else if(!(size == ISBN10_SIZE || size == ISBN13_SIZE)){
59-
throw new InvalidISBNLengthException(value);
60-
}
61-
62-
if (to10 && ISBN13_SIZE == size) {
63-
result = isbn13to10(result);
64-
65-
} else if (to13 && ISBN10_SIZE == size) {
66-
result = isbn10to13(result);
58+
59+
if (verifyCheckDigit && !isValid(result)) {
60+
result = errorString;
61+
} else if (!(size == ISBN10_SIZE || size == ISBN13_SIZE)) {
62+
result = errorString;
63+
} else {
64+
if (to10 && ISBN13_SIZE == size) {
65+
result = isbn13to10(result);
66+
} else if (to13 && ISBN10_SIZE == size) {
67+
result = isbn10to13(result);
68+
}
6769
}
6870
return result;
6971
}
@@ -121,8 +123,7 @@ private static int charToInt(final char cha) {
121123

122124
public static String isbn13to10(final String isbn) {
123125
if (isbn.length() != ISBN13_SIZE) {
124-
throw new IllegalArgumentException(
125-
"isbn must be 13 characters long");
126+
throw new IllegalArgumentException("isbn must be 13 characters long");
126127
}
127128
final String isbn10Data = isbn.substring(3, 12);
128129

@@ -131,8 +132,7 @@ public static String isbn13to10(final String isbn) {
131132

132133
public static String isbn10to13(final String isbn) {
133134
if (isbn.length() != ISBN10_SIZE) {
134-
throw new IllegalArgumentException(
135-
"isbn must be 10 characters long");
135+
throw new IllegalArgumentException("isbn must be 10 characters long");
136136
}
137137

138138
final String isbn13Data = "978" + isbn.substring(0, ISBN10_SIZE - 1);
@@ -148,40 +148,15 @@ public static boolean isValid(final String isbn) {
148148
boolean result = false;
149149

150150
if (isbn.length() == ISBN10_SIZE) {
151-
result = check10(isbn.substring(0, ISBN10_SIZE - 1)) == isbn
152-
.charAt(ISBN10_SIZE - 1);
151+
result = check10(isbn.substring(0, ISBN10_SIZE - 1)) == isbn.charAt(ISBN10_SIZE - 1);
153152
} else if (isbn.length() == ISBN13_SIZE) {
154-
result = check13(isbn.substring(0, ISBN13_SIZE - 1)) == isbn
155-
.charAt(ISBN13_SIZE - 1);
153+
result = check13(isbn.substring(0, ISBN13_SIZE - 1)) == isbn.charAt(ISBN13_SIZE - 1);
156154
}
157155
return result;
158156
}
159-
157+
160158
public void setVerifyCheckDigit(final String verifyCheckDigit) {
161159
this.verifyCheckDigit = "true".equals(verifyCheckDigit);
162160
}
163-
164-
/**
165-
* Thrown if the ISBN length does not correspond to specification
166-
*
167-
*/
168-
public static final class InvalidISBNLengthException extends MorphException{
169-
private static final long serialVersionUID = 921922231931724504L;
170-
171-
public InvalidISBNLengthException(final String isbn) {
172-
super(APOSTROPH + isbn + APOSTROPH);
173-
}
174-
}
175-
176-
/**
177-
* Thrown if the ISBN check digit is wrong
178-
*
179-
*/
180-
public static final class InvalidISBNCheckDigitException extends MorphException{
181-
private static final long serialVersionUID = 921922231931724504L;
182-
public InvalidISBNCheckDigitException(final String isbn) {
183-
super(APOSTROPH + isbn + APOSTROPH);
184-
}
185-
}
186161

187162
}

src/main/java/org/culturegraph/mf/morph/functions/Regexp.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.regex.Matcher;
2121
import java.util.regex.Pattern;
2222

23-
import org.culturegraph.mf.exceptions.MorphException;
2423
import org.culturegraph.mf.morph.NamedValueSource;
2524
import org.culturegraph.mf.util.StringUtil;
2625

@@ -83,16 +82,4 @@ public void setFormat(final String format) {
8382
this.format = format;
8483
}
8584

86-
/**
87-
* Thrown if no match was found
88-
*
89-
*/
90-
public static final class PatternNotFoundException extends MorphException {
91-
private static final long serialVersionUID = 4113458605196557204L;
92-
93-
public PatternNotFoundException(final Pattern pattern, final String input) {
94-
super("Pattern '" + pattern + "' not found in '" + input + "'");
95-
}
96-
}
97-
9885
}

0 commit comments

Comments
 (0)