public void AddField(ACompilerInfoField field) { uint skip = (_binarySize % field.Alignment); if (skip != 0) { var pad = new PaddingField(field.Alignment - skip); pad.Offset = _binarySize; _fields.Add(pad); _binarySize += pad.Size; } field.Offset = _binarySize; _fields.Add(field); _binarySize += field.Size; }
private static void Binarize(CompoundField compilerInfo, string prefix, XmlSchemaElement elem, uint index, uint cardinality) { XmlSchemaDatatypeVariety dataType = elem.ElementSchemaType.Datatype.Variety; string name = prefix + elem.Name; if (cardinality > 1) { throw new XmlBinaryConverterException("cardinality greater than 1 is not supported"); //name += "[" + index + "]"; } ACompilerInfoField cif = null; IEnumerable <XmlSchemaLengthFacet> lengthFacets; IEnumerable <XmlSchemaMaxLengthFacet> maxLengthFacet; IEnumerable <XmlSchemaEnumerationFacet> enumerationFacets; switch (elem.ElementSchemaType.TypeCode) { case XmlTypeCode.String: try { var simpleType = elem.ElementSchemaType as XmlSchemaSimpleType; var restriction = simpleType.Content as XmlSchemaSimpleTypeRestriction; maxLengthFacet = restriction.Facets.OfType <XmlSchemaMaxLengthFacet>(); enumerationFacets = restriction.Facets.OfType <XmlSchemaEnumerationFacet>(); } catch (Exception e) { throw new XmlBinaryConverterException($"Unexpected exception while processing element {name}"); } if (maxLengthFacet.Count() != 0) { cif = new StringField(name, uint.Parse(maxLengthFacet.ElementAt(0).Value)); } else if (enumerationFacets.Count() != 0) { List <string> values = enumerationFacets.Select(e => e.Value).ToList(); cif = new StringEnumField(name, values); } else { throw new XmlBinaryConverterException( $"String {name} have unrestricted length and thus is not supported for schema generation"); } break; case XmlTypeCode.Int: cif = new Int32Field(name); break; case XmlTypeCode.UnsignedInt: cif = new UInt32Field(name); break; case XmlTypeCode.Short: cif = new Int16Field(name); break; case XmlTypeCode.UnsignedShort: cif = new UInt16Field(name); break; case XmlTypeCode.Byte: cif = new SByteField(name); break; case XmlTypeCode.UnsignedByte: cif = new ByteField(name); break; case XmlTypeCode.UnsignedLong: cif = new UInt64Field(name); break; case XmlTypeCode.Boolean: cif = new BooleanField(name); break; case XmlTypeCode.HexBinary: try { var simpleType = elem.ElementSchemaType as XmlSchemaSimpleType; var restriction = simpleType.Content as XmlSchemaSimpleTypeRestriction; lengthFacets = restriction.Facets.OfType <XmlSchemaLengthFacet>(); } catch (Exception e) { throw new XmlBinaryConverterException($"Unexpected exception while processing element {name}"); } if (lengthFacets.Count() != 0) { uint size = uint.Parse(lengthFacets.ElementAt(0).Value); cif = new HexBinaryField(name, size); } else { throw new XmlBinaryConverterException( $"HexBinary {name} have unrestricted length and thus is not supported for schema generation"); } break; case XmlTypeCode.DateTime: cif = new DateTimeField(name, "yyyyMMddHHmmss"); break; default: { throw new XmlBinaryConverterException($"{name} have unsupported type {elem.ElementSchemaType.TypeCode}"); } } if (cif == null) { throw new XmlBinaryConverterException($"Internal error: unable to create a conversion structure for {name}"); } compilerInfo.AddField(cif); }