-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParameterCollection.cs
179 lines (159 loc) · 5.67 KB
/
ParameterCollection.cs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace HttpServer
{
/// <summary>
/// Collection of parameters.
/// </summary>
/// <remarks>
/// <see cref="Dictionary{TKey,TValue}"/> or <see cref="NameValueCollection"/> is not used since each parameter can
/// have multiple values.
/// </remarks>
public class ParameterCollection : IParameterCollection
{
private readonly Dictionary<string, IParameter> _items = new Dictionary<string, IParameter>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Initializes a new instance of the <see cref="ParameterCollection"/> class.
/// </summary>
/// <param name="collections">Collections to merge.</param>
/// <remarks>
/// Later collections will overwrite parameters from earlier collections.
/// </remarks>
public ParameterCollection(params IParameterCollection[] collections)
{
foreach (IParameterCollection collection in collections)
{
if (collection == null)
continue;
foreach (IParameter p in collection)
{
foreach (string value in p)
Add(p.Name, value);
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ParameterCollection"/> class.
/// </summary>
public ParameterCollection()
{
}
/// <summary>
/// Get a list of string arrays.
/// </summary>
/// <returns></returns>
public string[] GetArrayNames()
{
var names = new List<string>();
foreach (var item in _items)
{
int pos = item.Key.IndexOf("[");
if (pos == -1)
continue;
names.Add(item.Key.Substring(0, pos));
}
return names.ToArray();
}
/// <summary>
/// Get parameters
/// </summary>
/// <param name="arrayName">Sub array (text array)</param>
/// <returns></returns>
public IParameterCollection GetParameters(string arrayName)
{
var collection = new ParameterCollection();
arrayName = arrayName + "[";
foreach (var item in _items)
{
if (!item.Key.StartsWith(arrayName)) continue;
int pos = arrayName.IndexOf("]");
if (pos == -1) continue;
string name = arrayName.Substring(arrayName.Length, pos - arrayName.Length);
foreach (string value in item.Value)
collection.Add(name, value);
}
return collection;
}
#region IParameterCollection Members
/// <summary>
/// Gets number of parameters.
/// </summary>
public int Count
{
get { return _items.Count; }
}
/// <summary>
/// Gets last value of an parameter.
/// </summary>
/// <param name="name">Parameter name</param>
/// <returns>String if found; otherwise <c>null</c>.</returns>
public string this[string name]
{
get
{
IParameter param = Get(name);
return param != null ? param.Value : null;
}
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>1</filterpriority>
public IEnumerator<IParameter> GetEnumerator()
{
return _items.Values.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>2</filterpriority>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Get a parameter.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IParameter Get(string name)
{
IParameter parameter;
return _items.TryGetValue(name, out parameter) ? parameter : null;
}
/// <summary>
/// Add a query string parameter.
/// </summary>
/// <param name="name">Parameter name</param>
/// <param name="value">Value</param>
public void Add(string name, string value)
{
IParameter parameter;
if (!_items.TryGetValue(name, out parameter))
{
parameter = new Parameter(name, value);
_items.Add(name, parameter);
}
else
parameter.Values.Add(value);
}
/// <summary>
/// Checks if the specified parameter exists
/// </summary>
/// <param name="name">Parameter name.</param>
/// <returns><c>true</c> if found; otherwise <c>false</c>;</returns>
public bool Exists(string name)
{
return _items.ContainsKey(name);
}
#endregion
}
}