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 the MSI (Modified Plessey) bar code symbology.
 *
 * <p>
 * MSI Plessey can encode a string of numeric digits and has a range of check digit options.
 *
 * @author <a href="mailto:rstuart114@gmail.com">Robin Stuart</a>
 */
public class MsiPlessey extends Symbol {

    public enum CheckDigit {
        NONE, MOD10, MOD10_MOD10, MOD11, MOD11_MOD10
    }

    private final static String[] MSI_PLESS_TABLE = { "12121212", "12121221", "12122112", "12122121", "12211212", "12211221", "12212112", "12212121", "21121212", "21121221" };

    private CheckDigit checkDigit = CheckDigit.NONE;

    /**
     * Set the check digit scheme to use. Options are: None, Modulo-10, 2 x Modulo-10, Modulo-11 and
     * Modulo-11 &amp; 10.
     *
     * @param checkDigit the type of check digit to add to symbol
     */
    public void setCheckDigit(final CheckDigit checkDigit) {
        this.checkDigit = checkDigit;
    }

    /**
     * Returns the check digit scheme being used.
     *
     * @return the check digit scheme being used
     */
    public CheckDigit getCheckDigit() {
        return this.checkDigit;
    }

    @Override
    protected void encode() {

        String intermediate;
        final int length = this.content.length();
        int i;
        String evenString;
        String oddString;
        String addupString;
        int spacer;
        int addup;
        int weight;
        int checkDigit1;
        int checkDigit2;

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

        intermediate = "21"; // Start
        for (i = 0; i < length; i++) {
            intermediate += MSI_PLESS_TABLE[Character.getNumericValue(this.content.charAt(i))];
        }

        this.readable = this.content;

        if (this.checkDigit == CheckDigit.MOD10 || this.checkDigit == CheckDigit.MOD10_MOD10) {
            /* Add Modulo-10 check digit */
            evenString = "";
            oddString = "";

            spacer = this.content.length() & 1;

            for (i = this.content.length() - 1; i >= 0; i--) {
                if (spacer == 1) {
                    if ((i & 1) != 0) {
                        evenString = this.content.charAt(i) + evenString;
                    } else {
                        oddString = this.content.charAt(i) + oddString;
                    }
                } else {
                    if ((i & 1) != 0) {
                        oddString = this.content.charAt(i) + oddString;
                    } else {
                        evenString = this.content.charAt(i) + evenString;
                    }
                }
            }

            if (oddString.length() == 0) {
                addupString = "0";
            } else {
                addupString = Integer.toString(Integer.parseInt(oddString) * 2);
            }

            addupString += evenString;

            addup = 0;
            for (i = 0; i < addupString.length(); i++) {
                addup += addupString.charAt(i) - '0';
            }

            checkDigit1 = 10 - addup % 10;
            if (checkDigit1 == 10) {
                checkDigit1 = 0;
            }

            intermediate += MSI_PLESS_TABLE[checkDigit1];
            this.readable += checkDigit1;
        }

        if (this.checkDigit == CheckDigit.MOD11 || this.checkDigit == CheckDigit.MOD11_MOD10) {
            /* Add a Modulo-11 check digit */
            weight = 2;
            addup = 0;
            for (i = this.content.length() - 1; i >= 0; i--) {
                addup += (this.content.charAt(i) - '0') * weight;
                weight++;

                if (weight == 8) {
                    weight = 2;
                }
            }

            checkDigit1 = 11 - addup % 11;

            if (checkDigit1 == 11) {
                checkDigit1 = 0;
            }

            this.readable += checkDigit1;
            if (checkDigit1 == 10) {
                intermediate += MSI_PLESS_TABLE[1];
                intermediate += MSI_PLESS_TABLE[0];
            } else {
                intermediate += MSI_PLESS_TABLE[checkDigit1];
            }
        }

        if (this.checkDigit == CheckDigit.MOD10_MOD10 || this.checkDigit == CheckDigit.MOD11_MOD10) {
            /* Add a second Modulo-10 check digit */
            evenString = "";
            oddString = "";

            spacer = this.readable.length() & 1;

            for (i = this.readable.length() - 1; i >= 0; i--) {
                if (spacer == 1) {
                    if ((i & 1) != 0) {
                        evenString = this.readable.charAt(i) + evenString;
                    } else {
                        oddString = this.readable.charAt(i) + oddString;
                    }
                } else {
                    if ((i & 1) != 0) {
                        oddString = this.readable.charAt(i) + oddString;
                    } else {
                        evenString = this.readable.charAt(i) + evenString;
                    }
                }
            }

            if (oddString.length() == 0) {
                addupString = "0";
            } else {
                addupString = Integer.toString(Integer.parseInt(oddString) * 2);
            }

            addupString += evenString;

            addup = 0;
            for (i = 0; i < addupString.length(); i++) {
                addup += addupString.charAt(i) - '0';
            }

            checkDigit2 = 10 - addup % 10;
            if (checkDigit2 == 10) {
                checkDigit2 = 0;
            }

            intermediate += MSI_PLESS_TABLE[checkDigit2];
            this.readable += checkDigit2;
        }

        intermediate += "121"; // Stop

        this.pattern = new String[] { intermediate };
        this.row_count = 1;
        this.row_height = new int[] { -1 };
    }
}