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