~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/BinaryXmlWriter.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
// BinaryXmlWriter.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 BinaryXmlWriter
 
38
        {
 
39
                BinaryWriter writer;
 
40
                BinaryXmlTypeMap typeMap;
 
41
                Hashtable stringTable = new Hashtable ();
 
42
                
 
43
                public BinaryXmlWriter (Stream stream, BinaryXmlTypeMap typeMap)
 
44
                {
 
45
                        this.typeMap = typeMap;
 
46
                        writer = new BinaryWriter (stream);
 
47
                }
 
48
                
 
49
                public void WriteBeginElement (string name)
 
50
                {
 
51
                        writer.Write (BinaryXmlReader.TagBeginElement);
 
52
                        WriteString (name);
 
53
                }
 
54
                
 
55
                public void WriteEndElement ()
 
56
                {
 
57
                        writer.Write (BinaryXmlReader.TagEndElement);
 
58
                }
 
59
                
 
60
                void WriteValueHeader (string name, TypeCode type)
 
61
                {
 
62
                        writer.Write (BinaryXmlReader.TagValue);
 
63
                        WriteString (name);
 
64
                        writer.Write ((byte) type);
 
65
                }
 
66
                
 
67
                public void WriteValue (string name, bool b)
 
68
                {
 
69
                        WriteValueHeader (name, TypeCode.Boolean);
 
70
                        writer.Write (b);
 
71
                }
 
72
                
 
73
                public void WriteValue (string name, string s)
 
74
                {
 
75
                        WriteValueHeader (name, TypeCode.String);
 
76
                        WriteString (s);
 
77
                }
 
78
                
 
79
                public void WriteValue (string name, char value)
 
80
                {
 
81
                        WriteValueHeader (name, TypeCode.Char);
 
82
                        writer.Write (value);
 
83
                }
 
84
                
 
85
                public void WriteValue (string name, byte value)
 
86
                {
 
87
                        WriteValueHeader (name, TypeCode.Byte);
 
88
                        writer.Write (value);
 
89
                }
 
90
                
 
91
                public void WriteValue (string name, short value)
 
92
                {
 
93
                        WriteValueHeader (name, TypeCode.Int16);
 
94
                        writer.Write (value);
 
95
                }
 
96
                
 
97
                public void WriteValue (string name, int value)
 
98
                {
 
99
                        WriteValueHeader (name, TypeCode.Int32);
 
100
                        writer.Write (value);
 
101
                }
 
102
                
 
103
                public void WriteValue (string name, long value)
 
104
                {
 
105
                        WriteValueHeader (name, TypeCode.Int64);
 
106
                        writer.Write (value);
 
107
                }
 
108
                
 
109
                public void WriteValue (string name, DateTime value)
 
110
                {
 
111
                        WriteValueHeader (name, TypeCode.DateTime);
 
112
                        writer.Write (value.Ticks);
 
113
                }
 
114
                
 
115
                public void WriteValue (string name, object ob)
 
116
                {
 
117
                        TypeCode t = ob != null ? Type.GetTypeCode (ob.GetType ()) : TypeCode.Empty;
 
118
                        WriteValueHeader (name, t);
 
119
                        if (t != TypeCode.Empty)
 
120
                                WriteValue (ob, t);
 
121
                }
 
122
                
 
123
                public void WriteValue (string name, IBinaryXmlElement ob)
 
124
                {
 
125
                        if (ob == null)
 
126
                                WriteValueHeader (name, TypeCode.Empty);
 
127
                        else {
 
128
                                WriteValueHeader (name, TypeCode.Object);
 
129
                                WriteObject (ob);
 
130
                        }
 
131
                }
 
132
                
 
133
                void WriteValue (object ob)
 
134
                {
 
135
                        if (ob == null)
 
136
                                writer.Write ((byte) TypeCode.Empty);
 
137
                        else {
 
138
                                TypeCode t = Type.GetTypeCode (ob.GetType ());
 
139
                                writer.Write ((byte) t);
 
140
                                WriteValue (ob, t);
 
141
                        }
 
142
                }
 
143
                
 
144
                void WriteValue (object ob, TypeCode t)
 
145
                {
 
146
                        switch (t) {
 
147
                                case TypeCode.Boolean: writer.Write ((bool)ob); break;
 
148
                                case TypeCode.Char: writer.Write ((char)ob); break;
 
149
                                case TypeCode.SByte: writer.Write ((sbyte)ob); break;
 
150
                                case TypeCode.Byte: writer.Write ((byte)ob); break;
 
151
                                case TypeCode.Int16: writer.Write ((short)ob); break;
 
152
                                case TypeCode.UInt16: writer.Write ((ushort)ob); break;
 
153
                                case TypeCode.Int32: writer.Write ((int)ob); break;
 
154
                                case TypeCode.UInt32: writer.Write ((uint)ob); break;
 
155
                                case TypeCode.Int64: writer.Write ((long)ob); break;
 
156
                                case TypeCode.UInt64: writer.Write ((ulong)ob); break;
 
157
                                case TypeCode.Single: writer.Write ((float)ob); break;
 
158
                                case TypeCode.Double: writer.Write ((double)ob); break;
 
159
                                case TypeCode.DateTime: writer.Write (((DateTime)ob).Ticks); break;
 
160
                                case TypeCode.String: WriteString ((string)ob); break;
 
161
                                case TypeCode.Object: WriteObject (ob); break;
 
162
                                default:
 
163
                                        throw new InvalidOperationException ("Unexpected value type: " + t);
 
164
                        }
 
165
                }
 
166
                
 
167
                void WriteObject (object ob)
 
168
                {
 
169
                        if (ob == null)
 
170
                                writer.Write (BinaryXmlReader.TagObjectNull);
 
171
                        else {
 
172
                                IBinaryXmlElement elem = ob as IBinaryXmlElement;
 
173
                                if (elem != null) {
 
174
                                        writer.Write (BinaryXmlReader.TagObject);
 
175
                                        WriteString (typeMap.GetTypeName (elem));
 
176
                                        elem.Write (this);
 
177
                                        WriteEndElement ();
 
178
                                }
 
179
                                else if (ob is IDictionary) {
 
180
                                        IDictionary dict = (IDictionary) ob;
 
181
                                        writer.Write (BinaryXmlReader.TagObjectDictionary);
 
182
                                        writer.Write (dict.Count);
 
183
                                        foreach (DictionaryEntry e in dict) {
 
184
                                                WriteValue (e.Key);
 
185
                                                WriteValue (e.Value);
 
186
                                        }
 
187
                                }
 
188
                                else if (ob is ICollection) {
 
189
                                        ICollection col = (ICollection) ob;
 
190
                                        writer.Write (BinaryXmlReader.TagObjectArray);
 
191
                                        if (ob is Array)
 
192
                                                writer.Write ((byte) Type.GetTypeCode (ob.GetType().GetElementType ()));
 
193
                                        else
 
194
                                                writer.Write ((byte) TypeCode.Object);
 
195
                                        writer.Write (col.Count);
 
196
                                        foreach (object e in col) {
 
197
                                                WriteValue (e);
 
198
                                        }
 
199
                                }
 
200
                                else
 
201
                                        throw new InvalidOperationException ("Invalid object type: " + ob.GetType ());
 
202
                        }
 
203
                }
 
204
                
 
205
                void WriteString (string s)
 
206
                {
 
207
                        if (s == null)
 
208
                                writer.Write (-1);
 
209
                        else {
 
210
                                object ind = stringTable [s];
 
211
                                if (ind == null) {
 
212
                                        stringTable.Add (s, stringTable.Count);
 
213
                                        byte[] bytes = Encoding.UTF8.GetBytes (s);
 
214
                                        writer.Write (bytes.Length);
 
215
                                        writer.Write (bytes);
 
216
                                } else {
 
217
                                        // +2 because -1 is reserved for null, and 0 is considered positive
 
218
                                        writer.Write (-((int)ind + 2));
 
219
                                }
 
220
                        }
 
221
                }
 
222
        }
 
223
}