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
 
15
package uk.org.okapibarcode.backend;
16
 
17
/**
18
 * <p>
19
 * Implements the MSI (Modified Plessey) bar code symbology.
20
 *
21
 * <p>
22
 * MSI Plessey can encode a string of numeric digits and has a range of check digit options.
23
 *
24
 * @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
25
 */
26
public class MsiPlessey extends Symbol {
27
 
28
    public enum CheckDigit {
29
        NONE, MOD10, MOD10_MOD10, MOD11, MOD11_MOD10
30
    }
31
 
32
    private final static String[] MSI_PLESS_TABLE = { "12121212", "12121221", "12122112", "12122121", "12211212", "12211221", "12212112", "12212121", "21121212", "21121221" };
33
 
34
    private CheckDigit checkDigit = CheckDigit.NONE;
35
 
36
    /**
37
     * Set the check digit scheme to use. Options are: None, Modulo-10, 2 x Modulo-10, Modulo-11 and
38
     * Modulo-11 &amp; 10.
39
     *
40
     * @param checkDigit the type of check digit to add to symbol
41
     */
42
    public void setCheckDigit(final CheckDigit checkDigit) {
43
        this.checkDigit = checkDigit;
44
    }
45
 
46
    /**
47
     * Returns the check digit scheme being used.
48
     *
49
     * @return the check digit scheme being used
50
     */
51
    public CheckDigit getCheckDigit() {
52
        return this.checkDigit;
53
    }
54
 
55
    @Override
56
    protected void encode() {
57
 
58
        String intermediate;
59
        final int length = this.content.length();
60
        int i;
61
        String evenString;
62
        String oddString;
63
        String addupString;
64
        int spacer;
65
        int addup;
66
        int weight;
67
        int checkDigit1;
68
        int checkDigit2;
69
 
70
        if (!this.content.matches("[0-9]+")) {
71
            throw new OkapiException("Invalid characters in input");
72
        }
73
 
74
        intermediate = "21"; // Start
75
        for (i = 0; i < length; i++) {
76
            intermediate += MSI_PLESS_TABLE[Character.getNumericValue(this.content.charAt(i))];
77
        }
78
 
79
        this.readable = this.content;
80
 
81
        if (this.checkDigit == CheckDigit.MOD10 || this.checkDigit == CheckDigit.MOD10_MOD10) {
82
            /* Add Modulo-10 check digit */
83
            evenString = "";
84
            oddString = "";
85
 
86
            spacer = this.content.length() & 1;
87
 
88
            for (i = this.content.length() - 1; i >= 0; i--) {
89
                if (spacer == 1) {
90
                    if ((i & 1) != 0) {
91
                        evenString = this.content.charAt(i) + evenString;
92
                    } else {
93
                        oddString = this.content.charAt(i) + oddString;
94
                    }
95
                } else {
96
                    if ((i & 1) != 0) {
97
                        oddString = this.content.charAt(i) + oddString;
98
                    } else {
99
                        evenString = this.content.charAt(i) + evenString;
100
                    }
101
                }
102
            }
103
 
104
            if (oddString.length() == 0) {
105
                addupString = "0";
106
            } else {
107
                addupString = Integer.toString(Integer.parseInt(oddString) * 2);
108
            }
109
 
110
            addupString += evenString;
111
 
112
            addup = 0;
113
            for (i = 0; i < addupString.length(); i++) {
114
                addup += addupString.charAt(i) - '0';
115
            }
116
 
117
            checkDigit1 = 10 - addup % 10;
118
            if (checkDigit1 == 10) {
119
                checkDigit1 = 0;
120
            }
121
 
122
            intermediate += MSI_PLESS_TABLE[checkDigit1];
123
            this.readable += checkDigit1;
124
        }
125
 
126
        if (this.checkDigit == CheckDigit.MOD11 || this.checkDigit == CheckDigit.MOD11_MOD10) {
127
            /* Add a Modulo-11 check digit */
128
            weight = 2;
129
            addup = 0;
130
            for (i = this.content.length() - 1; i >= 0; i--) {
131
                addup += (this.content.charAt(i) - '0') * weight;
132
                weight++;
133
 
134
                if (weight == 8) {
135
                    weight = 2;
136
                }
137
            }
138
 
139
            checkDigit1 = 11 - addup % 11;
140
 
141
            if (checkDigit1 == 11) {
142
                checkDigit1 = 0;
143
            }
144
 
145
            this.readable += checkDigit1;
146
            if (checkDigit1 == 10) {
147
                intermediate += MSI_PLESS_TABLE[1];
148
                intermediate += MSI_PLESS_TABLE[0];
149
            } else {
150
                intermediate += MSI_PLESS_TABLE[checkDigit1];
151
            }
152
        }
153
 
154
        if (this.checkDigit == CheckDigit.MOD10_MOD10 || this.checkDigit == CheckDigit.MOD11_MOD10) {
155
            /* Add a second Modulo-10 check digit */
156
            evenString = "";
157
            oddString = "";
158
 
159
            spacer = this.readable.length() & 1;
160
 
161
            for (i = this.readable.length() - 1; i >= 0; i--) {
162
                if (spacer == 1) {
163
                    if ((i & 1) != 0) {
164
                        evenString = this.readable.charAt(i) + evenString;
165
                    } else {
166
                        oddString = this.readable.charAt(i) + oddString;
167
                    }
168
                } else {
169
                    if ((i & 1) != 0) {
170
                        oddString = this.readable.charAt(i) + oddString;
171
                    } else {
172
                        evenString = this.readable.charAt(i) + evenString;
173
                    }
174
                }
175
            }
176
 
177
            if (oddString.length() == 0) {
178
                addupString = "0";
179
            } else {
180
                addupString = Integer.toString(Integer.parseInt(oddString) * 2);
181
            }
182
 
183
            addupString += evenString;
184
 
185
            addup = 0;
186
            for (i = 0; i < addupString.length(); i++) {
187
                addup += addupString.charAt(i) - '0';
188
            }
189
 
190
            checkDigit2 = 10 - addup % 10;
191
            if (checkDigit2 == 10) {
192
                checkDigit2 = 0;
193
            }
194
 
195
            intermediate += MSI_PLESS_TABLE[checkDigit2];
196
            this.readable += checkDigit2;
197
        }
198
 
199
        intermediate += "121"; // Stop
200
 
201
        this.pattern = new String[] { intermediate };
202
        this.row_count = 1;
203
        this.row_height = new int[] { -1 };
204
    }
205
}