summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Configuration/HostNameOrRegexCollection.cs
blob: 8f537c339e7fec6bd73b5b82238de24e87ac471b (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
59
60
61
62
63
64
65
66
67
68
69
//-----------------------------------------------------------------------
// <copyright file="HostNameOrRegexCollection.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Configuration {
	using System.Collections.Generic;
	using System.Configuration;
	using System.Text.RegularExpressions;
	using Validation;

	/// <summary>
	/// Represents a collection of child elements that describe host names either as literal host names or regex patterns.
	/// </summary>
	internal class HostNameOrRegexCollection : ConfigurationElementCollection {
		/// <summary>
		/// Initializes a new instance of the <see cref="HostNameOrRegexCollection"/> class.
		/// </summary>
		public HostNameOrRegexCollection() {
		}

		/// <summary>
		/// Gets all the members of the collection assuming they are all literal host names.
		/// </summary>
		internal IEnumerable<string> KeysAsStrings {
			get {
				foreach (HostNameElement element in this) {
					yield return element.Name;
				}
			}
		}

		/// <summary>
		/// Gets all the members of the collection assuming they are all host names regex patterns.
		/// </summary>
		internal IEnumerable<Regex> KeysAsRegexs {
			get {
				foreach (HostNameElement element in this) {
					if (element.Name != null) {
						yield return new Regex(element.Name);
					}
				}
			}
		}

		/// <summary>
		/// Creates a new child host name element.
		/// </summary>
		/// <returns>
		/// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
		/// </returns>
		protected override ConfigurationElement CreateNewElement() {
			return new HostNameElement();
		}

		/// <summary>
		/// Gets the element key for a specified configuration element.
		/// </summary>
		/// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
		/// <returns>
		/// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
		/// </returns>
		protected override object GetElementKey(ConfigurationElement element) {
			Requires.NotNull(element, "element");
			return ((HostNameElement)element).Name ?? string.Empty;
		}
	}
}