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    /**
022     * This class, which implements the {@link BarcodeStrategy} interface,
023     * knows how to encode the 3:1 (wide)
024     * variant of the Extended Code 3 of 9 barcode type.
025     */
026    public class ExtendedCode39 extends BaseCode39 implements java.io.Serializable {
027    
028      private static Conversion[] conversions = {
029        new Conversion('\u0000', "%U"),
030        new Conversion('\u0001', "$A"),
031        new Conversion('\u0002', "$B"),
032        new Conversion('\u0003', "$C"),
033        new Conversion('\u0004', "$D"),
034        new Conversion('\u0005', "$E"),
035        new Conversion('\u0006', "$F"),
036        new Conversion('\u0007', "$G"),
037        new Conversion('\b', "$H"),
038        new Conversion('\t', "$I"),
039        new Conversion('\n', "$J"),
040        new Conversion('\u000B', "$K"),
041        new Conversion('\f', "$L"),
042        new Conversion('\r', "$M"),
043        new Conversion('\u000E', "$N"),
044        new Conversion('\u000F', "$O"),
045        new Conversion('\u0010', "$P"),
046        new Conversion('\u0011', "$Q"),
047        new Conversion('\u0012', "$R"),
048        new Conversion('\u0013', "$S"),
049        new Conversion('\u0014', "$T"),
050        new Conversion('\u0015', "$U"),
051        new Conversion('\u0016', "$V"),
052        new Conversion('\u0017', "$W"),
053        new Conversion('\u0018', "$X"),
054        new Conversion('\u0019', "$Y"),
055        new Conversion('\u001A', "$Z"),
056        new Conversion('\u001B', "%A"),
057        new Conversion('\u001C', "%B"),
058        new Conversion('\u001D', "%C"),
059        new Conversion('\u001E', "%D"),
060        new Conversion('\u001F', "%E"),
061        // Space is not converted
062        new Conversion('!', "/A"),
063        new Conversion('"', "/B"),
064        new Conversion('#', "/C"),
065        new Conversion('$', "/D"),
066        new Conversion('%', "/E"),
067        new Conversion('&', "/F"),
068        new Conversion('\'', "/G"),
069        new Conversion('(', "/H"),
070        new Conversion(')', "/I"),
071        new Conversion('*', "/J"),
072        new Conversion('+', "/K"),
073        new Conversion(',', "/L"),
074        new Conversion('-', "/M"),
075        new Conversion('.', "/N"),
076        new Conversion('/', "/O"),
077        // Digits 0 through 9 not converted
078        new Conversion(':', "/Z"),
079        new Conversion(';', "%F"),
080        new Conversion('<', "%G"),
081        new Conversion('=', "%H"),
082        new Conversion('>', "%I"),
083        new Conversion('?', "%J"),
084        new Conversion('@', "%V"),
085        // Upper case letters A through Z not converted
086        new Conversion('[', "%K"),
087        new Conversion('\\', "%L"),
088        new Conversion(']', "%M"),
089        new Conversion('^', "%N"),
090        new Conversion('_', "%O"),
091        new Conversion('`', "%W"),
092        new Conversion('a', "+A"),
093        new Conversion('b', "+B"),
094        new Conversion('c', "+C"),
095        new Conversion('d', "+D"),
096        new Conversion('e', "+E"),
097        new Conversion('f', "+F"),
098        new Conversion('g', "+G"),
099        new Conversion('h', "+H"),
100        new Conversion('i', "+I"),
101        new Conversion('j', "+J"),
102        new Conversion('k', "+K"),
103        new Conversion('l', "+L"),
104        new Conversion('m', "+M"),
105        new Conversion('n', "+N"),
106        new Conversion('o', "+O"),
107        new Conversion('p', "+P"),
108        new Conversion('q', "+Q"),
109        new Conversion('r', "+R"),
110        new Conversion('s', "+S"),
111        new Conversion('t', "+T"),
112        new Conversion('u', "+U"),
113        new Conversion('v', "+V"),
114        new Conversion('w', "+W"),
115        new Conversion('x', "+X"),
116        new Conversion('y', "+Y"),
117        new Conversion('z', "+Z"),
118        new Conversion('{', "%P"),
119        new Conversion('|', "%Q"),
120        new Conversion('}', "%R"),
121        new Conversion('~', "%S"),
122        new Conversion('\u007F', "%T")
123      };
124    
125      /**
126       * This implementation of <tt>getCodes</tt> returns {@link BaseCode39#codes}.
127       */
128      protected CharacterCode[] getCodes() {
129        return BaseCode39.codes;
130      }
131    
132      /**
133       * Converts ASCII to Code 39 characters.
134       */
135      protected String preprocess(String text) {
136        // Convert ASCII to extended Code 39 codes.
137        String toText = "";
138        int len = text.length();
139        for (int i = 0; i < len; i++) {
140          char ch = text.charAt(i);
141          boolean found = false;
142          for (int j = 0; j < conversions.length; j++) {
143            if (ch == conversions[j].from) {
144              toText += conversions[j].to;
145              found = true;
146              break;
147            }
148          }
149          if (!found) {
150            toText += ch;
151          }
152        }
153        return toText;
154      }
155    
156      /**
157       * Returns the <tt>text</tt> parameter unmodified.
158       */
159      protected String getBarcodeLabelText(String text) {
160        return text;
161      }
162    
163      private static class Conversion {
164        char from;
165        String to;
166        public Conversion(char from, String to) {
167          this.from = from;
168          this.to = to;
169        }
170      }
171    }