001    /**
002     *  This library is free software; you can redistribute it and/or modify it
003     *  under the terms of the GNU Lesser General Public License (LGPL) as
004     *  published by the Free Software Foundation; either version 3.0 of the
005     *  License, or (at your option) any later version.
006     *
007     *  This library is distributed in the hope that it will be useful, but
008     *  WITHOUT ANY WARRANTY; without even the implied warranty of
009     *  MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010     *  Lesser General Public License for more details. 
011     */
012    
013    /**
014     * Title:        JBarcodeBean
015     * Description:  Barcode JavaBeans Component
016     * Copyright:    Copyright (C) 2004
017     * Company:      Dafydd Walters
018     */
019    package net.sourceforge.jbarcodebean.model;
020    
021    import net.sourceforge.jbarcodebean.BarcodeException;
022    
023    /**
024     * This class, which implements the {@link BarcodeStrategy} interface,
025     * knows how to encode the MSI barcode type.
026     */
027    public class MSI extends AbstractBarcodeStrategy implements java.io.Serializable {
028    
029      private static CharacterCode[] codes = {
030        new CharacterCode('1', new byte[] {1,2,1,2,1,2,2,1}, 1),
031        new CharacterCode('2', new byte[] {1,2,1,2,2,1,1,3}, 2),
032        new CharacterCode('3', new byte[] {1,2,1,2,2,1,2,1}, 3),
033        new CharacterCode('4', new byte[] {1,2,2,1,1,2,1,3}, 4),
034        new CharacterCode('5', new byte[] {1,2,2,1,1,2,2,1}, 5),
035        new CharacterCode('6', new byte[] {1,2,2,1,2,1,1,3}, 6),
036        new CharacterCode('7', new byte[] {1,2,2,1,2,1,2,1}, 7),
037        new CharacterCode('8', new byte[] {2,1,1,2,1,2,1,3}, 8),
038        new CharacterCode('9', new byte[] {2,1,1,2,1,2,2,1}, 9),
039        new CharacterCode('0', new byte[] {1,2,1,2,1,2,1,3}, 0),
040        new CharacterCode('A', new byte[] {2,1}, -1),     // Start
041        new CharacterCode('B', new byte[] {1,2,1}, -1)    // Stop
042      };
043    
044      /**
045       * Always returns {@link BarcodeStrategy#MANDATORY_CHECKSUM}.
046       */
047      public int requiresChecksum() {
048        // Checksum is mandatory
049        return MANDATORY_CHECKSUM;
050      }
051    
052      /**
053       * This implementation of <tt>getCodes</tt> returns an array of
054       * {@link AbstractBarcodeStrategy.CharacterCode CharacterCode} objects
055       * for the MSI format.
056       */
057      protected CharacterCode[] getCodes() {
058        return MSI.codes;
059      }
060    
061      /**
062       * Returns a String containing the checksum-encoded version of the text passed
063       * to the method.
064       * Start and End sentinels must NOT be included in the text passed to this method.
065       */
066      protected String augmentWithChecksum(String text) throws BarcodeException {
067        String newNum = "";
068        for (int i = text.length() - 1; i >= 0; i -= 2 ) {
069          newNum = text.charAt(i) + newNum;
070        }
071        int check1 = Integer.parseInt(newNum) * 2;
072        newNum = new Integer(check1).toString();
073        int check2 = 0;
074        for (int i = 0; i < newNum.length(); i++) {
075          check2 += Integer.parseInt(newNum.substring(i, i + 1));
076        }
077        for (int i = text.length() - 2; i >= 0; i -= 2 ) {
078          check2 += Integer.parseInt(text.substring(i, i + 1));
079        }
080        int checkDigit = (10 - ((check2) % 10)) % 10;
081        text = text + new Integer(checkDigit).toString();
082        return text;
083      }
084    
085      /**
086       * Does nothing except return the String passed to the method.
087       */
088      protected String postprocess(String text) {
089        return text;
090      }
091    
092      /**
093       * Does nothing except return the String passed to the method.
094       */
095      protected String preprocess(String text) {
096        return text;
097      }
098    
099      /**
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    }