From 623426421132fdc41d9de006fb55aedc660d5362 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Fri, 3 Feb 2012 22:45:50 +0000 Subject: Refactor common deserialization processor code to generic method ExternalRepresentationUtils.ExecuteReadProcessors() --- .../External/ExternalRepresentationUtils.cs | 53 +++++++++++++++- .../Serialization/External/LandDataSerializer.cs | 74 ++-------------------- .../External/UserInventoryItemSerializer.cs | 33 ++-------- 3 files changed, 62 insertions(+), 98 deletions(-) (limited to 'OpenSim') diff --git a/OpenSim/Framework/Serialization/External/ExternalRepresentationUtils.cs b/OpenSim/Framework/Serialization/External/ExternalRepresentationUtils.cs index 7447ac2..39a9f37 100644 --- a/OpenSim/Framework/Serialization/External/ExternalRepresentationUtils.cs +++ b/OpenSim/Framework/Serialization/External/ExternalRepresentationUtils.cs @@ -24,11 +24,13 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + using System; using System.Collections.Generic; using System.IO; +using System.Reflection; using System.Xml; - +using log4net; using OpenMetaverse; using OpenSim.Services.Interfaces; @@ -39,6 +41,55 @@ namespace OpenSim.Framework.Serialization.External /// public class ExternalRepresentationUtils { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + /// + /// Populate a node with data read from xml using a dictinoary of processors + /// + /// + /// + /// A + /// + /// + /// A + /// + public static void ExecuteReadProcessors( + NodeType nodeToFill, Dictionary> processors, XmlTextReader xtr) + { + string nodeName = string.Empty; + while (xtr.NodeType != XmlNodeType.EndElement) + { + nodeName = xtr.Name; + +// m_log.DebugFormat("[ExternalRepresentationUtils]: Processing: {0}", nodeName); + + Action p = null; + if (processors.TryGetValue(xtr.Name, out p)) + { +// m_log.DebugFormat("[ExternalRepresentationUtils]: Found {0} processor, nodeName); + + try + { + p(nodeToFill, xtr); + } + catch (Exception e) + { + m_log.ErrorFormat( + "[ExternalRepresentationUtils]: Exception while parsing element {0}, continuing. Exception {1}{2}", + nodeName, e.Message, e.StackTrace); + + if (xtr.NodeType == XmlNodeType.EndElement) + xtr.Read(); + } + } + else + { + // m_log.DebugFormat("[LandDataSerializer]: caught unknown element {0}", nodeName); + xtr.ReadOuterXml(); // ignore + } + } + } + /// /// Takes a XML representation of a SceneObjectPart and returns another XML representation /// with creator data added to it. diff --git a/OpenSim/Framework/Serialization/External/LandDataSerializer.cs b/OpenSim/Framework/Serialization/External/LandDataSerializer.cs index bf6c4e5..a12877a 100644 --- a/OpenSim/Framework/Serialization/External/LandDataSerializer.cs +++ b/OpenSim/Framework/Serialization/External/LandDataSerializer.cs @@ -46,13 +46,11 @@ namespace OpenSim.Framework.Serialization.External protected static UTF8Encoding m_utf8Encoding = new UTF8Encoding(); - private delegate void LandDataProcessor(LandData landData, XmlTextReader reader); - private static Dictionary m_ldProcessors - = new Dictionary(); + private static Dictionary> m_ldProcessors + = new Dictionary>(); - private delegate void LandAccessEntryProcessor(LandAccessEntry lae, XmlTextReader reader); - private static Dictionary m_laeProcessors - = new Dictionary(); + private static Dictionary> m_laeProcessors + = new Dictionary>(); static LandDataSerializer() { @@ -146,38 +144,7 @@ namespace OpenSim.Framework.Serialization.External xtr.ReadStartElement("ParcelAccessEntry"); - string nodeName = string.Empty; - while (xtr.NodeType != XmlNodeType.EndElement) - { - nodeName = xtr.Name; - -// m_log.DebugFormat("[LandDataSerializer]: Processing: {0} in ParcelAccessEntry", nodeName); - - LandAccessEntryProcessor p = null; - if (m_laeProcessors.TryGetValue(xtr.Name, out p)) - { -// m_log.DebugFormat("[LandDataSerializer]: Found {0} processor in ParcelAccessEntry", nodeName); - - try - { - p(lae, xtr); - } - catch (Exception e) - { - m_log.ErrorFormat( - "[LandDataSerializer]: Exception while parsing element {0} in ParcelAccessEntry, continuing. Exception {1}{2}", - nodeName, e.Message, e.StackTrace); - - if (xtr.NodeType == XmlNodeType.EndElement) - xtr.Read(); - } - } - else - { - // m_log.DebugFormat("[LandDataSerializer]: caught unknown element {0}", nodeName); - xtr.ReadOuterXml(); // ignore - } - } + ExternalRepresentationUtils.ExecuteReadProcessors(lae, m_laeProcessors, xtr); xtr.ReadEndElement(); @@ -213,36 +180,7 @@ namespace OpenSim.Framework.Serialization.External { reader.ReadStartElement("LandData"); - string nodeName = string.Empty; - while (reader.NodeType != XmlNodeType.EndElement) - { - nodeName = reader.Name; - -// m_log.DebugFormat("[LandDataSerializer]: Processing: {0}", nodeName); - - LandDataProcessor p = null; - if (m_ldProcessors.TryGetValue(reader.Name, out p)) - { - try - { - p(landData, reader); - } - catch (Exception e) - { - m_log.ErrorFormat( - "[LandDataSerializer]: exception while parsing element {0}, continuing. Exception {1}{2}", - nodeName, e.Message, e.StackTrace); - - if (reader.NodeType == XmlNodeType.EndElement) - reader.Read(); - } - } - else - { - // m_log.DebugFormat("[LandDataSerializer]: caught unknown element {0}", nodeName); - reader.ReadOuterXml(); // ignore - } - } + ExternalRepresentationUtils.ExecuteReadProcessors(landData, m_ldProcessors, reader); reader.ReadEndElement(); } diff --git a/OpenSim/Framework/Serialization/External/UserInventoryItemSerializer.cs b/OpenSim/Framework/Serialization/External/UserInventoryItemSerializer.cs index 5d986d5..f6fdc4b 100644 --- a/OpenSim/Framework/Serialization/External/UserInventoryItemSerializer.cs +++ b/OpenSim/Framework/Serialization/External/UserInventoryItemSerializer.cs @@ -46,8 +46,8 @@ namespace OpenSim.Framework.Serialization.External { 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(); + private static Dictionary> m_InventoryItemXmlProcessors + = new Dictionary>(); #region InventoryItemBase Processor initialization static UserInventoryItemSerializer() @@ -204,39 +204,14 @@ namespace OpenSim.Framework.Serialization.External { 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 - } - - } + ExternalRepresentationUtils.ExecuteReadProcessors( + item, m_InventoryItemXmlProcessors, reader); reader.ReadEndElement(); // InventoryItem } //m_log.DebugFormat("[XXX]: parsed InventoryItemBase {0} - {1}", obj.Name, obj.UUID); return item; - } public static string Serialize(InventoryItemBase inventoryItem, Dictionary options, IUserAccountService userAccountService) -- cgit v1.1