diff --git a/OpenSim/Framework/Serialization/External/UserInventoryItemSerializer.cs b/OpenSim/Framework/Serialization/External/UserInventoryItemSerializer.cs index 862cc72f84..df9af329c7 100644 --- a/OpenSim/Framework/Serialization/External/UserInventoryItemSerializer.cs +++ b/OpenSim/Framework/Serialization/External/UserInventoryItemSerializer.cs @@ -26,9 +26,13 @@ */ using System; +using System.Collections.Generic; using System.IO; +using System.Reflection; using System.Text; using System.Xml; + +using log4net; using OpenMetaverse; using OpenSim.Framework; @@ -40,6 +44,133 @@ namespace OpenSim.Framework.Serialization.External /// XXX: Please do not use yet. public class UserInventoryItemSerializer { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + private delegate void InventoryItemXmlProcessor(InventoryItemBase item, XmlTextReader reader); + private static Dictionary m_InventoryItemXmlProcessors = new Dictionary(); + + #region InventoryItemBase Processor initialization + static UserInventoryItemSerializer() + { + m_InventoryItemXmlProcessors.Add("Name", ProcessName); + m_InventoryItemXmlProcessors.Add("ID", ProcessID); + m_InventoryItemXmlProcessors.Add("InvType", ProcessInvType); + m_InventoryItemXmlProcessors.Add("CreatorUUID", ProcessCreatorUUID); + m_InventoryItemXmlProcessors.Add("CreationDate", ProcessCreationDate); + m_InventoryItemXmlProcessors.Add("Owner", ProcessOwner); + m_InventoryItemXmlProcessors.Add("Description", ProcessDescription); + m_InventoryItemXmlProcessors.Add("AssetType", ProcessAssetType); + m_InventoryItemXmlProcessors.Add("AssetID", ProcessAssetID); + m_InventoryItemXmlProcessors.Add("SaleType", ProcessSaleType); + m_InventoryItemXmlProcessors.Add("SalePrice", ProcessSalePrice); + m_InventoryItemXmlProcessors.Add("BasePermissions", ProcessBasePermissions); + m_InventoryItemXmlProcessors.Add("CurrentPermissions", ProcessCurrentPermissions); + m_InventoryItemXmlProcessors.Add("EveryOnePermissions", ProcessEveryOnePermissions); + m_InventoryItemXmlProcessors.Add("NextPermissions", ProcessNextPermissions); + m_InventoryItemXmlProcessors.Add("Flags", ProcessFlags); + m_InventoryItemXmlProcessors.Add("GroupID", ProcessGroupID); + m_InventoryItemXmlProcessors.Add("GroupOwned", ProcessGroupOwned); + } + #endregion + + #region InventoryItemBase Processors + private static void ProcessName(InventoryItemBase item, XmlTextReader reader) + { + item.Name = reader.ReadElementContentAsString("Name", String.Empty); + } + + private static void ProcessID(InventoryItemBase item, XmlTextReader reader) + { + item.ID = Util.ReadUUID(reader, "ID"); + } + + private static void ProcessInvType(InventoryItemBase item, XmlTextReader reader) + { + item.InvType = reader.ReadElementContentAsInt("InvType", String.Empty); + } + + private static void ProcessCreatorUUID(InventoryItemBase item, XmlTextReader reader) + { + item.CreatorId = reader.ReadElementContentAsString("CreatorUUID", String.Empty); + } + + private static void ProcessCreationDate(InventoryItemBase item, XmlTextReader reader) + { + item.CreationDate = reader.ReadElementContentAsInt("CreationDate", String.Empty); + } + + private static void ProcessOwner(InventoryItemBase item, XmlTextReader reader) + { + item.Owner = Util.ReadUUID(reader, "Owner"); + } + + private static void ProcessDescription(InventoryItemBase item, XmlTextReader reader) + { + item.Description = reader.ReadElementContentAsString("Description", String.Empty); + } + + private static void ProcessAssetType(InventoryItemBase item, XmlTextReader reader) + { + item.AssetType = reader.ReadElementContentAsInt("AssetType", String.Empty); + } + + private static void ProcessAssetID(InventoryItemBase item, XmlTextReader reader) + { + item.AssetID = Util.ReadUUID(reader, "AssetID"); + } + + private static void ProcessSaleType(InventoryItemBase item, XmlTextReader reader) + { + item.SaleType = (byte)reader.ReadElementContentAsInt("SaleType", String.Empty); + } + + private static void ProcessSalePrice(InventoryItemBase item, XmlTextReader reader) + { + item.SalePrice = reader.ReadElementContentAsInt("SalePrice", String.Empty); + } + + private static void ProcessBasePermissions(InventoryItemBase item, XmlTextReader reader) + { + item.BasePermissions = (uint)reader.ReadElementContentAsInt("BasePermissions", String.Empty); + } + + private static void ProcessCurrentPermissions(InventoryItemBase item, XmlTextReader reader) + { + item.CurrentPermissions = (uint)reader.ReadElementContentAsInt("CurrentPermissions", String.Empty); + } + + private static void ProcessEveryOnePermissions(InventoryItemBase item, XmlTextReader reader) + { + item.EveryOnePermissions = (uint)reader.ReadElementContentAsInt("EveryOnePermissions", String.Empty); + } + + private static void ProcessNextPermissions(InventoryItemBase item, XmlTextReader reader) + { + item.NextPermissions = (uint)reader.ReadElementContentAsInt("NextPermissions", String.Empty); + } + + private static void ProcessFlags(InventoryItemBase item, XmlTextReader reader) + { + item.Flags = (uint)reader.ReadElementContentAsInt("Flags", String.Empty); + } + + private static void ProcessGroupID(InventoryItemBase item, XmlTextReader reader) + { + item.GroupID = Util.ReadUUID(reader, "GroupID"); + } + + private static void ProcessGroupOwned(InventoryItemBase item, XmlTextReader reader) + { + //item.GroupOwned = reader.ReadElementContentAsBoolean("GroupOwned", String.Empty); + // We don't do that, because ReadElementContentAsBoolean assumes lower case strings, + // and they may not be lower case + reader.ReadStartElement(); // GroupOwned + item.GroupOwned = Boolean.Parse(reader.ReadContentAsString().ToLower()); + reader.ReadEndElement(); + } + + #endregion + /// /// Deserialize item /// @@ -60,37 +191,44 @@ namespace OpenSim.Framework.Serialization.External public static InventoryItemBase Deserialize(string serialization) { InventoryItemBase item = new InventoryItemBase(); - - StringReader sr = new StringReader(serialization); - XmlTextReader xtr = new XmlTextReader(sr); - - xtr.ReadStartElement("InventoryItem"); - - item.Name = xtr.ReadElementString("Name"); - item.ID = UUID.Parse( xtr.ReadElementString("ID")); - item.InvType = Convert.ToInt32( xtr.ReadElementString("InvType")); - item.CreatorId = xtr.ReadElementString("CreatorUUID"); - item.CreationDate = Convert.ToInt32( xtr.ReadElementString("CreationDate")); - item.Owner = UUID.Parse( xtr.ReadElementString("Owner")); - item.Description = xtr.ReadElementString("Description"); - item.AssetType = Convert.ToInt32( xtr.ReadElementString("AssetType")); - item.AssetID = UUID.Parse( xtr.ReadElementString("AssetID")); - item.SaleType = Convert.ToByte( xtr.ReadElementString("SaleType")); - item.SalePrice = Convert.ToInt32( xtr.ReadElementString("SalePrice")); - item.BasePermissions = Convert.ToUInt32( xtr.ReadElementString("BasePermissions")); - item.CurrentPermissions = Convert.ToUInt32( xtr.ReadElementString("CurrentPermissions")); - item.EveryOnePermissions = Convert.ToUInt32( xtr.ReadElementString("EveryOnePermissions")); - item.NextPermissions = Convert.ToUInt32( xtr.ReadElementString("NextPermissions")); - item.Flags = Convert.ToUInt32( xtr.ReadElementString("Flags")); - item.GroupID = UUID.Parse( xtr.ReadElementString("GroupID")); - item.GroupOwned = Convert.ToBoolean(xtr.ReadElementString("GroupOwned")); - - xtr.ReadEndElement(); - - xtr.Close(); - sr.Close(); - + + using (XmlTextReader reader = new XmlTextReader(new StringReader(serialization))) + { + reader.ReadStartElement("InventoryItem"); + + string nodeName = string.Empty; + while (reader.NodeType != XmlNodeType.EndElement) + { + nodeName = reader.Name; + InventoryItemXmlProcessor p = null; + if (m_InventoryItemXmlProcessors.TryGetValue(reader.Name, out p)) + { + //m_log.DebugFormat("[XXX] Processing: {0}", reader.Name); + try + { + p(item, reader); + } + catch (Exception e) + { + m_log.DebugFormat("[InventoryItemSerializer]: exception while parsing {0}: {1}", nodeName, e); + if (reader.NodeType == XmlNodeType.EndElement) + reader.Read(); + } + } + else + { + // m_log.DebugFormat("[InventoryItemSerializer]: caught unknown element {0}", nodeName); + reader.ReadOuterXml(); // ignore + } + + } + + reader.ReadEndElement(); // InventoryItem + } + + //m_log.DebugFormat("[XXX]: parsed InventoryItemBase {0} - {1}", obj.Name, obj.UUID); return item; + } public static string Serialize(InventoryItemBase inventoryItem) diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index e8f8e012de..101ece4044 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -1558,5 +1558,66 @@ namespace OpenSim.Framework return string.Empty; } + public static UUID ReadUUID(XmlTextReader reader, string name) + { + UUID id; + string idStr; + + reader.ReadStartElement(name); + + if (reader.Name == "Guid") + idStr = reader.ReadElementString("Guid"); + else if (reader.Name == "UUID") + idStr = reader.ReadElementString("UUID"); + else // no leading tag + idStr = reader.ReadContentAsString(); + UUID.TryParse(idStr, out id); + reader.ReadEndElement(); + + return id; + } + + public static Vector3 ReadVector(XmlTextReader reader, string name) + { + Vector3 vec; + + reader.ReadStartElement(name); + vec.X = reader.ReadElementContentAsFloat(reader.Name, String.Empty); // X or x + vec.Y = reader.ReadElementContentAsFloat(reader.Name, String.Empty); // Y or y + vec.Z = reader.ReadElementContentAsFloat(reader.Name, String.Empty); // Z or z + reader.ReadEndElement(); + + return vec; + } + + public static Quaternion ReadQuaternion(XmlTextReader reader, string name) + { + Quaternion quat = new Quaternion(); + + reader.ReadStartElement(name); + while (reader.NodeType != XmlNodeType.EndElement) + { + switch (reader.Name.ToLower()) + { + case "x": + quat.X = reader.ReadElementContentAsFloat(reader.Name, String.Empty); + break; + case "y": + quat.Y = reader.ReadElementContentAsFloat(reader.Name, String.Empty); + break; + case "z": + quat.Z = reader.ReadElementContentAsFloat(reader.Name, String.Empty); + break; + case "w": + quat.W = reader.ReadElementContentAsFloat(reader.Name, String.Empty); + break; + } + } + + reader.ReadEndElement(); + + return quat; + } + } } diff --git a/OpenSim/Region/Framework/Scenes/Serialization/SceneObjectSerializer.cs b/OpenSim/Region/Framework/Scenes/Serialization/SceneObjectSerializer.cs index 9cf5a39bf7..da25e80b02 100644 --- a/OpenSim/Region/Framework/Scenes/Serialization/SceneObjectSerializer.cs +++ b/OpenSim/Region/Framework/Scenes/Serialization/SceneObjectSerializer.cs @@ -414,7 +414,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessCreatorID(SceneObjectPart obj, XmlTextReader reader) { - obj.CreatorID = ReadUUID(reader, "CreatorID"); + obj.CreatorID = Util.ReadUUID(reader, "CreatorID"); } private static void ProcessCreatorData(SceneObjectPart obj, XmlTextReader reader) @@ -424,7 +424,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessFolderID(SceneObjectPart obj, XmlTextReader reader) { - obj.FolderID = ReadUUID(reader, "FolderID"); + obj.FolderID = Util.ReadUUID(reader, "FolderID"); } private static void ProcessInventorySerial(SceneObjectPart obj, XmlTextReader reader) @@ -439,7 +439,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessUUID(SceneObjectPart obj, XmlTextReader reader) { - obj.UUID = ReadUUID(reader, "UUID"); + obj.UUID = Util.ReadUUID(reader, "UUID"); } private static void ProcessLocalId(SceneObjectPart obj, XmlTextReader reader) @@ -474,32 +474,32 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessGroupPosition(SceneObjectPart obj, XmlTextReader reader) { - obj.GroupPosition = ReadVector(reader, "GroupPosition"); + obj.GroupPosition = Util.ReadVector(reader, "GroupPosition"); } private static void ProcessOffsetPosition(SceneObjectPart obj, XmlTextReader reader) { - obj.OffsetPosition = ReadVector(reader, "OffsetPosition"); ; + obj.OffsetPosition = Util.ReadVector(reader, "OffsetPosition"); ; } private static void ProcessRotationOffset(SceneObjectPart obj, XmlTextReader reader) { - obj.RotationOffset = ReadQuaternion(reader, "RotationOffset"); + obj.RotationOffset = Util.ReadQuaternion(reader, "RotationOffset"); } private static void ProcessVelocity(SceneObjectPart obj, XmlTextReader reader) { - obj.Velocity = ReadVector(reader, "Velocity"); + obj.Velocity = Util.ReadVector(reader, "Velocity"); } private static void ProcessAngularVelocity(SceneObjectPart obj, XmlTextReader reader) { - obj.AngularVelocity = ReadVector(reader, "AngularVelocity"); + obj.AngularVelocity = Util.ReadVector(reader, "AngularVelocity"); } private static void ProcessAcceleration(SceneObjectPart obj, XmlTextReader reader) { - obj.Acceleration = ReadVector(reader, "Acceleration"); + obj.Acceleration = Util.ReadVector(reader, "Acceleration"); } private static void ProcessDescription(SceneObjectPart obj, XmlTextReader reader) @@ -553,7 +553,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessScale(SceneObjectPart obj, XmlTextReader reader) { - obj.Scale = ReadVector(reader, "Scale"); + obj.Scale = Util.ReadVector(reader, "Scale"); } private static void ProcessUpdateFlag(SceneObjectPart obj, XmlTextReader reader) @@ -563,22 +563,22 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessSitTargetOrientation(SceneObjectPart obj, XmlTextReader reader) { - obj.SitTargetOrientation = ReadQuaternion(reader, "SitTargetOrientation"); + obj.SitTargetOrientation = Util.ReadQuaternion(reader, "SitTargetOrientation"); } private static void ProcessSitTargetPosition(SceneObjectPart obj, XmlTextReader reader) { - obj.SitTargetPosition = ReadVector(reader, "SitTargetPosition"); + obj.SitTargetPosition = Util.ReadVector(reader, "SitTargetPosition"); } private static void ProcessSitTargetPositionLL(SceneObjectPart obj, XmlTextReader reader) { - obj.SitTargetPositionLL = ReadVector(reader, "SitTargetPositionLL"); + obj.SitTargetPositionLL = Util.ReadVector(reader, "SitTargetPositionLL"); } private static void ProcessSitTargetOrientationLL(SceneObjectPart obj, XmlTextReader reader) { - obj.SitTargetOrientationLL = ReadQuaternion(reader, "SitTargetOrientationLL"); + obj.SitTargetOrientationLL = Util.ReadQuaternion(reader, "SitTargetOrientationLL"); } private static void ProcessParentID(SceneObjectPart obj, XmlTextReader reader) @@ -614,17 +614,17 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessGroupID(SceneObjectPart obj, XmlTextReader reader) { - obj.GroupID = ReadUUID(reader, "GroupID"); + obj.GroupID = Util.ReadUUID(reader, "GroupID"); } private static void ProcessOwnerID(SceneObjectPart obj, XmlTextReader reader) { - obj.OwnerID = ReadUUID(reader, "OwnerID"); + obj.OwnerID = Util.ReadUUID(reader, "OwnerID"); } private static void ProcessLastOwnerID(SceneObjectPart obj, XmlTextReader reader) { - obj.LastOwnerID = ReadUUID(reader, "LastOwnerID"); + obj.LastOwnerID = Util.ReadUUID(reader, "LastOwnerID"); } private static void ProcessBaseMask(SceneObjectPart obj, XmlTextReader reader) @@ -663,7 +663,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessCollisionSound(SceneObjectPart obj, XmlTextReader reader) { - obj.CollisionSound = ReadUUID(reader, "CollisionSound"); + obj.CollisionSound = Util.ReadUUID(reader, "CollisionSound"); } private static void ProcessCollisionSoundVolume(SceneObjectPart obj, XmlTextReader reader) @@ -690,7 +690,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization #region TaskInventoryXmlProcessors private static void ProcessTIAssetID(TaskInventoryItem item, XmlTextReader reader) { - item.AssetID = ReadUUID(reader, "AssetID"); + item.AssetID = Util.ReadUUID(reader, "AssetID"); } private static void ProcessTIBasePermissions(TaskInventoryItem item, XmlTextReader reader) @@ -705,7 +705,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessTICreatorID(TaskInventoryItem item, XmlTextReader reader) { - item.CreatorID = ReadUUID(reader, "CreatorID"); + item.CreatorID = Util.ReadUUID(reader, "CreatorID"); } private static void ProcessTICreatorData(TaskInventoryItem item, XmlTextReader reader) @@ -730,7 +730,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessTIGroupID(TaskInventoryItem item, XmlTextReader reader) { - item.GroupID = ReadUUID(reader, "GroupID"); + item.GroupID = Util.ReadUUID(reader, "GroupID"); } private static void ProcessTIGroupPermissions(TaskInventoryItem item, XmlTextReader reader) @@ -745,20 +745,20 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessTIItemID(TaskInventoryItem item, XmlTextReader reader) { - item.ItemID = ReadUUID(reader, "ItemID"); + item.ItemID = Util.ReadUUID(reader, "ItemID"); } private static void ProcessTIOldItemID(TaskInventoryItem item, XmlTextReader reader) { - ReadUUID(reader, "OldItemID"); + Util.ReadUUID(reader, "OldItemID"); // On deserialization, the old item id MUST BE UUID.Zero!!!!! // Setting this to the saved value will BREAK script persistence! - // item.OldItemID = ReadUUID(reader, "OldItemID"); + // item.OldItemID = Util.ReadUUID(reader, "OldItemID"); } private static void ProcessTILastOwnerID(TaskInventoryItem item, XmlTextReader reader) { - item.LastOwnerID = ReadUUID(reader, "LastOwnerID"); + item.LastOwnerID = Util.ReadUUID(reader, "LastOwnerID"); } private static void ProcessTIName(TaskInventoryItem item, XmlTextReader reader) @@ -773,7 +773,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessTIOwnerID(TaskInventoryItem item, XmlTextReader reader) { - item.OwnerID = ReadUUID(reader, "OwnerID"); + item.OwnerID = Util.ReadUUID(reader, "OwnerID"); } private static void ProcessTICurrentPermissions(TaskInventoryItem item, XmlTextReader reader) @@ -783,17 +783,17 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessTIParentID(TaskInventoryItem item, XmlTextReader reader) { - item.ParentID = ReadUUID(reader, "ParentID"); + item.ParentID = Util.ReadUUID(reader, "ParentID"); } private static void ProcessTIParentPartID(TaskInventoryItem item, XmlTextReader reader) { - item.ParentPartID = ReadUUID(reader, "ParentPartID"); + item.ParentPartID = Util.ReadUUID(reader, "ParentPartID"); } private static void ProcessTIPermsGranter(TaskInventoryItem item, XmlTextReader reader) { - item.PermsGranter = ReadUUID(reader, "PermsGranter"); + item.PermsGranter = Util.ReadUUID(reader, "PermsGranter"); } private static void ProcessTIPermsMask(TaskInventoryItem item, XmlTextReader reader) @@ -922,7 +922,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessShpScale(PrimitiveBaseShape shp, XmlTextReader reader) { - shp.Scale = ReadVector(reader, "Scale"); + shp.Scale = Util.ReadVector(reader, "Scale"); } private static void ProcessShpState(PrimitiveBaseShape shp, XmlTextReader reader) @@ -950,7 +950,7 @@ namespace OpenSim.Region.Framework.Scenes.Serialization private static void ProcessShpSculptTexture(PrimitiveBaseShape shp, XmlTextReader reader) { - shp.SculptTexture = ReadUUID(reader, "SculptTexture"); + shp.SculptTexture = Util.ReadUUID(reader, "SculptTexture"); } private static void ProcessShpSculptType(PrimitiveBaseShape shp, XmlTextReader reader) @@ -1459,66 +1459,6 @@ namespace OpenSim.Region.Framework.Scenes.Serialization return obj; } - static UUID ReadUUID(XmlTextReader reader, string name) - { - UUID id; - string idStr; - - reader.ReadStartElement(name); - - if (reader.Name == "Guid") - idStr = reader.ReadElementString("Guid"); - else // UUID - idStr = reader.ReadElementString("UUID"); - - UUID.TryParse(idStr, out id); - reader.ReadEndElement(); - - return id; - } - - static Vector3 ReadVector(XmlTextReader reader, string name) - { - Vector3 vec; - - reader.ReadStartElement(name); - vec.X = reader.ReadElementContentAsFloat(reader.Name, String.Empty); // X or x - vec.Y = reader.ReadElementContentAsFloat(reader.Name, String.Empty); // Y or y - vec.Z = reader.ReadElementContentAsFloat(reader.Name, String.Empty); // Z or z - reader.ReadEndElement(); - - return vec; - } - - static Quaternion ReadQuaternion(XmlTextReader reader, string name) - { - Quaternion quat = new Quaternion(); - - reader.ReadStartElement(name); - while (reader.NodeType != XmlNodeType.EndElement) - { - switch (reader.Name.ToLower()) - { - case "x": - quat.X = reader.ReadElementContentAsFloat(reader.Name, String.Empty); - break; - case "y": - quat.Y = reader.ReadElementContentAsFloat(reader.Name, String.Empty); - break; - case "z": - quat.Z = reader.ReadElementContentAsFloat(reader.Name, String.Empty); - break; - case "w": - quat.W = reader.ReadElementContentAsFloat(reader.Name, String.Empty); - break; - } - } - - reader.ReadEndElement(); - - return quat; - } - static TaskInventoryDictionary ReadTaskInventory(XmlTextReader reader, string name) { TaskInventoryDictionary tinv = new TaskInventoryDictionary();