type = addType(subTypeNode, subTypeName, new EncodedDataType(subTypeNode, givenName, referencedName)); break; type = addType(subTypeNode, subTypeName, new EnumType(subTypeNode, givenName, referencedName)); break; type = addType(subTypeNode, subTypeName, new SetType(subTypeNode, givenName, referencedName)); break; type = addType( subTypeNode, subTypeName, new CompositeType(subTypeNode, givenName, referencedName, compositesPath)); break; type = processType(refTypeNode, refName, refName, refTypeName);
.name(type.name()) .referencedName(type.referencedName()) .offset(currOffset) .size(type.encodedLength()) .version(type.sinceVersion()) .deprecated(type.deprecated()) .description(type.description()) .encoding(encoding); builder.version(Math.max(field.sinceVersion(), type.sinceVersion())); for (final Type elementType : type.getTypeList())
/** * Construct a new compositeType from XML Schema. * * @param node from the XML Schema Parsing * @param givenName for this node. * @param referencedName of the type when created from a ref in a composite. * @param compositesPath with the path of composites that represents the levels of composition. * @throws XPathExpressionException if the XPath is invalid. */ public CompositeType( final Node node, final String givenName, final String referencedName, final List<String> compositesPath) throws XPathExpressionException { super(node, givenName, referencedName); this.compositesPath.addAll(compositesPath); this.compositesPath.add(getAttributeValue(node, "name")); final XPath xPath = XPathFactory.newInstance().newXPath(); final NodeList list = (NodeList)xPath.compile(SUB_TYPES_EXP).evaluate(node, NODESET); for (int i = 0, size = list.getLength(); i < size; i++) { final Node subTypeNode = list.item(i); final String subTypeName = XmlSchemaParser.getAttributeValue(subTypeNode, "name"); processType(subTypeNode, subTypeName, null, null); } checkForValidOffsets(node); }
final CompositeType compositeType = (CompositeType)composite.type(); assertThat(compositeType.name(), is("outer")); final List<Type> elements = compositeType.getTypeList(); assertThat(encodedDataType.name(), is("zeroth")); assertThat(setType.name(), is("setOne")); assertThat(nestedCompositeType.name(), is("inner")); final List<Type> nestedElements = nestedCompositeType.getTypeList(); assertThat(second.name(), is("second")); assertThat(nestedCompositeType.encodedLength(), is(16)); assertThat(compositeType.encodedLength(), is(22));
@Test public void shouldHandleDecimalCompositeType() throws Exception { final String testXmlString = "<types>" + "<composite name=\"decimal\">" + " <type name=\"mantissa\" primitiveType=\"int64\"/>" + " <type name=\"exponent\" primitiveType=\"int8\"/>" + "</composite>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/composite", testXmlString); final CompositeType decimal = (CompositeType)map.get("decimal"); assertThat(decimal.name(), is("decimal")); final EncodedDataType mantissa = (EncodedDataType)decimal.getType("mantissa"); final EncodedDataType exponent = (EncodedDataType)decimal.getType("exponent"); assertThat(mantissa.primitiveType(), is(PrimitiveType.INT64)); assertThat(exponent.primitiveType(), is(PrimitiveType.INT8)); assertThat(decimal.encodedLength(), is(9)); }
((CompositeType)fieldType).checkForWellFormedVariableLengthDataEncoding(node); ((CompositeType)fieldType).makeDataFieldCompositeType();
(node) -> addTypeWithNameCheck(typeByNameMap, new CompositeType(node), node));
public MessageSchema( final Node schemaNode, final Map<String, Type> typeByNameMap, final Map<Long, Message> messageByIdMap) { this.packageName = getAttributeValue(schemaNode, "package"); this.description = getAttributeValueOrNull(schemaNode, "description"); this.id = Integer.parseInt(getAttributeValue(schemaNode, "id")); this.version = Integer.parseInt(getAttributeValue(schemaNode, "version", "0")); this.semanticVersion = getAttributeValueOrNull(schemaNode, "semanticVersion"); this.byteOrder = getByteOrder(getAttributeValue(schemaNode, "byteOrder", "littleEndian")); this.typeByNameMap = typeByNameMap; this.messageByIdMap = messageByIdMap; final String headerType = getAttributeValueOrNull(schemaNode, "headerType"); this.headerType = null == headerType ? HEADER_TYPE_DEFAULT : headerType; Verify.present(typeByNameMap, this.headerType, "Message header"); ((CompositeType)typeByNameMap.get(this.headerType)).checkForWellFormedMessageHeader(schemaNode); }
((CompositeType)dimensionType).checkForWellFormedGroupSizeEncoding(node);
@Test public void shouldCalculateCompositeSizeWithOffsetsSpecified() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "composite-offsets-schema.xml"), ParserOptions.DEFAULT); final CompositeType header = schema.messageHeader(); assertThat(valueOf(header.encodedLength()), is(valueOf(12))); }
@Test public void shouldHandleCompositeTypeList() throws Exception { final String testXmlString = "<types>" + "<composite name=\"decimal\">" + " <type name=\"mantissa\" primitiveType=\"int64\"/>" + " <type name=\"exponent\" primitiveType=\"int8\"/>" + "</composite>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/composite", testXmlString); final CompositeType c = (CompositeType)map.get("decimal"); assertThat(c.getTypeList().size(), is(2)); assertThat(c.getTypeList().get(0).name(), is("mantissa")); assertThat(c.getTypeList().get(1).name(), is("exponent")); }
@Test public void shouldParseSchemaSuccessfully() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "group-with-data-schema.xml"), ParserOptions.DEFAULT); final List<Field> fields = schema.getMessage(1).fields(); final Field entriesGroup = fields.get(1); final CompositeType dimensionType = entriesGroup.dimensionType(); final List<Field> entriesFields = entriesGroup.groupFields(); assertThat(entriesGroup.name(), is("Entries")); assertThat(dimensionType.name(), is("groupSizeEncoding")); final Field varDataField = entriesFields.get(2); assertThat(varDataField.name(), is("varDataField")); assertTrue(varDataField.isVariableLength()); } }
/** * 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); }
private void add(final CompositeType type, final int currOffset, final Field field) { final Token.Builder builder = new Token.Builder() .signal(Signal.BEGIN_COMPOSITE) .name(type.name()) .offset(currOffset) .size(type.encodedLength()) .encoding(new Encoding.Builder() .semanticType(semanticTypeOf(type, field)) .build()); if (field != null) { builder.version(field.sinceVersion()); } tokenList.add(builder.build()); int offset = 0; for (final EncodedDataType edt : type.getTypeList()) { if (edt.offsetAttribute() != -1) { offset = edt.offsetAttribute(); } add(edt, offset, field); offset += edt.encodedLength(); } tokenList.add(builder.signal(Signal.END_COMPOSITE).build()); }
@Test public void shouldHandleDecimal64CompositeType() throws Exception { final String testXmlString = "<types>" + "<composite name=\"decimal64\">" + " <type name=\"mantissa\" primitiveType=\"int64\"/>" + " <type name=\"exponent\" primitiveType=\"int8\" presence=\"constant\">-2</type>" + "</composite>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/composite", testXmlString); final CompositeType decimal64 = (CompositeType)map.get("decimal64"); assertThat(decimal64.name(), is("decimal64")); final EncodedDataType mantissa = (EncodedDataType)decimal64.getType("mantissa"); final EncodedDataType exponent = (EncodedDataType)decimal64.getType("exponent"); assertThat(mantissa.primitiveType(), is(PrimitiveType.INT64)); assertThat(exponent.primitiveType(), is(PrimitiveType.INT8)); assertThat(exponent.presence(), is(Presence.CONSTANT)); assertThat(exponent.constVal(), is(PrimitiveValue.parse("-2", PrimitiveType.INT8))); assertThat(decimal64.encodedLength(), is(8)); }
((CompositeType)fieldType).checkForWellFormedVariableLengthDataEncoding(nodeList.item(nodeIndex)); ((CompositeType)fieldType).makeDataFieldCompositeType();
private static Map<String, Type> parseTestXmlWithMap(final String xPathExpr, final String xml) throws ParserConfigurationException, XPathExpressionException, IOException, SAXException { final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( new ByteArrayInputStream(xml.getBytes())); final XPath xPath = XPathFactory.newInstance().newXPath(); final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET); final Map<String, Type> map = new HashMap<>(); final ParserOptions options = ParserOptions.builder().stopOnError(true).suppressOutput(true).build(); document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, new ErrorHandler(options), null); for (int i = 0, size = list.getLength(); i < size; i++) { final Type t = new CompositeType(list.item(i)); map.put(t.name(), t); } return map; } }
public MessageSchema( final Node schemaNode, final Map<String, Type> typeByNameMap, final Map<Long, Message> messageByIdMap) { this.packageName = getAttributeValue(schemaNode, "package"); this.description = getAttributeValueOrNull(schemaNode, "description"); this.id = Integer.parseInt(getAttributeValue(schemaNode, "id")); this.version = Integer.parseInt(getAttributeValue(schemaNode, "version", "0")); this.semanticVersion = getAttributeValueOrNull(schemaNode, "semanticVersion"); this.byteOrder = getByteOrder(getAttributeValue(schemaNode, "byteOrder", "littleEndian")); this.typeByNameMap = typeByNameMap; this.messageByIdMap = messageByIdMap; final String headerType = getAttributeValueOrNull(schemaNode, "headerType"); this.headerType = null == headerType ? HEADER_TYPE_DEFAULT : headerType; Verify.present(typeByNameMap, this.headerType, "Message header"); ((CompositeType)typeByNameMap.get(this.headerType)).checkForWellFormedMessageHeader(schemaNode); }
private Field parseGroupField(final NodeList nodeList, final int nodeIndex) throws XPathExpressionException { final String dimensionTypeName = getAttributeValue(nodeList.item(nodeIndex), "dimensionType", "groupSizeEncoding"); Type dimensionType = typeByNameMap.get(dimensionTypeName); if (dimensionType == null) { handleError(nodeList.item(nodeIndex), "could not find dimensionType: " + dimensionTypeName); } else if (!(dimensionType instanceof CompositeType)) { handleError(nodeList.item(nodeIndex), "dimensionType should be a composite type: " + dimensionTypeName); dimensionType = null; } else { ((CompositeType)dimensionType).checkForWellFormedGroupSizeEncoding(nodeList.item(nodeIndex)); } final Field field = new Field.Builder() .name(getAttributeValue(nodeList.item(nodeIndex), "name")) .description(getAttributeValueOrNull(nodeList.item(nodeIndex), "description")) .id(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "id"))) .blockLength(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "blockLength", "0"))) .sinceVersion(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "sinceVersion", "0"))) .dimensionType((CompositeType)dimensionType) .build(); XmlSchemaParser.checkForValidName(nodeList.item(nodeIndex), field.name()); field.groupFields(parseFieldsAndGroups(nodeList.item(nodeIndex))); // recursive call return field; }
@Test public void shouldCalculateDimensionSizeWithOffsetsSpecified() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "composite-offsets-schema.xml"), ParserOptions.DEFAULT); final CompositeType dimensions = schema.getMessage(1).fields().get(0).dimensionType(); assertThat(valueOf(dimensions.encodedLength()), is(valueOf(8))); } }