1 /***********************************************************************************************************************
2 * Copyright (c) 2003, International Barcode Consortium
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this list of
11 * conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 * Neither the name of the International Barcode Consortium nor the names of any contributors may be used to endorse
14 * or promote products derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 ***********************************************************************************************************************/
26
27 package net.sourceforge.barbecue.linear.twoOfFive;
28
29 import net.sourceforge.barbecue.BarcodeException;
30 import net.sourceforge.barbecue.BlankModule;
31 import net.sourceforge.barbecue.Module;
32 import net.sourceforge.barbecue.Modulo10;
33 import net.sourceforge.barbecue.SeparatorModule;
34 import net.sourceforge.barbecue.linear.LinearBarcode;
35
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.text.CharacterIterator;
39 import java.text.StringCharacterIterator;
40
41 /**
42 * This is a concrete implementation of the Interleave 2 of 5 barcode
43 *
44 * The Interleave 2 of 5 barcode requires an even number of characters. At
45 * the same time, a modulo 10 check digit can be used. The default constructor
46 * presumes that if the barcode is of an odd length, then a check digit is
47 * required. This will automaticaly be generated.
48 * If the constructor with the check digit flag is used, the check digit flag is
49 * flase and the length of the barcode is odd, then an exception will be thrown.
50 *
51 * @author <a href="mailto:james@metalskin.com">James Jenner</a>
52 */
53 public class Int2of5Barcode extends Std2of5Barcode {
54 /**
55 * Constructs a new Interleave 2 of 5 barcode with the specified data.
56 * No check digit will be added
57 * @param data The data to encode
58 * @throws BarcodeException If the data is invalid
59 */
60 public Int2of5Barcode(String data) throws BarcodeException {
61 this(data, false);
62 }
63
64 /**
65 * Constructs a new Interleave 2 of 5 barcode with thte specified data.
66 * @param data The data to encode
67 * @param checkDigit if true then a check digit is automaticaly appened to data
68 * @throws BarcodeException If the data is invalid
69 */
70 public Int2of5Barcode(String data, boolean checkDigit) throws BarcodeException {
71 super(checkDigit ? data + Modulo10.getMod10CheckDigit(data, 3) : data);
72 }
73
74 /**
75 * Returns the pre-amble for the barcode.
76 * @return A BlankModule
77 */
78 protected Module getPreAmble() {
79 return Int2of5ModuleFactory.START_CHAR;
80 }
81
82 /**
83 * Returns the post-amble for the barcode.
84 * @return A BlankModule
85 */
86 protected Module getPostAmble() {
87 return Int2of5ModuleFactory.END_CHAR;
88 }
89
90 /**
91 * Encodes the data of the barcode into bars.
92 * @return The encoded bar data
93 */
94 protected Module[] encodeData() {
95 List modules = new ArrayList();
96
97 for(int i = 0; i < data.length() - 1; i += 2) {
98 Module module = Int2of5ModuleFactory.getModule(String.valueOf(data.charAt(i)), String.valueOf(data.charAt(i + 1)));
99
100 modules.add(module);
101 }
102
103 return (Module[])modules.toArray(new Module[0]);
104 }
105
106 protected void validateData() throws BarcodeException {
107 if(data.length() % 2 != 0) {
108 throw new BarcodeException("The Interleave 2 of 5 encoding requires an even number of data");
109 }
110
111 super.validateData();
112 }
113 }