UserInventoryItemSerializer.cs 15 KB

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