⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ean128ai.java

📁 生成二维条形码的java程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2005 Dietmar B黵kle.
 * 
 * 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 org.krysalis.barcode4j.impl.code128;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

/**
 * This class keeps Informations about EAN 128 Application Identifiers (AIs).
 * 
 * @author Dietmar B黵kle
 */
public class EAN128AI {
    
    public final static byte CONSTLenMax = 48; // Max according to EAN128 specification.
    public final static byte TYPEAlphaNum = 0;
    public final static byte TYPENum = 1;
    public final static byte TYPEAlpha = 2;    //Unused at the moment, but mentioned by  
                                                //the EAN128 specification.
    public final static byte TYPENumDate = 3;
    public final static byte TYPEError = 4;
    public final static byte TYPECD = 5; //Check digit
    private final static String[] typeToString = {"an", "n", "a", "d", "e", "cd"};

//    public final static byte TYPEAlphaNum421 = 7;


    String id;
    byte lenID, lenMinAll, lenMaxAll;
    byte minLenAfterVariableLen;
    
    byte[] lenMin, lenMax, type, checkDigitStart;
    boolean fixed = false, canDoChecksumADD = false;

    private static String[] fixedLenTable = new String[]{
        "00", "01", "02", "03", "04", 
        "11", "12", "13", "14", "15", "16", "17", "18", "19", 
        "20", 
        "31", "32", "33", "34", "35", "36", 
        "41"};
    private static byte[] fixedLenValueTable = new byte[]{
        20, 16, 16, 16, 18, 
        8, 8, 8, 8, 8, 8, 8, 8, 8, 
        4, 
        10, 10, 10, 10, 10, 10, 
        16};
    private static EAN128AI dft = parseSpecPrivate("xx", "an1-48");
    private static Object[] aiTable = new Object[] 
                                        {dft, dft, dft, dft, dft, dft, dft, dft, dft, dft};
    private static boolean propertiesLoaded = false;

    
    private static class AIProperties extends Properties {
        public synchronized Object put(Object arg0, Object arg1) {
            EAN128AI ai = parseSpecPrivate((String)arg0, (String)arg1);
            try { 
                setAI((String)arg0, ai);
            } catch (Exception e) {
                System.err.println(e);
            }
            return super.put(arg0, arg1);
        }
    }
    
    private static void initFixedLen(String aiName, byte aiLen) {
        byte lenID = (byte)aiName.length();
        EAN128AI ai = new EAN128AI(aiName, "an" + aiLen, lenID, TYPEAlphaNum, aiLen);
        try { 
            setAI(aiName, ai);
        } catch (Exception e) {
            System.err.println(e);
        }
    }
    
    static {
        for (int i = 0; i <= 9; i++) {
            initFixedLen("23" + i, (byte)(1 + 9 * i));
        }
        for (int i = fixedLenValueTable.length - 1; i >= 0; i--) {
            initFixedLen(fixedLenTable[i], (byte)(fixedLenValueTable[i] - 2));
        }
//        loadProperties();
    }
    
    public static synchronized void loadProperties() throws Exception {
        if (propertiesLoaded) return;

        final String bundlename = "EAN128AIs"; 
        final String filename = bundlename + ".properties"; 
        Properties p = new AIProperties();
        try {
            InputStream is = EAN128AI.class.getResourceAsStream(filename);
            if (is == null) {
//               System.err.println(filename + " could not be loaded with class.getResourceAsStream()");
               is = EAN128AI.class.getClassLoader().getResourceAsStream(filename);
            }
            if (is != null) {
                try {
                    p.load(is);
                } finally {
                    is.close();
                }
            } else {
//                System.err.println(filename + " could not be loaded with getClassLoader().getResourceAsStream()");
                // The getResourceAsStream variants do not work if an applet is loading 
                // several jars from different directories (as in examples\demo-applet\html\index.html)!
                // ResourceBundle does this job. It seems to have more privileges. 
                // It is not the best choice (as we never want to translate EAN128AIs.properties),
                // but it works.
                String rbName = EAN128AI.class.getPackage().getName() + "." + bundlename;
                ResourceBundle rb = ResourceBundle.getBundle(rbName);
                Enumeration keys = rb.getKeys();
                while (keys.hasMoreElements()){
                    String key = (String) keys.nextElement();
                    p.put(key, rb.getObject(key));
                }
            }
        } catch (Exception e) {
            System.err.println(filename + " could not be loaded!");
            e.printStackTrace();
            // Not loading EAN128AIs.properties is a severe error. 
            // But the code is still usable, if you use templates or do not rely on checkdigits.
            // Maybe it would be better to throw this exception and find out how this cold happen.
        }
        propertiesLoaded = true;
    }    
    
    private EAN128AI(String id, byte lenID, byte[] type, byte[] lenMin, byte[] lenMax, byte[] checkDigitStart){
        this.id = id;
        this.lenID = lenID;
        this.type = type;
        this.lenMin = lenMin;
        this.lenMax = lenMax;
        this.checkDigitStart = checkDigitStart;
        lenMinAll = lenMaxAll = minLenAfterVariableLen = 0;
        int idxVarLen = type.length;
        int idxFirstChecksum = -1;
//        canDoChecksumADD = true;
        for (int i = 0; i < type.length; i++) {
            lenMinAll += lenMin[i];
            lenMaxAll += lenMax[i];
            if (i > idxVarLen) 
                minLenAfterVariableLen += lenMin[i];  
            if (lenMin[i] != lenMax[i]) {
                if (idxVarLen < type.length) 
                    throw new IllegalArgumentException("Only one Part with var len!"); //TODO
                idxVarLen = i;
            }
            if (idxFirstChecksum == -1 && type[i] == TYPECD)
                idxFirstChecksum = i;
        }
        canDoChecksumADD = (idxFirstChecksum == type.length - 1 && lenMinAll == lenMaxAll);
    }
    private EAN128AI(String id, String spec, byte lenID, byte type, byte len) {
        this(id, lenID, 
                new byte[] {type}, new byte[] {len}, new byte[] {len}, 
                new byte[] {CheckDigit.CDNone});
        fixed = true;
    }

//    public boolean isCheckDigit(int i) {
//        return (type[i] == TYPECDWight31 || type[i] == TYPECDWight1);
//    }
    private static void checkFixed(EAN128AI aiNew, EAN128AI aiOld) {
        if (aiOld.fixed && !aiNew.fixed) {
            if (aiNew.lenMaxAll != aiNew.lenMinAll
                    || aiNew.lenID + aiNew.lenMinAll != aiOld.lenID + aiOld.lenMinAll) {
                throw new IllegalArgumentException("AI \"" + aiNew.toString()
                        + "\" must have fixed len: " + aiOld.lenID + "+" + aiOld.lenMinAll);
            }
            aiNew.fixed = true;
        }
    }
    private static void SetAIHere(EAN128AI ai, Object[] aitParent) {
        for (int idx = 0; idx <= 9; idx++) {
            SetAIHere(ai, aitParent, idx);
        }
    }
    private static void SetAIHere(EAN128AI aiNew, Object[] aitParent, int idx) {
        Object tmp = aitParent[idx];
        if (tmp instanceof EAN128AI) {
            EAN128AI aiOld = (EAN128AI)tmp;
            if (aiNew.type[0] == TYPEError) {
                aiOld.type[0] = TYPEError;
            } else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -