| | 1 | | namespace Songhay.Xml; |
| | 2 | |
|
| | 3 | | public static partial class XObjectUtility |
| | 4 | | { |
| | 5 | | /// <summary> |
| | 6 | | /// Returns an object for parsing |
| | 7 | | /// and adding to a list of parameters for data access. |
| | 8 | | /// </summary> |
| | 9 | | /// <param name="node">The <see cref="XNode"/>.</param> |
| | 10 | | /// <param name="nodeQuery">The node query <see cref="string"/>.</param> |
| | 11 | | /// <param name="throwException">When <code>true</code>, throw an exception for null nodes.</param> |
| | 12 | | public static string? GetXAttributeValue(XNode? node, string? nodeQuery, bool throwException) => |
| 0 | 13 | | GetXAttributeValue(node, nodeQuery, throwException, null); |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Returns an object for parsing |
| | 17 | | /// and adding to a list of parameters for data access. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="node">The <see cref="XNode"/>.</param> |
| | 20 | | /// <param name="nodeQuery">The node query <see cref="string"/>.</param> |
| | 21 | | /// <param name="throwException">When <code>true</code>, throw an exception for null nodes.</param> |
| | 22 | | /// <param name="defaultValue">Return the specified default value for “zero-length” text nodes.</param> |
| | 23 | | public static string? |
| | 24 | | GetXAttributeValue(XNode? node, string? nodeQuery, bool throwException, string? defaultValue) => |
| 0 | 25 | | GetXAttributeValue(node, nodeQuery, throwException, defaultValue, null); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Returns an object for parsing |
| | 29 | | /// and adding to a list of parameters for data access. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="node">The <see cref="XNode"/>.</param> |
| | 32 | | /// <param name="nodeQuery">The node query <see cref="string"/>.</param> |
| | 33 | | /// <param name="throwException">When <code>true</code>, throw an exception for null nodes.</param> |
| | 34 | | /// <param name="defaultValue">Return the specified default value for “zero-length” text nodes.</param> |
| | 35 | | /// <param name="resolver">The <see cref="IXmlNamespaceResolver"/> to use to resolve prefixes.</param> |
| | 36 | | public static string? GetXAttributeValue(XNode? node, string? nodeQuery, bool throwException, string? defaultValue, |
| | 37 | | IXmlNamespaceResolver? resolver) |
| 0 | 38 | | { |
| 0 | 39 | | if (node == null) return defaultValue; |
| | 40 | |
|
| 0 | 41 | | nodeQuery.ThrowWhenNullOrWhiteSpace(); |
| | 42 | |
|
| 0 | 43 | | var a = resolver == null |
| 0 | 44 | | ? ((IEnumerable) node.XPathEvaluate(nodeQuery)).OfType<XAttribute>().FirstOrDefault() |
| 0 | 45 | | : ((IEnumerable) node.XPathEvaluate(nodeQuery, resolver)).OfType<XAttribute>().FirstOrDefault(); |
| | 46 | |
|
| 0 | 47 | | return a switch |
| 0 | 48 | | { |
| 0 | 49 | | null when throwException => throw new XmlException($"Element at “{nodeQuery}” was not found."), |
| 0 | 50 | | null => defaultValue, |
| 0 | 51 | | _ => a.Value |
| 0 | 52 | | }; |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Returns an object for parsing |
| | 57 | | /// and adding to a list of parameters for data access. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="node">The <see cref="XNode"/>.</param> |
| | 60 | | /// <param name="nodeQuery">The XPath <see cref="string"/>.</param> |
| | 61 | | /// <param name="throwException"> |
| | 62 | | /// When <code>true</code>, throw an exception for null nodes |
| | 63 | | /// and nodes that do not parse into the specified type. |
| | 64 | | /// </param> |
| | 65 | | /// <param name="defaultValue">Return a boxing <see cref="Object"/> for “zero-length” text nodes.</param> |
| | 66 | | /// <typeparam name="T">The type to parse from the node value.</typeparam> |
| | 67 | | public static object? |
| | 68 | | GetXAttributeValueAndParse<T>(XNode? node, string? nodeQuery, bool throwException, T defaultValue) => |
| 0 | 69 | | GetXAttributeValueAndParse(node, nodeQuery, throwException, defaultValue, null); |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Returns an object for parsing |
| | 73 | | /// and adding to a list of parameters for data access. |
| | 74 | | /// </summary> |
| | 75 | | /// <typeparam name="T">The type to parse from the node value.</typeparam> |
| | 76 | | /// <param name="node">The <see cref="XNode"/>.</param> |
| | 77 | | /// <param name="nodeQuery">The XPath <see cref="string"/>.</param> |
| | 78 | | /// <param name="throwException"> |
| | 79 | | /// When <code>true</code>, throw an exception for null nodes |
| | 80 | | /// and nodes that do not parse into the specified type. |
| | 81 | | /// </param> |
| | 82 | | /// <param name="defaultValue">Return a boxing <see cref="Object"/> for “zero-length” text nodes.</param> |
| | 83 | | /// <param name="resolver">The <see cref="IXmlNamespaceResolver"/> to use to resolve prefixes.</param> |
| | 84 | | public static object? GetXAttributeValueAndParse<T>(XNode? node, string? nodeQuery, bool throwException, |
| | 85 | | T? defaultValue, IXmlNamespaceResolver? resolver) |
| 0 | 86 | | { |
| | 87 | | object? o; |
| | 88 | |
|
| 0 | 89 | | var s = GetXAttributeValue(node, nodeQuery, throwException, null, resolver); |
| | 90 | |
|
| | 91 | | try |
| 0 | 92 | | { |
| 0 | 93 | | if (string.IsNullOrWhiteSpace(s?.Trim())) |
| 0 | 94 | | { |
| 0 | 95 | | return defaultValue; |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | T? stronglyOfT = default(T); |
| 0 | 99 | | switch (stronglyOfT) |
| | 100 | | { |
| | 101 | | case bool: |
| 0 | 102 | | o = bool.Parse(s); |
| 0 | 103 | | break; |
| | 104 | | case byte: |
| 0 | 105 | | o = Byte.Parse(s, CultureInfo.InvariantCulture); |
| 0 | 106 | | break; |
| | 107 | | case DateTime: |
| 0 | 108 | | o = DateTime.Parse(s, CultureInfo.InvariantCulture); |
| 0 | 109 | | break; |
| | 110 | | case decimal: |
| 0 | 111 | | o = Decimal.Parse(s, CultureInfo.InvariantCulture); |
| 0 | 112 | | break; |
| | 113 | | case double: |
| 0 | 114 | | o = Double.Parse(s, CultureInfo.InvariantCulture); |
| 0 | 115 | | break; |
| | 116 | | case short: |
| 0 | 117 | | o = Int16.Parse(s, CultureInfo.InvariantCulture); |
| 0 | 118 | | break; |
| | 119 | | case int: |
| 0 | 120 | | o = Int32.Parse(s, CultureInfo.InvariantCulture); |
| 0 | 121 | | break; |
| | 122 | | case long: |
| 0 | 123 | | o = Int64.Parse(s, CultureInfo.InvariantCulture); |
| 0 | 124 | | break; |
| | 125 | | default: |
| 0 | 126 | | { |
| 0 | 127 | | if (typeof(T).IsAssignableFrom(typeof(string))) |
| 0 | 128 | | { |
| 0 | 129 | | o = s; |
| 0 | 130 | | } |
| | 131 | | else |
| 0 | 132 | | { |
| 0 | 133 | | Type t = typeof(T); |
| 0 | 134 | | throw new NotSupportedException($"The specified type, “{t.FullName},” is not supported."); |
| | 135 | | } |
| | 136 | |
|
| 0 | 137 | | break; |
| | 138 | | } |
| | 139 | | } |
| 0 | 140 | | } |
| 0 | 141 | | catch (Exception ex) |
| 0 | 142 | | { |
| 0 | 143 | | Type t = typeof(T); |
| 0 | 144 | | var errMsg = |
| 0 | 145 | | $"Parse for “{t.FullName}” fails for element in “{nodeQuery}.” Value to parse: “{s ?? "Null"}.” Default |
| 0 | 146 | | throw new XmlException(errMsg); |
| | 147 | | } |
| | 148 | |
|
| 0 | 149 | | return o; |
| 0 | 150 | | } |
| | 151 | | } |