123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725 |
- /*
- * 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(byte[] e, StringBuilder sb)
- {
- if(e == null || e.Length == 0)
- sb.Append("binary />");
- else
- {
- sb.Append("<binary>"); // encode64 is default
- sb.Append(Convert.ToBase64String(e,Base64FormattingOptions.None));
- sb.Append("</binary>");
- }
- }
- 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(Vector2 e, StringBuilder sb)
- {
- sb.Append("<map><key>x</key>");
- if(e.X == 0)
- sb.Append("<real /><key>y</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>y</key>");
- }
- if(e.Y == 0)
- sb.Append("<real /></map>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real></map>");
- }
- }
- public static void AddElem(Vector3 e, StringBuilder sb)
- {
- sb.Append("<map>key>x</key>");
- if(e.X == 0)
- sb.Append("<real /><key>y</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>y</key>");
- }
- if(e.Y == 0)
- sb.Append("<real /><key>z</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>z</key>");
- }
- if(e.Z == 0)
- sb.Append("<real /></map>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real></map>");
- }
- }
- public static void AddElem(Quaternion e, StringBuilder sb)
- {
- sb.Append("<map><key>x</key>");
- if(e.X == 0)
- sb.Append("<real /><key>y</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>y</key>");
- }
- if(e.Y == 0)
- sb.Append("<real /><key>z</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>z</key>");
- }
- if(e.Z == 0)
- sb.Append("<real /><key>w</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>w</key>");
- }
- if(e.W == 0)
- sb.Append("<real /></map>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.W.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real></map>");
- }
- }
- 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>");
- EscapeToXML(e, sb);
- 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, byte[] e, StringBuilder sb)
- {
- sb.Append("<key>");
- sb.Append(name);
- sb.Append("</key>");
- if(e == null || e.Length == 0)
- sb.Append("binary />");
- else
- {
- sb.Append("<binary>"); // encode64 is default
- sb.Append(Convert.ToBase64String(e,Base64FormattingOptions.None));
- sb.Append("</binary>");
- }
- }
- 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, Vector2 e, StringBuilder sb)
- {
- sb.Append("<key>");
- sb.Append(name);
- sb.Append("</key><map><key>x</key>");
- if(e.X == 0)
- sb.Append("<real /><key>y</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>y</key>");
- }
- if(e.Y == 0)
- sb.Append("<real /></map>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real></map>");
- }
- }
- public static void AddElem(string name, Vector3 e, StringBuilder sb)
- {
- sb.Append("<key>");
- sb.Append(name);
- sb.Append("</key><map>key>x</key>");
- if(e.X == 0)
- sb.Append("<real /><key>y</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>y</key>");
- }
- if(e.Y == 0)
- sb.Append("<real /><key>z</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>z</key>");
- }
- if(e.Z == 0)
- sb.Append("<real /></map>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real></map>");
- }
- }
- public static void AddElem(string name, Quaternion e, StringBuilder sb)
- {
- sb.Append("<key>");
- sb.Append(name);
- sb.Append("</key><map><key>x</key>");
- if(e.X == 0)
- sb.Append("<real /><key>y</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.X.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>y</key>");
- }
- if(e.Y == 0)
- sb.Append("<real /><key>z</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Y.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>z</key>");
- }
- if(e.Z == 0)
- sb.Append("<real /><key>w</key>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.Z.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real><key>w</key>");
- }
- if(e.W == 0)
- sb.Append("<real /></map>");
- else
- {
- sb.Append("<real>");
- sb.Append(e.W.ToString(CultureInfo.InvariantCulture));
- sb.Append("</real></map>");
- }
- }
- 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>");
- EscapeToXML(e.ToString(), sb);
- 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("<");
- break;
- case '>':
- sb.Append(">");
- break;
- case '&':
- sb.Append("&");
- break;
- case '"':
- sb.Append(""");
- break;
- case '\\':
- sb.Append("'");
- break;
- default:
- sb.Append(c);
- break;
- }
- }
- }
- }
- }
|