summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.AspNet/Clients/DictionaryExtensions.cs
blob: f441c072bb76ac17d66a1a9f52c0be59b323197f (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
47
48
49
50
51
52
53
54
55
56
57
58
//-----------------------------------------------------------------------
// <copyright file="DictionaryExtensions.cs" company="Microsoft">
//     Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.AspNet.Clients {
	using System;
	using System.Collections.Generic;
	using System.Xml.Linq;

	/// <summary>
	/// The dictionary extensions.
	/// </summary>
	internal static class DictionaryExtensions {
		/// <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);
			}
		}

		/// <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;
			}
		}
	}
}