예제 #1
0
        /// <summary>
        /// Creates an instance of <see cref="SdbSchemaData"/> using embedded streams for constraint data and simple types.
        /// </summary>
        /// <remarks>
        /// Constraint data are contained in a binary file that is defined as:
        ///
        /// <![CDATA[
        /// [SdbDataHead][SdbClassIdToSchemaTypeIndex][SdbSchemaType][SdbParticleConstraint][SdbParticleChildrenIndex][SdbAttributeConstraint]
        /// ]]>
        ///
        /// Since the size of the arrays are different for different <paramref name="fileFormat"/>, these are all marshalled separately, with the information
        /// of offsets contained in <see cref="SdbDataHead"/>. These items are deserialized in this method, while <see cref="SerializeSdbData(Stream)"/> provides
        /// the infrastructure to serialize them back to a binary file in the correct order
        ///
        /// Simple type data is contained in an XML file that is deserialized with <see cref="GetSimpleTypeRestrictions(FileFormatVersions)"/>
        /// </remarks>
        /// <param name="fileFormat">The version to load</param>
        private SdbSchemaData(FileFormatVersions fileFormat)
        {
            if (!fileFormat.Any())
            {
                throw new ArgumentOutOfRangeException(nameof(fileFormat));
            }

            _fileFormat  = fileFormat;
            Restrictions = GetSimpleTypeRestrictions(fileFormat);

            using (var schema = GetStream(fileFormat, Constraints))
            {
                var dataHead = SdbData.Deserialize <SdbDataHead>(schema);

                dataHead.Validate();

                var length = (int)schema.Length;

                if (dataHead.End != length)
                {
                    throw new InvalidDataException();
                }

                ClassIdMap      = SdbData.Deserialize <SdbClassIdToSchemaTypeIndex>(schema, dataHead.ClassIds);
                SchemaTypes     = SdbData.Deserialize <SdbSchemaType>(schema, dataHead.SchemaType);
                Particles       = SdbData.Deserialize <SdbParticleConstraint>(schema, dataHead.Particles);
                ParticleIndexes = SdbData.Deserialize <SdbParticleChildrenIndex>(schema, dataHead.ParticleChildren);
                Attributes      = SdbData.Deserialize <SdbAttributeConstraint>(schema, dataHead.Attributes);
            }
        }
예제 #2
0
 private static SimpleTypeRestrictions GetSimpleTypeRestrictions(FileFormatVersions fileFormat)
 {
     using (var simpleTypes = GetStream(fileFormat, SimpleTypes))
     {
         return(SimpleTypeRestrictions.Deserialize(simpleTypes, fileFormat));
     }
 }
예제 #3
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);
            this.SdbDataHead.LoadFromBytes(headBytes, 0);

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

            byte[] dataBytes;
            int    count;

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

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

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

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

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

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

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

            CheckData();
#endif
        }
예제 #4
0
 public SdbSchemaData(
     FileFormatVersions fileFormat,
     SdbClassIdToSchemaTypeIndex[] classIdMap,
     SdbSchemaType[] schemaTypes,
     SdbParticleConstraint[] particles,
     SdbParticleChildrenIndex[] particleIndexes,
     SdbAttributeConstraint[] attributes,
     SimpleTypeRestrictions restrictions)
 {
     _fileFormat     = fileFormat;
     ClassIdMap      = classIdMap;
     SchemaTypes     = schemaTypes;
     Particles       = particles;
     ParticleIndexes = particleIndexes;
     Attributes      = attributes;
     Restrictions    = restrictions;
 }