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 MSI barcode type.
26   */
27  public class MSI extends AbstractBarcodeStrategy implements java.io.Serializable {
28  
29    private static CharacterCode[] codes = {
30      new CharacterCode('1', new byte[] {1,2,1,2,1,2,2,1}, 1),
31      new CharacterCode('2', new byte[] {1,2,1,2,2,1,1,3}, 2),
32      new CharacterCode('3', new byte[] {1,2,1,2,2,1,2,1}, 3),
33      new CharacterCode('4', new byte[] {1,2,2,1,1,2,1,3}, 4),
34      new CharacterCode('5', new byte[] {1,2,2,1,1,2,2,1}, 5),
35      new CharacterCode('6', new byte[] {1,2,2,1,2,1,1,3}, 6),
36      new CharacterCode('7', new byte[] {1,2,2,1,2,1,2,1}, 7),
37      new CharacterCode('8', new byte[] {2,1,1,2,1,2,1,3}, 8),
38      new CharacterCode('9', new byte[] {2,1,1,2,1,2,2,1}, 9),
39      new CharacterCode('0', new byte[] {1,2,1,2,1,2,1,3}, 0),
40      new CharacterCode('A', new byte[] {2,1}, -1),     // Start
41      new CharacterCode('B', new byte[] {1,2,1}, -1)    // Stop
42    };
43  
44    /**
45     * Always returns {@link BarcodeStrategy#MANDATORY_CHECKSUM}.
46     */
47    public int requiresChecksum() {
48      // Checksum is mandatory
49      return MANDATORY_CHECKSUM;
50    }
51  
52    /**
53     * This implementation of <tt>getCodes</tt> returns an array of
54     * {@link AbstractBarcodeStrategy.CharacterCode CharacterCode} objects
55     * for the MSI format.
56     */
57    protected CharacterCode[] getCodes() {
58      return MSI.codes;
59    }
60  
61    /**
62     * Returns a String containing the checksum-encoded version of the text passed
63     * to the method.
64     * Start and End sentinels must NOT be included in the text passed to this method.
65     */
66    protected String augmentWithChecksum(String text) throws BarcodeException {
67      String newNum = "";
68      for (int i = text.length() - 1; i >= 0; i -= 2 ) {
69        newNum = text.charAt(i) + newNum;
70      }
71      int check1 = Integer.parseInt(newNum) * 2;
72      newNum = new Integer(check1).toString();
73      int check2 = 0;
74      for (int i = 0; i < newNum.length(); i++) {
75        check2 += Integer.parseInt(newNum.substring(i, i + 1));
76      }
77      for (int i = text.length() - 2; i >= 0; i -= 2 ) {
78        check2 += Integer.parseInt(text.substring(i, i + 1));
79      }
80      int checkDigit = (10 - ((check2) % 10)) % 10;
81      text = text + new Integer(checkDigit).toString();
82      return text;
83    }
84  
85    /**
86     * Does nothing except return the String passed to the method.
87     */
88    protected String postprocess(String text) {
89      return text;
90    }
91  
92    /**
93     * Does nothing except return the String passed to the method.
94     */
95    protected String preprocess(String text) {
96      return text;
97    }
98  
99    /**
100    * Always returns <tt>false</tt>
101    */
102   protected boolean isInterleaved() {
103     return false;
104   }
105 
106   /**
107    * Always returns 'A'.
108    */
109   protected char getStartSentinel() {
110     return 'A';
111   }
112 
113   /**
114    * Always returns 'B'.
115    */
116   protected char getStopSentinel() {
117     return 'B';
118   }
119 
120   /**
121    * Always returns 11 (eleven).
122    */
123   protected byte getMarginWidth() {
124     return 11;
125   }
126 
127   /**
128    * Does nothing except return the String passed to the method.
129    */
130   protected String getBarcodeLabelText(String text) {
131     return text;
132   }
133 
134 }