View Javadoc

1   /**
2    *  This library is free software; you can redistribute it and/or modify it
3    *  under the terms of the GNU Lesser General Public License (LGPL) as
4    *  published by the Free Software Foundation; either version 3.0 of the
5    *  License, or (at your option) any later version.
6    *
7    *  This library is distributed in the hope that it will be useful, but
8    *  WITHOUT ANY WARRANTY; without even the implied warranty of
9    *  MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10   *  Lesser General Public License for more details. 
11   */
12  
13  /**
14   * Title:        JBarcodeBean
15   * Description:  Barcode JavaBeans Component
16   * Copyright:    Copyright (C) 2004
17   * Company:      Dafydd Walters
18   */
19  package net.sourceforge.jbarcodebean.model;
20  
21  import net.sourceforge.jbarcodebean.BarcodeException;
22  
23  /**
24   * This class, which implements the {@link BarcodeStrategy} interface,
25   * knows how to encode the 3:1 (wide)
26   * variant of the Interleaved Code 2 of 5 barcode type.
27   */
28  public class Interleaved25 extends AbstractBarcodeStrategy implements java.io.Serializable {
29  
30    private static CharacterCode[] codes = {
31      new CharacterCode('1', new byte[] {3,1,1,1,3}, 1),
32      new CharacterCode('2', new byte[] {1,3,1,1,3}, 2),
33      new CharacterCode('3', new byte[] {3,3,1,1,1}, 3),
34      new CharacterCode('4', new byte[] {1,1,3,1,3}, 4),
35      new CharacterCode('5', new byte[] {3,1,3,1,1}, 5),
36      new CharacterCode('6', new byte[] {1,3,3,1,1}, 6),
37      new CharacterCode('7', new byte[] {1,1,1,3,3}, 7),
38      new CharacterCode('8', new byte[] {3,1,1,3,1}, 8),
39      new CharacterCode('9', new byte[] {1,3,1,3,1}, 9),
40      new CharacterCode('0', new byte[] {1,1,3,3,1}, 0),
41      new CharacterCode('A', new byte[] {1,1,1,1}, -1),   // Start
42      new CharacterCode('B', new byte[] {3,1,1}, -1)      // Stop
43    };
44  
45    /**
46     * Always returns {@link BarcodeStrategy#OPTIONAL_CHECKSUM}.
47     */
48    public int requiresChecksum() {
49      // Checksum is not mandatory
50      return OPTIONAL_CHECKSUM;
51    }
52  
53    /**
54     * This implementation of <tt>getCodes</tt> returns an array of
55     * {@link AbstractBarcodeStrategy.CharacterCode CharacterCode} objects
56     * for the wide Interleaved Code25 format.
57     */
58    protected CharacterCode[] getCodes() {
59      return Interleaved25.codes;
60    }
61  
62    /**
63     * Returns a String containing the checksum-encoded version of the text passed
64     * to the method.
65     * Start and End sentinels must NOT be included in the text passed to this method.
66     */
67    protected String augmentWithChecksum(String text) throws BarcodeException {
68      int check1 = 0;
69      int check2 = 0;
70      CharacterCode cc;
71      for (int i = 0; i < text.length(); i++) {
72        char ch = text.charAt(i);
73        cc = getCharacterCode(ch);            // get code by character
74        if (cc == null) {
75          throw new BarcodeException("Invalid character in barcode");
76        }
77        // Exclude start and end sentinels from checksum
78        if (cc.check > 0) {
79          if ((i + text.length()) % 2 == 0) {
80            check1 += cc.check;
81          } else {
82            check2 += cc.check;
83          }
84        }
85      }
86      check2 *= 3;
87      int checkDigit = (10 - ((check1 + check2) % 10)) % 10;
88      text = text + new Integer(checkDigit).toString();
89      return text;
90    }
91  
92    /**
93     * Adds a leading zero if the length of <tt>text</tt> is odd.
94     */
95    protected String postprocess(String text) {
96      if (text.length() % 2 != 0) {
97        // Length is odd; add a leading zero.
98        text = "0" + text;
99      }
100     return text;
101   }
102 
103   /**
104    * No preprocessing performed. <tt>text</tt> is returned unmodified.
105    */
106   protected String preprocess(String text) {
107     return text;
108   }
109 
110   /**
111    * Always returns <tt>true</tt>.
112    */
113   protected boolean isInterleaved() {
114     return true;
115   }
116 
117   /**
118    * Always returns 'A'.
119    */
120   protected char getStartSentinel() {
121     return 'A';
122   }
123 
124   /**
125    * Always returns 'B'.
126    */
127   protected char getStopSentinel() {
128     return 'B';
129   }
130 
131   /**
132    * Always returns 11 (eleven).
133    */
134   protected byte getMarginWidth() {
135     return 11;
136   }
137 
138   /**
139    * Returns <tt>text</tt> unmodified.
140    */
141   protected String getBarcodeLabelText(String text) {
142     return text;
143   }
144 
145 }