/** * 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; }
/** * 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(); } }
type.semanticType() != null && !semanticType.equals(type.semanticType())) else if (!valueRefType.equals(type.name()))
@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)); }
private static void addTypeWithNameCheck(final Map<String, Type> typeByNameMap, final Type type, final Node node) { if (typeByNameMap.get(type.name()) != null) { handleWarning(node, "type already exists for name: " + type.name()); } checkForValidName(node, type.name()); typeByNameMap.put(type.name(), type); }
for (final Type elementType : type.getTypeList()) if (elementType.offsetAttribute() != -1) offset = elementType.offsetAttribute(); offset += elementType.encodedLength();
private static String semanticTypeOf(final Type type, final Field field) { final String typeSemanticType = null != type ? type.semanticType() : null; if (typeSemanticType != null) { return typeSemanticType; } return null != field ? field.semanticType() : null; }
@Test public void shouldSetAppropriateDefaultsWhenNoneSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testType\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); // assert that testType is in map and name of Type is correct assertThat(map.get("testType").name(), is("testType")); // assert defaults for length, variableLength and presence final Type t = map.get("testType"); assertThat(t.presence(), is(Presence.REQUIRED)); final EncodedDataType d = (EncodedDataType)t; assertThat(d.length(), is(1)); assertThat(d.isVariableLength(), is(false)); }
private static Presence getPresence(final Node node, final Type fieldType) { final String presenceStr = getAttributeValueOrNull(node, "presence"); final Presence presence; if (null != presenceStr) { presence = Presence.get(presenceStr); } else if (null != fieldType) { presence = fieldType.presence(); } else { presence = Presence.REQUIRED; } return presence; }
type.offsetAttribute(refOffset);
@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 shouldHandleMultipleTypes() throws Exception { final String testXmlString = "<types>" + " <type name=\"testType1\" presence=\"required\" primitiveType=\"char\" length=\"1\" " + "variableLength=\"false\"/>" + " <type name=\"testType2\" presence=\"required\" primitiveType=\"int8\" length=\"1\" " + "variableLength=\"false\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); // assert that testType is in map and name of Type is correct assertThat(map.size(), is(2)); assertThat(map.get("testType1").name(), is("testType1")); assertThat(map.get("testType2").name(), is("testType2")); }
for (final Type elementType : type.getTypeList()) if (elementType.offsetAttribute() != -1) offset = elementType.offsetAttribute(); offset += elementType.encodedLength();
@Test public void shouldReturnCorrectSemanticTypeForType() throws Exception { final String semanticType = "char"; final String testXmlString = "<types>" + " <type name=\"testType\" primitiveType=\"char\" semanticType=\"" + semanticType + "\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat(map.get("testType").semanticType(), is(semanticType)); }
@Test public void shouldHandleSettingAllAttributes() throws Exception { final String testXmlString = "<types>" + " <type name=\"testType\" presence=\"required\" primitiveType=\"char\" length=\"1\" " + "variableLength=\"false\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); // assert that testType is in map and name of Type is correct final Type t = map.get("testType"); assertThat(t.name(), is("testType")); assertThat(t.presence(), is(Presence.REQUIRED)); final EncodedDataType d = (EncodedDataType)t; assertThat(d.primitiveType(), is(PrimitiveType.CHAR)); assertThat(d.length(), is(1)); assertThat(d.isVariableLength(), is(false)); }
@Test public void shouldUseAppropriatePresence() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeDefault\" primitiveType=\"char\"/>" + " <type name=\"testTypeRequired\" presence=\"required\" primitiveType=\"char\"/>" + " <type name=\"testTypeOptional\" presence=\"optional\" primitiveType=\"char\"/>" + " <type name=\"testTypeConstant\" presence=\"constant\" primitiveType=\"char\">A</type>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat(map.get("testTypeDefault").presence(), is(Presence.REQUIRED)); assertThat(map.get("testTypeRequired").presence(), is(Presence.REQUIRED)); assertThat(map.get("testTypeOptional").presence(), is(Presence.OPTIONAL)); assertThat(map.get("testTypeConstant").presence(), is(Presence.CONSTANT)); }
type.offsetAttribute(refOffset);
/** * 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; }
/** * 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(); } }
@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))); }