XML utils untangling, potential optimizations

This commit is contained in:
Chris Cameron
2018-01-22 10:26:33 -05:00
parent e4be5e7e7c
commit 3c3d6fa52e
2 changed files with 192 additions and 116 deletions

View File

@@ -182,6 +182,27 @@ namespace ICD.Common.Utils.Xml
return false; return false;
} }
/// <summary>
/// Gets the child element with the given name under the current element.
/// </summary>
/// <param name="extends"></param>
/// <param name="element"></param>
/// <returns></returns>
public static IcdXmlReader GetChildElement(this IcdXmlReader extends, string element)
{
if (extends == null)
throw new ArgumentNullException("extends");
try
{
return extends.GetChildElements(element).First();
}
catch (InvalidOperationException e)
{
throw new FormatException("No child element with name " + element, e);
}
}
/// <summary> /// <summary>
/// Gets the child elements for the current element. /// Gets the child elements for the current element.
/// </summary> /// </summary>
@@ -199,6 +220,24 @@ namespace ICD.Common.Utils.Xml
} }
} }
/// <summary>
/// Gets the child elements for the current element.
/// </summary>
/// <param name="extends"></param>
/// <param name="element"></param>
/// <returns></returns>
public static IEnumerable<IcdXmlReader> GetChildElements(this IcdXmlReader extends, string element)
{
if (extends == null)
throw new ArgumentNullException("extends");
foreach (IcdXmlReader output in extends.GetChildElementsAsString(element).Select(child => new IcdXmlReader(child)))
{
output.SkipToNextElement();
yield return output;
}
}
/// <summary> /// <summary>
/// Gets the child elements for the current element. /// Gets the child elements for the current element.
/// </summary> /// </summary>
@@ -230,6 +269,57 @@ namespace ICD.Common.Utils.Xml
} }
} }
/// <summary>
/// Gets the child elements for the current element.
/// </summary>
/// <param name="extends"></param>
/// <param name="element"></param>
/// <returns></returns>
[PublicAPI]
public static IEnumerable<string> GetChildElementsAsString(this IcdXmlReader extends, string element)
{
if (extends == null)
throw new ArgumentNullException("extends");
// Step into the first child.
extends.SkipToNextElement();
while (extends.NodeType == XmlNodeType.Element || extends.NodeType == XmlNodeType.Comment)
{
switch (extends.NodeType)
{
case XmlNodeType.Comment:
extends.Skip();
break;
case XmlNodeType.Element:
string name = extends.Name;
string output = extends.ReadOuterXml();
if (name == element)
yield return output;
break;
}
extends.SkipInsignificantWhitespace();
}
}
public static bool TryGetChildElementAsString(this IcdXmlReader extends, string element, out string output)
{
output = null;
try
{
output = extends.GetChildElementsAsString(element).First();
return true;
}
catch (InvalidOperationException)
{
return false;
}
}
#endregion #endregion
#region Read Element Content #region Read Element Content

View File

@@ -24,7 +24,9 @@ namespace ICD.Common.Utils.Xml
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.SkipToNextElement(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
return reader.ReadInnerXml(); return reader.ReadInnerXml();
} }
} }
@@ -158,20 +160,13 @@ namespace ICD.Common.Utils.Xml
if (callback == null) if (callback == null)
throw new ArgumentNullException("callback"); throw new ArgumentNullException("callback");
IcdXmlReader childReader; using (IcdXmlReader childReader = new IcdXmlReader(xml))
try
{
childReader = new IcdXmlReader(xml);
childReader.SkipToNextElement();
}
catch (IcdXmlException)
{ {
if (!childReader.SkipToNextElement())
return; return;
}
path.Push(childReader.Name); path.Push(childReader.Name);
string[] pathOutput = path.Reverse().ToArray(); string[] pathOutput = path.Reverse().ToArray(path.Count);
callback(new XmlRecursionEventArgs(xml, pathOutput)); callback(new XmlRecursionEventArgs(xml, pathOutput));
@@ -179,7 +174,7 @@ namespace ICD.Common.Utils.Xml
Recurse(child, path, callback); Recurse(child, path, callback);
path.Pop(); path.Pop();
childReader.Dispose(); }
} }
#endregion #endregion
@@ -195,11 +190,31 @@ namespace ICD.Common.Utils.Xml
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.SkipToNextElement(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
return reader.HasChildElements(); return reader.HasChildElements();
} }
} }
/// <summary>
/// Gets the child elements from the topmost element in the xml.
/// </summary>
/// <param name="xml"></param>
/// <param name="element"></param>
/// <returns></returns>
[PublicAPI]
public static IcdXmlReader GetChildElement(string xml, string element)
{
using (IcdXmlReader reader = new IcdXmlReader(xml))
{
if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
return reader.GetChildElement(element);
}
}
/// <summary> /// <summary>
/// Gets the child elements from the topmost element in the xml. /// Gets the child elements from the topmost element in the xml.
/// </summary> /// </summary>
@@ -208,13 +223,14 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static IEnumerable<IcdXmlReader> GetChildElements(string xml) public static IEnumerable<IcdXmlReader> GetChildElements(string xml)
{ {
IcdXmlReader reader = new IcdXmlReader(xml); using (IcdXmlReader reader = new IcdXmlReader(xml))
reader.SkipToNextElement(); {
if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
foreach (IcdXmlReader child in reader.GetChildElements()) foreach (IcdXmlReader child in reader.GetChildElements())
yield return child; yield return child;
}
reader.Dispose();
} }
/// <summary> /// <summary>
@@ -227,19 +243,11 @@ namespace ICD.Common.Utils.Xml
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.SkipToNextElement(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
foreach (IcdXmlReader child in reader.GetChildElements()) foreach (string item in reader.GetChildElementsAsString(element))
{ yield return item;
string output = null;
if (child.Name == element)
output = child.ReadOuterXml();
child.Dispose();
if (output != null)
yield return output;
}
} }
} }
@@ -253,7 +261,9 @@ namespace ICD.Common.Utils.Xml
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.SkipToNextElement(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
foreach (string item in reader.GetChildElementsAsString()) foreach (string item in reader.GetChildElementsAsString())
yield return item; yield return item;
} }
@@ -287,23 +297,12 @@ namespace ICD.Common.Utils.Xml
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.SkipToNextElement(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
foreach (IcdXmlReader child in reader.GetChildElements()) return reader.TryGetChildElementAsString(element, out output);
{
output = null;
if (child.Name == element)
output = child.ReadOuterXml();
child.Dispose();
if (output != null)
return true;
} }
} }
output = null;
return false;
}
#endregion #endregion
@@ -318,13 +317,9 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static string ReadChildElementContentAsString(string xml, string childElement) public static string ReadChildElementContentAsString(string xml, string childElement)
{ {
string child = GetChildElementAsString(xml, childElement); using (IcdXmlReader reader = GetChildElement(xml, childElement))
using (IcdXmlReader reader = new IcdXmlReader(child))
{
reader.SkipToNextElement();
return reader.ReadElementContentAsString(); return reader.ReadElementContentAsString();
} }
}
/// <summary> /// <summary>
/// Gets the content of an immediate child. /// Gets the content of an immediate child.
@@ -335,13 +330,9 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static int ReadChildElementContentAsInt(string xml, string childElement) public static int ReadChildElementContentAsInt(string xml, string childElement)
{ {
string child = GetChildElementAsString(xml, childElement); using (IcdXmlReader reader = GetChildElement(xml, childElement))
using (IcdXmlReader reader = new IcdXmlReader(child))
{
reader.SkipToNextElement();
return reader.ReadElementContentAsInt(); return reader.ReadElementContentAsInt();
} }
}
/// <summary> /// <summary>
/// Gets the content of an immediate child. /// Gets the content of an immediate child.
@@ -352,13 +343,9 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static uint ReadChildElementContentAsUint(string xml, string childElement) public static uint ReadChildElementContentAsUint(string xml, string childElement)
{ {
string child = GetChildElementAsString(xml, childElement); using (IcdXmlReader reader = GetChildElement(xml, childElement))
using (IcdXmlReader reader = new IcdXmlReader(child))
{
reader.SkipToNextElement();
return reader.ReadElementContentAsUint(); return reader.ReadElementContentAsUint();
} }
}
/// <summary> /// <summary>
/// Gets the content of an immediate child. /// Gets the content of an immediate child.
@@ -369,13 +356,9 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static long ReadChildElementContentAsLong(string xml, string childElement) public static long ReadChildElementContentAsLong(string xml, string childElement)
{ {
string child = GetChildElementAsString(xml, childElement); using (IcdXmlReader reader = GetChildElement(xml, childElement))
using (IcdXmlReader reader = new IcdXmlReader(child))
{
reader.SkipToNextElement();
return reader.ReadElementContentAsLong(); return reader.ReadElementContentAsLong();
} }
}
/// <summary> /// <summary>
/// Gets the content of an immediate child. /// Gets the content of an immediate child.
@@ -386,13 +369,9 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static ushort ReadChildElementContentAsUShort(string xml, string childElement) public static ushort ReadChildElementContentAsUShort(string xml, string childElement)
{ {
string child = GetChildElementAsString(xml, childElement); using (IcdXmlReader reader = GetChildElement(xml, childElement))
using (IcdXmlReader reader = new IcdXmlReader(child))
{
reader.SkipToNextElement();
return reader.ReadElementContentAsUShort(); return reader.ReadElementContentAsUShort();
} }
}
/// <summary> /// <summary>
/// Gets the content of an immediate child. /// Gets the content of an immediate child.
@@ -403,13 +382,9 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static float ReadChildElementContentAsFloat(string xml, string childElement) public static float ReadChildElementContentAsFloat(string xml, string childElement)
{ {
string child = GetChildElementAsString(xml, childElement); using (IcdXmlReader reader = GetChildElement(xml, childElement))
using (IcdXmlReader reader = new IcdXmlReader(child))
{
reader.SkipToNextElement();
return reader.ReadElementContentAsFloat(); return reader.ReadElementContentAsFloat();
} }
}
/// <summary> /// <summary>
/// Gets the content of an immediate child. /// Gets the content of an immediate child.
@@ -434,13 +409,9 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static byte ReadChildElementContentAsByte(string xml, string childElement) public static byte ReadChildElementContentAsByte(string xml, string childElement)
{ {
string child = GetChildElementAsString(xml, childElement); using (IcdXmlReader reader = GetChildElement(xml, childElement))
using (IcdXmlReader reader = new IcdXmlReader(child))
{
reader.SkipToNextElement();
return reader.ReadElementContentAsByte(); return reader.ReadElementContentAsByte();
} }
}
/// <summary> /// <summary>
/// Gets the content of an immediate child. /// Gets the content of an immediate child.
@@ -455,13 +426,9 @@ namespace ICD.Common.Utils.Xml
if (!EnumUtils.IsEnumType<T>()) if (!EnumUtils.IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
string child = GetChildElementAsString(xml, childElement); using (IcdXmlReader reader = GetChildElement(xml, childElement))
using (IcdXmlReader reader = new IcdXmlReader(child))
{
reader.SkipToNextElement();
return reader.ReadElementContentAsEnum<T>(ignoreCase); return reader.ReadElementContentAsEnum<T>(ignoreCase);
} }
}
#endregion #endregion
@@ -678,7 +645,9 @@ namespace ICD.Common.Utils.Xml
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.SkipToNextElement(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
return reader.ReadElementContentAsString(); return reader.ReadElementContentAsString();
} }
} }
@@ -693,7 +662,9 @@ namespace ICD.Common.Utils.Xml
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.Read(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
return reader.ReadElementContentAsUint(); return reader.ReadElementContentAsUint();
} }
} }
@@ -708,7 +679,9 @@ namespace ICD.Common.Utils.Xml
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.Read(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
return reader.ReadElementContentAsInt(); return reader.ReadElementContentAsInt();
} }
} }
@@ -723,7 +696,9 @@ namespace ICD.Common.Utils.Xml
{ {
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.Read(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
return reader.ReadElementContentAsUShort(); return reader.ReadElementContentAsUShort();
} }
} }
@@ -743,7 +718,9 @@ namespace ICD.Common.Utils.Xml
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))
{ {
reader.Read(); if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
return reader.ReadElementContentAsEnum<T>(ignoreCase); return reader.ReadElementContentAsEnum<T>(ignoreCase);
} }
} }
@@ -760,8 +737,11 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static uint? TryReadElementContentAsUint(string xml) public static uint? TryReadElementContentAsUint(string xml)
{ {
IcdXmlReader reader = new IcdXmlReader(xml); using (IcdXmlReader reader = new IcdXmlReader(xml))
reader.Read(); {
if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
try try
{ {
return reader.ReadElementContentAsUint(); return reader.ReadElementContentAsUint();
@@ -771,6 +751,7 @@ namespace ICD.Common.Utils.Xml
return null; return null;
} }
} }
}
/// <summary> /// <summary>
/// Parses the element content as a uint. /// Parses the element content as a uint.
@@ -780,8 +761,11 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static int? TryReadElementContentAsInt(string xml) public static int? TryReadElementContentAsInt(string xml)
{ {
IcdXmlReader reader = new IcdXmlReader(xml); using (IcdXmlReader reader = new IcdXmlReader(xml))
reader.Read(); {
if (!reader.SkipToNextElement())
throw new FormatException("Unable to find element in given xml");
try try
{ {
return reader.ReadElementContentAsInt(); return reader.ReadElementContentAsInt();
@@ -791,6 +775,7 @@ namespace ICD.Common.Utils.Xml
return null; return null;
} }
} }
}
#endregion #endregion
@@ -807,6 +792,7 @@ namespace ICD.Common.Utils.Xml
{ {
if (!reader.SkipToNextElement()) if (!reader.SkipToNextElement())
throw new FormatException("Unable to read element name, no element in given xml"); throw new FormatException("Unable to read element name, no element in given xml");
return reader.Name; return reader.Name;
} }
} }