From 212af3a7ddfc770647f4531fd6ed3c68cbbfa6c3 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 3 Nov 2022 12:31:21 -0700 Subject: [PATCH] Add throwOnError flag for CMP and XML (unused) --- SabreTools.IO/Writers/ClrMameProWriter.cs | 44 +++++++++++++++++++++-- SabreTools.IO/XmlTextWriterExtensions.cs | 18 +++++++--- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/SabreTools.IO/Writers/ClrMameProWriter.cs b/SabreTools.IO/Writers/ClrMameProWriter.cs index 4ecdf872..f07567fc 100644 --- a/SabreTools.IO/Writers/ClrMameProWriter.cs +++ b/SabreTools.IO/Writers/ClrMameProWriter.cs @@ -184,14 +184,23 @@ namespace SabreTools.IO.Writers /// /// Ensure writing writing null values as empty strings /// - public void WriteRequiredElementString(string name, string value) + /// Name of the element + /// Value to write in the element + /// Indicates if an error should be thrown on a missing required value + public void WriteRequiredElementString(string name, string value, bool throwOnError = false) { + // Throw an exception if we are configured to + if (value == null && throwOnError) + throw new ArgumentNullException(nameof(value)); + WriteElementString(name, value ?? string.Empty); } /// /// Write an element, if the value is not null or empty /// + /// Name of the element + /// Value to write in the element public void WriteOptionalElementString(string name, string value) { if (!string.IsNullOrEmpty(value)) @@ -201,6 +210,7 @@ namespace SabreTools.IO.Writers /// /// Write the start of an attribute node /// + /// Name of the attribute public void WriteStartAttribute(string name, bool? quoteOverride = null) { try @@ -241,6 +251,9 @@ namespace SabreTools.IO.Writers /// /// Write a complete attribute with content /// + /// Name of the attribute + /// Value to write in the attribute + /// Non-null to overwrite the writer setting, null otherwise public void WriteAttributeString(string name, string value, bool? quoteOverride = null) { WriteStartAttribute(name, quoteOverride); @@ -251,14 +264,25 @@ namespace SabreTools.IO.Writers /// /// Ensure writing writing null values as empty strings /// - public void WriteRequiredAttributeString(string name, string value, bool? quoteOverride = null) + /// Name of the attribute + /// Value to write in the attribute + /// Non-null to overwrite the writer setting, null otherwise + /// Indicates if an error should be thrown on a missing required value + public void WriteRequiredAttributeString(string name, string value, bool? quoteOverride = null, bool throwOnError = false) { + // Throw an exception if we are configured to + if (value == null && throwOnError) + throw new ArgumentNullException(nameof(value)); + WriteAttributeString(name, value ?? string.Empty, quoteOverride); } /// /// Write an attribute, if the value is not null or empty /// + /// Name of the attribute + /// Value to write in the attribute + /// Non-null to overwrite the writer setting, null otherwise public void WriteOptionalAttributeString(string name, string value, bool? quoteOverride = null) { if (!string.IsNullOrEmpty(value)) @@ -268,6 +292,9 @@ namespace SabreTools.IO.Writers /// /// Write a standalone attribute /// + /// Name of the attribute + /// Value to write in the attribute + /// Non-null to overwrite the writer setting, null otherwise public void WriteStandalone(string name, string value, bool? quoteOverride = null) { try @@ -308,14 +335,25 @@ namespace SabreTools.IO.Writers /// /// Ensure writing writing null values as empty strings /// - public void WriteRequiredStandalone(string name, string value, bool? quoteOverride = null) + /// Name of the attribute + /// Value to write in the attribute + /// Non-null to overwrite the writer setting, null otherwise + /// Indicates if an error should be thrown on a missing required value + public void WriteRequiredStandalone(string name, string value, bool? quoteOverride = null, bool throwOnError = false) { + // Throw an exception if we are configured to + if (value == null && throwOnError) + throw new ArgumentNullException(nameof(value)); + WriteStandalone(name, value ?? string.Empty, quoteOverride); } /// /// Write an standalone, if the value is not null or empty /// + /// Name of the attribute + /// Value to write in the attribute + /// Non-null to overwrite the writer setting, null otherwise public void WriteOptionalStandalone(string name, string value, bool? quoteOverride = null) { if (!string.IsNullOrEmpty(value)) diff --git a/SabreTools.IO/XmlTextWriterExtensions.cs b/SabreTools.IO/XmlTextWriterExtensions.cs index 857c8832..7a5f5d89 100644 --- a/SabreTools.IO/XmlTextWriterExtensions.cs +++ b/SabreTools.IO/XmlTextWriterExtensions.cs @@ -1,6 +1,6 @@ -using System.Xml; +using System; +using System.Xml; -// TODO: Introduce a "strict" flag or a "lenient" flag to throw an exception on WriteRequired* namespace SabreTools.IO { /// @@ -14,8 +14,13 @@ namespace SabreTools.IO /// XmlTextWriter to write out with /// Name of the element /// Value to write in the element - public static void WriteRequiredAttributeString(this XmlTextWriter writer, string localName, string value) + /// Indicates if an error should be thrown on a missing required value + public static void WriteRequiredAttributeString(this XmlTextWriter writer, string localName, string value, bool throwOnError = false) { + // Throw an exception if we are configured to + if (value == null && throwOnError) + throw new ArgumentNullException(nameof(value)); + writer.WriteAttributeString(localName, value ?? string.Empty); } @@ -25,8 +30,13 @@ namespace SabreTools.IO /// XmlTextWriter to write out with /// Name of the element /// Value to write in the element - public static void WriteRequiredElementString(this XmlTextWriter writer, string localName, string value) + /// Indicates if an error should be thrown on a missing required value + public static void WriteRequiredElementString(this XmlTextWriter writer, string localName, string value, bool throwOnError = false) { + // Throw an exception if we are configured to + if (value == null && throwOnError) + throw new ArgumentNullException(nameof(value)); + writer.WriteStartElement(localName); if (value == null) writer.WriteRaw(string.Empty);