diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/LLSDxmlEncode.cs | 501 |
1 files changed, 501 insertions, 0 deletions
diff --git a/OpenSim/Framework/LLSDxmlEncode.cs b/OpenSim/Framework/LLSDxmlEncode.cs new file mode 100644 index 0000000..5447963 --- /dev/null +++ b/OpenSim/Framework/LLSDxmlEncode.cs | |||
@@ -0,0 +1,501 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | // a class for low level LLSD encoding into a provided StringBuilder | ||
29 | // for cases where we already need to know the low level detail | ||
30 | // and so using something like OSD or even protbuf is just a pure waste | ||
31 | |||
32 | using System; | ||
33 | using System.Globalization; | ||
34 | using System.Text; | ||
35 | using OpenMetaverse; | ||
36 | |||
37 | namespace OpenSim.Framework | ||
38 | { | ||
39 | public static class LLSDxmlEncode | ||
40 | { | ||
41 | static readonly DateTime depoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
42 | |||
43 | public static void AddStart(StringBuilder sb, bool addxmlversion = false) | ||
44 | { | ||
45 | if(addxmlversion) | ||
46 | sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd>"); // legacy llsd xml name still valid | ||
47 | else | ||
48 | sb.Append("<llsd>"); | ||
49 | } | ||
50 | |||
51 | // got tired of creating a stringbuilder all the time; | ||
52 | public static StringBuilder Start(int size = 256, bool addxmlversion = false) | ||
53 | { | ||
54 | StringBuilder sb = new StringBuilder(size); | ||
55 | if(addxmlversion) | ||
56 | sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><llsd>"); // legacy llsd xml name still valid | ||
57 | else | ||
58 | sb.Append("<llsd>"); | ||
59 | return sb; | ||
60 | } | ||
61 | |||
62 | public static void AddEnd(StringBuilder sb) | ||
63 | { | ||
64 | sb.Append("</llsd>"); | ||
65 | } | ||
66 | |||
67 | public static string End(StringBuilder sb) | ||
68 | { | ||
69 | sb.Append("</llsd>"); | ||
70 | return sb.ToString(); | ||
71 | } | ||
72 | |||
73 | // map == a list of key value pairs | ||
74 | public static void AddMap(StringBuilder sb) | ||
75 | { | ||
76 | sb.Append("<map>"); | ||
77 | } | ||
78 | |||
79 | public static void AddEndMap(StringBuilder sb) | ||
80 | { | ||
81 | sb.Append("</map>"); | ||
82 | } | ||
83 | |||
84 | public static void AddEmpyMap(StringBuilder sb) | ||
85 | { | ||
86 | sb.Append("<map />"); | ||
87 | } | ||
88 | |||
89 | // array == a list values | ||
90 | public static void AddArray(StringBuilder sb) | ||
91 | { | ||
92 | sb.Append("<array>"); | ||
93 | } | ||
94 | |||
95 | public static void AddEndArray(StringBuilder sb) | ||
96 | { | ||
97 | sb.Append("</array>"); | ||
98 | } | ||
99 | |||
100 | public static void AddEmpyArray(StringBuilder sb) | ||
101 | { | ||
102 | sb.Append("<array />"); | ||
103 | } | ||
104 | |||
105 | // undefined or null | ||
106 | public static void AddUnknownElem(StringBuilder sb) | ||
107 | { | ||
108 | sb.Append("<undef />"); | ||
109 | } | ||
110 | |||
111 | public static void AddElem(bool e, StringBuilder sb) | ||
112 | { | ||
113 | if(e) | ||
114 | sb.Append("<boolean>1</boolean>"); | ||
115 | else | ||
116 | sb.Append("<boolean />"); | ||
117 | } | ||
118 | |||
119 | public static void AddElem(byte e, StringBuilder sb) | ||
120 | { | ||
121 | if(e == 0) | ||
122 | sb.Append("<integer />"); | ||
123 | else | ||
124 | { | ||
125 | sb.Append("<integer>"); | ||
126 | sb.Append(e.ToString()); | ||
127 | sb.Append("</integer>"); | ||
128 | } | ||
129 | } | ||
130 | |||
131 | public static void AddElem(int e, StringBuilder sb) | ||
132 | { | ||
133 | if(e == 0) | ||
134 | sb.Append("<integer />"); | ||
135 | else | ||
136 | { | ||
137 | sb.Append("<integer>"); | ||
138 | sb.Append(e.ToString()); | ||
139 | sb.Append("</integer>"); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | public static void AddElem(float e, StringBuilder sb) | ||
144 | { | ||
145 | if(e == 0) | ||
146 | sb.Append("<real />"); | ||
147 | else | ||
148 | { | ||
149 | sb.Append("<real>"); | ||
150 | sb.Append(e.ToString(CultureInfo.InvariantCulture)); | ||
151 | sb.Append("</real>"); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | public static void AddElem(double e, StringBuilder sb) | ||
156 | { | ||
157 | if(e == 0) | ||
158 | sb.Append("<real />"); | ||
159 | else | ||
160 | { | ||
161 | sb.Append("<real>"); | ||
162 | sb.Append(e.ToString(CultureInfo.InvariantCulture)); | ||
163 | sb.Append("</real>"); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | public static void AddElem(UUID e, StringBuilder sb) | ||
168 | { | ||
169 | if(e == UUID.Zero) | ||
170 | sb.Append("<uuid />"); | ||
171 | else | ||
172 | { | ||
173 | sb.Append("<uuid>"); | ||
174 | EscapeToXML(e.ToString(), sb); | ||
175 | sb.Append("</uuid>"); | ||
176 | } | ||
177 | } | ||
178 | |||
179 | public static void AddElem(string e, StringBuilder sb) | ||
180 | { | ||
181 | if(String.IsNullOrEmpty(e)) | ||
182 | sb.Append("<string />"); | ||
183 | else | ||
184 | { | ||
185 | sb.Append("<string>"); | ||
186 | sb.Append(e.ToString()); | ||
187 | sb.Append("</string>"); | ||
188 | } | ||
189 | } | ||
190 | |||
191 | public static void AddRawElem(string e, StringBuilder sb) | ||
192 | { | ||
193 | if(String.IsNullOrEmpty(e)) | ||
194 | sb.Append("<string />"); | ||
195 | else | ||
196 | { | ||
197 | sb.Append("<string>"); | ||
198 | sb.Append(e); | ||
199 | sb.Append("</string>"); | ||
200 | } | ||
201 | } | ||
202 | |||
203 | public static void AddElem(Uri e, StringBuilder sb) | ||
204 | { | ||
205 | if(e == null) | ||
206 | { | ||
207 | sb.Append("<uri />"); | ||
208 | return; | ||
209 | } | ||
210 | |||
211 | string s; | ||
212 | if (e.IsAbsoluteUri) | ||
213 | s = e.AbsoluteUri; | ||
214 | else | ||
215 | s = e.ToString(); | ||
216 | |||
217 | if(String.IsNullOrEmpty(s)) | ||
218 | sb.Append("<uri />"); | ||
219 | else | ||
220 | { | ||
221 | sb.Append("<uri>"); | ||
222 | sb.Append(s); | ||
223 | sb.Append("</uri>"); | ||
224 | } | ||
225 | } | ||
226 | |||
227 | public static void AddElem(DateTime e, StringBuilder sb) | ||
228 | { | ||
229 | DateTime u = e.ToUniversalTime(); | ||
230 | if(u == depoch) | ||
231 | { | ||
232 | sb.Append("<date />"); | ||
233 | return; | ||
234 | } | ||
235 | string format; | ||
236 | if(u.Hour == 0 && u.Minute == 0 && u.Second == 0) | ||
237 | format = "yyyy-MM-dd"; | ||
238 | else if (u.Millisecond > 0) | ||
239 | format = "yyyy-MM-ddTHH:mm:ss.ffZ"; | ||
240 | else | ||
241 | format = "yyyy-MM-ddTHH:mm:ssZ"; | ||
242 | sb.Append("<date>"); | ||
243 | sb.Append(u.ToString(format,CultureInfo.InvariantCulture)); | ||
244 | sb.Append("</date>"); | ||
245 | } | ||
246 | |||
247 | //************ key value ******************* | ||
248 | // assumes name is a valid llsd key | ||
249 | |||
250 | public static void AddMap(string name, StringBuilder sb) | ||
251 | { | ||
252 | sb.Append("<key>"); | ||
253 | sb.Append(name); | ||
254 | sb.Append("</key><map>"); | ||
255 | } | ||
256 | |||
257 | public static void AddEmpyMap(string name, StringBuilder sb) | ||
258 | { | ||
259 | sb.Append("<key>"); | ||
260 | sb.Append(name); | ||
261 | sb.Append("</key><map />"); | ||
262 | } | ||
263 | |||
264 | // array == a list values | ||
265 | public static void AddArray(string name, StringBuilder sb) | ||
266 | { | ||
267 | sb.Append("<key>"); | ||
268 | sb.Append(name); | ||
269 | sb.Append("</key><array>"); | ||
270 | } | ||
271 | |||
272 | public static void AddEmpyArray(string name, StringBuilder sb) | ||
273 | { | ||
274 | sb.Append("<key>"); | ||
275 | sb.Append(name); | ||
276 | sb.Append("</key><array />"); | ||
277 | } | ||
278 | |||
279 | // undefined or null | ||
280 | public static void AddUnknownElem(string name, StringBuilder sb) | ||
281 | { | ||
282 | sb.Append("<key>"); | ||
283 | sb.Append(name); | ||
284 | sb.Append("</key><undef />"); | ||
285 | } | ||
286 | |||
287 | public static void AddElem(string name, bool e, StringBuilder sb) | ||
288 | { | ||
289 | sb.Append("<key>"); | ||
290 | sb.Append(name); | ||
291 | sb.Append("</key>"); | ||
292 | |||
293 | if(e) | ||
294 | sb.Append("<boolean>1</boolean>"); | ||
295 | else | ||
296 | sb.Append("<boolean />"); | ||
297 | } | ||
298 | |||
299 | public static void AddElem(string name, byte e, StringBuilder sb) | ||
300 | { | ||
301 | sb.Append("<key>"); | ||
302 | sb.Append(name); | ||
303 | sb.Append("</key>"); | ||
304 | |||
305 | if(e == 0) | ||
306 | sb.Append("<integer />"); | ||
307 | else | ||
308 | { | ||
309 | sb.Append("<integer>"); | ||
310 | sb.Append(e.ToString()); | ||
311 | sb.Append("</integer>"); | ||
312 | } | ||
313 | } | ||
314 | |||
315 | public static void AddElem(string name, int e, StringBuilder sb) | ||
316 | { | ||
317 | sb.Append("<key>"); | ||
318 | sb.Append(name); | ||
319 | sb.Append("</key>"); | ||
320 | |||
321 | if(e == 0) | ||
322 | sb.Append("<integer />"); | ||
323 | else | ||
324 | { | ||
325 | sb.Append("<integer>"); | ||
326 | sb.Append(e.ToString()); | ||
327 | sb.Append("</integer>"); | ||
328 | } | ||
329 | } | ||
330 | |||
331 | public static void AddElem(string name, float e, StringBuilder sb) | ||
332 | { | ||
333 | sb.Append("<key>"); | ||
334 | sb.Append(name); | ||
335 | sb.Append("</key>"); | ||
336 | |||
337 | if(e == 0) | ||
338 | sb.Append("<real />"); | ||
339 | else | ||
340 | { | ||
341 | sb.Append("<real>"); | ||
342 | sb.Append(e.ToString(CultureInfo.InvariantCulture)); | ||
343 | sb.Append("</real>"); | ||
344 | } | ||
345 | } | ||
346 | |||
347 | public static void AddElem(string name, double e, StringBuilder sb) | ||
348 | { | ||
349 | sb.Append("<key>"); | ||
350 | sb.Append(name); | ||
351 | sb.Append("</key>"); | ||
352 | |||
353 | if(e == 0) | ||
354 | sb.Append("<real />"); | ||
355 | else | ||
356 | { | ||
357 | sb.Append("<real>"); | ||
358 | sb.Append(e.ToString(CultureInfo.InvariantCulture)); | ||
359 | sb.Append("</real>"); | ||
360 | } | ||
361 | } | ||
362 | |||
363 | public static void AddElem(string name, UUID e, StringBuilder sb) | ||
364 | { | ||
365 | sb.Append("<key>"); | ||
366 | sb.Append(name); | ||
367 | sb.Append("</key>"); | ||
368 | |||
369 | if(e == UUID.Zero) | ||
370 | sb.Append("<uuid />"); | ||
371 | else | ||
372 | { | ||
373 | sb.Append("<uuid>"); | ||
374 | EscapeToXML(e.ToString(), sb); | ||
375 | sb.Append("</uuid>"); | ||
376 | } | ||
377 | } | ||
378 | |||
379 | public static void AddElem(string name, string e, StringBuilder sb) | ||
380 | { | ||
381 | sb.Append("<key>"); | ||
382 | sb.Append(name); | ||
383 | sb.Append("</key>"); | ||
384 | |||
385 | if(String.IsNullOrEmpty(e)) | ||
386 | sb.Append("<string />"); | ||
387 | else | ||
388 | { | ||
389 | sb.Append("<string>"); | ||
390 | sb.Append(e.ToString()); | ||
391 | sb.Append("</string>"); | ||
392 | } | ||
393 | } | ||
394 | |||
395 | public static void AddRawElem(string name, string e, StringBuilder sb) | ||
396 | { | ||
397 | sb.Append("<key>"); | ||
398 | sb.Append(name); | ||
399 | sb.Append("</key>"); | ||
400 | |||
401 | if(String.IsNullOrEmpty(e)) | ||
402 | sb.Append("<string />"); | ||
403 | else | ||
404 | { | ||
405 | sb.Append("<string>"); | ||
406 | sb.Append(e); | ||
407 | sb.Append("</string>"); | ||
408 | } | ||
409 | } | ||
410 | |||
411 | public static void AddElem(string name, Uri e, StringBuilder sb) | ||
412 | { | ||
413 | sb.Append("<key>"); | ||
414 | sb.Append(name); | ||
415 | sb.Append("</key>"); | ||
416 | |||
417 | if(e == null) | ||
418 | { | ||
419 | sb.Append("<uri />"); | ||
420 | return; | ||
421 | } | ||
422 | |||
423 | string s; | ||
424 | if (e.IsAbsoluteUri) | ||
425 | s = e.AbsoluteUri; | ||
426 | else | ||
427 | s = e.ToString(); | ||
428 | |||
429 | if(String.IsNullOrEmpty(s)) | ||
430 | sb.Append("<uri />"); | ||
431 | else | ||
432 | { | ||
433 | sb.Append("<uri>"); | ||
434 | sb.Append(s); | ||
435 | sb.Append("</uri>"); | ||
436 | } | ||
437 | } | ||
438 | |||
439 | public static void AddElem(string name, DateTime e, StringBuilder sb) | ||
440 | { | ||
441 | sb.Append("<key>"); | ||
442 | sb.Append(name); | ||
443 | sb.Append("</key>"); | ||
444 | |||
445 | DateTime u = e.ToUniversalTime(); | ||
446 | if(u == depoch) | ||
447 | { | ||
448 | sb.Append("<date />"); | ||
449 | return; | ||
450 | } | ||
451 | string format; | ||
452 | if(u.Hour == 0 && u.Minute == 0 && u.Second == 0) | ||
453 | format = "yyyy-MM-dd"; | ||
454 | else if (u.Millisecond > 0) | ||
455 | format = "yyyy-MM-ddTHH:mm:ss.ffZ"; | ||
456 | else | ||
457 | format = "yyyy-MM-ddTHH:mm:ssZ"; | ||
458 | sb.Append("<date>"); | ||
459 | sb.Append(u.ToString(format,CultureInfo.InvariantCulture)); | ||
460 | sb.Append("</date>"); | ||
461 | } | ||
462 | |||
463 | public static void AddLLSD(string e, StringBuilder sb) | ||
464 | { | ||
465 | sb.Append(e); | ||
466 | } | ||
467 | |||
468 | public static void EscapeToXML(string s, StringBuilder sb) | ||
469 | { | ||
470 | int i; | ||
471 | char c; | ||
472 | int len = s.Length; | ||
473 | |||
474 | for (i = 0; i < len; i++) | ||
475 | { | ||
476 | c = s[i]; | ||
477 | switch (c) | ||
478 | { | ||
479 | case '<': | ||
480 | sb.Append("<"); | ||
481 | break; | ||
482 | case '>': | ||
483 | sb.Append(">"); | ||
484 | break; | ||
485 | case '&': | ||
486 | sb.Append("&"); | ||
487 | break; | ||
488 | case '"': | ||
489 | sb.Append("""); | ||
490 | break; | ||
491 | case '\\': | ||
492 | sb.Append("'"); | ||
493 | break; | ||
494 | default: | ||
495 | sb.Append(c); | ||
496 | break; | ||
497 | } | ||
498 | } | ||
499 | } | ||
500 | } | ||
501 | } | ||