OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
181 ilm 1
/*
2
 * Copyright 2014 Robin Stuart
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
 * in compliance with the License. You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software distributed under the License
10
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
 * or implied. See the License for the specific language governing permissions and limitations under
12
 * the License.
13
 */
14
package uk.org.okapibarcode.backend;
15
 
16
/**
17
 * <p>
18
 * Implements Code 32, also known as Italian Pharmacode, A variation of Code 39 used by the Italian
19
 * Ministry of Health ("Ministero della Sanità")
20
 *
21
 * <p>
22
 * Requires a numeric input up to 8 digits in length. Check digit is calculated.
23
 *
24
 * @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
25
 */
26
public class Code32 extends Symbol {
27
 
28
    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',
29
            'Z' };
30
 
31
    @Override
32
    protected void encode() {
33
        int i, checksum, checkpart, checkdigit;
34
        int pharmacode, remainder, devisor;
35
        String localstr, risultante;
36
        final int[] codeword = new int[6];
37
        final Code3Of9 c39 = new Code3Of9();
38
 
39
        if (this.content.length() > 8) {
40
            throw new OkapiException("Input too long");
41
        }
42
 
43
        if (!this.content.matches("[0-9]+")) {
44
            throw new OkapiException("Invalid characters in input");
45
        }
46
 
47
        /* Add leading zeros as required */
48
        localstr = "";
49
        for (i = this.content.length(); i < 8; i++) {
50
            localstr += "0";
51
        }
52
        localstr += this.content;
53
 
54
        /* Calculate the check digit */
55
        checksum = 0;
56
        checkpart = 0;
57
        for (i = 0; i < 4; i++) {
58
            checkpart = Character.getNumericValue(localstr.charAt(i * 2));
59
            checksum += checkpart;
60
            checkpart = 2 * Character.getNumericValue(localstr.charAt(i * 2 + 1));
61
            if (checkpart >= 10) {
62
                checksum += checkpart - 10 + 1;
63
            } else {
64
                checksum += checkpart;
65
            }
66
        }
67
 
68
        /* Add check digit to data string */
69
        checkdigit = checksum % 10;
70
        final char check = (char) (checkdigit + '0');
71
        localstr += check;
72
        infoLine("Check Digit: " + check);
73
 
74
        /* Convert string into an integer value */
75
        pharmacode = 0;
76
        for (i = 0; i < localstr.length(); i++) {
77
            pharmacode *= 10;
78
            pharmacode += Character.getNumericValue(localstr.charAt(i));
79
        }
80
 
81
        /* Convert from decimal to base-32 */
82
        devisor = 33554432;
83
        for (i = 5; i >= 0; i--) {
84
            codeword[i] = pharmacode / devisor;
85
            remainder = pharmacode % devisor;
86
            pharmacode = remainder;
87
            devisor /= 32;
88
        }
89
 
90
        /* Look up values in 'Tabella di conversione' */
91
        risultante = "";
92
        for (i = 5; i >= 0; i--) {
93
            risultante += TABLE[codeword[i]];
94
        }
95
 
96
        /* Plot the barcode using Code 39 */
97
 
98
        this.readable = "A" + localstr;
99
        this.pattern = new String[1];
100
        this.row_count = 1;
101
        this.row_height = new int[] { -1 };
102
        infoLine("Code 39 Equivalent: " + risultante);
103
 
104
        c39.setContent(risultante);
105
        this.pattern[0] = c39.pattern[0];
106
    }
107
}