UserInventoryItemSerializer.cs 13 KB

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