mirror of
https://github.com/zxing/zxing.git
synced 2025-02-02 05:41:08 -08:00
Check if characters are digits for EAN_13, EAN_8, ITF and UPC_EAN_EXTENSION (#1039)
* Check if characters are digits * Use regex to check if input is valid. Added tests * Create checkNumeric method * Remove unused locals, moved attributes to top of class. * Simplify testcase * Reduce repeated code * Fixed indentation
This commit is contained in:
parent
0cf3b9be71
commit
48bb5fd860
|
@ -78,6 +78,7 @@ public final class EAN13Writer extends UPCEANWriter {
|
|||
"Requested contents should be 12 or 13 digits long, but got " + length);
|
||||
}
|
||||
|
||||
checkNumeric(contents);
|
||||
|
||||
int firstDigit = Character.digit(contents.charAt(0), 10);
|
||||
int parities = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit];
|
||||
|
|
|
@ -82,6 +82,8 @@ public final class EAN8Writer extends UPCEANWriter {
|
|||
"Requested contents should be 8 digits long, but got " + length);
|
||||
}
|
||||
|
||||
checkNumeric(contents);
|
||||
|
||||
boolean[] result = new boolean[CODE_WIDTH];
|
||||
int pos = 0;
|
||||
|
||||
|
|
|
@ -74,6 +74,9 @@ public final class ITFWriter extends OneDimensionalCodeWriter {
|
|||
throw new IllegalArgumentException(
|
||||
"Requested contents should be less than 80 digits long, but got " + length);
|
||||
}
|
||||
|
||||
checkNumeric(contents);
|
||||
|
||||
boolean[] result = new boolean[9 + 9 * length];
|
||||
int pos = appendPattern(result, 0, START_PATTERN, true);
|
||||
for (int i = 0; i < length; i += 2) {
|
||||
|
|
|
@ -23,6 +23,7 @@ import com.google.zxing.WriterException;
|
|||
import com.google.zxing.common.BitMatrix;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* <p>Encapsulates functionality and implementation that is common to one-dimensional barcodes.</p>
|
||||
|
@ -30,6 +31,7 @@ import java.util.Map;
|
|||
* @author dsbnatut@gmail.com (Kazuki Nishiura)
|
||||
*/
|
||||
public abstract class OneDimensionalCodeWriter implements Writer {
|
||||
private static final Pattern NUMERIC = Pattern.compile("[0-9]+");
|
||||
|
||||
@Override
|
||||
public final BitMatrix encode(String contents, BarcodeFormat format, int width, int height)
|
||||
|
@ -90,6 +92,14 @@ public abstract class OneDimensionalCodeWriter implements Writer {
|
|||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw IllegalArgumentException if input contains characters other than digits 0-9.
|
||||
*/
|
||||
protected static final void checkNumeric(String contents) throws IllegalArgumentException {
|
||||
if (!NUMERIC.matcher(contents).matches()) {
|
||||
throw new IllegalArgumentException("Input should only contain digits 0-9");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param target encode black/white pattern into this array
|
||||
|
|
|
@ -76,6 +76,8 @@ public final class UPCEWriter extends UPCEANWriter {
|
|||
"Requested contents should be 8 digits long, but got " + length);
|
||||
}
|
||||
|
||||
checkNumeric(contents);
|
||||
|
||||
int firstDigit = Character.digit(contents.charAt(0), 10);
|
||||
if (firstDigit != 0 && firstDigit != 1) {
|
||||
throw new IllegalArgumentException("Number system must be 0 or 1");
|
||||
|
|
|
@ -42,4 +42,8 @@ public final class EAN13WriterTestCase extends Assert {
|
|||
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testEncodeIllegalCharacters() throws WriterException {
|
||||
new EAN13Writer().encode("5901234123abc", BarcodeFormat.EAN_13, 0, 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,4 +42,8 @@ public final class EAN8WriterTestCase extends Assert {
|
|||
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testEncodeIllegalCharacters() throws WriterException {
|
||||
new EAN8Writer().encode("96385abc", BarcodeFormat.EAN_8, 0, 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,4 +40,9 @@ public final class ITFWriterTestCase extends Assert {
|
|||
assertEquals(expected, BitMatrixTestCase.matrixToString(result));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testEncodeIllegalCharacters() throws WriterException {
|
||||
new ITFWriter().encode("00123456789abc", BarcodeFormat.ITF, 0, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -51,4 +51,8 @@ public final class UPCEWriterTestCase extends Assert {
|
|||
assertEquals(encoding, BitMatrixTestCase.matrixToString(result));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testEncodeIllegalCharacters() throws WriterException {
|
||||
new UPCEWriter().encode("05096abc", BarcodeFormat.UPC_E, 0, 0);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue