OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Copyright 2014 Robin Stuart
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package uk.org.okapibarcode.backend;

/**
 * <p>
 * Implements Code 32, also known as Italian Pharmacode, A variation of Code 39 used by the Italian
 * Ministry of Health ("Ministero della Sanità")
 *
 * <p>
 * Requires a numeric input up to 8 digits in length. Check digit is calculated.
 *
 * @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
 */
public class Code32 extends Symbol {

    private static final char[] TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
            'Z' };

    @Override
    protected void encode() {
        int i, checksum, checkpart, checkdigit;
        int pharmacode, remainder, devisor;
        String localstr, risultante;
        final int[] codeword = new int[6];
        final Code3Of9 c39 = new Code3Of9();

        if (this.content.length() > 8) {
            throw new OkapiException("Input too long");
        }

        if (!this.content.matches("[0-9]+")) {
            throw new OkapiException("Invalid characters in input");
        }

        /* Add leading zeros as required */
        localstr = "";
        for (i = this.content.length(); i < 8; i++) {
            localstr += "0";
        }
        localstr += this.content;

        /* Calculate the check digit */
        checksum = 0;
        checkpart = 0;
        for (i = 0; i < 4; i++) {
            checkpart = Character.getNumericValue(localstr.charAt(i * 2));
            checksum += checkpart;
            checkpart = 2 * Character.getNumericValue(localstr.charAt(i * 2 + 1));
            if (checkpart >= 10) {
                checksum += checkpart - 10 + 1;
            } else {
                checksum += checkpart;
            }
        }

        /* Add check digit to data string */
        checkdigit = checksum % 10;
        final char check = (char) (checkdigit + '0');
        localstr += check;
        infoLine("Check Digit: " + check);

        /* Convert string into an integer value */
        pharmacode = 0;
        for (i = 0; i < localstr.length(); i++) {
            pharmacode *= 10;
            pharmacode += Character.getNumericValue(localstr.charAt(i));
        }

        /* Convert from decimal to base-32 */
        devisor = 33554432;
        for (i = 5; i >= 0; i--) {
            codeword[i] = pharmacode / devisor;
            remainder = pharmacode % devisor;
            pharmacode = remainder;
            devisor /= 32;
        }

        /* Look up values in 'Tabella di conversione' */
        risultante = "";
        for (i = 5; i >= 0; i--) {
            risultante += TABLE[codeword[i]];
        }

        /* Plot the barcode using Code 39 */

        this.readable = "A" + localstr;
        this.pattern = new String[1];
        this.row_count = 1;
        this.row_height = new int[] { -1 };
        infoLine("Code 39 Equivalent: " + risultante);

        c39.setContent(risultante);
        this.pattern[0] = c39.pattern[0];
    }
}