Fix #5 - Add field number to parse error messages

This commit is contained in:
Enrique Zamudio
2013-01-18 13:29:39 -06:00
parent d570eb4885
commit 913d74a133
15 changed files with 253 additions and 112 deletions
@@ -36,28 +36,34 @@ public class LllvarParseInfo extends FieldParseInfo {
super(IsoType.LLLVAR, 0);
}
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom)
public IsoValue<?> parse(final int field, final byte[] buf,
final int pos, final CustomField<?> custom)
throws ParseException, UnsupportedEncodingException {
if (pos < 0) {
throw new ParseException(String.format("Invalid LLLVAR position %d", pos), pos);
throw new ParseException(String.format("Invalid LLLVAR field %d pos %d",
field, pos), pos);
} else if (pos+3 > buf.length) {
throw new ParseException(String.format("Insufficient data for LLLVAR header, pos %d", pos), pos);
throw new ParseException(String.format(
"Insufficient data for LLLVAR header field %d pos %d", field, pos), pos);
}
if (!(Character.isDigit(buf[pos]) && Character.isDigit(buf[pos+1]) && Character.isDigit(buf[pos+2]))) {
throw new ParseException(String.format("Invalid LLLVAR length '%s' pos %d",
new String(buf, pos, 3), pos), pos);
throw new ParseException(String.format("Invalid LLLVAR length '%s' field %d pos %d",
new String(buf, pos, 3), field, pos), pos);
}
length = ((buf[pos] - 48) * 100) + ((buf[pos + 1] - 48) * 10) + (buf[pos + 2] - 48);
if (length < 0) {
throw new ParseException(String.format("Invalid LLLVAR length %d pos %d", length, pos), pos);
throw new ParseException(String.format("Invalid LLLVAR length %d field %d pos %d",
length, field, pos), pos);
} else if (length+pos+3 > buf.length) {
throw new ParseException(String.format("Insufficient data for LLLVAR field, pos %d", pos), pos);
throw new ParseException(String.format("Insufficient data for LLLVAR field %d, pos %d",
field, pos), pos);
}
String _v;
try {
_v = length == 0 ? "" : new String(buf, pos + 3, length, getCharacterEncoding());
} catch (IndexOutOfBoundsException ex) {
throw new ParseException(String.format("Insufficient data for LLLVAR header, pos %d", pos), pos);
throw new ParseException(String.format(
"Insufficient data for LLLVAR header, field %d pos %d", field, pos), pos);
}
//This is new: if the String's length is different from the specified length in the buffer,
//there are probably some extended characters. So we create a String from the rest of the buffer,
@@ -74,18 +80,22 @@ public class LllvarParseInfo extends FieldParseInfo {
}
}
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom)
public IsoValue<?> parseBinary(final int field, final byte[] buf,
final int pos, final CustomField<?> custom)
throws ParseException, UnsupportedEncodingException {
if (pos < 0) {
throw new ParseException(String.format("Invalid bin LLLVAR position %d", pos), pos);
throw new ParseException(String.format("Invalid bin LLLVAR field %d pos %d", field, pos), pos);
} else if (pos+3 > buf.length) {
throw new ParseException(String.format("Insufficient data for bin LLLVAR header, pos %d", pos), pos);
throw new ParseException(String.format(
"Insufficient data for bin LLLVAR header, field %d pos %d", field, pos), pos);
}
length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f);
if (length < 0) {
throw new ParseException(String.format("Invalid bin LLLVAR length %d pos %d", length, pos), pos);
throw new ParseException(String.format(
"Invalid bin LLLVAR length %d, field %d pos %d", length, field, pos), pos);
} else if (length+pos+2 > buf.length) {
throw new ParseException(String.format("Insufficient data for bin LLLVAR field, pos %d", pos), pos);
throw new ParseException(String.format(
"Insufficient data for bin LLLVAR field %d, pos %d", field, pos), pos);
}
if (custom == null) {
return new IsoValue<String>(type, new String(buf, pos + 2, length, getCharacterEncoding()), null);