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