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 UPCALogicImpl extends UPCEANLogicImpl { 70 71 75 public UPCALogicImpl(ChecksumMode mode) { 76 super(mode); 77 } 78 79 84 public static void validateMessage(String msg) { 85 UPCEANLogicImpl.validateMessage(msg); 86 if ((msg.length() < 11) || (msg.length() > 12)) { 87 throw new IllegalArgumentException ( 88 "Message must be 11 or 12 characters long. Message: " + msg); 89 } 90 } 91 92 98 public static String handleChecksum(String msg, ChecksumMode mode) { 99 if (mode == ChecksumMode.CP_AUTO) { 100 if (msg.length() == 11) { 101 mode = ChecksumMode.CP_ADD; 102 } else if (msg.length() == 12) { 103 mode = ChecksumMode.CP_CHECK; 104 } else { 105 throw new RuntimeException ("Internal error"); 107 } 108 } 109 if (mode == ChecksumMode.CP_ADD) { 110 if (msg.length() != 11) { 111 throw new IllegalArgumentException ( 112 "Message must be 11 characters long"); 113 } 114 return msg + calcChecksum(msg); 115 } else if (mode == ChecksumMode.CP_CHECK) { 116 if (msg.length() != 12) { 117 throw new IllegalArgumentException ( 118 "Message must be 12 characters long"); 119 } 120 char check = msg.charAt(11); 121 char expected = calcChecksum(msg.substring(0, 11)); 122 if (check != expected) { 123 throw new IllegalArgumentException ( 124 "Checksum is bad (" + check + "). Expected: " + expected); 125 } 126 return msg; 127 } else if (mode == ChecksumMode.CP_IGNORE) { 128 if (msg.length() != 12) { 129 throw new IllegalArgumentException ( 130 "Message must be 12 characters long"); 131 } 132 return msg; 133 } else { 134 throw new UnsupportedOperationException ( 135 "Unknown checksum mode: " + mode); 136 } 137 } 138 139 private String handleChecksum(String msg) { 140 return handleChecksum(msg, getChecksumMode()); 141 } 142 143 144 public void generateBarcodeLogic(ClassicBarcodeLogicHandler logic, String msg) { 145 String supp = retrieveSupplemental(msg); 146 String s = removeSupplemental(msg); 147 validateMessage(s); 148 s = handleChecksum(s); 149 150 String canonicalMessage = s; 151 if (supp != null) { 152 canonicalMessage = canonicalMessage + "+" + supp; 153 } 154 logic.startBarcode(canonicalMessage); 155 156 drawSideGuard(logic); 158 159 final char lead = s.charAt(0); 161 logic.startBarGroup(BarGroup.UPC_EAN_LEAD, new Character (lead).toString()); 162 encodeChar(logic, lead, LEFT_HAND_A); 163 logic.endBarGroup(); 164 165 logic.startBarGroup(BarGroup.UPC_EAN_GROUP, s.substring(1, 6)); 166 167 for (int i = 1; i < 6; i++) { 169 encodeChar(logic, s.charAt(i), LEFT_HAND_A); 170 } 171 172 logic.endBarGroup(); 173 174 drawCenterGuard(logic); 176 177 logic.startBarGroup(BarGroup.UPC_EAN_GROUP, s.substring(6, 11)); 178 179 for (int i = 6; i < 11; i++) { 181 encodeChar(logic, s.charAt(i), RIGHT_HAND); 182 } 183 184 logic.endBarGroup(); 185 186 final char check = s.charAt(11); 188 logic.startBarGroup(BarGroup.UPC_EAN_CHECK, new Character (check).toString()); 189 encodeChar(logic, check, RIGHT_HAND); 190 logic.endBarGroup(); 191 192 drawSideGuard(logic); 194 195 if (supp != null) { 197 drawSupplemental(logic, supp); 198 } 199 logic.endBarcode(); 200 } 201 202 } 203 | Popular Tags |