From 323300f710401462bd7ac05587d4a064ed874a36 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 21 Jul 2017 11:01:32 -0400 Subject: [PATCH] Rethrowing XmlExceptions as IcdXmlExceptions --- ICD.Common.Utils/Xml/IcdXmlDocument.cs | 2 +- ICD.Common.Utils/Xml/IcdXmlException.cs | 14 ++++ ICD.Common.Utils/Xml/IcdXmlReader.cs | 105 ++++++++++++++++++++---- ICD.Common.Utils/Xml/XmlUtils.cs | 9 ++ 4 files changed, 115 insertions(+), 15 deletions(-) diff --git a/ICD.Common.Utils/Xml/IcdXmlDocument.cs b/ICD.Common.Utils/Xml/IcdXmlDocument.cs index cd2973b..09a50f1 100644 --- a/ICD.Common.Utils/Xml/IcdXmlDocument.cs +++ b/ICD.Common.Utils/Xml/IcdXmlDocument.cs @@ -28,7 +28,7 @@ namespace ICD.Common.Utils.Xml } catch (XmlException e) { - throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition); + throw new IcdXmlException(e); } } diff --git a/ICD.Common.Utils/Xml/IcdXmlException.cs b/ICD.Common.Utils/Xml/IcdXmlException.cs index 708228f..984fbb4 100644 --- a/ICD.Common.Utils/Xml/IcdXmlException.cs +++ b/ICD.Common.Utils/Xml/IcdXmlException.cs @@ -1,4 +1,9 @@ using System; +#if SIMPLSHARP +using Crestron.SimplSharp; +#else +using System.Xml; +#endif namespace ICD.Common.Utils.Xml { @@ -20,5 +25,14 @@ namespace ICD.Common.Utils.Xml m_LineNumber = lineNumber; m_LinePosition = linePosition; } + + /// + /// Constructor. + /// + /// + public IcdXmlException(XmlException inner) + : this(inner.Message, inner, inner.LineNumber, inner.LinePosition) + { + } } } diff --git a/ICD.Common.Utils/Xml/IcdXmlReader.cs b/ICD.Common.Utils/Xml/IcdXmlReader.cs index 4103949..109c9db 100644 --- a/ICD.Common.Utils/Xml/IcdXmlReader.cs +++ b/ICD.Common.Utils/Xml/IcdXmlReader.cs @@ -50,26 +50,54 @@ namespace ICD.Common.Utils.Xml public bool MoveToNextAttribute() { - return m_Reader.MoveToNextAttribute(); + try + { + return m_Reader.MoveToNextAttribute(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public void MoveToElement() { - m_Reader.MoveToElement(); + try + { + m_Reader.MoveToElement(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string GetAttribute(string name) { - return m_Reader.GetAttribute(name); + try + { + return m_Reader.GetAttribute(name); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string ReadString() { + try + { #if SIMPLSHARP - return m_Reader.ReadString(); + return m_Reader.ReadString(); #else - return m_Reader.ReadElementContentAsString(); + return m_Reader.ReadElementContentAsString(); #endif + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public bool Read() @@ -80,47 +108,96 @@ namespace ICD.Common.Utils.Xml } catch (XmlException e) { - throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition); + throw new IcdXmlException(e); } } public void Dispose() { + try + { #if SIMPLSHARP - m_Reader.Dispose(true); + m_Reader.Dispose(true); #else - m_Reader.Dispose(); + m_Reader.Dispose(); #endif + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public void Skip() { - m_Reader.Skip(); + try + { + m_Reader.Skip(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string ReadElementContentAsString() { - return m_Reader.ReadElementContentAsString(); + try + { + return m_Reader.ReadElementContentAsString(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string ReadOuterXml() { - return m_Reader.ReadOuterXml(); + try + { + return m_Reader.ReadOuterXml(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string ReadInnerXml() { - return m_Reader.ReadInnerXml(); + try + { + return m_Reader.ReadInnerXml(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public long ReadElementContentAsLong() { - return m_Reader.ReadElementContentAsLong(); + try + { + return m_Reader.ReadElementContentAsLong(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public float ReadElementContentAsFloat() { - return m_Reader.ReadElementContentAsFloat(); + try + { + return m_Reader.ReadElementContentAsFloat(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } #endregion diff --git a/ICD.Common.Utils/Xml/XmlUtils.cs b/ICD.Common.Utils/Xml/XmlUtils.cs index 4a224dd..ee07697 100644 --- a/ICD.Common.Utils/Xml/XmlUtils.cs +++ b/ICD.Common.Utils/Xml/XmlUtils.cs @@ -135,6 +135,9 @@ namespace ICD.Common.Utils.Xml [PublicAPI] public static void Recurse(string xml, Action callback) { + if (callback == null) + throw new ArgumentNullException("callback"); + Recurse(xml, new Stack(), callback); } @@ -146,6 +149,12 @@ namespace ICD.Common.Utils.Xml /// private static void Recurse(string xml, Stack path, Action callback) { + if (path == null) + throw new ArgumentNullException("path"); + + if (callback == null) + throw new ArgumentNullException("callback"); + IcdXmlReader childReader; try