~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins.Serialization/BinaryXmlReader.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// BinaryXmlReader.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
 
 
30
using System;
 
31
using System.Collections;
 
32
using System.Text;
 
33
using System.IO;
 
34
 
 
35
namespace Mono.Addins.Serialization
 
36
{
 
37
        internal class BinaryXmlReader
 
38
        {
 
39
                BinaryReader reader;
 
40
                
 
41
                internal const byte TagEndOfFile = 0;
 
42
                internal const byte TagBeginElement = 1;
 
43
                internal const byte TagEndElement = 2;
 
44
                internal const byte TagValue = 4;
 
45
                
 
46
                internal const byte TagObject = 5;
 
47
                internal const byte TagObjectArray = 6;
 
48
                internal const byte TagObjectDictionary = 7;
 
49
                internal const byte TagObjectNull = 8;
 
50
                
 
51
                byte currentType;
 
52
                string currentName;
 
53
                ArrayList stringTable = new ArrayList ();
 
54
                BinaryXmlTypeMap typeMap;
 
55
                object contextData;
 
56
                bool ignoreDesc;
 
57
                
 
58
                public BinaryXmlReader (Stream stream, BinaryXmlTypeMap typeMap)
 
59
                {
 
60
                        reader = new BinaryReader (stream);
 
61
                        this.typeMap = typeMap;
 
62
                        ReadNext ();
 
63
                }
 
64
                
 
65
                public BinaryXmlTypeMap TypeMap {
 
66
                        get { return typeMap; }
 
67
                        set { typeMap = value; }
 
68
                }
 
69
                
 
70
                public object ContextData {
 
71
                        get { return contextData; }
 
72
                        set { contextData = value; }
 
73
                }
 
74
                
 
75
                // Returns 'true' if description data must be ignored when reading the contents of a file
 
76
                public bool IgnoreDescriptionData {
 
77
                        get { return ignoreDesc; }
 
78
                        set { ignoreDesc = value; }
 
79
                }
 
80
                
 
81
                void ReadNext ()
 
82
                {
 
83
                        int b = reader.BaseStream.ReadByte ();
 
84
                        if (b == -1) {
 
85
                                currentType = TagEndOfFile;
 
86
                                return;
 
87
                        }
 
88
                        currentType = (byte) b;
 
89
                        if (currentType == TagBeginElement || currentType == TagValue)
 
90
                                currentName = ReadString ();
 
91
                }
 
92
                
 
93
                string ReadString ()
 
94
                {
 
95
                        // The first integer means:
 
96
                        // >=0: string of the specified length
 
97
                        // -1: null string
 
98
                        // <-1: a string from the string table
 
99
                        
 
100
                        int len = reader.ReadInt32 ();
 
101
                        if (len == -1)
 
102
                                return null;
 
103
                        if (len < -1)
 
104
                                return (string) stringTable [-(len + 2)];
 
105
 
 
106
                        byte[] bytes = new byte [len];
 
107
                        int n = 0;
 
108
                        while (n < len) {
 
109
                                int read = reader.Read (bytes, n, len - n);
 
110
                                if (read == 0)
 
111
                                        throw new InvalidOperationException ("Length too high for string: " + len);
 
112
                                n += read;
 
113
                        }
 
114
                        string s = Encoding.UTF8.GetString (bytes);
 
115
                        stringTable.Add (s);
 
116
                        return s;
 
117
                }
 
118
                
 
119
                public string LocalName {
 
120
                        get { return currentName; }
 
121
                }
 
122
                
 
123
                public bool IsElement {
 
124
                        get { return currentType == TagBeginElement; }
 
125
                }
 
126
                
 
127
                public bool IsValue {
 
128
                        get { return currentType == TagValue; }
 
129
                }
 
130
                
 
131
                TypeCode ReadValueType (TypeCode type)
 
132
                {
 
133
                        if (currentType != TagValue)
 
134
                                throw new InvalidOperationException ("Reader not positioned on a value.");
 
135
                        TypeCode t = (TypeCode) reader.ReadByte ();
 
136
                        if (t != type && type != TypeCode.Empty)
 
137
                                throw new InvalidOperationException ("Invalid value type. Expected " + type + ", found " + t);
 
138
                        return t;
 
139
                }
 
140
                
 
141
                public string ReadStringValue (string name)
 
142
                {
 
143
                        if (!SkipToValue (name))
 
144
                                return null;
 
145
                        return ReadStringValue ();
 
146
                }
 
147
                
 
148
                public string ReadStringValue ()
 
149
                {
 
150
                        if (currentType != TagValue)
 
151
                                throw new InvalidOperationException ("Reader not positioned on a value.");
 
152
                                
 
153
                        TypeCode t = (TypeCode) reader.ReadByte ();
 
154
                        if (t == TypeCode.Empty) {
 
155
                                ReadNext ();
 
156
                                return null;
 
157
                        }
 
158
                        if (t != TypeCode.String)
 
159
                                throw new InvalidOperationException ("Invalid value type. Expected String, found " + t);
 
160
                        
 
161
                        string s = ReadString ();
 
162
                        ReadNext ();
 
163
                        return s;
 
164
                }
 
165
                
 
166
                public bool ReadBooleanValue (string name)
 
167
                {
 
168
                        if (!SkipToValue (name))
 
169
                                return false;
 
170
                        return ReadBooleanValue ();
 
171
                }
 
172
                
 
173
                public bool ReadBooleanValue ()
 
174
                {
 
175
                        ReadValueType (TypeCode.Boolean);
 
176
                        bool value = reader.ReadBoolean ();
 
177
                        ReadNext ();
 
178
                        return value;
 
179
                }
 
180
                
 
181
                public char ReadCharValue (string name)
 
182
                {
 
183
                        if (!SkipToValue (name))
 
184
                                return (char)0;
 
185
                        return ReadCharValue ();
 
186
                }
 
187
                
 
188
                public char ReadCharValue ()
 
189
                {
 
190
                        ReadValueType (TypeCode.Char);
 
191
                        char value = reader.ReadChar ();
 
192
                        ReadNext ();
 
193
                        return value;
 
194
                }
 
195
                
 
196
                public byte ReadByteValue (string name)
 
197
                {
 
198
                        if (!SkipToValue (name))
 
199
                                return (byte)0;
 
200
                        return ReadByteValue ();
 
201
                }
 
202
                
 
203
                public byte ReadByteValue ()
 
204
                {
 
205
                        ReadValueType (TypeCode.Byte);
 
206
                        byte value = reader.ReadByte ();
 
207
                        ReadNext ();
 
208
                        return value;
 
209
                }
 
210
                
 
211
                public short ReadInt16Value (string name)
 
212
                {
 
213
                        if (!SkipToValue (name))
 
214
                                return (short)0;
 
215
                        return ReadInt16Value ();
 
216
                }
 
217
                
 
218
                public short ReadInt16Value ()
 
219
                {
 
220
                        ReadValueType (TypeCode.Int16);
 
221
                        short value = reader.ReadInt16 ();
 
222
                        ReadNext ();
 
223
                        return value;
 
224
                }
 
225
                
 
226
                public int ReadInt32Value (string name)
 
227
                {
 
228
                        if (!SkipToValue (name))
 
229
                                return 0;
 
230
                        return ReadInt32Value ();
 
231
                }
 
232
                
 
233
                public int ReadInt32Value ()
 
234
                {
 
235
                        ReadValueType (TypeCode.Int32);
 
236
                        int value = reader.ReadInt32 ();
 
237
                        ReadNext ();
 
238
                        return value;
 
239
                }
 
240
                
 
241
                public long ReadInt64Value (string name)
 
242
                {
 
243
                        if (!SkipToValue (name))
 
244
                                return (long)0;
 
245
                        return ReadInt64Value ();
 
246
                }
 
247
                
 
248
                public long ReadInt64Value ()
 
249
                {
 
250
                        ReadValueType (TypeCode.Int64);
 
251
                        long value = reader.ReadInt64 ();
 
252
                        ReadNext ();
 
253
                        return value;
 
254
                }
 
255
                
 
256
                public DateTime ReadDateTimeValue (string name)
 
257
                {
 
258
                        if (!SkipToValue (name))
 
259
                                return DateTime.MinValue;
 
260
                        return ReadDateTimeValue ();
 
261
                }
 
262
                
 
263
                public DateTime ReadDateTimeValue ()
 
264
                {
 
265
                        ReadValueType (TypeCode.DateTime);
 
266
                        DateTime value = new DateTime (reader.ReadInt64 ());
 
267
                        ReadNext ();
 
268
                        return value;
 
269
                }
 
270
                
 
271
                public object ReadValue (string name)
 
272
                {
 
273
                        if (!SkipToValue (name))
 
274
                                return null;
 
275
                        return ReadValue ();
 
276
                }
 
277
                
 
278
                public object ReadValue ()
 
279
                {
 
280
                        object res = ReadValueInternal ();
 
281
                        ReadNext ();
 
282
                        return res;
 
283
                }
 
284
                
 
285
                public object ReadValue (string name, object targetInstance)
 
286
                {
 
287
                        if (!SkipToValue (name))
 
288
                                return null;
 
289
                        return ReadValue (targetInstance);
 
290
                }
 
291
                
 
292
                public object ReadValue (object targetInstance)
 
293
                {
 
294
                        TypeCode t = (TypeCode) reader.ReadByte ();
 
295
                        if (t == TypeCode.Empty) {
 
296
                                ReadNext ();
 
297
                                return null;
 
298
                        }
 
299
                        if (t != TypeCode.Object)
 
300
                                throw new InvalidOperationException ("Invalid value type. Expected Object, found " + t);
 
301
                        
 
302
                        object res = ReadObject (targetInstance);
 
303
                        ReadNext ();
 
304
                        return res;
 
305
                }
 
306
                
 
307
                object ReadValueInternal ()
 
308
                {
 
309
                        TypeCode t = (TypeCode) reader.ReadByte ();
 
310
                        if (t == TypeCode.Empty)
 
311
                                return null;
 
312
                        return ReadValueInternal (t);
 
313
                }
 
314
                
 
315
                object ReadValueInternal (TypeCode t)
 
316
                {
 
317
                        object res;
 
318
                        switch (t) {
 
319
                                case TypeCode.Boolean: res = reader.ReadBoolean (); break;
 
320
                                case TypeCode.Char: res = reader.ReadChar (); break;
 
321
                                case TypeCode.SByte: res = reader.ReadSByte (); break;
 
322
                                case TypeCode.Byte: res = reader.ReadByte (); break;
 
323
                                case TypeCode.Int16: res = reader.ReadInt16 (); break;
 
324
                                case TypeCode.UInt16: res = reader.ReadUInt16 (); break;
 
325
                                case TypeCode.Int32: res = reader.ReadInt32 (); break;
 
326
                                case TypeCode.UInt32: res = reader.ReadUInt32 (); break;
 
327
                                case TypeCode.Int64: res = reader.ReadInt64 (); break;
 
328
                                case TypeCode.UInt64: res = reader.ReadUInt64 (); break;
 
329
                                case TypeCode.Single: res = reader.ReadSingle (); break;
 
330
                                case TypeCode.Double: res = reader.ReadDouble (); break;
 
331
                                case TypeCode.DateTime: res = new DateTime (reader.ReadInt64 ()); break;
 
332
                                case TypeCode.String: res = ReadString (); break;
 
333
                                case TypeCode.Object: res = ReadObject (null); break;
 
334
                                case TypeCode.Empty: res = null; break;
 
335
                                default:
 
336
                                        throw new InvalidOperationException ("Unexpected value type: " + t);
 
337
                        }
 
338
                        return res;
 
339
                }
 
340
                
 
341
                bool SkipToValue (string name)
 
342
                {
 
343
                        do {
 
344
                                if ((currentType == TagBeginElement || currentType == TagValue) && currentName == name)
 
345
                                        return true;
 
346
                                if (EndOfElement)
 
347
                                        return false;
 
348
                                Skip ();
 
349
                        } while (true);
 
350
                }
 
351
 
 
352
                public void ReadBeginElement ()
 
353
                {
 
354
                        if (currentType != TagBeginElement)
 
355
                                throw new InvalidOperationException ("Reader not positioned on an element.");
 
356
                        ReadNext ();
 
357
                }
 
358
                
 
359
                public void ReadEndElement ()
 
360
                {
 
361
                        if (currentType != TagEndElement)
 
362
                                throw new InvalidOperationException ("Reader not positioned on an element.");
 
363
                        ReadNext ();
 
364
                }
 
365
                
 
366
                public bool EndOfElement {
 
367
                        get { return currentType == TagEndElement; }
 
368
                }
 
369
                
 
370
                public void Skip ()
 
371
                {
 
372
                        if (currentType == TagValue)
 
373
                                ReadValue ();
 
374
                        else if (currentType == TagEndElement)
 
375
                                ReadNext ();
 
376
                        else if (currentType == TagBeginElement) {
 
377
                                ReadNext ();
 
378
                                while (!EndOfElement)
 
379
                                        Skip ();
 
380
                                ReadNext ();
 
381
                        }
 
382
                }
 
383
                
 
384
                object ReadObject (object targetInstance)
 
385
                {
 
386
                        byte ot = reader.ReadByte ();
 
387
                        if (ot == TagObjectNull) {
 
388
                                return null;
 
389
                        }
 
390
                        else if (ot == TagObject) {
 
391
                                string tname = ReadString ();
 
392
                                IBinaryXmlElement ob;
 
393
                                if (targetInstance != null) {
 
394
                                        ob = targetInstance as IBinaryXmlElement;
 
395
                                        if (ob == null)
 
396
                                                throw new InvalidOperationException ("Target instance has an invalid type. Expected an IBinaryXmlElement implementation.");
 
397
                                } else {
 
398
                                        ob = typeMap.CreateObject (tname);
 
399
                                }
 
400
                                ReadNext ();
 
401
                                ob.Read (this);
 
402
                                while (currentType != TagEndElement)
 
403
                                        Skip ();
 
404
                                return ob;
 
405
                        }
 
406
                        else if (ot == TagObjectArray) {
 
407
                                TypeCode tc = (TypeCode) reader.ReadByte ();
 
408
                                int len = reader.ReadInt32 ();
 
409
                                if (targetInstance != null) {
 
410
                                        IList list = targetInstance as IList;
 
411
                                        if (list == null)
 
412
                                                throw new InvalidOperationException ("Target instance has an invalid type. Expected an IList implementation.");
 
413
                                        for (int n=0; n<len; n++)
 
414
                                                list.Add (ReadValueInternal ());
 
415
                                        return list;
 
416
                                }
 
417
                                else {
 
418
                                        Array obs = CreateArray (tc, len);
 
419
                                        for (int n=0; n<len; n++)
 
420
                                                obs.SetValue (ReadValueInternal (), n);
 
421
                                        return obs;
 
422
                                }
 
423
                        }
 
424
                        else if (ot == TagObjectDictionary) {
 
425
                                int len = reader.ReadInt32 ();
 
426
                                IDictionary table;
 
427
                                if (targetInstance != null) {
 
428
                                        table = targetInstance as IDictionary;
 
429
                                        if (table == null)
 
430
                                                throw new InvalidOperationException ("Target instance has an invalid type. Expected an IDictionary implementation.");
 
431
                                } else {
 
432
                                        table = new Hashtable ();
 
433
                                }
 
434
                                for (int n=0; n<len; n++) {
 
435
                                        object key = ReadValueInternal ();
 
436
                                        object val = ReadValueInternal ();
 
437
                                        table [key] = val;
 
438
                                }
 
439
                                return table;
 
440
                        }
 
441
                        else
 
442
                                throw new InvalidOperationException ("Unknown object type tag: " + ot);
 
443
                }
 
444
                
 
445
                Array CreateArray (TypeCode t, int len)
 
446
                {
 
447
                        switch (t) {
 
448
                                case TypeCode.Boolean: return new bool [len];
 
449
                                case TypeCode.Char: return new Char [len];
 
450
                                case TypeCode.SByte: return new SByte [len];
 
451
                                case TypeCode.Byte: return new Byte [len];
 
452
                                case TypeCode.Int16: return new Int16 [len];
 
453
                                case TypeCode.UInt16: return new UInt16 [len];
 
454
                                case TypeCode.Int32: return new Int32 [len];
 
455
                                case TypeCode.UInt32: return new UInt32 [len];
 
456
                                case TypeCode.Int64: return new Int64 [len];
 
457
                                case TypeCode.UInt64: return new UInt64 [len];
 
458
                                case TypeCode.Single: return new Single [len];
 
459
                                case TypeCode.Double: return new Double [len];
 
460
                                case TypeCode.DateTime: return new DateTime [len];
 
461
                                case TypeCode.String: return new String [len];
 
462
                                case TypeCode.Object: return new Object [len];
 
463
                                default:
 
464
                                        throw new InvalidOperationException ("Unexpected value type: " + t);
 
465
                        }
 
466
                }
 
467
                
 
468
                const int IndSize = 2;
 
469
                
 
470
                public static void DumpFile (string file)
 
471
                {
 
472
                        Console.WriteLine ("FILE: " + file);
 
473
                        using (Stream s = File.OpenRead (file)) {
 
474
                                BinaryXmlReader r = new BinaryXmlReader (s, new BinaryXmlTypeMap ());
 
475
                                r.Dump (0);
 
476
                        }
 
477
                }
 
478
                
 
479
                public void Dump (int ind)
 
480
                {
 
481
                        if (currentType == TagValue) {
 
482
                                Console.Write (new string (' ', ind) + LocalName + ": ");
 
483
                                DumpValue (ind);
 
484
                                Console.WriteLine ();
 
485
                        }
 
486
                        else if (currentType == TagBeginElement) {
 
487
                                string name = LocalName;
 
488
                                Console.WriteLine (new string (' ', ind) + "<" + name + ">");
 
489
                                DumpElement (ind + IndSize);
 
490
                                Console.WriteLine (new string (' ', ind) + "</" + name + ">");
 
491
                        }
 
492
                }
 
493
                
 
494
                public void DumpElement (int ind)
 
495
                {
 
496
                        ReadNext ();
 
497
                        while (currentType != TagEndElement) {
 
498
                                Dump (ind + IndSize);
 
499
                                ReadNext ();
 
500
                        }
 
501
                }
 
502
                
 
503
                void DumpValue (int ind)
 
504
                {
 
505
                        TypeCode t = (TypeCode) reader.ReadByte ();
 
506
                        if (t != TypeCode.Object) {
 
507
                                object ob = ReadValueInternal (t);
 
508
                                if (ob == null) ob = "(null)";
 
509
                                Console.Write (ob);
 
510
                        } else {
 
511
                                byte ot = reader.ReadByte ();
 
512
                                switch (ot) {
 
513
                                        case TagObjectNull: {
 
514
                                                Console.Write ("(null)");
 
515
                                                break;
 
516
                                        }
 
517
                                        case TagObject: {
 
518
                                                string tname = ReadString ();
 
519
                                                Console.WriteLine ("(" + tname + ")");
 
520
                                                DumpElement (ind + IndSize);
 
521
                                                break;
 
522
                                        }
 
523
                                        case TagObjectArray: {
 
524
                                                TypeCode tc = (TypeCode) reader.ReadByte ();
 
525
                                                int len = reader.ReadInt32 ();
 
526
                                                Console.WriteLine ("(" + tc + "[" + len + "])");
 
527
                                                for (int n=0; n<len; n++) {
 
528
                                                        Console.Write (new string (' ', ind + IndSize) + n + ": ");
 
529
                                                        DumpValue (ind + IndSize*2);
 
530
                                                        Console.WriteLine ();
 
531
                                                }
 
532
                                                break;
 
533
                                        }
 
534
                                        case TagObjectDictionary: {
 
535
                                                int len = reader.ReadInt32 ();
 
536
                                                Console.WriteLine ("(IDictionary)");
 
537
                                                for (int n=0; n<len; n++) {
 
538
                                                        Console.Write (new string (' ', ind + IndSize) + "key: ");
 
539
                                                        DumpValue (ind + IndSize*2);
 
540
                                                        Console.WriteLine ();
 
541
                                                        Console.Write (new string (' ', ind + IndSize) + "val: ");
 
542
                                                        DumpValue (ind + IndSize*2);
 
543
                                                        Console.WriteLine ();
 
544
                                                }
 
545
                                                break;
 
546
                                        }
 
547
                                        default:
 
548
                                                throw new InvalidOperationException ("Invalid object tag: " + ot);
 
549
                                }
 
550
                        }
 
551
                }
 
552
        }
 
553
}