summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Web/Clients/DictionaryExtensions.cs
blob: 517fc47590461eb962afbe96690a2dc283b7a786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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);
            }
        }
    }
}