refactor PDF417HighLevelEncoder to avoid code duplication

extend UT to new method PDF417HighLevelEncoder#checkCharset
This commit is contained in:
ftiercelin 2024-10-22 09:25:52 +01:00
parent dab104695a
commit 7cba81bc2e
2 changed files with 40 additions and 14 deletions

View file

@ -176,24 +176,13 @@ final class PDF417HighLevelEncoder {
}
if (Compaction.TEXT == compaction) {
for (int i = 0; i < msg.length(); i++) {
if (msg.charAt(i) > 255) {
throw new WriterException("Non-encodable character detected: " + msg.charAt(i) + " (Unicode: " +
(int) msg.charAt(i) +
"). Consider specifying Compaction.AUTO instead of Compaction.TEXT");
}
}
checkCharset(msg,255,"Consider specifying Compaction.AUTO instead of Compaction.TEXT");
}
if (encoding == null && !autoECI) {
for (int i = 0; i < msg.length(); i++) {
if (msg.charAt(i) > 255) {
throw new WriterException("Non-encodable character detected: " + msg.charAt(i) + " (Unicode: " +
(int) msg.charAt(i) +
"). Consider specifying EncodeHintType.PDF417_AUTO_ECI and/or EncodeTypeHint.CHARACTER_SET.");
}
}
checkCharset(msg,255,"Consider specifying EncodeHintType.PDF417_AUTO_ECI and/or EncodeTypeHint.CHARACTER_SET.");
}
//the codewords 0..928 are encoded as Unicode characters
StringBuilder sb = new StringBuilder(msg.length());
@ -294,6 +283,22 @@ final class PDF417HighLevelEncoder {
return sb.toString();
}
/**
* Check if input is only made of characters between 0 and the upper limit
* @param input the input
* @param max the upper limit for charset
* @param errorMessage the message to explain the error
* @throws WriterException exception highlighting the offending charcater and a suggestion to avoid the error
*/
protected static void checkCharset(String input,int max, String errorMessage) throws WriterException {
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) > max) {
throw new WriterException("Non-encodable character detected: " + input.charAt(i) + " (Unicode: " +
(int) input.charAt(i) + "). " + errorMessage);
}
}
}
/**
* Encode parts of the message using Text Compaction as described in ISO/IEC 15438:2001(E),
* chapter 4.4.2.

View file

@ -18,6 +18,7 @@ package com.google.zxing.pdf417.encoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
@ -79,6 +80,26 @@ public final class PDF417EncoderTestCase extends Assert {
}
}
@Test
public void testCheckCharset() throws Exception {
String input = "Hello!";
String errorMessage = UUID.randomUUID().toString();
// no exception
PDF417HighLevelEncoder.checkCharset(input,255,errorMessage);
PDF417HighLevelEncoder.checkCharset(input,1255,errorMessage);
PDF417HighLevelEncoder.checkCharset(input,112,errorMessage);
try {
// should throw an exception for character 'o' because it exceeds upper limit 110
PDF417HighLevelEncoder.checkCharset(input,110,errorMessage);
} catch (WriterException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().contains("111"));
assertTrue(e.getMessage().contains(errorMessage));
}
}
@Test
public void testEncodeIso88591WithSpecialChars() throws Exception {
// Just check if this does not throw an exception