LLSDHelpers.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 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. using System;
  28. using System.Collections;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Xml;
  32. namespace OpenSim.Framework.Communications.Capabilities
  33. {
  34. public class LLSDHelpers
  35. {
  36. // private static readonly log4net.ILog m_log
  37. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  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. //m_log.DebugFormat("[LLSD Helpers]: Generated serialized LLSD reply {0}", sw.ToString());
  48. return sw.ToString();
  49. }
  50. private static void SerializeLLSDType(XmlTextWriter writer, object obj)
  51. {
  52. Type myType = obj.GetType();
  53. LLSDType[] llsdattributes = (LLSDType[]) myType.GetCustomAttributes(typeof (LLSDType), false);
  54. if (llsdattributes.Length > 0)
  55. {
  56. switch (llsdattributes[0].ObjectType)
  57. {
  58. case "MAP":
  59. writer.WriteStartElement(String.Empty, "map", String.Empty);
  60. FieldInfo[] fields = myType.GetFields();
  61. for (int i = 0; i < fields.Length; i++)
  62. {
  63. object fieldValue = fields[i].GetValue(obj);
  64. LLSDType[] fieldAttributes =
  65. (LLSDType[]) fieldValue.GetType().GetCustomAttributes(typeof (LLSDType), false);
  66. if (fieldAttributes.Length > 0)
  67. {
  68. writer.WriteStartElement(String.Empty, "key", String.Empty);
  69. string fieldName = fields[i].Name;
  70. fieldName = fieldName.Replace("___", "-");
  71. writer.WriteString(fieldName);
  72. writer.WriteEndElement();
  73. SerializeLLSDType(writer, fieldValue);
  74. }
  75. else
  76. {
  77. writer.WriteStartElement(String.Empty, "key", String.Empty);
  78. string fieldName = fields[i].Name;
  79. fieldName = fieldName.Replace("___", "-");
  80. writer.WriteString(fieldName);
  81. writer.WriteEndElement();
  82. LLSD.LLSDWriteOne(writer, fieldValue);
  83. // libsecondlife.StructuredData.LLSDParser.SerializeXmlElement(
  84. // writer, libsecondlife.StructuredData.LLSD.FromObject(fieldValue));
  85. }
  86. }
  87. writer.WriteEndElement();
  88. break;
  89. case "ARRAY":
  90. // LLSDArray arrayObject = obj as LLSDArray;
  91. // ArrayList a = arrayObject.Array;
  92. ArrayList a = (ArrayList) obj.GetType().GetField("Array").GetValue(obj);
  93. if (a != null)
  94. {
  95. writer.WriteStartElement(String.Empty, "array", String.Empty);
  96. foreach (object item in a)
  97. {
  98. SerializeLLSDType(writer, item);
  99. }
  100. writer.WriteEndElement();
  101. }
  102. break;
  103. }
  104. }
  105. else
  106. {
  107. LLSD.LLSDWriteOne(writer, obj);
  108. //libsecondlife.StructuredData.LLSDParser.SerializeXmlElement(
  109. // writer, libsecondlife.StructuredData.LLSD.FromObject(obj));
  110. }
  111. }
  112. public static object DeserialiseLLSDMap(Hashtable llsd, object obj)
  113. {
  114. Type myType = obj.GetType();
  115. LLSDType[] llsdattributes = (LLSDType[]) myType.GetCustomAttributes(typeof (LLSDType), false);
  116. if (llsdattributes.Length > 0)
  117. {
  118. switch (llsdattributes[0].ObjectType)
  119. {
  120. case "MAP":
  121. IDictionaryEnumerator enumerator = llsd.GetEnumerator();
  122. while (enumerator.MoveNext())
  123. {
  124. string keyName = (string)enumerator.Key;
  125. keyName = keyName.Replace("-","_");
  126. FieldInfo field = myType.GetField(keyName);
  127. if (field != null)
  128. {
  129. // if (enumerator.Value is libsecondlife.StructuredData.LLSDMap)
  130. if (enumerator.Value is Hashtable)
  131. {
  132. object fieldValue = field.GetValue(obj);
  133. DeserialiseLLSDMap((Hashtable) enumerator.Value, fieldValue);
  134. // DeserialiseLLSDMap((libsecondlife.StructuredData.LLSDMap) enumerator.Value, fieldValue);
  135. }
  136. else if (enumerator.Value is ArrayList)
  137. {
  138. object fieldValue = field.GetValue(obj);
  139. fieldValue.GetType().GetField("Array").SetValue(fieldValue, enumerator.Value);
  140. //TODO
  141. // the LLSD map/array types in the array need to be deserialised
  142. // but first we need to know the right class to deserialise them into.
  143. }
  144. else
  145. {
  146. field.SetValue(obj, enumerator.Value);
  147. }
  148. }
  149. }
  150. break;
  151. }
  152. }
  153. return obj;
  154. }
  155. }
  156. }