KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > impl > UPCALogicImpl


1 /*
2  * $Id: UPCALogicImpl.java,v 1.8 2003/06/11 09:25:12 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

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 /**
65  * This class is an implementation of the UPC-A barcode.
66  *
67  * @author Jeremias Maerki
68  */

69 public class UPCALogicImpl extends UPCEANLogicImpl {
70
71     /**
72      * Main constructor
73      * @param mode the checksum mode
74      */

75     public UPCALogicImpl(ChecksumMode mode) {
76         super(mode);
77     }
78
79     /**
80      * Validates a UPC-A message. The method throws IllegalArgumentExceptions
81      * if an invalid message is passed.
82      * @param msg the message to validate
83      */

84     public static void validateMessage(String JavaDoc msg) {
85         UPCEANLogicImpl.validateMessage(msg);
86         if ((msg.length() < 11) || (msg.length() > 12)) {
87             throw new IllegalArgumentException JavaDoc(
88                 "Message must be 11 or 12 characters long. Message: " + msg);
89         }
90     }
91     
92     /**
93      * Does checksum processing according to the checksum mode.
94      * @param msg the message to process
95      * @param mode the checksum mode
96      * @return the possibly modified message
97      */

98     public static String JavaDoc handleChecksum(String JavaDoc 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                 //Shouldn't happen because of validateMessage
106
throw new RuntimeException JavaDoc("Internal error");
107             }
108         }
109         if (mode == ChecksumMode.CP_ADD) {
110             if (msg.length() != 11) {
111                 throw new IllegalArgumentException JavaDoc(
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 JavaDoc(
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 JavaDoc(
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 JavaDoc(
130                     "Message must be 12 characters long");
131             }
132             return msg;
133         } else {
134             throw new UnsupportedOperationException JavaDoc(
135                 "Unknown checksum mode: " + mode);
136         }
137     }
138
139     private String JavaDoc handleChecksum(String JavaDoc msg) {
140         return handleChecksum(msg, getChecksumMode());
141     }
142     
143     /** @see org.krysalis.barcode.impl.UPCEANLogicImpl */
144     public void generateBarcodeLogic(ClassicBarcodeLogicHandler logic, String JavaDoc msg) {
145         String JavaDoc supp = retrieveSupplemental(msg);
146         String JavaDoc s = removeSupplemental(msg);
147         validateMessage(s);
148         s = handleChecksum(s);
149
150         String JavaDoc canonicalMessage = s;
151         if (supp != null) {
152             canonicalMessage = canonicalMessage + "+" + supp;
153         }
154         logic.startBarcode(canonicalMessage);
155         
156         //Left guard
157
drawSideGuard(logic);
158
159         //Number system character
160
final char lead = s.charAt(0);
161         logic.startBarGroup(BarGroup.UPC_EAN_LEAD, new Character JavaDoc(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         //First five data characters
168
for (int i = 1; i < 6; i++) {
169             encodeChar(logic, s.charAt(i), LEFT_HAND_A);
170         }
171         
172         logic.endBarGroup();
173
174         //Center guard
175
drawCenterGuard(logic);
176
177         logic.startBarGroup(BarGroup.UPC_EAN_GROUP, s.substring(6, 11));
178         
179         //Last five data characters
180
for (int i = 6; i < 11; i++) {
181             encodeChar(logic, s.charAt(i), RIGHT_HAND);
182         }
183
184         logic.endBarGroup();
185
186         //Checksum
187
final char check = s.charAt(11);
188         logic.startBarGroup(BarGroup.UPC_EAN_CHECK, new Character JavaDoc(check).toString());
189         encodeChar(logic, check, RIGHT_HAND);
190         logic.endBarGroup();
191
192         //Right guard
193
drawSideGuard(logic);
194         
195         //Optional Supplemental
196
if (supp != null) {
197             drawSupplemental(logic, supp);
198         }
199         logic.endBarcode();
200     }
201
202 }
203
Popular Tags