18
18
import java .util .regex .Matcher ;
19
19
import java .util .regex .Pattern ;
20
20
21
- import org .culturegraph .mf .exceptions .MorphException ;
22
-
23
-
24
21
/**
25
22
* Offers ISBN conversions
26
23
*
27
24
* @author Markus Michael Geipel
28
25
*/
29
26
public final class ISBN extends AbstractSimpleStatelessFunction {
30
- private static final String CHECK = "0123456789X0" ;
31
-
32
- private static final String APOSTROPH = "'" ;
27
+
33
28
29
+ private static final String CHECK = "0123456789X0" ;
30
+
34
31
private static final String ISBN10 = "isbn10" ;
35
32
private static final String ISBN13 = "isbn13" ;
36
33
private static final Pattern ISBN_PATTERN = Pattern .compile ("[\\ dX]+" );
@@ -42,28 +39,33 @@ public final class ISBN extends AbstractSimpleStatelessFunction {
42
39
private static final int ISBN10_MOD = 11 ;
43
40
private static final int ISBN13_MOD = 10 ;
44
41
42
+
43
+
45
44
private boolean to10 ;
46
45
private boolean to13 ;
47
46
private boolean verifyCheckDigit ;
47
+ private String errorString ;
48
48
49
-
50
-
49
+
50
+ public void setErrorString (final String errorString ) {
51
+ this .errorString = errorString ;
52
+ }
53
+
51
54
@ Override
52
55
public String process (final String value ) {
53
56
String result = cleanse (value );
54
57
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
+ }
67
69
}
68
70
return result ;
69
71
}
@@ -121,8 +123,7 @@ private static int charToInt(final char cha) {
121
123
122
124
public static String isbn13to10 (final String isbn ) {
123
125
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" );
126
127
}
127
128
final String isbn10Data = isbn .substring (3 , 12 );
128
129
@@ -131,8 +132,7 @@ public static String isbn13to10(final String isbn) {
131
132
132
133
public static String isbn10to13 (final String isbn ) {
133
134
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" );
136
136
}
137
137
138
138
final String isbn13Data = "978" + isbn .substring (0 , ISBN10_SIZE - 1 );
@@ -148,40 +148,15 @@ public static boolean isValid(final String isbn) {
148
148
boolean result = false ;
149
149
150
150
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 );
153
152
} 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 );
156
154
}
157
155
return result ;
158
156
}
159
-
157
+
160
158
public void setVerifyCheckDigit (final String verifyCheckDigit ) {
161
159
this .verifyCheckDigit = "true" .equals (verifyCheckDigit );
162
160
}
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
- }
186
161
187
162
}
0 commit comments