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; }
public void parse(final String data) { assert structureParserContext.getEmitter() != null: "No emitter set"; // According to http://stackoverflow.com/a/11876086 it is faster to copy // a string into a char array then to use charAt(): buffer = StringUtil.copyToBuffer(data, buffer); final int bufferLen = data.length(); structureParserContext.reset(); StructureParserState state = StructureParserState.ITEM_NAME; int i = 0; try { for (; i < bufferLen; ++i) { state = state.processChar(buffer[i], structureParserContext); } } catch (final FormatException e) { final String errorMsg = "Parsing error at position " + (i + 1) + ": " + getErrorSnippet(data, i) + ", " + e.getMessage(); throw new FormatException(errorMsg, e); } try { state.endOfInput(structureParserContext); } catch (final FormatException e) { throw new FormatException("Parsing error: " + e.getMessage(), e); } }
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"); } }
public void endOfInput(final TextParserContext ctx) { throw new FormatException("incomplete escape sequence and quoted string is not terminated"); } },
public void endOfInput(final TextParserContext ctx) { throw new FormatException("incomplete escape sequence"); } },
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(); }
@Override public void startEntity(final String name) { // Here begins a field (i.e. "028A ", which is given in the name. // It is unknown, whether there are any subfields in the field. final Matcher fieldNameMatcher = FIELD_NAME_PATTERN.matcher(name); if (!fieldNameMatcher.matches()) { throw new FormatException(name); } if (entityOpen) { //No nested entities are allowed in pica+. throw new FormatException(name); } builder.append(name.trim()+ " "); idnControlSubField = !ignoreRecordId && FIELD_IDN_INTERN.equals(name.trim()); //Now literals can be opened but no more entities. this.entityOpen = true; }
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"); } }