/* * 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 { /// /// A tool for decoding XmlSerializationTag /// /// /// /// The type of the property container, class-constrained. /// /// public class PropertyAttributeDecoder : Exceptional where T : class { /// /// Initialize an instance of the Xml.PropertyAttributeDecoder class. /// /// /// /// The containing the properties to be decoded. /// /// public PropertyAttributeDecoder(T propertyContainer) { try { PropertyContainer = propertyContainer; } catch (System.Exception ex) { throw new Exception("encountered problem constructing " + GetType().FullName, ex); } } /// /// Get the object containing the properties to be decoded by this class instance. /// public T PropertyContainer { get => _PropertyContainer.Value; private set => _PropertyContainer.Value = value; } private Property _PropertyContainer = new Property( typeof(PropertyAttributeDecoder).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(); 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 ?? "<>") + " property", ex); } } /// /// Extract the specified boolean attribute property value from the decoder's /// bound object. /// /// /// /// The name of the property to be extracted. /// /// /// /// The the property value is to be decoded from. /// /// /// /// The specified value. /// /// 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(); 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 ?? "<>") + " property", ex); } } /// /// Extract the specified int attribute property value from the decoder's /// bound object. /// /// /// /// The name of the property to be extracted. /// /// /// /// The the property value is to be decoded from. /// /// /// /// The specified value. /// 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(); 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 ?? "<>") + " property", ex); } } /// /// Extract the specified double attribute property value from the decoder's /// bound object. /// /// /// /// The name of the property to be extracted. /// /// /// /// The the property value is to be decoded from. /// /// /// /// The specified value. /// /// 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(); 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 ?? "<>") + " property", ex); } } /// /// Extract the specified double attribute property value from the decoder's /// bound object. /// /// /// /// The name of the property to be extracted. /// /// /// /// The the property value is to be decoded from. /// /// /// /// /// The specified value. /// /// 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(); 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 ?? "<>") + " property", ex); } } /// /// Extract the specified string attribute property value from the decoder's /// bound object. /// /// /// /// The name of the property to be extracted. /// /// /// /// The the property value is to be decoded from. /// /// /// /// The specified value. /// /// 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(); 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 ?? "<>") + " property", ex); } } /// /// Extract the specified enum attribute property value from the decoder's /// bound object. /// /// /// /// The name of the property to be extracted. /// /// /// /// The the property value is to be decoded from. /// /// /// the Enumeration that the enum value belongs to /// /// The specified enum value. /// 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(); return Enum.Parse( enumType, reader.GetAttribute(attributeExtractor.ExtractAttachedAttributeFromProperty(PropertyContainer, propertyName).Value)); } } catch (System.Exception ex) { throw new Exception("encountered problem parsing " + (propertyName ?? "<>") + " property", ex); } } } }