From 1769e93c42384125eb75fcc21dee43bb52a6090a Mon Sep 17 00:00:00 2001 From: Oren Hurvitz Date: Mon, 18 Nov 2013 13:14:49 +0200 Subject: [PATCH] Fixed parsing of coalesced objects if the XML starts with an XML Declaration ("") Resolves http://opensimulator.org/mantis/view.php?id=6944 --- .../CoalescedSceneObjectsSerializer.cs | 93 ++++++++++--------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/OpenSim/Region/Framework/Scenes/Serialization/CoalescedSceneObjectsSerializer.cs b/OpenSim/Region/Framework/Scenes/Serialization/CoalescedSceneObjectsSerializer.cs index 5cb271ddad..a556f9d0b9 100644 --- a/OpenSim/Region/Framework/Scenes/Serialization/CoalescedSceneObjectsSerializer.cs +++ b/OpenSim/Region/Framework/Scenes/Serialization/CoalescedSceneObjectsSerializer.cs @@ -119,21 +119,22 @@ namespace OpenSim.Region.Framework.Scenes.Serialization return output; } } - + public static bool TryFromXml(string xml, out CoalescedSceneObjects coa) { // m_log.DebugFormat("[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() deserializing {0}", xml); coa = null; - int i = 0; - using (StringReader sr = new StringReader(xml)) - { - using (XmlTextReader reader = new XmlTextReader(sr)) - { - try + try + { + // Quickly check if this is a coalesced object, without fully parsing the XML + using (StringReader sr = new StringReader(xml)) + { + using (XmlTextReader reader = new XmlTextReader(sr)) { - reader.Read(); + reader.MoveToContent(); // skip possible xml declaration + if (reader.Name != "CoalescedObject") { // m_log.DebugFormat( @@ -142,49 +143,49 @@ namespace OpenSim.Region.Framework.Scenes.Serialization return false; } - - coa = new CoalescedSceneObjects(UUID.Zero); - reader.Read(); - - while (reader.NodeType != XmlNodeType.EndElement && reader.Name != "CoalescedObject") - { - if (reader.Name == "SceneObjectGroup") - { - string soXml = reader.ReadOuterXml(); - - SceneObjectGroup so = SceneObjectSerializer.FromOriginalXmlFormat(soXml); - - if (so != null) - { - coa.Add(so); - } - else - { - // XXX: Possibly we should fail outright here rather than continuing if a particular component of the - // coalesced object fails to load. - m_log.WarnFormat( - "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml for component {0} failed. Continuing.", - i); - } - - i++; - } - } - - reader.ReadEndElement(); // CoalescedObject } - catch (Exception e) - { - m_log.ErrorFormat( - "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml failed with {0} {1}", - e.Message, e.StackTrace); + } + + XmlDocument doc = new XmlDocument(); + doc.LoadXml(xml); + XmlElement e = (XmlElement)doc.SelectSingleNode("/CoalescedObject"); + if (e == null) + return false; - return false; - } + coa = new CoalescedSceneObjects(UUID.Zero); + + XmlNodeList groups = e.SelectNodes("SceneObjectGroup"); + int i = 0; + + foreach (XmlNode n in groups) + { + SceneObjectGroup so = SceneObjectSerializer.FromOriginalXmlFormat(n.OuterXml); + if (so != null) + { + coa.Add(so); + } + else + { + // XXX: Possibly we should fail outright here rather than continuing if a particular component of the + // coalesced object fails to load. + m_log.WarnFormat( + "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml for component {0} failed. Continuing.", + i); + } + + i++; } } + catch (Exception e) + { + m_log.Error(string.Format( + "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml failed with {0} ", + e.Message), e); + + return false; + } return true; } } -} \ No newline at end of file +}