/** * Check the composite for any specified offsets and validate they are correctly specified. * * @param node of the XML for this composite */ public void checkForValidOffsets(final Node node) { int offset = 0; for (final Type edt : containedTypeByNameMap.values()) { final int offsetAttribute = edt.offsetAttribute(); if (-1 != offsetAttribute) { if (offsetAttribute < offset) { XmlSchemaParser.handleError( node, String.format("composite element \"%s\" has incorrect offset specified", edt.name())); } offset = offsetAttribute; } offset += edt.encodedLength(); } }
/** * The encodedLength (in octets) of the list of EncodedDataTypes * * @return encodedLength of the compositeType */ public int encodedLength() { int length = 0; for (final Type t : containedTypeByNameMap.values()) { if (t.isVariableLength()) { return Token.VARIABLE_LENGTH; } if (t.offsetAttribute() != -1) { length = t.offsetAttribute(); } if (t.presence() != Presence.CONSTANT) { length += t.encodedLength(); } } return length; }
@Test public void shouldReturnCorrectSizeForPrimitiveTypes() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeChar\" primitiveType=\"char\"/>" + " <type name=\"testTypeInt8\" primitiveType=\"int8\"/>" + " <type name=\"testTypeInt16\" primitiveType=\"int16\"/>" + " <type name=\"testTypeInt32\" primitiveType=\"int32\"/>" + " <type name=\"testTypeInt64\" primitiveType=\"int64\"/>" + " <type name=\"testTypeUInt8\" primitiveType=\"uint8\"/>" + " <type name=\"testTypeUInt16\" primitiveType=\"uint16\"/>" + " <type name=\"testTypeUInt32\" primitiveType=\"uint32\"/>" + " <type name=\"testTypeUInt64\" primitiveType=\"uint64\"/>" + " <type name=\"testTypeFloat\" primitiveType=\"float\"/>" + " <type name=\"testTypeDouble\" primitiveType=\"double\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat(map.get("testTypeChar").encodedLength(), is(1)); assertThat(map.get("testTypeInt8").encodedLength(), is(1)); assertThat(map.get("testTypeInt32").encodedLength(), is(4)); assertThat(map.get("testTypeInt64").encodedLength(), is(8)); assertThat(map.get("testTypeUInt8").encodedLength(), is(1)); assertThat(map.get("testTypeUInt16").encodedLength(), is(2)); assertThat(map.get("testTypeUInt32").encodedLength(), is(4)); assertThat(map.get("testTypeUInt64").encodedLength(), is(8)); assertThat(map.get("testTypeFloat").encodedLength(), is(4)); assertThat(map.get("testTypeDouble").encodedLength(), is(8)); }
@Test public void shouldCalculateDataOffsetWithPaddingFromBlockLength() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "block-length-schema.xml"), ParserOptions.DEFAULT); final List<Field> fields = schema.getMessage(4).fields(); assertThat(valueOf(fields.get(0).computedOffset()), is(valueOf(0))); assertThat(valueOf(fields.get(0).type().encodedLength()), is(valueOf(8))); assertThat(valueOf(fields.get(1).computedOffset()), is(valueOf(64))); assertThat(valueOf(fields.get(1).type().encodedLength()), is(valueOf(-1))); }
@Test public void shouldHandleAllTypeOffsets() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "basic-types-schema.xml"), ParserOptions.DEFAULT); final List<Field> fields = schema.getMessage(1).fields(); assertThat(valueOf(fields.get(0).computedOffset()), is(valueOf(0))); assertThat(valueOf(fields.get(0).type().encodedLength()), is(valueOf(8))); assertThat(valueOf(fields.get(1).computedOffset()), is(valueOf(8))); assertThat(valueOf(fields.get(1).type().encodedLength()), is(valueOf(20))); assertThat(valueOf(fields.get(2).computedOffset()), is(valueOf(28))); assertThat(valueOf(fields.get(2).type().encodedLength()), is(valueOf(1))); assertThat(valueOf(fields.get(3).computedOffset()), is(valueOf(29))); assertThat(valueOf(fields.get(3).type().encodedLength()), is(valueOf(4))); assertThat(valueOf(fields.get(4).computedOffset()), is(valueOf(33))); assertThat(valueOf(fields.get(4).type().encodedLength()), is(valueOf(8))); }
@Test public void shouldHandleAllTypeOffsetsSetByXML() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "basic-types-schema.xml"), ParserOptions.DEFAULT); final List<Field> fields = schema.getMessage(2).fields(); assertThat(valueOf(fields.get(0).computedOffset()), is(valueOf(0))); assertThat(valueOf(fields.get(0).type().encodedLength()), is(valueOf(8))); assertThat(valueOf(fields.get(1).computedOffset()), is(valueOf(8))); assertThat(valueOf(fields.get(1).type().encodedLength()), is(valueOf(20))); assertThat(valueOf(fields.get(2).computedOffset()), is(valueOf(32))); assertThat(valueOf(fields.get(2).type().encodedLength()), is(valueOf(1))); assertThat(valueOf(fields.get(3).computedOffset()), is(valueOf(128))); assertThat(valueOf(fields.get(3).type().encodedLength()), is(valueOf(4))); assertThat(valueOf(fields.get(4).computedOffset()), is(valueOf(136))); assertThat(valueOf(fields.get(4).type().encodedLength()), is(valueOf(8))); }
@Test public void shouldCalculateGroupOffsetWithPaddingFromBlockLength() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "block-length-schema.xml"), ParserOptions.DEFAULT); final List<Field> fields = schema.getMessage(2).fields(); assertThat(valueOf(fields.get(0).computedOffset()), is(valueOf(0))); assertThat(valueOf(fields.get(0).type().encodedLength()), is(valueOf(8))); assertThat(valueOf(fields.get(1).computedOffset()), is(valueOf(64))); Assert.assertNull(fields.get(1).type()); assertThat(valueOf(fields.get(1).computedBlockLength()), is(valueOf(12))); final List<Field> groupFields = fields.get(1).groupFields(); assertThat(valueOf(groupFields.size()), is(valueOf(2))); assertThat(valueOf(groupFields.get(0).computedOffset()), is(valueOf(0))); assertThat(valueOf(groupFields.get(0).type().encodedLength()), is(valueOf(4))); assertThat(valueOf(groupFields.get(1).computedOffset()), is(valueOf(4))); assertThat(valueOf(groupFields.get(1).type().encodedLength()), is(valueOf(8))); }
@Test public void shouldCalculateGroupOffsetWithNoPaddingFromBlockLength() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "block-length-schema.xml"), ParserOptions.DEFAULT); final Message msg = schema.getMessage(1); assertThat(valueOf(msg.blockLength()), is(valueOf(8))); final List<Field> fields = msg.fields(); assertThat(valueOf(fields.get(0).computedOffset()), is(valueOf(0))); assertThat(valueOf(fields.get(0).type().encodedLength()), is(valueOf(8))); assertThat(valueOf(fields.get(1).computedOffset()), is(valueOf(8))); Assert.assertNull(fields.get(1).type()); final List<Field> groupFields = fields.get(1).groupFields(); assertThat(valueOf(groupFields.size()), is(valueOf(2))); assertThat(valueOf(groupFields.get(0).computedOffset()), is(valueOf(0))); assertThat(valueOf(groupFields.get(0).type().encodedLength()), is(valueOf(4))); assertThat(valueOf(groupFields.get(1).computedOffset()), is(valueOf(4))); assertThat(valueOf(groupFields.get(1).type().encodedLength()), is(valueOf(8))); }
@Test public void shouldCalculateGroupOffsetWithPaddingFromBlockLengthAndGroupBlockLength() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "block-length-schema.xml"), ParserOptions.DEFAULT); final List<Field> fields = schema.getMessage(3).fields(); assertThat(valueOf(fields.get(0).computedOffset()), is(valueOf(0))); assertThat(valueOf(fields.get(0).type().encodedLength()), is(valueOf(8))); assertThat(valueOf(fields.get(1).computedOffset()), is(valueOf(64))); Assert.assertNull(fields.get(1).type()); assertThat(valueOf(fields.get(1).computedBlockLength()), is(valueOf(16))); final List<Field> groupFields = fields.get(1).groupFields(); assertThat(valueOf(groupFields.size()), is(valueOf(2))); assertThat(valueOf(groupFields.get(0).computedOffset()), is(valueOf(0))); assertThat(valueOf(groupFields.get(0).type().encodedLength()), is(valueOf(4))); assertThat(valueOf(groupFields.get(1).computedOffset()), is(valueOf(4))); assertThat(valueOf(groupFields.get(1).type().encodedLength()), is(valueOf(8))); }
private static int computeMessageRootBlockLength(final List<Field> fields) { int blockLength = 0; for (final Field field : fields) { if (field.groupFields() != null) { return blockLength; } else if (field.type() != null) { final int fieldLength = field.type().encodedLength(); if (Token.VARIABLE_LENGTH == fieldLength) { return blockLength; } if (field.presence() == Presence.CONSTANT) { blockLength = field.computedOffset(); } else { blockLength = field.computedOffset() + fieldLength; } } } return blockLength; }
offset += elementType.encodedLength();
final int size = field.type().encodedLength();
/** * Check the composite for any specified offsets and validate they are correctly specified. * * @param node of the XML for this composite */ public void checkForValidOffsets(final Node node) { int offset = 0; for (final Type edt : containedTypeByNameMap.values()) { final int offsetAttribute = edt.offsetAttribute(); if (-1 != offsetAttribute) { if (offsetAttribute < offset) { XmlSchemaParser.handleError( node, String.format("composite element \"%s\" has incorrect offset specified", edt.name())); } offset = offsetAttribute; } offset += edt.encodedLength(); } }
/** * Check the composite for any specified offsets and validate they are correctly specified. * * @param node of the XML for this composite */ public void checkForValidOffsets(final Node node) { int offset = 0; for (final Type edt : containedTypeByNameMap.values()) { final int offsetAttribute = edt.offsetAttribute(); if (-1 != offsetAttribute) { if (offsetAttribute < offset) { XmlSchemaParser.handleError( node, String.format("composite element \"%s\" has incorrect offset specified", edt.name())); } offset = offsetAttribute; } offset += edt.encodedLength(); } }
/** * The encodedLength (in octets) of the list of EncodedDataTypes * * @return encodedLength of the compositeType */ public int encodedLength() { int length = 0; for (final Type t : containedTypeByNameMap.values()) { if (t.isVariableLength()) { return Token.VARIABLE_LENGTH; } if (t.offsetAttribute() != -1) { length = t.offsetAttribute(); } if (t.presence() != Presence.CONSTANT) { length += t.encodedLength(); } } return length; }
/** * The encodedLength (in octets) of the list of EncodedDataTypes * * @return encodedLength of the compositeType */ public int encodedLength() { int length = 0; for (final Type t : containedTypeByNameMap.values()) { if (t.isVariableLength()) { return Token.VARIABLE_LENGTH; } if (t.offsetAttribute() != -1) { length = t.offsetAttribute(); } if (t.presence() != Presence.CONSTANT) { length += t.encodedLength(); } } return length; }
private int computeMessageRootBlockLength() { int blockLength = 0; for (final Field field : fieldList) { if (field.groupFields() != null) { return blockLength; } else if (field.type() != null) { final int fieldLength = field.type().encodedLength(); if (Token.VARIABLE_LENGTH == fieldLength) { return blockLength; } blockLength = field.computedOffset() + fieldLength; } } return blockLength; }
private static int computeMessageRootBlockLength(final List<Field> fields) { int blockLength = 0; for (final Field field : fields) { if (field.groupFields() != null) { return blockLength; } else if (field.type() != null) { final int fieldLength = field.type().encodedLength(); if (Token.VARIABLE_LENGTH == fieldLength) { return blockLength; } if (field.presence() == Presence.CONSTANT) { blockLength = field.computedOffset(); } else { blockLength = field.computedOffset() + fieldLength; } } } return blockLength; }
private static int computeMessageRootBlockLength(final List<Field> fields) { int blockLength = 0; for (final Field field : fields) { if (field.groupFields() != null) { return blockLength; } else if (field.type() != null) { final int fieldLength = field.type().encodedLength(); if (Token.VARIABLE_LENGTH == fieldLength) { return blockLength; } if (field.presence() == Presence.CONSTANT) { blockLength = field.computedOffset(); } else { blockLength = field.computedOffset() + fieldLength; } } } return blockLength; }
offset += elementType.encodedLength();