Example #1
0
        public BinarySdbSchemaDatas(FileFormatVersions fileFormat)
        {
            Debug.Assert(fileFormat.Any());

            SdbDataHead = new SdbDataHead();
            _fileFormat = fileFormat;

            var name = $"DocumentFormat.OpenXml.GeneratedCode.{fileFormat}.constraints";

#if DEBUG
            var names = typeof(ValidationResources).GetTypeInfo().Assembly.GetManifestResourceNames();
#endif

            using (var schema = typeof(ValidationResources).GetTypeInfo().Assembly.GetManifestResourceStream(name))
            {
                if (schema == null)
                {
                    var message = string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                                                ExceptionMessages.FileFormatNotSupported,
                                                fileFormat);

                    throw new ArgumentOutOfRangeException(nameof(fileFormat), message);
                }

                Load(schema);
            }
        }
Example #2
0
        /// <summary>
        /// Load the schema constraint data from the stream.
        /// </summary>
        /// <param name="dataStream">The data stream.</param>
        internal void Load(Stream dataStream)
        {
            Debug.Assert(dataStream != null);
            Debug.Assert(dataStream.CanRead);
            Debug.Assert(dataStream.CanSeek);
            Debug.Assert(dataStream.Length > SdbDataHead.HeadSize);

            byte[] headBytes = new byte[SdbDataHead.HeadSize];

            dataStream.Read(headBytes, 0, SdbDataHead.HeadSize);
            SdbDataHead.LoadFromBytes(headBytes, 0);

#if DEBUG
            CheckDataHead((int)dataStream.Length);
#endif

            byte[] dataBytes;
            int    count;

            // class ID map
            count     = SdbDataHead.ClassIdsCount * SdbClassIdToSchemaTypeIndex.TypeSize;
            dataBytes = new byte[count];
            dataStream.Read(dataBytes, 0, count);
            SdbClassIdMap = new SdbDataArray <SdbClassIdToSchemaTypeIndex>(dataBytes, SdbDataHead.ClassIdsCount);

            // schema types
            count     = SdbDataHead.SchemaTypeCount * SdbSchemaType.TypeSize;
            dataBytes = new byte[count];
            dataStream.Read(dataBytes, 0, count);
            SdbSchemaTypes = new SdbDataArray <SdbSchemaType>(dataBytes, SdbDataHead.SchemaTypeCount);

            // particle constraints
            count     = SdbDataHead.ParticleCount * SdbParticleConstraint.TypeSize;
            dataBytes = new byte[count];
            dataStream.Read(dataBytes, 0, count);
            SdbParticles = new SdbDataArray <SdbParticleConstraint>(dataBytes, SdbDataHead.ParticleCount);

            // particle children index
            count     = SdbDataHead.ParticleChildrenIndexCount * SdbParticleChildrenIndex.TypeSize;
            dataBytes = new byte[count];
            dataStream.Read(dataBytes, 0, count);
            SdbParticleIndexs = new SdbDataArray <SdbParticleChildrenIndex>(dataBytes, SdbDataHead.ParticleChildrenIndexCount);

            // attribute constraints
            count     = SdbDataHead.AttributeCount * SdbAttributeConstraint.TypeSize;
            dataBytes = new byte[count];
            dataStream.Read(dataBytes, 0, count);
            SdbAttributes = new SdbDataArray <SdbAttributeConstraint>(dataBytes, SdbDataHead.AttributeCount);

            // simple type constraints
            dataStream.Seek(SdbDataHead.SimpleTypeDataOffset, SeekOrigin.Begin);
            SimpleTypeRestrictions = Restrictions.SimpleTypeRestrictions.Deserialize(dataStream, _fileFormat);

#if DEBUG
            Assert(SdbDataHead.SimpleTypeCount == SimpleTypeRestrictions.SimpleTypeCount);

            CheckData();
#endif
        }
Example #3
0
        public void SdbDataHeadTest()
        {
            const FileFormatVersions FileFormat = FileFormatVersions.Office2010;
            var classIds                = new SdbClassIdToSchemaTypeIndex[5];
            var schemaTypes             = new SdbSchemaType[8];
            var particleConstraints     = new SdbParticleConstraint[2];
            var particleChildrenIndexes = new SdbParticleChildrenIndex[11];
            var attributes              = new SdbAttributeConstraint[4];

            var instance = new SdbDataHead(
                FileFormat,
                classIds,
                schemaTypes,
                particleConstraints,
                particleChildrenIndexes,
                attributes);

            Assert.Equal(FileFormat, instance.FileFormat);

            Assert.Equal(SdbSpan.Create(instance.ClassIds.Offset, classIds), instance.ClassIds);
            Assert.Equal(instance.ClassIds.End, instance.SchemaType.Offset);

            Assert.Equal(SdbSpan.Create(instance.SchemaType.Offset, schemaTypes), instance.SchemaType);
            Assert.Equal(instance.SchemaType.End, instance.Particles.Offset);

            Assert.Equal(SdbSpan.Create(instance.Particles.Offset, particleConstraints), instance.Particles);
            Assert.Equal(instance.Particles.End, instance.ParticleChildren.Offset);

            Assert.Equal(SdbSpan.Create(instance.ParticleChildren.Offset, particleChildrenIndexes), instance.ParticleChildren);
            Assert.Equal(instance.ParticleChildren.End, instance.Attributes.Offset);

            Assert.Equal(SdbSpan.Create(instance.Attributes.Offset, attributes), instance.Attributes);
            Assert.Equal(instance.Attributes.End, instance.End);

            var expected = new byte[]
            {
                0xB7, 0x55, 0xDD, 0x06, 0x4D, 0xEF, 0xEE, 0x46, 0xA6, 0x18, 0x04, 0x2A, 0xF4,
                0xC3, 0x90, 0x4E, 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00,
                0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
                0x00, 0x08, 0x00, 0x00, 0x00, 0xA4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
                0x0D, 0x00, 0x00, 0x00, 0xBE, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x02,
                0x00, 0x00, 0x00, 0xD4, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00,
                0x00, 0x00,
            };

            var bytes = VerifyBytes(instance, expected);

            var deserialized = Assert.Single(Deserialize <SdbDataHead>(bytes));

            Assert.Equal(instance, deserialized);
        }
Example #4
0
        public void SerializeSdbData(Stream stream)
        {
            var head = new SdbDataHead(
                _fileFormat,
                ClassIdMap,
                SchemaTypes,
                Particles,
                ParticleIndexes,
                Attributes);

            head.Validate();

            void ValidatePosition(int position)
            {
                if (position != stream.Position)
                {
                    throw new InvalidDataException();
                }
            }

            ValidatePosition(0);
            SdbData.Serialize(head, stream);

            ValidatePosition(head.ClassIds.Offset);
            SdbData.Serialize(ClassIdMap, stream);

            ValidatePosition(head.SchemaType.Offset);
            SdbData.Serialize(SchemaTypes, stream);

            ValidatePosition(head.Particles.Offset);
            SdbData.Serialize(Particles, stream);

            ValidatePosition(head.ParticleChildren.Offset);
            SdbData.Serialize(ParticleIndexes, stream);

            ValidatePosition(head.Attributes.Offset);
            SdbData.Serialize(Attributes, stream);
        }