summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Web/Clients/DictionaryExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/DotNetOpenAuth.Web/Clients/DictionaryExtensions.cs')
-rw-r--r--src/DotNetOpenAuth.Web/Clients/DictionaryExtensions.cs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Web/Clients/DictionaryExtensions.cs b/src/DotNetOpenAuth.Web/Clients/DictionaryExtensions.cs
new file mode 100644
index 0000000..517fc47
--- /dev/null
+++ b/src/DotNetOpenAuth.Web/Clients/DictionaryExtensions.cs
@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Xml.Linq;
+
+namespace DotNetOpenAuth.Web.Clients
+{
+ internal static class DictionaryExtensions
+ {
+ /// <summary>
+ /// Adds a key/value pair to the specified dictionary if the value is not null or empty.
+ /// </summary>
+ /// <param name="dictionary">The dictionary.</param>
+ /// <param name="key">The key.</param>
+ /// <param name="value">The value.</param>
+ public static void AddItemIfNotEmpty(this IDictionary<string, string> dictionary, string key, string value)
+ {
+ if (key == null)
+ {
+ throw new ArgumentNullException("key");
+ }
+
+ if (!String.IsNullOrEmpty(value))
+ {
+ dictionary[key] = value;
+ }
+ }
+
+ /// <summary>
+ /// Adds the value from an XDocument with the specified element name if it's not empty.
+ /// </summary>
+ /// <param name="dictionary">The dictionary.</param>
+ /// <param name="document">The document.</param>
+ /// <param name="elementName">Name of the element.</param>
+ public static void AddDataIfNotEmpty(
+ this Dictionary<string, string> dictionary,
+ XDocument document,
+ string elementName)
+ {
+ var element = document.Root.Element(elementName);
+ if (element != null)
+ {
+ dictionary.AddItemIfNotEmpty(elementName, element.Value);
+ }
+ }
+ }
+}