encodedDataType = new EncodedDataType(encodingTypeNode); if (encodedDataType.length() != 1) encodingType = encodedDataType.primitiveType(); if (null != encodedDataType && null != encodedDataType.nullValue()) nullValue = encodedDataType.nullValue();
.primitiveType(type.primitiveType()) .byteOrder(schema.byteOrder()) .characterEncoding(type.characterEncoding()); .name(type.name()) .referencedName(type.referencedName()) .size(type.encodedLength()) .description(type.description()) .version(type.sinceVersion()) .deprecated(type.deprecated()) .offset(offset); switch (type.presence()) .minValue(type.minValue()) .maxValue(type.maxValue()); break; encodingBuilder .presence(Encoding.Presence.OPTIONAL) .minValue(type.minValue()) .maxValue(type.maxValue()) .nullValue(type.nullValue()); break; encodingBuilder .presence(Encoding.Presence.CONSTANT) .constValue(type.constVal()); break;
else if (!isUnsigned(blockLengthType.primitiveType())) if (blockLengthType.primitiveType() != UINT8 && blockLengthType.primitiveType() != UINT16) final PrimitiveValue blockLengthTypeMaxValue = blockLengthType.maxValue(); validateGroupMaxValue(node, blockLengthType.primitiveType(), blockLengthTypeMaxValue); else if (!isUnsigned(numInGroupType.primitiveType())) final PrimitiveValue numInGroupMinValue = numInGroupType.minValue(); if (null == numInGroupMinValue) if (numInGroupType.primitiveType() != UINT8 && numInGroupType.primitiveType() != UINT16) final PrimitiveValue numInGroupMaxValue = numInGroupType.maxValue(); validateGroupMaxValue(node, numInGroupType.primitiveType(), numInGroupMaxValue); final PrimitiveValue numInGroupMinValue = numInGroupType.minValue(); if (null != numInGroupMinValue) numInGroupMaxValue.longValue() : numInGroupType.primitiveType().maxValue().longValue();
private void add(final EncodedDataType type, final int offset, final Field field) .primitiveType(type.primitiveType()) .byteOrder(byteOrder) .semanticType(semanticTypeOf(type, field)) .characterEncoding(type.characterEncoding()); .name(type.name()) .size(type.encodedLength()) .offset(offset); switch (type.presence()) .minValue(type.minValue()) .maxValue(type.maxValue()); break; .minValue(type.minValue()) .maxValue(type.maxValue()) .nullValue(type.nullValue()); break; .constValue(type.constVal()); break;
@Test public void shouldHandleDecimal32CompositeType() throws Exception { final String testXmlString = "<types>" + "<composite name=\"decimal32\">" + " <type name=\"mantissa\" primitiveType=\"int32\"/>" + " <type name=\"exponent\" primitiveType=\"int8\" presence=\"constant\">-2</type>" + "</composite>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/composite", testXmlString); final CompositeType decimal32 = (CompositeType)map.get("decimal32"); assertThat(decimal32.name(), is("decimal32")); final EncodedDataType mantissa = (EncodedDataType)decimal32.getType("mantissa"); final EncodedDataType exponent = (EncodedDataType)decimal32.getType("exponent"); assertThat(mantissa.primitiveType(), is(PrimitiveType.INT32)); assertThat(exponent.primitiveType(), is(PrimitiveType.INT8)); assertThat(exponent.presence(), is(Presence.CONSTANT)); assertThat(exponent.constVal(), is(PrimitiveValue.parse("-2", PrimitiveType.INT8))); assertThat(decimal32.encodedLength(), is(4)); }
if (presence() != CONSTANT) if (presence() == CONSTANT) if (PrimitiveType.CHAR == primitiveType) constValue = processConstantChar(node, lengthAttr, nodeValue); constValue = lookupValueRef(node); if (nullValStr != null) if (presence() != Presence.OPTIONAL)
final PrimitiveType primitiveType = lengthType.primitiveType(); if (!isUnsigned(primitiveType)) validateGroupMaxValue(node, primitiveType, lengthType.maxValue());
@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)); }
typeByNameMap.put("char", new EncodedDataType("char", REQUIRED, null, null, CHAR, 1, false)); typeByNameMap.put("int8", new EncodedDataType("int8", REQUIRED, null, null, INT8, 1, false)); typeByNameMap.put("int16", new EncodedDataType("int16", REQUIRED, null, null, INT16, 1, false)); typeByNameMap.put("int32", new EncodedDataType("int32", REQUIRED, null, null, INT32, 1, false)); typeByNameMap.put("int64", new EncodedDataType("int64", REQUIRED, null, null, INT64, 1, false)); typeByNameMap.put("uint8", new EncodedDataType("uint8", REQUIRED, null, null, UINT8, 1, false)); typeByNameMap.put("uint16", new EncodedDataType("uint16", REQUIRED, null, null, UINT16, 1, false)); typeByNameMap.put("uint32", new EncodedDataType("uint32", REQUIRED, null, null, UINT32, 1, false)); typeByNameMap.put("uint64", new EncodedDataType("uint64", REQUIRED, null, null, UINT64, 1, false)); typeByNameMap.put("float", new EncodedDataType("float", REQUIRED, null, null, FLOAT, 1, false)); typeByNameMap.put("double", new EncodedDataType("double", REQUIRED, null, null, DOUBLE, 1, false)); (node) -> addTypeWithNameCheck(typeByNameMap, new EncodedDataType(node), node));
/** * The constant value of the type if specified * * @return value of the constant for this type */ public PrimitiveValue constVal() throws IllegalArgumentException { if (presence() != CONSTANT) { throw new IllegalStateException("type is not of constant presence"); } return constValue; }
else if (!isUnsigned(blockLengthType.primitiveType())) else if (blockLengthType.primitiveType() != UINT16) else if (templateIdType.primitiveType() != UINT16) else if (schemaIdType.primitiveType() != UINT16) else if (versionType.primitiveType() != UINT16)
/** * 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 EncodedDataType edt : containedTypeList) { 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(); } } }
/** * Construct a new compositeType from XML Schema. * * @param node from the XML Schema Parsing * @throws XPathExpressionException if the XPath is invalid. */ public CompositeType(final Node node) throws XPathExpressionException { super(node); sinceVersion = Integer.parseInt(XmlSchemaParser.getAttributeValue(node, "sinceVersion", "0")); final XPath xPath = XPathFactory.newInstance().newXPath(); final NodeList list = (NodeList)xPath.compile("type").evaluate(node, XPathConstants.NODESET); for (int i = 0, size = list.getLength(); i < size; i++) { final EncodedDataType type = new EncodedDataType(list.item(i)); if (containedTypeByNameMap.get(type.name()) != null) { XmlSchemaParser.handleError(node, "composite already contains type named: " + type.name()); } else { containedTypeList.add(type); containedTypeByNameMap.put(type.name(), type); } } checkForValidOffsets(node); }
@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)); }
@Test public void shouldReturnDefaultNullValueWhenSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeDefaultCharNullValue\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); Assert.assertNull(((EncodedDataType)map.get("testTypeDefaultCharNullValue")).nullValue()); }
@Test public void shouldReturnDefaultMinValueWhenSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeDefaultCharMinValue\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); Assert.assertNull(((EncodedDataType)map.get("testTypeDefaultCharMinValue")).minValue()); }
@Test public void shouldReturnDefaultMaxValueWhenSpecified() throws Exception { final String testXmlString = "<types>" + " <type name=\"testTypeDefaultCharMaxValue\" primitiveType=\"char\"/>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); Assert.assertNull(((EncodedDataType)map.get("testTypeDefaultCharMaxValue")).maxValue()); }
@Test public void shouldReturnCorrectConstantStringWhenSpecified() throws Exception { final String strConst = "string constant"; final String testXmlString = "<types>" + " <type name=\"testTypeConstString\" primitiveType=\"char\" presence=\"constant\" " + "length=\"" + strConst.length() + "\"" + ">" + strConst + "</type>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/type", testXmlString); assertThat((((EncodedDataType)map.get("testTypeConstString")).constVal()), is(parse(strConst, strConst.length(), "US-ASCII"))); }
else if (!isUnsigned(blockLengthType.primitiveType())) if (blockLengthType.primitiveType() != UINT8 && blockLengthType.primitiveType() != UINT16) final PrimitiveValue blockLengthTypeMaxValue = blockLengthType.maxValue(); validateGroupMaxValue(node, blockLengthType.primitiveType(), blockLengthTypeMaxValue); else if (!isUnsigned(numInGroupType.primitiveType())) final PrimitiveValue numInGroupMinValue = numInGroupType.minValue(); if (null == numInGroupMinValue) if (numInGroupType.primitiveType() != UINT8 && numInGroupType.primitiveType() != UINT16) final PrimitiveValue numInGroupMaxValue = numInGroupType.maxValue(); validateGroupMaxValue(node, numInGroupType.primitiveType(), numInGroupMaxValue); final PrimitiveValue numInGroupMinValue = numInGroupType.minValue(); if (null != numInGroupMinValue) numInGroupMaxValue.longValue() : numInGroupType.primitiveType().maxValue().longValue();