Permalink
Browse files

Check if characters are digits for EAN_13, EAN_8, ITF and UPC_EAN_EXT…

…ENSION (#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
  • Loading branch information...
keoni29 authored and srowen committed Jul 10, 2018
1 parent 0cf3b9b commit 48bb5fd86031d70807f374be4f77e26b989b6b54
@@ -78,6 +78,7 @@ public BitMatrix encode(String contents,
"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 BitMatrix encode(String contents,
"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 BitMatrix encode(String contents,
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,13 +23,15 @@
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>
*
* @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 @@ private static BitMatrix renderResult(boolean[] code, int width, int height, int
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 BitMatrix encode(String contents,
"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 void testAddChecksumAndEncode() throws WriterException {
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 void testAddChecksumAndEncode() throws WriterException {
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 @@ private static void doTest(String input, CharSequence expected) throws WriterExc
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 @@ private static void doTest(String content, String encoding) throws WriterExcepti
assertEquals(encoding, BitMatrixTestCase.matrixToString(result));
}
@Test(expected = IllegalArgumentException.class)
public void testEncodeIllegalCharacters() throws WriterException {
new UPCEWriter().encode("05096abc", BarcodeFormat.UPC_E, 0, 0);
}
}

0 comments on commit 48bb5fd

Please sign in to comment.