1
0

LLSDHelpers.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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 OpenSim 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. using System;
  29. using System.Collections;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Xml;
  33. using libsecondlife;
  34. namespace OpenSim.Region.Capabilities
  35. {
  36. public class LLSDHelpers
  37. {
  38. public static string SerialiseLLSDReply(object obj)
  39. {
  40. StringWriter sw = new StringWriter();
  41. XmlTextWriter writer = new XmlTextWriter(sw);
  42. writer.Formatting = Formatting.None;
  43. writer.WriteStartElement(String.Empty, "llsd", String.Empty);
  44. SerializeLLSDType(writer, obj);
  45. writer.WriteEndElement();
  46. writer.Close();
  47. return sw.ToString();
  48. }
  49. public static void SerializeLLSDType(XmlTextWriter writer, object obj)
  50. {
  51. Type myType = obj.GetType();
  52. LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false);
  53. if (llsdattributes.Length > 0)
  54. {
  55. switch (llsdattributes[0].ObjectType)
  56. {
  57. case "MAP":
  58. writer.WriteStartElement(String.Empty, "map", String.Empty);
  59. FieldInfo[] fields = myType.GetFields();
  60. for (int i = 0; i < fields.Length; i++)
  61. {
  62. object fieldValue = fields[i].GetValue(obj);
  63. LLSDType[] fieldAttributes = (LLSDType[])fieldValue.GetType().GetCustomAttributes(typeof(LLSDType), false);
  64. if (fieldAttributes.Length > 0)
  65. {
  66. writer.WriteStartElement(String.Empty, "key", String.Empty);
  67. writer.WriteString(fields[i].Name);
  68. writer.WriteEndElement();
  69. SerializeLLSDType(writer, fieldValue);
  70. }
  71. else
  72. {
  73. writer.WriteStartElement(String.Empty, "key", String.Empty);
  74. writer.WriteString(fields[i].Name);
  75. writer.WriteEndElement();
  76. LLSD.LLSDWriteOne(writer, fieldValue);
  77. }
  78. }
  79. writer.WriteEndElement();
  80. break;
  81. case "ARRAY":
  82. // LLSDArray arrayObject = obj as LLSDArray;
  83. // ArrayList a = arrayObject.Array;
  84. ArrayList a = (ArrayList)obj.GetType().GetField("Array").GetValue(obj);
  85. if (a != null)
  86. {
  87. writer.WriteStartElement(String.Empty, "array", String.Empty);
  88. foreach (object item in a)
  89. {
  90. SerializeLLSDType(writer, item);
  91. }
  92. writer.WriteEndElement();
  93. }
  94. break;
  95. }
  96. }
  97. else
  98. {
  99. LLSD.LLSDWriteOne(writer, obj);
  100. }
  101. }
  102. public static object DeserialiseLLSDMap(Hashtable llsd, object obj)
  103. {
  104. Type myType = obj.GetType();
  105. LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false);
  106. if (llsdattributes.Length > 0)
  107. {
  108. switch (llsdattributes[0].ObjectType)
  109. {
  110. case "MAP":
  111. IDictionaryEnumerator enumerator = llsd.GetEnumerator();
  112. while (enumerator.MoveNext())
  113. {
  114. FieldInfo field = myType.GetField((string)enumerator.Key);
  115. if (field != null)
  116. {
  117. if (enumerator.Value is Hashtable)
  118. {
  119. object fieldValue = field.GetValue(obj);
  120. DeserialiseLLSDMap((Hashtable) enumerator.Value, fieldValue);
  121. }
  122. else if (enumerator.Value is ArrayList)
  123. {
  124. object fieldValue = field.GetValue(obj);
  125. fieldValue.GetType().GetField("Array").SetValue(fieldValue, enumerator.Value);
  126. //TODO
  127. // the LLSD map/array types in the array need to be deserialised
  128. // but first we need to know the right class to deserialise them into.
  129. }
  130. else
  131. {
  132. field.SetValue(obj, enumerator.Value);
  133. }
  134. }
  135. }
  136. break;
  137. }
  138. }
  139. return obj;
  140. }
  141. }
  142. }