aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/LLSDxmlEncode.cs
blob: bd99cd628a2383b9f941aeae4cd4d8e794eb925d (plain)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSimulator Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// a class for low level LLSD encoding into a provided StringBuilder
// for cases where we already need to know the low level detail
// and so using something like OSD or even protbuf is just a pure waste

using System;
using System.Globalization;
using System.Text;
using OpenMetaverse;

namespace OpenSim.Framework
{
    public static class LLSDxmlEncode
    {
        static readonly  DateTime depoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public static void AddStart(StringBuilder sb, bool addxmlversion = false)
        {
            if(addxmlversion)
                sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd>"); // legacy llsd xml name still valid
            else
                sb.Append("<llsd>");
        }

        // got tired of creating a stringbuilder all the time;
        public static StringBuilder Start(int size = 256, bool addxmlversion = false)
        {
            StringBuilder sb = new StringBuilder(size);
            if(addxmlversion)
                sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd>"); // legacy llsd xml name still valid
            else
                sb.Append("<llsd>");
            return sb;
        }

        public static void AddEnd(StringBuilder sb)
        {
            sb.Append("</llsd>");
        }

        public static string End(StringBuilder sb)
        {
            sb.Append("</llsd>");
            return sb.ToString();
        }

        // map == a list of key value pairs
        public static void AddMap(StringBuilder sb)
        {
            sb.Append("<map>");
        }

        public static void AddEndMap(StringBuilder sb)
        {
            sb.Append("</map>");
        }

        public static void AddEmptyMap(StringBuilder sb)
        {
            sb.Append("<map />");
        }

        // array == a list values
        public static void AddArray(StringBuilder sb)
        {
            sb.Append("<array>");
        }

        public static void AddEndArray(StringBuilder sb)
        {
            sb.Append("</array>");
        }

        public static void AddEmptyArray(StringBuilder sb)
        {
            sb.Append("<array />");
        }

        // undefined or null
        public static void AddUnknownElem(StringBuilder sb)
        {
            sb.Append("<undef />");
        }

        public static void AddElem(bool e, StringBuilder sb)
        {
            if(e)
                sb.Append("<boolean>1</boolean>");
            else
                sb.Append("<boolean />");
        }

        public static void AddElem(byte e, StringBuilder sb)
        {
            if(e == 0)
                sb.Append("<integer />");
            else
            {
                sb.Append("<integer>");
                sb.Append(e.ToString());     
                sb.Append("</integer>");
            }
        }

        public static void AddElem(int e, StringBuilder sb)
        {
            if(e == 0)
                sb.Append("<integer />");
            else
            {
                sb.Append("<integer>");
                sb.Append(e.ToString());     
                sb.Append("</integer>");
            }
        }

        public static void AddElem(float e, StringBuilder sb)
        {
            if(e == 0)
                sb.Append("<real />");
            else
            {
                sb.Append("<real>");
                sb.Append(e.ToString(CultureInfo.InvariantCulture));     
                sb.Append("</real>");
            }
        }

        public static void AddElem(double e, StringBuilder sb)
        {
            if(e == 0)
                sb.Append("<real />");
            else
            {
                sb.Append("<real>");
                sb.Append(e.ToString(CultureInfo.InvariantCulture));     
                sb.Append("</real>");
            }
        }

        public static void AddElem(UUID e, StringBuilder sb)
        {
            if(e == UUID.Zero)
                sb.Append("<uuid />");
            else
            {
                sb.Append("<uuid>");
                EscapeToXML(e.ToString(), sb);     
                sb.Append("</uuid>");
            }
        }

        public static void AddElem(string e, StringBuilder sb)
        {
            if(String.IsNullOrEmpty(e))
                sb.Append("<string />");
            else
            {
                sb.Append("<string>");
                sb.Append(e.ToString());     
                sb.Append("</string>");
            }
        }

        public static void AddRawElem(string e, StringBuilder sb)
        {
            if(String.IsNullOrEmpty(e))
                sb.Append("<string />");
            else
            {
                sb.Append("<string>");
                sb.Append(e);     
                sb.Append("</string>");
            }
        }

        public static void AddElem(Uri e, StringBuilder sb)
        {
            if(e == null)
            {
                sb.Append("<uri />");
                return;
            }

            string s;
            if (e.IsAbsoluteUri)
                s = e.AbsoluteUri;
            else
                s = e.ToString();

            if(String.IsNullOrEmpty(s))
                sb.Append("<uri />");
            else
            {
                sb.Append("<uri>");
                sb.Append(s);     
                sb.Append("</uri>");
            }
        }

        public static void AddElem(DateTime e, StringBuilder sb)
        {
            DateTime u = e.ToUniversalTime();
            if(u == depoch)
            {
                sb.Append("<date />");
                return;
            }    
            string format;
            if(u.Hour == 0 && u.Minute == 0 && u.Second == 0)
                format = "yyyy-MM-dd";
            else if (u.Millisecond > 0)
                format = "yyyy-MM-ddTHH:mm:ss.ffZ";
            else
                format = "yyyy-MM-ddTHH:mm:ssZ";
            sb.Append("<date>");
            sb.Append(u.ToString(format,CultureInfo.InvariantCulture));     
            sb.Append("</date>");
        }

//************ key value *******************
// assumes name is a valid llsd key

        public static void AddMap(string name, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key><map>");
        }

        public static void AddEmptyMap(string name, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key><map />");
        }

        // array == a list values
        public static void AddArray(string name, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key><array>");
        }

        public static void AddEmptyArray(string name, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key><array />");
        }

        // undefined or null
        public static void AddUnknownElem(string name, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key><undef />");
        }

        public static void AddElem(string name, bool e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            if(e)
                sb.Append("<boolean>1</boolean>");
            else
                sb.Append("<boolean />");
        }

        public static void AddElem(string name, byte e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            if(e == 0)
                sb.Append("<integer />");
            else
            {
                sb.Append("<integer>");
                sb.Append(e.ToString());     
                sb.Append("</integer>");
            }
        }

        public static void AddElem(string name, int e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            if(e == 0)
                sb.Append("<integer />");
            else
            {
                sb.Append("<integer>");
                sb.Append(e.ToString());     
                sb.Append("</integer>");
            }
        }

        public static void AddElem(string name, float e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            if(e == 0)
                sb.Append("<real />");
            else
            {
                sb.Append("<real>");
                sb.Append(e.ToString(CultureInfo.InvariantCulture));     
                sb.Append("</real>");
            }
        }

        public static void AddElem(string name, double e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            if(e == 0)
                sb.Append("<real />");
            else
            {
                sb.Append("<real>");
                sb.Append(e.ToString(CultureInfo.InvariantCulture));     
                sb.Append("</real>");
            }
        }

        public static void AddElem(string name, UUID e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            if(e == UUID.Zero)
                sb.Append("<uuid />");
            else
            {
                sb.Append("<uuid>");
                EscapeToXML(e.ToString(), sb);     
                sb.Append("</uuid>");
            }
        }

        public static void AddElem(string name, string e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            if(String.IsNullOrEmpty(e))
                sb.Append("<string />");
            else
            {
                sb.Append("<string>");
                sb.Append(e.ToString());     
                sb.Append("</string>");
            }
        }

        public static void AddRawElem(string name, string e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            if(String.IsNullOrEmpty(e))
                sb.Append("<string />");
            else
            {
                sb.Append("<string>");
                sb.Append(e);     
                sb.Append("</string>");
            }
        }

        public static void AddElem(string name, Uri e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            if(e == null)
            {
                sb.Append("<uri />");
                return;
            }

            string s;
            if (e.IsAbsoluteUri)
                s = e.AbsoluteUri;
            else
                s = e.ToString();

            if(String.IsNullOrEmpty(s))
                sb.Append("<uri />");
            else
            {
                sb.Append("<uri>");
                sb.Append(s);     
                sb.Append("</uri>");
            }
        }

        public static void AddElem(string name, DateTime e, StringBuilder sb)
        {
            sb.Append("<key>");
            sb.Append(name);
            sb.Append("</key>");

            DateTime u = e.ToUniversalTime();
            if(u == depoch)
            {
                sb.Append("<date />");
                return;
            }    
            string format;
            if(u.Hour == 0 && u.Minute == 0 && u.Second == 0)
                format = "yyyy-MM-dd";
            else if (u.Millisecond > 0)
                format = "yyyy-MM-ddTHH:mm:ss.ffZ";
            else
                format = "yyyy-MM-ddTHH:mm:ssZ";
            sb.Append("<date>");
            sb.Append(u.ToString(format,CultureInfo.InvariantCulture));     
            sb.Append("</date>");
        }

        public static void AddLLSD(string e, StringBuilder sb)
        {
            sb.Append(e);     
        }

        public static void EscapeToXML(string s, StringBuilder sb)
        {
            int i;
            char c;
            int len = s.Length;

            for (i = 0; i < len; i++)
            {
                c = s[i];
                switch (c)
                {
                    case '<':
                        sb.Append("&lt;");
                        break;
                    case '>':
                        sb.Append("&gt;");
                        break;
                    case '&':
                        sb.Append("&amp;");
                        break;
                    case '"':
                        sb.Append("&quot;");
                        break;
                    case '\\':
                        sb.Append("&apos;");
                        break;
                    default:
                        sb.Append(c);
                        break;
                }
            }
         }
    }
}