📄 ean128ai.java
字号:
checkFixed(aiNew, aiOld);
aitParent[idx] = aiNew;
}
} else { //tmp instanceof Object[]
SetAIHere(aiNew, (Object[])tmp);
}
}
private static void setAI(String aiName, EAN128AI ai) {
Object[] aitParent = aiTable;
int aiLastRelevantIdx = aiName.length() - 1;
while (aiLastRelevantIdx >= 0
&& !Character.isDigit(aiName.charAt(aiLastRelevantIdx))) {
aiLastRelevantIdx--;
}
Object tmp;
for (int i = 0; i <= aiLastRelevantIdx; i++) {
int idx = aiName.charAt(i) - '0';
if (i == aiLastRelevantIdx) {
SetAIHere(ai, aitParent, idx);
} else {
tmp = aitParent[idx];
if (tmp instanceof EAN128AI) {
tmp = new Object[] {tmp, tmp, tmp, tmp, tmp, tmp, tmp, tmp, tmp, tmp};
aitParent[idx] = tmp;
}
aitParent = (Object[])tmp;
}
}
}
public static EAN128AI parseSpec(String ai, String spec) {
EAN128AI ret = parseSpecPrivate(ai, spec);
checkAI(ret);
return ret;
}
private static void parseSpecPrivate(int i, String spec,
byte[] type, byte[] lenMin, byte[] lenMax, byte[] checkDigitStart) {
int startLen = 0;
checkDigitStart[i] = 1;
lenMin[i] = lenMax[i] = -1;
if (spec.startsWith("an")) {
type[i] = TYPEAlphaNum;
startLen = 2;
} else if (spec.startsWith("a")) {
type[i] = TYPEAlpha;
startLen = 1;
} else if (spec.startsWith("cd")) {
type[i] = TYPECD;
if (spec.length() > 2) {
checkDigitStart[i] = Byte.parseByte(spec.substring(2));
}
lenMin[i] = lenMax[i] = 1;
return;
} else if (spec.startsWith("n")) {
type[i] = TYPENum;
startLen = 1;
} else if (spec.startsWith("d")) {
type[i] = TYPENumDate;
lenMin[i] = lenMax[i] = 6;
startLen = 1;
} else if (spec.startsWith("e")) {
type[i] = TYPEError;
lenMin[i] = lenMax[i] = 0;
return;
} else {
throw new IllegalArgumentException("Unknown type!");
}
int hyphenIdx = spec.indexOf('-', startLen);
if (hyphenIdx < 0) {
lenMin[i] = lenMax[i] = parseByte(spec.substring(startLen), lenMin[i], spec);
} else if (hyphenIdx == startLen) {
lenMin[i] = 1;
lenMax[i] = parseByte(spec.substring(startLen + 1), lenMax[i], spec);
} else { // hyphenIdx > startLen
lenMin[i] = parseByte(spec.substring(startLen, hyphenIdx), lenMin[i], spec);
lenMax[i] = parseByte(spec.substring(hyphenIdx + 1), lenMax[i], spec);
}
if (type[i] == TYPENumDate) {
if (lenMin[i] != 6 || lenMax[i] != 6) {
throw new IllegalArgumentException("Date field (" + spec + ") must have length 6!");
}
}
}
private static byte parseByte(String val, byte dft, String spec) {
try {
return Byte.parseByte(val);
} catch (Exception e) {
if (dft == -1) {
throw new IllegalArgumentException("Can't read field length from \"" + spec + "\"");
}
return dft;
}
}
private static EAN128AI parseSpecPrivate(String ai, String spec) {
try {
byte lenID = (byte) ai.trim().length();
spec = spec.trim();
StringTokenizer st = new StringTokenizer(spec, "+", false);
int count = st.countTokens();
byte[] type = new byte[count];
byte[] checkDigitStart = new byte[count];
byte[] lenMin = new byte[count];
byte[] lenMax = new byte[count];
for (int i = 0; i < count; i++) {
parseSpecPrivate(i, st.nextToken(), type, lenMin, lenMax, checkDigitStart);
}
return new EAN128AI(ai, lenID, type, lenMin, lenMax, checkDigitStart);
} catch (IllegalArgumentException iae) {
throw iae;
} catch (Exception e) {
throw new IllegalArgumentException(
"Cannot Parse AI: \"" + ai + "\" spec: \"" + spec + "\" ");
}
}
public static boolean checkAI(EAN128AI ai) {
EAN128AI aiCompare = getAIPrivate(ai.id + "0000", 0);
checkFixed(ai, aiCompare);
return true;
}
public static EAN128AI getAI(String msg, int msgStart) throws Exception {
loadProperties();
return getAIPrivate(msg, msgStart);
}
private static EAN128AI getAIPrivate(String msg, int msgStart) {
EAN128AI ret = dft;
Object o = aiTable;
int c;
for (int i = 0; i < msg.length() - msgStart; i++) {
c = getIDChar(msg, msgStart + i) - '0';
o = ((Object[])o)[c];
if (o == null) {
return dft;
}
if (o instanceof EAN128AI) {
ret = (EAN128AI)o;
break;
}
}
return ret;
}
private static char getIDChar(String msg, int msgStart) {
char ret;
try {
ret = msg.charAt(msgStart);
} catch (Exception e) {
throw new IllegalArgumentException("Unable to read AI: Message too short!");
}
if (!Character.isDigit(ret)) {
throw new IllegalArgumentException("Unable to read AI: Characters must be numerical!");
}
return ret;
}
public static final boolean isCheckDigitType(byte type) {
return (type == TYPECD);
}
public final boolean isCheckDigit(byte idx) {
return isCheckDigitType(type[idx]);
}
public static final String getType(byte type) {
String ret = "?";
try {
ret = typeToString[type];
} catch (Exception e) {
//ignore
}
return ret;
}
/** {@inheritDoc} */
public String toString() {
StringBuffer ret = new StringBuffer();
ret.append('(').append(id).append(")");
for (int i = 0; i < lenMin.length; i++) {
if (i != 0) {
ret.append('+');
}
ret.append(getType(type[i]));
// if (checkDigit[i] == CheckDigit.CD11)
// ret.append("w1");
if (type[i] < TYPEError) {
ret.append(lenMin[i]);
if (lenMin[i] != lenMax[i]) {
ret.append('-').append(lenMax[i]);
}
}
}
ret.append((fixed) ? " (fixed)" : "");
return ret.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -