File: XmlQueryStaticData.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (404 lines) | stat: -rw-r--r-- 14,704 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//------------------------------------------------------------------------------
// <copyright file="XmlQueryStaticData.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml.Xsl.IlGen;
using System.Xml.Xsl.Qil;

namespace System.Xml.Xsl.Runtime {
    /// <summary>
    /// Contains all static data that is used by the runtime.
    /// </summary>
    internal class XmlQueryStaticData {
        // Name of the field to serialize to
        public const string DataFieldName   = "staticData";
        public const string TypesFieldName  = "ebTypes";

        // Format version marker to support versioning: (major << 8) | minor
        private const int CurrentFormatVersion = (0 << 8) | 0;

        private XmlWriterSettings defaultWriterSettings;
        private IList<WhitespaceRule> whitespaceRules;
        private string[] names;
        private StringPair[][] prefixMappingsList;
        private Int32Pair[] filters;
        private XmlQueryType[] types;
        private XmlCollation[] collations;
        private string[] globalNames;
        private EarlyBoundInfo[] earlyBound;

        /// <summary>
        /// Constructor.
        /// </summary>
        public XmlQueryStaticData(XmlWriterSettings defaultWriterSettings, IList<WhitespaceRule> whitespaceRules, StaticDataManager staticData) {
            Debug.Assert(defaultWriterSettings != null && staticData != null);
            this.defaultWriterSettings = defaultWriterSettings;
            this.whitespaceRules = whitespaceRules;
            this.names = staticData.Names;
            this.prefixMappingsList = staticData.PrefixMappingsList;
            this.filters = staticData.NameFilters;
            this.types = staticData.XmlTypes;
            this.collations = staticData.Collations;
            this.globalNames = staticData.GlobalNames;
            this.earlyBound = staticData.EarlyBound;

        #if DEBUG
            // Round-trip check
            byte[] data;
            Type[] ebTypes;
            this.GetObjectData(out data, out ebTypes);
            XmlQueryStaticData copy = new XmlQueryStaticData(data, ebTypes);

            this.defaultWriterSettings = copy.defaultWriterSettings;
            this.whitespaceRules = copy.whitespaceRules;
            this.names = copy.names;
            this.prefixMappingsList = copy.prefixMappingsList;
            this.filters = copy.filters;
            this.types = copy.types;
            this.collations = copy.collations;
            this.globalNames = copy.globalNames;
            this.earlyBound = copy.earlyBound;
        #endif
        }

        /// <summary>
        /// Deserialize XmlQueryStaticData object from a byte array.
        /// </summary>
        public XmlQueryStaticData(byte[] data, Type[] ebTypes) {
            MemoryStream dataStream = new MemoryStream(data, /*writable:*/false);
            XmlQueryDataReader dataReader = new XmlQueryDataReader(dataStream);
            int length;

            // Read a format version
            int formatVersion = dataReader.ReadInt32Encoded();

            // Changes in the major part of version are not supported
            if ((formatVersion & ~0xff) > CurrentFormatVersion)
                throw new NotSupportedException();

            // XmlWriterSettings defaultWriterSettings;
            defaultWriterSettings = new XmlWriterSettings(dataReader);

            // IList<WhitespaceRule> whitespaceRules;
            length = dataReader.ReadInt32();
            if (length != 0) {
                this.whitespaceRules = new WhitespaceRule[length];
                for (int idx = 0; idx < length; idx++) {
                    this.whitespaceRules[idx] = new WhitespaceRule(dataReader);
                }
            }

            // string[] names;
            length = dataReader.ReadInt32();
            if (length != 0) {
                this.names = new string[length];
                for (int idx = 0; idx < length; idx++) {
                    this.names[idx] = dataReader.ReadString();
                }
            }

            // StringPair[][] prefixMappingsList;
            length = dataReader.ReadInt32();
            if (length != 0) {
                this.prefixMappingsList = new StringPair[length][];
                for (int idx = 0; idx < length; idx++) {
                    int length2 = dataReader.ReadInt32();
                    this.prefixMappingsList[idx] = new StringPair[length2];
                    for (int idx2 = 0; idx2 < length2; idx2++) {
                        this.prefixMappingsList[idx][idx2] = new StringPair(dataReader.ReadString(), dataReader.ReadString());
                    }
                }
            }

            // Int32Pair[] filters;
            length = dataReader.ReadInt32();
            if (length != 0) {
                this.filters = new Int32Pair[length];
                for (int idx = 0; idx < length; idx++) {
                    this.filters[idx] = new Int32Pair(dataReader.ReadInt32Encoded(), dataReader.ReadInt32Encoded());
                }
            }

            // XmlQueryType[] types;
            length = dataReader.ReadInt32();
            if (length != 0) {
                this.types = new XmlQueryType[length];
                for (int idx = 0; idx < length; idx++) {
                    this.types[idx] = XmlQueryTypeFactory.Deserialize(dataReader);
                }
            }

            // XmlCollation[] collations;
            length = dataReader.ReadInt32();
            if (length != 0) {
                this.collations = new XmlCollation[length];
                for (int idx = 0; idx < length; idx++) {
                    this.collations[idx] = new XmlCollation(dataReader);
                }
            }

            // string[] globalNames;
            length = dataReader.ReadInt32();
            if (length != 0) {
                this.globalNames = new string[length];
                for (int idx = 0; idx < length; idx++) {
                    this.globalNames[idx] = dataReader.ReadString();
                }
            }

            // EarlyBoundInfo[] earlyBound;
            length = dataReader.ReadInt32();
            if (length != 0) {
                this.earlyBound = new EarlyBoundInfo[length];
                for (int idx = 0; idx < length; idx++) {
                    this.earlyBound[idx] = new EarlyBoundInfo(dataReader.ReadString(), ebTypes[idx]);
                }
            }

            Debug.Assert(formatVersion != CurrentFormatVersion || dataReader.Read() == -1, "Extra data at the end of the stream");
            dataReader.Close();
        }

        /// <summary>
        /// Serialize XmlQueryStaticData object into a byte array.
        /// </summary>
        public void GetObjectData(out byte[] data, out Type[] ebTypes) {
            MemoryStream dataStream = new MemoryStream(4096);
            XmlQueryDataWriter dataWriter = new XmlQueryDataWriter(dataStream);

            // First put the format version
            dataWriter.WriteInt32Encoded(CurrentFormatVersion);

            // XmlWriterSettings defaultWriterSettings;
            defaultWriterSettings.GetObjectData(dataWriter);

            // IList<WhitespaceRule> whitespaceRules;
            if (this.whitespaceRules == null) {
                dataWriter.Write(0);
            }
            else {
                dataWriter.Write(this.whitespaceRules.Count);
                foreach (WhitespaceRule rule in this.whitespaceRules) {
                    rule.GetObjectData(dataWriter);
                }
            }

            // string[] names;
            if (this.names == null) {
                dataWriter.Write(0);
            }
            else {
                dataWriter.Write(this.names.Length);
                foreach (string name in this.names) {
                    dataWriter.Write(name);
                }
            }

            // StringPair[][] prefixMappingsList;
            if (this.prefixMappingsList == null) {
                dataWriter.Write(0);
            }
            else {
                dataWriter.Write(this.prefixMappingsList.Length);
                foreach (StringPair[] mappings in this.prefixMappingsList) {
                    dataWriter.Write(mappings.Length);
                    foreach (StringPair mapping in mappings) {
                        dataWriter.Write(mapping.Left);
                        dataWriter.Write(mapping.Right);
                    }
                }
            }

            // Int32Pair[] filters;
            if (this.filters == null) {
                dataWriter.Write(0);
            }
            else {
                dataWriter.Write(this.filters.Length);
                foreach (Int32Pair filter in this.filters) {
                    dataWriter.WriteInt32Encoded(filter.Left);
                    dataWriter.WriteInt32Encoded(filter.Right);
                }
            }

            // XmlQueryType[] types;
            if (this.types == null) {
                dataWriter.Write(0);
            }
            else {
                dataWriter.Write(this.types.Length);
                foreach (XmlQueryType type in this.types) {
                    XmlQueryTypeFactory.Serialize(dataWriter, type);                    
                }
            }

            // XmlCollation[] collations;
            if (collations == null) {
                dataWriter.Write(0);
            }
            else {
                dataWriter.Write(this.collations.Length);
                foreach (XmlCollation collation in this.collations) {
                    collation.GetObjectData(dataWriter);
                }
            }

            // string[] globalNames;
            if (this.globalNames == null) {
                dataWriter.Write(0);
            }
            else {
                dataWriter.Write(this.globalNames.Length);
                foreach (string name in this.globalNames) {
                    dataWriter.Write(name);
                }
            }

            // EarlyBoundInfo[] earlyBound;
            if (this.earlyBound == null) {
                dataWriter.Write(0);
                ebTypes = null;
            }
            else {
                dataWriter.Write(this.earlyBound.Length);
                ebTypes = new Type[this.earlyBound.Length];
                int idx = 0;
                foreach (EarlyBoundInfo info in this.earlyBound) {
                    dataWriter.Write(info.NamespaceUri);
                    ebTypes[idx++] = info.EarlyBoundType;
                }
            }

            dataWriter.Close();
            data = dataStream.ToArray();
        }

        /// <summary>
        /// Return the default writer settings.
        /// </summary>
        public XmlWriterSettings DefaultWriterSettings {
            get { return this.defaultWriterSettings; }
        }

        /// <summary>
        /// Return the rules used for whitespace stripping/preservation.
        /// </summary>
        public IList<WhitespaceRule> WhitespaceRules {
            get { return this.whitespaceRules; }
        }

        /// <summary>
        /// Return array of names used by this query.
        /// </summary>
        public string[] Names {
            get { return this.names; }
        }

        /// <summary>
        /// Return array of prefix mappings used by this query.
        /// </summary>
        public StringPair[][] PrefixMappingsList {
            get { return this.prefixMappingsList; }
        }

        /// <summary>
        /// Return array of name filter specifications used by this query.
        /// </summary>
        public Int32Pair[] Filters {
            get { return this.filters; }
        }

        /// <summary>
        /// Return array of types used by this query.
        /// </summary>
        public XmlQueryType[] Types {
            get { return this.types; }
        }

        /// <summary>
        /// Return array of collations used by this query.
        /// </summary>
        public XmlCollation[] Collations {
            get { return this.collations; }
        }

        /// <summary>
        /// Return names of all global variables and parameters used by this query.
        /// </summary>
        public string[] GlobalNames {
            get { return this.globalNames; }
        }

        /// <summary>
        /// Return array of early bound object information related to this query.
        /// </summary>
        public EarlyBoundInfo[] EarlyBound {
            get { return this.earlyBound; }
        }
    }

    /// <summary>
    /// Subclass of BinaryReader used to serialize query static data.
    /// </summary>
    internal class XmlQueryDataReader : BinaryReader {
        public XmlQueryDataReader(Stream input) : base(input) { }

        /// <summary>
        /// Read in a 32-bit integer in compressed format.
        /// </summary>
        public int ReadInt32Encoded() {
            return Read7BitEncodedInt();
        }

        /// <summary>
        /// Read a string value from the stream. Value can be null.
        /// </summary>
        public string ReadStringQ() {
            return ReadBoolean() ? ReadString() : null;
        }

        /// <summary>
        /// Read a signed byte value from the stream and check if it belongs to the given diapason.
        /// </summary>
        public sbyte ReadSByte(sbyte minValue, sbyte maxValue) {
            sbyte value = ReadSByte();
            if (value < minValue)
                throw new ArgumentOutOfRangeException("minValue");
            if (maxValue < value)
                throw new ArgumentOutOfRangeException("maxValue");

            return value;
        }
    }

    /// <summary>
    /// Subclass of BinaryWriter used to deserialize query static data.
    /// </summary>
    internal class XmlQueryDataWriter : BinaryWriter {
        public XmlQueryDataWriter(Stream output) : base(output) { }

        /// <summary>
        /// Write a 32-bit integer in a compressed format.
        /// </summary>
        public void WriteInt32Encoded(int value) {
            Write7BitEncodedInt(value);
        }

        /// <summary>
        /// Write a string value to the stream. Value can be null.
        /// </summary>
        public void WriteStringQ(string value) {
            Write(value != null);
            if (value != null) {
                Write(value);
            }
        }
    }
}