Files
DP44/Common/DTS.Common.Utilities/Xml.PropertyAttributeDecoder.cs
2026-04-17 14:55:32 -04:00

419 lines
18 KiB
C#

/*
* Xml.PropertyAttributeDecoder.cs
*
* Copyright © 2009
* Diversified Technical Systems, Inc.
* All Rights Reserved
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DTS.Common.Utilities.DotNetProgrammingConstructs;
namespace DTS.Common.Utilities.Xml
{
/// <summary>
/// A tool for decoding XmlSerializationTag
/// </summary>
///
/// <typeparam name="T">
/// The type of the property container, class-constrained.
/// </typeparam>
///
public class PropertyAttributeDecoder<T>
: Exceptional where T : class
{
/// <summary>
/// Initialize an instance of the Xml.PropertyAttributeDecoder class.
/// </summary>
///
/// <param name="propertyContainer">
/// The <see cref="Object"/> containing the properties to be decoded.
/// </param>
///
public PropertyAttributeDecoder(T propertyContainer)
{
try
{
PropertyContainer = propertyContainer;
}
catch (System.Exception ex)
{
throw new Exception("encountered problem constructing " + GetType().FullName, ex);
}
}
/// <summary>
/// Get the object containing the properties to be decoded by this class instance.
/// </summary>
public T PropertyContainer
{
get => _PropertyContainer.Value;
private set => _PropertyContainer.Value = value;
}
private Property<T> _PropertyContainer
= new Property<T>(
typeof(PropertyAttributeDecoder<T>).Namespace + ".PropertyAttributeDecoder.PropertyContainer",
null,
false
);
public bool ExtractBoolProperty(string propertyName, XmlReader reader, bool defaultValue)
{
try
{ //
// Get the specified integer property.
//
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("cannot extract value from null/empty attribute name");
else if (null == reader)
throw new ArgumentNullException("cannot extract value from null/empty reader");
else
{
var attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
string attributeName;
try { attributeName = attributeExtractor.ExtractAttachedAttributeFromProperty(PropertyContainer, propertyName).Value; }
catch (System.Exception ex) { throw new Exception("encountered problem extracting attribute name for \"" + propertyName + "\"", ex); }
string attributeValueString;
try { attributeValueString = reader.GetAttribute(attributeName); }
catch (System.Exception ex) { throw new Exception("encountered problem extracting value string from XML attribute \"" + attributeName + "\"", ex); }
try
{
if (string.IsNullOrWhiteSpace(attributeValueString)) { return defaultValue; }
return bool.Parse(attributeValueString);
}
catch (System.Exception ex) { throw new Exception("encountered problem parsing integer value out of value text \"" + attributeValueString + "\"", ex); }
}
}
catch (System.Exception ex)
{
throw new Exception("encountered problem parsing " + (propertyName ?? "<<NULL>>") + " property", ex);
}
}
/// <summary>
/// Extract the specified boolean attribute property value from the decoder's
/// bound object.
/// </summary>
///
/// <param name="propertyName">
/// The <see cref="string"/> name of the property to be extracted.
/// </param>
///
/// <param name="reader">
/// The <see cref="System.Xml.XmlReader"/> the property value is to be decoded from.
/// </param>
///
/// <returns>
/// The specified <see cref="bool"/> value.
/// </returns>
///
public bool ExtractBoolProperty(string propertyName, XmlReader reader)
{
try
{ //
// Get the specified integer property.
//
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("cannot extract value from null/empty attribute name");
else if (null == reader)
throw new ArgumentNullException("cannot extract value from null/empty reader");
else
{
var attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
string attributeName;
try { attributeName = attributeExtractor.ExtractAttachedAttributeFromProperty(PropertyContainer, propertyName).Value; }
catch (System.Exception ex) { throw new Exception("encountered problem extracting attribute name for \"" + propertyName + "\"", ex); }
string attributeValueString;
try { attributeValueString = reader.GetAttribute(attributeName); }
catch (System.Exception ex) { throw new Exception("encountered problem extracting value string from XML attribute \"" + attributeName + "\"", ex); }
try
{
return bool.Parse(attributeValueString);
}
catch (System.Exception ex) { throw new Exception("encountered problem parsing integer value out of value text \"" + attributeValueString + "\"", ex); }
}
}
catch (System.Exception ex)
{
throw new Exception("encountered problem parsing " + (propertyName ?? "<<NULL>>") + " property", ex);
}
}
/// <summary>
/// Extract the specified int attribute property value from the decoder's
/// bound object.
/// </summary>
///
/// <param name="propertyName">
/// The <see cref="string"/> name of the property to be extracted.
/// </param>
///
/// <param name="reader">
/// The <see cref="System.Xml.XmlReader"/> the property value is to be decoded from.
/// </param>
///
/// <returns>
/// The specified <see cref="int"/> value.
/// </returns>
public int ExtractIntProperty(string propertyName, XmlReader reader)
{
try
{ //
// Get the specified integer property.
//
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("cannot extract value from null/empty attribute name");
else if (null == reader)
throw new ArgumentNullException("cannot extract value from null/empty reader");
else
{
var attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
string attributeName;
try { attributeName = attributeExtractor.ExtractAttachedAttributeFromProperty(PropertyContainer, propertyName).Value; }
catch (System.Exception ex) { throw new Exception("encountered problem extracting attribute name for \"" + propertyName + "\"", ex); }
string attributeValueString;
try { attributeValueString = reader.GetAttribute(attributeName); }
catch (System.Exception ex) { throw new Exception("encountered problem extracting value string from XML attribute \"" + attributeName + "\"", ex); }
try
{
var cult = new System.Globalization.CultureInfo("");
return int.Parse(attributeValueString, cult);
}
catch (System.Exception ex) { throw new Exception("encountered problem parsing integer value out of value text \"" + attributeValueString + "\"", ex); }
}
}
catch (System.Exception ex)
{
throw new Exception("encountered problem parsing " + (propertyName ?? "<<NULL>>") + " property", ex);
}
}
/// <summary>
/// Extract the specified double attribute property value from the decoder's
/// bound object.
/// </summary>
///
/// <param name="propertyName">
/// The <see cref="string"/> name of the property to be extracted.
/// </param>
///
/// <param name="reader">
/// The <see cref="System.Xml.XmlReader"/> the property value is to be decoded from.
/// </param>
///
/// <returns>
/// The specified <see cref="double"/> value.
/// </returns>
///
public double ExtractDoubleProperty(string propertyName, XmlReader reader)
{
try
{ //
// Get the specified double property.
//
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("cannot extract value from null/empty attribute name");
else if (null == reader)
throw new ArgumentNullException("cannot extract value from null/empty reader");
else
{
var attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
string attributeName;
try { attributeName = attributeExtractor.ExtractAttachedAttributeFromProperty(PropertyContainer, propertyName).Value; }
catch (System.Exception ex) { throw new Exception("encountered problem extracting attribute name for \"" + propertyName + "\"", ex); }
string attributeValueString;
try { attributeValueString = reader.GetAttribute(attributeName); }
catch (System.Exception ex) { throw new Exception("encountered problem extracting value string from XML attribute \"" + attributeName + "\"", ex); }
try
{
var cult = new System.Globalization.CultureInfo("");
return double.Parse(attributeValueString, cult);
}
catch (System.Exception ex) { throw new Exception("encountered problem parsing double value out of value text \"" + attributeValueString + "\"", ex); }
}
}
catch (System.Exception ex)
{
throw new Exception("encountered problem parsing " + (propertyName ?? "<<NULL>>") + " property", ex);
}
}
/// <summary>
/// Extract the specified double attribute property value from the decoder's
/// bound object.
/// </summary>
///
/// <param name="propertyName">
/// The <see cref="string"/> name of the property to be extracted.
/// </param>
///
/// <param name="reader">
/// The <see cref="System.Xml.XmlReader"/> the property value is to be decoded from.
/// </param>
///
/// <param name="dValue"></param>
/// <returns>
/// The specified <see cref="double"/> value.
/// </returns>
///
public bool ExtractDoubleProperty(string propertyName, XmlReader reader, out double dValue)
{
try
{
dValue = 0D;
//
// Get the specified double property.
//
if (string.IsNullOrEmpty(propertyName)) { return false; }
else if (null == reader) { return false; }
else
{
var attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
string attributeName;
try { attributeName = attributeExtractor.ExtractAttachedAttributeFromProperty(PropertyContainer, propertyName).Value; }
catch (System.Exception ex) { throw new Exception("encountered problem extracting attribute name for \"" + propertyName + "\"", ex); }
string attributeValueString;
try
{
attributeValueString = reader.GetAttribute(attributeName);
}
catch (System.Exception ex)
{
throw new Exception(
"encountered problem extracting value string from XML attribute \"" + attributeName + "\"",
ex);
}
try
{
if (string.IsNullOrWhiteSpace(attributeValueString))
{
return false;
}
if (double.TryParse(attributeValueString, System.Globalization.NumberStyles.Any,
new System.Globalization.CultureInfo(""), out dValue))
{
return true;
}
return false;
}
catch (System.Exception ex) { throw new Exception("encountered problem parsing double value out of value text \"" + attributeValueString + "\"", ex); }
}
}
catch (System.Exception ex)
{
throw new Exception("encountered problem parsing " + (propertyName ?? "<<NULL>>") + " property", ex);
}
}
/// <summary>
/// Extract the specified string attribute property value from the decoder's
/// bound object.
/// </summary>
///
/// <param name="propertyName">
/// The <see cref="string"/> name of the property to be extracted.
/// </param>
///
/// <param name="reader">
/// The <see cref="System.Xml.XmlReader"/> the property value is to be decoded from.
/// </param>
///
/// <returns>
/// The specified <see cref="string"/> value.
/// </returns>
///
public string ExtractStringProperty(string propertyName, XmlReader reader)
{
try
{ //
// Get the specified double property.
//
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("cannot extract value from null/empty attribute name");
else if (null == reader)
throw new ArgumentNullException("cannot extract value from null/empty reader");
else
{
var attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
string attributeName;
try { attributeName = attributeExtractor.ExtractAttachedAttributeFromProperty(PropertyContainer, propertyName).Value; }
catch (System.Exception ex) { throw new Exception("encountered problem extracting attribute name for \"" + propertyName + "\"", ex); }
try
{
var cult = new System.Globalization.CultureInfo("");
return reader.GetAttribute(attributeName);
}
catch (System.Exception ex) { throw new Exception("encountered problem extracting value string from XML attribute \"" + attributeName + "\"", ex); }
}
}
catch (System.Exception ex)
{
throw new Exception("encountered problem parsing " + (propertyName ?? "<<NULL>>") + " property", ex);
}
}
/// <summary>
/// Extract the specified enum attribute property value from the decoder's
/// bound object.
/// </summary>
///
/// <param name="propertyName">
/// The <see cref="string"/> name of the property to be extracted.
/// </param>
///
/// <param name="reader">
/// The <see cref="System.Xml.XmlReader"/> the property value is to be decoded from.
/// </param>
///
/// <param name="enumType">the Enumeration that the enum value belongs to</param>
/// <returns>
/// The specified enum value.
/// </returns>
public object ExtractEnumProperty(string propertyName, Type enumType, XmlReader reader)
{
try
{ //
// Get the specified enum property.
//
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("cannot extract value from null/empty attribute name");
else if (null == enumType)
throw new ArgumentNullException("cannot extract value with no specified enumeration type");
else if (null == reader)
throw new ArgumentNullException("cannot extract value from null/empty reader");
else
{
var cult = new System.Globalization.CultureInfo("");
var attributeExtractor = new AttributeExtractor<XmlSerializationTagAttribute>();
return Enum.Parse(
enumType,
reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(PropertyContainer, propertyName).Value));
}
}
catch (System.Exception ex)
{
throw new Exception("encountered problem parsing " + (propertyName ?? "<<NULL>>") + " property", ex);
}
}
}
}