1
0

TaskInventoryDictionary.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.Xml;
  30. using System.Xml.Schema;
  31. using System.Xml.Serialization;
  32. using OpenMetaverse;
  33. namespace OpenSim.Framework
  34. {
  35. /// <summary>
  36. /// A dictionary for task inventory.
  37. /// </summary>
  38. /// This class is not thread safe. Callers must synchronize on Dictionary methods or Clone() this object before
  39. /// iterating over it.
  40. public class TaskInventoryDictionary : Dictionary<UUID, TaskInventoryItem>,
  41. ICloneable, IXmlSerializable
  42. {
  43. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private static XmlSerializer tiiSerializer = new XmlSerializer(typeof (TaskInventoryItem));
  45. #region ICloneable Members
  46. public Object Clone()
  47. {
  48. TaskInventoryDictionary clone = new TaskInventoryDictionary();
  49. lock (this)
  50. {
  51. foreach (UUID uuid in Keys)
  52. {
  53. clone.Add(uuid, (TaskInventoryItem) this[uuid].Clone());
  54. }
  55. }
  56. return clone;
  57. }
  58. #endregion
  59. // The alternative of simply serializing the list doesn't appear to work on mono, since
  60. // we get a
  61. //
  62. // System.TypeInitializationException: An exception was thrown by the type initializer for OpenSim.Framework.TaskInventoryDictionary ---> System.ArgumentOutOfRangeException: < 0
  63. // Parameter name: length
  64. // at System.String.Substring (Int32 startIndex, Int32 length) [0x00088] in /build/buildd/mono-1.2.4/mcs/class/corlib/System/String.cs:381
  65. // at System.Xml.Serialization.TypeTranslator.GetTypeData (System.Type runtimeType, System.String xmlDataType) [0x001f6] in /build/buildd/mono-1.2.4/mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs:217
  66. // ...
  67. // private static XmlSerializer tiiSerializer
  68. // = new XmlSerializer(typeof(Dictionary<UUID, TaskInventoryItem>.ValueCollection));
  69. // see IXmlSerializable
  70. #region IXmlSerializable Members
  71. public XmlSchema GetSchema()
  72. {
  73. return null;
  74. }
  75. // see IXmlSerializable
  76. public void ReadXml(XmlReader reader)
  77. {
  78. // m_log.DebugFormat("[TASK INVENTORY]: ReadXml current node before actions, {0}", reader.Name);
  79. if (!reader.IsEmptyElement)
  80. {
  81. reader.Read();
  82. while (tiiSerializer.CanDeserialize(reader))
  83. {
  84. TaskInventoryItem item = (TaskInventoryItem) tiiSerializer.Deserialize(reader);
  85. Add(item.ItemID, item);
  86. //m_log.DebugFormat("[TASK INVENTORY]: Instanted prim item {0}, {1} from xml", item.Name, item.ItemID);
  87. }
  88. // m_log.DebugFormat("[TASK INVENTORY]: Instantiated {0} prim items in total from xml", Count);
  89. }
  90. // else
  91. // {
  92. // m_log.DebugFormat("[TASK INVENTORY]: Skipping empty element {0}", reader.Name);
  93. // }
  94. // For some .net implementations, this last read is necessary so that we advance beyond the end tag
  95. // of the element wrapping this object so that the rest of the serialization can complete normally.
  96. reader.Read();
  97. // m_log.DebugFormat("[TASK INVENTORY]: ReadXml current node after actions, {0}", reader.Name);
  98. }
  99. // see IXmlSerializable
  100. public void WriteXml(XmlWriter writer)
  101. {
  102. lock (this)
  103. {
  104. foreach (TaskInventoryItem item in Values)
  105. {
  106. tiiSerializer.Serialize(writer, item);
  107. }
  108. }
  109. //tiiSerializer.Serialize(writer, Values);
  110. }
  111. #endregion
  112. // see ICloneable
  113. }
  114. }