private char byteToChar(final int index) { final byte value = byteArray[index]; if (value < 0) { throw new FormatException("Invalid character code found at index " + index); } return (char) value; }
private void checkFieldFitsInAddressSpace(final int fieldStart, final int fieldEnd) { final int fieldLength = fieldEnd - fieldStart; final int lastPartLength = fieldLength % maxFieldLength; final int lastPartStart = fieldEnd - lastPartLength; if (lastPartStart > maxFieldStart) { throw new FormatException("field is too long"); } }
public void endOfInput(final TextParserContext ctx) { throw new FormatException("quoted string is not terminated"); } },
private void checkRecordDataLength(final byte[] recordData) { if (recordData.length < MIN_RECORD_LENGTH) { throw new FormatException("record is too short"); } }
private void requireValidCode(final char code, final char[] validCodes) { for (final char validCode: validCodes) { if (validCode == code) { return; } } throw new FormatException("invalid code '" + code + "'; allowed codes are: " + Arrays.toString(validCodes)); }
private void verifyValidVersion(final Attributes attributes) { final String version = attributes.getValue("", VERSION_ATTR); if (!VERSION.equals(version)) { throw new FormatException("Invalid cg-xml version: " + version); } }
private void checkValidReferenceFieldTag(final char[] tag) { if (!REFERENCE_FIELD_TAG_PATTERN.matcher(String.valueOf(tag)).matches()) { throw new FormatException("invalid tag format for reference field"); } }
private void checkValidDataFieldTag(final char[] tag) { if (!DATA_FIELD_TAG_PATTERN.matcher(String.valueOf(tag)).matches()) { throw new FormatException("invalid tag format for data field"); } }
public void appendEscapedChar(final char ch) { if (Formeta.NEWLINE_ESC_SEQ == ch) { text.append('\n'); } else if (Formeta.CARRIAGE_RETURN_ESC_SEQ == ch) { text.append('\r'); } else if (ESCAPABLE_CHARS.indexOf(ch) > -1) { text.append(ch); } else { throw new FormatException("invalid escape sequence: " + ch); } lengthWithoutTrailingWs = text.length(); }
private void checkDirectoryCapacity(final int fieldStart, final int fieldEnd) { final int fieldLength = fieldEnd - fieldStart; final int numberOfEntries = fieldLength / maxFieldLength + (fieldLength % maxFieldLength == 0 ? 0 : 1); if (numberOfEntries * entryLength > buffer.getFreeSpace()) { throw new FormatException( "directory does not have enough free space for directory entry"); } }
private void requireMarc21RecordFormat(final RecordFormat format) { if (!Marc21Constants.MARC21_FORMAT.equals(format)) { throw new FormatException("invalid record format. Expected " + Marc21Constants.MARC21_FORMAT + " but got " + format); } }
@Override public void literal(final String name, final String value, final int nestingLevel) { if (nestingLevel == 0) { throw new FormatException("literals may only appear in records"); } receiver.literal(name, value); }
private void checkBaseAddress() { if (baseAddress < MIN_BASE_ADDRESS || baseAddress > buffer.getLength() - 1) { throw new FormatException("base address is out of range"); } }
private String extractIdFromRecord(final String record) { try{ final int fieldEnd = record.indexOf(FIELD_END, HEADER_SIZE); if(record.substring(HEADER_SIZE, HEADER_SIZE + TAG_LENGTH).equals(ID_TAG)){ return record.substring(HEADER_SIZE + TAG_LENGTH, fieldEnd); } throw new MissingIdException(record); } catch (IndexOutOfBoundsException e) { throw new FormatException(INVALID_FORMAT + record, e); } }
private void checkCapacity(final int dataLength) { if (dataLength > buffer.getFreeSpace()) { throw new FormatException("not enough space for field"); } }
private void requireUTF8Encoding(final Record record) { if (record.getImplCodes()[Marc21Constants.CHARACTER_CODING_INDEX] != 'a') { throw new FormatException( "invalid record encoding. Only UTF-8 is supported"); } }
private void startField(final String name) { if (name.length() != NAME_LENGTH) { throw new FormatException("invalid entity name: " + name); } final char[] tag = new char[Marc21Constants.MARC21_FORMAT.TAG_LENGTH]; final char[] indicators = new char[Marc21Constants.MARC21_FORMAT.getIndicatorLength()]; name.getChars(0, tag.length, tag, 0); name.getChars(tag.length, name.length(), indicators, 0); builder.startDataField(tag, indicators); }
private void emitStartEntity(final Attributes attributes) { final String name = attributes.getValue("", NAME_ATTR); if (name == null) { throw new FormatException("Entity name must not be null"); } getReceiver().startEntity(name); }
public void endOfInput(final StructureParserContext ctx) { ctx.processEOIWithTextParser(); if (!ctx.isTextEmpty() || ctx.isNested()) { throw new FormatException(UNEXPECTED_EOI); } } },
public void endOfInput(final StructureParserContext ctx) { ctx.processEOIWithTextParser(); if (ctx.isNested()) { throw new FormatException(UNEXPECTED_EOI); } ctx.endLiteral(); } };