UserInventoryItemSerializer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Xml;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Framework.Serialization.External
  38. {
  39. /// <summary>
  40. /// Serialize and deserialize user inventory items as an external format.
  41. /// </summary>
  42. public class UserInventoryItemSerializer
  43. {
  44. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private static Dictionary<string, Action<InventoryItemBase, XmlTextReader>> m_InventoryItemXmlProcessors
  46. = new Dictionary<string, Action<InventoryItemBase, XmlTextReader>>();
  47. #region InventoryItemBase Processor initialization
  48. static UserInventoryItemSerializer()
  49. {
  50. m_InventoryItemXmlProcessors.Add("Name", ProcessName);
  51. m_InventoryItemXmlProcessors.Add("ID", ProcessID);
  52. m_InventoryItemXmlProcessors.Add("InvType", ProcessInvType);
  53. m_InventoryItemXmlProcessors.Add("CreatorUUID", ProcessCreatorUUID);
  54. m_InventoryItemXmlProcessors.Add("CreatorID", ProcessCreatorID);
  55. m_InventoryItemXmlProcessors.Add("CreatorData", ProcessCreatorData);
  56. m_InventoryItemXmlProcessors.Add("CreationDate", ProcessCreationDate);
  57. m_InventoryItemXmlProcessors.Add("Owner", ProcessOwner);
  58. m_InventoryItemXmlProcessors.Add("Description", ProcessDescription);
  59. m_InventoryItemXmlProcessors.Add("AssetType", ProcessAssetType);
  60. m_InventoryItemXmlProcessors.Add("AssetID", ProcessAssetID);
  61. m_InventoryItemXmlProcessors.Add("SaleType", ProcessSaleType);
  62. m_InventoryItemXmlProcessors.Add("SalePrice", ProcessSalePrice);
  63. m_InventoryItemXmlProcessors.Add("BasePermissions", ProcessBasePermissions);
  64. m_InventoryItemXmlProcessors.Add("CurrentPermissions", ProcessCurrentPermissions);
  65. m_InventoryItemXmlProcessors.Add("EveryOnePermissions", ProcessEveryOnePermissions);
  66. m_InventoryItemXmlProcessors.Add("NextPermissions", ProcessNextPermissions);
  67. m_InventoryItemXmlProcessors.Add("Flags", ProcessFlags);
  68. m_InventoryItemXmlProcessors.Add("GroupID", ProcessGroupID);
  69. m_InventoryItemXmlProcessors.Add("GroupOwned", ProcessGroupOwned);
  70. }
  71. #endregion
  72. #region InventoryItemBase Processors
  73. private static void ProcessName(InventoryItemBase item, XmlTextReader reader)
  74. {
  75. item.Name = reader.ReadElementContentAsString("Name", String.Empty);
  76. }
  77. private static void ProcessID(InventoryItemBase item, XmlTextReader reader)
  78. {
  79. item.ID = Util.ReadUUID(reader, "ID");
  80. }
  81. private static void ProcessInvType(InventoryItemBase item, XmlTextReader reader)
  82. {
  83. item.InvType = reader.ReadElementContentAsInt("InvType", String.Empty);
  84. }
  85. private static void ProcessCreatorUUID(InventoryItemBase item, XmlTextReader reader)
  86. {
  87. item.CreatorId = reader.ReadElementContentAsString("CreatorUUID", String.Empty);
  88. }
  89. private static void ProcessCreatorID(InventoryItemBase item, XmlTextReader reader)
  90. {
  91. // when it exists, this overrides the previous
  92. item.CreatorId = reader.ReadElementContentAsString("CreatorID", String.Empty);
  93. }
  94. private static void ProcessCreationDate(InventoryItemBase item, XmlTextReader reader)
  95. {
  96. item.CreationDate = reader.ReadElementContentAsInt("CreationDate", String.Empty);
  97. }
  98. private static void ProcessOwner(InventoryItemBase item, XmlTextReader reader)
  99. {
  100. item.Owner = Util.ReadUUID(reader, "Owner");
  101. }
  102. private static void ProcessDescription(InventoryItemBase item, XmlTextReader reader)
  103. {
  104. item.Description = reader.ReadElementContentAsString("Description", String.Empty);
  105. }
  106. private static void ProcessAssetType(InventoryItemBase item, XmlTextReader reader)
  107. {
  108. item.AssetType = reader.ReadElementContentAsInt("AssetType", String.Empty);
  109. }
  110. private static void ProcessAssetID(InventoryItemBase item, XmlTextReader reader)
  111. {
  112. item.AssetID = Util.ReadUUID(reader, "AssetID");
  113. }
  114. private static void ProcessSaleType(InventoryItemBase item, XmlTextReader reader)
  115. {
  116. item.SaleType = (byte)reader.ReadElementContentAsInt("SaleType", String.Empty);
  117. }
  118. private static void ProcessSalePrice(InventoryItemBase item, XmlTextReader reader)
  119. {
  120. item.SalePrice = reader.ReadElementContentAsInt("SalePrice", String.Empty);
  121. }
  122. private static void ProcessBasePermissions(InventoryItemBase item, XmlTextReader reader)
  123. {
  124. item.BasePermissions = (uint)reader.ReadElementContentAsInt("BasePermissions", String.Empty);
  125. }
  126. private static void ProcessCurrentPermissions(InventoryItemBase item, XmlTextReader reader)
  127. {
  128. item.CurrentPermissions = (uint)reader.ReadElementContentAsInt("CurrentPermissions", String.Empty);
  129. }
  130. private static void ProcessEveryOnePermissions(InventoryItemBase item, XmlTextReader reader)
  131. {
  132. item.EveryOnePermissions = (uint)reader.ReadElementContentAsInt("EveryOnePermissions", String.Empty);
  133. }
  134. private static void ProcessNextPermissions(InventoryItemBase item, XmlTextReader reader)
  135. {
  136. item.NextPermissions = (uint)reader.ReadElementContentAsInt("NextPermissions", String.Empty);
  137. }
  138. private static void ProcessFlags(InventoryItemBase item, XmlTextReader reader)
  139. {
  140. item.Flags = (uint)reader.ReadElementContentAsInt("Flags", String.Empty);
  141. }
  142. private static void ProcessGroupID(InventoryItemBase item, XmlTextReader reader)
  143. {
  144. item.GroupID = Util.ReadUUID(reader, "GroupID");
  145. }
  146. private static void ProcessGroupOwned(InventoryItemBase item, XmlTextReader reader)
  147. {
  148. item.GroupOwned = Util.ReadBoolean(reader);
  149. }
  150. private static void ProcessCreatorData(InventoryItemBase item, XmlTextReader reader)
  151. {
  152. item.CreatorData = reader.ReadElementContentAsString("CreatorData", String.Empty);
  153. }
  154. #endregion
  155. /// <summary>
  156. /// Deserialize item
  157. /// </summary>
  158. /// <param name="serializedSettings"></param>
  159. /// <returns></returns>
  160. /// <exception cref="System.Xml.XmlException"></exception>
  161. public static InventoryItemBase Deserialize(byte[] serialization)
  162. {
  163. return Deserialize(Encoding.ASCII.GetString(serialization, 0, serialization.Length));
  164. }
  165. /// <summary>
  166. /// Deserialize settings
  167. /// </summary>
  168. /// <param name="serializedSettings"></param>
  169. /// <returns></returns>
  170. /// <exception cref="System.Xml.XmlException"></exception>
  171. public static InventoryItemBase Deserialize(string serialization)
  172. {
  173. InventoryItemBase item = new InventoryItemBase();
  174. using (XmlTextReader reader = new XmlTextReader(new StringReader(serialization)))
  175. {
  176. reader.ReadStartElement("InventoryItem");
  177. ExternalRepresentationUtils.ExecuteReadProcessors<InventoryItemBase>(
  178. item, m_InventoryItemXmlProcessors, reader);
  179. reader.ReadEndElement(); // InventoryItem
  180. }
  181. //m_log.DebugFormat("[XXX]: parsed InventoryItemBase {0} - {1}", obj.Name, obj.UUID);
  182. return item;
  183. }
  184. public static string Serialize(InventoryItemBase inventoryItem, Dictionary<string, object> options, IUserAccountService userAccountService)
  185. {
  186. StringWriter sw = new StringWriter();
  187. XmlTextWriter writer = new XmlTextWriter(sw);
  188. writer.Formatting = Formatting.Indented;
  189. writer.WriteStartDocument();
  190. writer.WriteStartElement("InventoryItem");
  191. writer.WriteStartElement("Name");
  192. writer.WriteString(inventoryItem.Name);
  193. writer.WriteEndElement();
  194. writer.WriteStartElement("ID");
  195. writer.WriteString(inventoryItem.ID.ToString());
  196. writer.WriteEndElement();
  197. writer.WriteStartElement("InvType");
  198. writer.WriteString(inventoryItem.InvType.ToString());
  199. writer.WriteEndElement();
  200. writer.WriteStartElement("CreatorUUID");
  201. writer.WriteString(OspResolver.MakeOspa(inventoryItem.CreatorIdAsUuid, userAccountService));
  202. writer.WriteEndElement();
  203. writer.WriteStartElement("CreationDate");
  204. writer.WriteString(inventoryItem.CreationDate.ToString());
  205. writer.WriteEndElement();
  206. writer.WriteStartElement("Owner");
  207. writer.WriteString(inventoryItem.Owner.ToString());
  208. writer.WriteEndElement();
  209. writer.WriteStartElement("Description");
  210. writer.WriteString(inventoryItem.Description);
  211. writer.WriteEndElement();
  212. writer.WriteStartElement("AssetType");
  213. writer.WriteString(inventoryItem.AssetType.ToString());
  214. writer.WriteEndElement();
  215. writer.WriteStartElement("AssetID");
  216. writer.WriteString(inventoryItem.AssetID.ToString());
  217. writer.WriteEndElement();
  218. writer.WriteStartElement("SaleType");
  219. writer.WriteString(inventoryItem.SaleType.ToString());
  220. writer.WriteEndElement();
  221. writer.WriteStartElement("SalePrice");
  222. writer.WriteString(inventoryItem.SalePrice.ToString());
  223. writer.WriteEndElement();
  224. writer.WriteStartElement("BasePermissions");
  225. writer.WriteString(inventoryItem.BasePermissions.ToString());
  226. writer.WriteEndElement();
  227. writer.WriteStartElement("CurrentPermissions");
  228. writer.WriteString(inventoryItem.CurrentPermissions.ToString());
  229. writer.WriteEndElement();
  230. writer.WriteStartElement("EveryOnePermissions");
  231. writer.WriteString(inventoryItem.EveryOnePermissions.ToString());
  232. writer.WriteEndElement();
  233. writer.WriteStartElement("NextPermissions");
  234. writer.WriteString(inventoryItem.NextPermissions.ToString());
  235. writer.WriteEndElement();
  236. writer.WriteStartElement("Flags");
  237. writer.WriteString(inventoryItem.Flags.ToString());
  238. writer.WriteEndElement();
  239. writer.WriteStartElement("GroupID");
  240. writer.WriteString(inventoryItem.GroupID.ToString());
  241. writer.WriteEndElement();
  242. writer.WriteStartElement("GroupOwned");
  243. writer.WriteString(inventoryItem.GroupOwned.ToString());
  244. writer.WriteEndElement();
  245. if (options.ContainsKey("creators") && !string.IsNullOrEmpty(inventoryItem.CreatorData))
  246. writer.WriteElementString("CreatorData", inventoryItem.CreatorData);
  247. else if (options.ContainsKey("home"))
  248. {
  249. if (userAccountService != null)
  250. {
  251. UserAccount account = userAccountService.GetUserAccount(UUID.Zero, inventoryItem.CreatorIdAsUuid);
  252. if (account != null)
  253. {
  254. writer.WriteElementString("CreatorData", (string)options["home"] + "/" + inventoryItem.CreatorIdAsUuid + ";" + account.FirstName + " " + account.LastName);
  255. }
  256. writer.WriteElementString("CreatorID", inventoryItem.CreatorId);
  257. }
  258. }
  259. writer.WriteEndElement();
  260. writer.Close();
  261. sw.Close();
  262. return sw.ToString();
  263. }
  264. }
  265. }