Refactor common deserialization processor code to generic method ExternalRepresentationUtils.ExecuteReadProcessors()

iar_mods
Justin Clark-Casey (justincc) 2012-02-03 22:45:50 +00:00
parent f17066b7bf
commit 6234264211
4 changed files with 63 additions and 98 deletions

View File

@ -24,11 +24,13 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Reflection;
using System.Xml; using System.Xml;
using log4net;
using OpenMetaverse; using OpenMetaverse;
using OpenSim.Services.Interfaces; using OpenSim.Services.Interfaces;
@ -39,6 +41,55 @@ namespace OpenSim.Framework.Serialization.External
/// </summary> /// </summary>
public class ExternalRepresentationUtils public class ExternalRepresentationUtils
{ {
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Populate a node with data read from xml using a dictinoary of processors
/// </summary>
/// <param name="nodeToFill"></param>
/// <param name="processors">
/// A <see cref="Dictionary<System.String, Action<NodeType, XmlTextReader>>"/>
/// </param>
/// <param name="xtr">
/// A <see cref="XmlTextReader"/>
/// </param>
public static void ExecuteReadProcessors<NodeType>(
NodeType nodeToFill, Dictionary<string, Action<NodeType, XmlTextReader>> processors, XmlTextReader xtr)
{
string nodeName = string.Empty;
while (xtr.NodeType != XmlNodeType.EndElement)
{
nodeName = xtr.Name;
// m_log.DebugFormat("[ExternalRepresentationUtils]: Processing: {0}", nodeName);
Action<NodeType, XmlTextReader> 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
}
}
}
/// <summary> /// <summary>
/// Takes a XML representation of a SceneObjectPart and returns another XML representation /// Takes a XML representation of a SceneObjectPart and returns another XML representation
/// with creator data added to it. /// with creator data added to it.

View File

@ -46,13 +46,11 @@ namespace OpenSim.Framework.Serialization.External
protected static UTF8Encoding m_utf8Encoding = new UTF8Encoding(); protected static UTF8Encoding m_utf8Encoding = new UTF8Encoding();
private delegate void LandDataProcessor(LandData landData, XmlTextReader reader); private static Dictionary<string, Action<LandData, XmlTextReader>> m_ldProcessors
private static Dictionary<string, LandDataProcessor> m_ldProcessors = new Dictionary<string, Action<LandData, XmlTextReader>>();
= new Dictionary<string, LandDataProcessor>();
private delegate void LandAccessEntryProcessor(LandAccessEntry lae, XmlTextReader reader); private static Dictionary<string, Action<LandAccessEntry, XmlTextReader>> m_laeProcessors
private static Dictionary<string, LandAccessEntryProcessor> m_laeProcessors = new Dictionary<string, Action<LandAccessEntry, XmlTextReader>>();
= new Dictionary<string, LandAccessEntryProcessor>();
static LandDataSerializer() static LandDataSerializer()
{ {
@ -146,38 +144,7 @@ namespace OpenSim.Framework.Serialization.External
xtr.ReadStartElement("ParcelAccessEntry"); xtr.ReadStartElement("ParcelAccessEntry");
string nodeName = string.Empty; ExternalRepresentationUtils.ExecuteReadProcessors<LandAccessEntry>(lae, m_laeProcessors, xtr);
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
}
}
xtr.ReadEndElement(); xtr.ReadEndElement();
@ -213,36 +180,7 @@ namespace OpenSim.Framework.Serialization.External
{ {
reader.ReadStartElement("LandData"); reader.ReadStartElement("LandData");
string nodeName = string.Empty; ExternalRepresentationUtils.ExecuteReadProcessors<LandData>(landData, m_ldProcessors, reader);
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
}
}
reader.ReadEndElement(); reader.ReadEndElement();
} }

View File

@ -46,8 +46,8 @@ namespace OpenSim.Framework.Serialization.External
{ {
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private delegate void InventoryItemXmlProcessor(InventoryItemBase item, XmlTextReader reader); private static Dictionary<string, Action<InventoryItemBase, XmlTextReader>> m_InventoryItemXmlProcessors
private static Dictionary<string, InventoryItemXmlProcessor> m_InventoryItemXmlProcessors = new Dictionary<string, InventoryItemXmlProcessor>(); = new Dictionary<string, Action<InventoryItemBase, XmlTextReader>>();
#region InventoryItemBase Processor initialization #region InventoryItemBase Processor initialization
static UserInventoryItemSerializer() static UserInventoryItemSerializer()
@ -204,39 +204,14 @@ namespace OpenSim.Framework.Serialization.External
{ {
reader.ReadStartElement("InventoryItem"); reader.ReadStartElement("InventoryItem");
string nodeName = string.Empty; ExternalRepresentationUtils.ExecuteReadProcessors<InventoryItemBase>(
while (reader.NodeType != XmlNodeType.EndElement) item, m_InventoryItemXmlProcessors, reader);
{
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 reader.ReadEndElement(); // InventoryItem
} }
//m_log.DebugFormat("[XXX]: parsed InventoryItemBase {0} - {1}", obj.Name, obj.UUID); //m_log.DebugFormat("[XXX]: parsed InventoryItemBase {0} - {1}", obj.Name, obj.UUID);
return item; return item;
} }
public static string Serialize(InventoryItemBase inventoryItem, Dictionary<string, object> options, IUserAccountService userAccountService) public static string Serialize(InventoryItemBase inventoryItem, Dictionary<string, object> options, IUserAccountService userAccountService)

View File

@ -218,6 +218,7 @@
<ReferencePath>../../../bin/</ReferencePath> <ReferencePath>../../../bin/</ReferencePath>
<Reference name="System"/> <Reference name="System"/>
<Reference name="System.Core"/>
<Reference name="System.Xml"/> <Reference name="System.Xml"/>
<Reference name="log4net" path="../../../bin/"/> <Reference name="log4net" path="../../../bin/"/>
<Reference name="OpenMetaverse" path="../../../bin/"/> <Reference name="OpenMetaverse" path="../../../bin/"/>