1 58 package org.krysalis.barcode.impl; 59 60 import org.krysalis.barcode.BarGroup; 61 import org.krysalis.barcode.ChecksumMode; 62 import org.krysalis.barcode.ClassicBarcodeLogicHandler; 63 64 69 public class POSTNETLogicImpl { 70 71 private static final byte[][] CHARSET = 72 {{2, 2, 1, 1, 1}, {1, 1, 1, 2, 2}, {1, 1, 2, 1, 2}, {1, 1, 2, 2, 1}, {1, 2, 1, 1, 2}, {1, 2, 1, 2, 1}, {1, 2, 2, 1, 1}, {2, 1, 1, 1, 2}, {2, 1, 1, 2, 1}, {2, 1, 2, 1, 1}}; 83 private ChecksumMode checksumMode = ChecksumMode.CP_AUTO; 84 private static final char DASH = '-'; 85 86 90 public POSTNETLogicImpl(ChecksumMode mode) { 91 this.checksumMode = mode; 92 } 93 94 98 public ChecksumMode getChecksumMode() { 99 return this.checksumMode; 100 } 101 102 108 public static char calcChecksum(String msg) { 109 int tmp = 0; 110 for (int i = 0; i < msg.length(); i++) { 111 if (isIgnoredChar(msg.charAt(i))) { 112 continue; 113 } 114 tmp += Character.digit(msg.charAt(i), 10); 115 if (tmp > 9) { 116 tmp -= 10; 117 } 118 } 119 return Character.forDigit((10 - tmp) % 10, 10); 120 } 121 122 123 128 public static boolean validateChecksum(String msg) { 129 char actual = msg.charAt(msg.length() - 1); 130 char expected = calcChecksum(msg.substring(0, msg.length() - 1)); 131 return (actual == expected); 132 } 133 134 private static boolean isValidChar(char ch) { 135 return Character.isDigit(ch) || isIgnoredChar(ch); 136 } 137 138 143 public static boolean isIgnoredChar(char c) { 144 return c == DASH; 145 } 146 147 152 public static String removeIgnoredCharacters(final String msg) { 153 StringBuffer sb = new StringBuffer (msg.length()); 154 for (int i = 0; i < msg.length(); i++) { 155 final char ch = msg.charAt(i); 156 if (!isValidChar(ch)) { 157 throw new IllegalArgumentException ("Invalid character: " + ch); 158 } 159 if (!isIgnoredChar(ch)) { 160 sb.append(ch); 161 } 162 } 163 return sb.toString(); 164 } 165 166 private int heightAt(char ch, int index) { 167 int chidx = Character.digit(ch, 10); 168 if (chidx >= 0) { 169 int height = CHARSET[chidx][index]; 170 return height; 171 } else { 172 throw new IllegalArgumentException ("Invalid character: " + ch); 173 } 174 } 175 176 181 protected void encodeChar(ClassicBarcodeLogicHandler logic, char c) { 182 if (isIgnoredChar(c)) { 183 return; } 185 logic.startBarGroup(BarGroup.MSG_CHARACTER, new Character (c).toString()); 186 for (byte i = 0; i < 5; i++) { 187 int height = heightAt(c, i); 188 logic.addBar(true, height); 189 addIntercharacterGap(logic); 190 } 191 logic.endBarGroup(); 192 } 193 194 private void addIntercharacterGap(ClassicBarcodeLogicHandler logic) { 195 logic.addBar(false, -1); } 197 198 private void handleChecksum(StringBuffer sb) { 199 if (getChecksumMode() == ChecksumMode.CP_ADD) { 200 sb.append(calcChecksum(sb.toString())); 201 } else if (getChecksumMode() == ChecksumMode.CP_CHECK) { 202 if (!validateChecksum(sb.toString())) { 203 throw new IllegalArgumentException ("Message '" 204 + sb.toString() 205 + "' has a bad checksum. Expected: " 206 + calcChecksum(sb.toString())); 207 } 208 } else if (getChecksumMode() == ChecksumMode.CP_IGNORE) { 209 return; 210 } else if (getChecksumMode() == ChecksumMode.CP_AUTO) { 211 return; } 213 } 214 215 220 public void generateBarcodeLogic(ClassicBarcodeLogicHandler logic, String msg) { 221 logic.startBarcode(msg); 222 223 StringBuffer sb = new StringBuffer (msg); 224 225 handleChecksum(sb); 226 227 logic.addBar(true, 2); 229 addIntercharacterGap(logic); 230 231 for (int i = 0; i < sb.length(); i++) { 233 final char ch = sb.charAt(i); 234 if (!isValidChar(ch)) { 235 throw new IllegalArgumentException ("Invalid character: " + ch); 236 } 237 encodeChar(logic, ch); 238 } 239 240 logic.addBar(true, 2); 242 243 logic.endBarcode(); 244 } 245 246 247 } 248 | Popular Tags |