UserInventoryItemSerializer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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, XmlReader>> m_InventoryItemXmlProcessors
  46. = new Dictionary<string, Action<InventoryItemBase, XmlReader>>();
  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, XmlReader reader)
  74. {
  75. item.Name = reader.ReadElementContentAsString("Name", String.Empty);
  76. }
  77. private static void ProcessID(InventoryItemBase item, XmlReader reader)
  78. {
  79. item.ID = Util.ReadUUID(reader, "ID");
  80. }
  81. private static void ProcessInvType(InventoryItemBase item, XmlReader reader)
  82. {
  83. item.InvType = reader.ReadElementContentAsInt("InvType", String.Empty);
  84. }
  85. private static void ProcessCreatorUUID(InventoryItemBase item, XmlReader reader)
  86. {
  87. item.CreatorId = reader.ReadElementContentAsString("CreatorUUID", String.Empty);
  88. }
  89. private static void ProcessCreatorID(InventoryItemBase item, XmlReader 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, XmlReader reader)
  95. {
  96. item.CreationDate = reader.ReadElementContentAsInt("CreationDate", String.Empty);
  97. }
  98. private static void ProcessOwner(InventoryItemBase item, XmlReader reader)
  99. {
  100. item.Owner = Util.ReadUUID(reader, "Owner");
  101. }
  102. private static void ProcessDescription(InventoryItemBase item, XmlReader reader)
  103. {
  104. item.Description = reader.ReadElementContentAsString("Description", String.Empty);
  105. }
  106. private static void ProcessAssetType(InventoryItemBase item, XmlReader reader)
  107. {
  108. item.AssetType = reader.ReadElementContentAsInt("AssetType", String.Empty);
  109. }
  110. private static void ProcessAssetID(InventoryItemBase item, XmlReader reader)
  111. {
  112. item.AssetID = Util.ReadUUID(reader, "AssetID");
  113. }
  114. private static void ProcessSaleType(InventoryItemBase item, XmlReader reader)
  115. {
  116. item.SaleType = (byte)reader.ReadElementContentAsInt("SaleType", String.Empty);
  117. }
  118. private static void ProcessSalePrice(InventoryItemBase item, XmlReader reader)
  119. {
  120. item.SalePrice = reader.ReadElementContentAsInt("SalePrice", String.Empty);
  121. }
  122. private static void ProcessBasePermissions(InventoryItemBase item, XmlReader reader)
  123. {
  124. item.BasePermissions = (uint)reader.ReadElementContentAsInt("BasePermissions", String.Empty);
  125. }
  126. private static void ProcessCurrentPermissions(InventoryItemBase item, XmlReader reader)
  127. {
  128. item.CurrentPermissions = (uint)reader.ReadElementContentAsInt("CurrentPermissions", String.Empty);
  129. }
  130. private static void ProcessEveryOnePermissions(InventoryItemBase item, XmlReader reader)
  131. {
  132. item.EveryOnePermissions = (uint)reader.ReadElementContentAsInt("EveryOnePermissions", String.Empty);
  133. }
  134. private static void ProcessNextPermissions(InventoryItemBase item, XmlReader reader)
  135. {
  136. item.NextPermissions = (uint)reader.ReadElementContentAsInt("NextPermissions", String.Empty);
  137. }
  138. private static void ProcessFlags(InventoryItemBase item, XmlReader reader)
  139. {
  140. item.Flags = (uint)reader.ReadElementContentAsInt("Flags", String.Empty);
  141. }
  142. private static void ProcessGroupID(InventoryItemBase item, XmlReader reader)
  143. {
  144. item.GroupID = Util.ReadUUID(reader, "GroupID");
  145. }
  146. private static void ProcessGroupOwned(InventoryItemBase item, XmlReader reader)
  147. {
  148. item.GroupOwned = Util.ReadBoolean(reader);
  149. }
  150. private static void ProcessCreatorData(InventoryItemBase item, XmlReader 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.DtdProcessing = DtdProcessing.Prohibit;
  177. reader.XmlResolver = null;
  178. reader.ReadStartElement("InventoryItem");
  179. ExternalRepresentationUtils.ExecuteReadProcessors<InventoryItemBase>(
  180. item, m_InventoryItemXmlProcessors, reader);
  181. reader.ReadEndElement(); // InventoryItem
  182. }
  183. //m_log.DebugFormat("[XXX]: parsed InventoryItemBase {0} - {1}", obj.Name, obj.UUID);
  184. return item;
  185. }
  186. public static string Serialize(InventoryItemBase inventoryItem, Dictionary<string, object> options, IUserAccountService userAccountService)
  187. {
  188. StringWriter sw = new StringWriter();
  189. XmlTextWriter writer = new XmlTextWriter(sw);
  190. writer.Formatting = Formatting.Indented;
  191. writer.WriteStartDocument();
  192. writer.WriteStartElement("InventoryItem");
  193. writer.WriteStartElement("Name");
  194. writer.WriteString(inventoryItem.Name);
  195. writer.WriteEndElement();
  196. writer.WriteStartElement("ID");
  197. writer.WriteString(inventoryItem.ID.ToString());
  198. writer.WriteEndElement();
  199. writer.WriteStartElement("InvType");
  200. writer.WriteString(inventoryItem.InvType.ToString());
  201. writer.WriteEndElement();
  202. writer.WriteStartElement("CreatorUUID");
  203. writer.WriteString(OspResolver.MakeOspa(inventoryItem.CreatorIdAsUuid, userAccountService));
  204. writer.WriteEndElement();
  205. writer.WriteStartElement("CreationDate");
  206. writer.WriteString(inventoryItem.CreationDate.ToString());
  207. writer.WriteEndElement();
  208. writer.WriteStartElement("Owner");
  209. writer.WriteString(inventoryItem.Owner.ToString());
  210. writer.WriteEndElement();
  211. writer.WriteStartElement("Description");
  212. writer.WriteString(inventoryItem.Description);
  213. writer.WriteEndElement();
  214. writer.WriteStartElement("AssetType");
  215. writer.WriteString(inventoryItem.AssetType.ToString());
  216. writer.WriteEndElement();
  217. writer.WriteStartElement("AssetID");
  218. writer.WriteString(inventoryItem.AssetID.ToString());
  219. writer.WriteEndElement();
  220. writer.WriteStartElement("SaleType");
  221. writer.WriteString(inventoryItem.SaleType.ToString());
  222. writer.WriteEndElement();
  223. writer.WriteStartElement("SalePrice");
  224. writer.WriteString(inventoryItem.SalePrice.ToString());
  225. writer.WriteEndElement();
  226. writer.WriteStartElement("BasePermissions");
  227. writer.WriteString(inventoryItem.BasePermissions.ToString());
  228. writer.WriteEndElement();
  229. writer.WriteStartElement("CurrentPermissions");
  230. writer.WriteString(inventoryItem.CurrentPermissions.ToString());
  231. writer.WriteEndElement();
  232. writer.WriteStartElement("EveryOnePermissions");
  233. writer.WriteString(inventoryItem.EveryOnePermissions.ToString());
  234. writer.WriteEndElement();
  235. writer.WriteStartElement("NextPermissions");
  236. writer.WriteString(inventoryItem.NextPermissions.ToString());
  237. writer.WriteEndElement();
  238. writer.WriteStartElement("Flags");
  239. writer.WriteString(inventoryItem.Flags.ToString());
  240. writer.WriteEndElement();
  241. writer.WriteStartElement("GroupID");
  242. writer.WriteString(inventoryItem.GroupID.ToString());
  243. writer.WriteEndElement();
  244. writer.WriteStartElement("GroupOwned");
  245. writer.WriteString(inventoryItem.GroupOwned.ToString());
  246. writer.WriteEndElement();
  247. if (options.ContainsKey("creators") && !string.IsNullOrEmpty(inventoryItem.CreatorData))
  248. writer.WriteElementString("CreatorData", inventoryItem.CreatorData);
  249. else if (options.ContainsKey("home"))
  250. {
  251. if (userAccountService != null)
  252. {
  253. UserAccount account = userAccountService.GetUserAccount(UUID.Zero, inventoryItem.CreatorIdAsUuid);
  254. if (account != null)
  255. {
  256. string creatorData = ExternalRepresentationUtils.CalcCreatorData((string)options["home"], inventoryItem.CreatorIdAsUuid, account.FirstName + " " + account.LastName);
  257. writer.WriteElementString("CreatorData", creatorData);
  258. }
  259. writer.WriteElementString("CreatorID", inventoryItem.CreatorId);
  260. }
  261. }
  262. writer.WriteEndElement();
  263. writer.Close();
  264. sw.Close();
  265. return sw.ToString();
  266. }
  267. }
  268. }