forked from stevenh/HttpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayParameterCollection.cs
166 lines (147 loc) · 5.47 KB
/
ArrayParameterCollection.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
using System;
using System.Collections.Generic;
namespace HttpServer
{
/// <summary>
/// Form parameters where form string arrays have been converted to real arrays.
/// </summary>
public class ArrayParameterCollection : Parameter, IParameterCollection
{
private readonly Dictionary<string, IParameter> _items =
new Dictionary<string, IParameter>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Initializes a new instance of the <see cref="ArrayParameterCollection"/> class.
/// </summary>
public ArrayParameterCollection() : base("root", string.Empty)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArrayParameterCollection"/> class.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
protected ArrayParameterCollection(string name, string value)
: base(name, value)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ArrayParameterCollection"/> class.
/// </summary>
/// <param name="collection">Parse parameters from the this collection.</param>
public ArrayParameterCollection(IEnumerable<IParameter> collection) : base("root", string.Empty)
{
foreach (var item in collection)
{
foreach (var value in item)
Add(item.Name, value);
}
}
/// <summary>
/// Gets first value of an item.
/// </summary>
/// <value></value>
/// <returns>String if found; otherwise <c>null</c>.</returns>
public ArrayParameterCollection this[string name]
{
get { return GetItem(name); }
}
private ArrayParameterCollection GetItem(string name)
{
IParameter parameter;
if (!_items.TryGetValue(name, out parameter))
return null;
return (ArrayParameterCollection) parameter;
}
#region IParameterCollection Members
/// <summary>
/// Get a parameter.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IParameter Get(string name)
{
return GetItem(name);
}
/// <summary>
/// Add a parameter
/// </summary>
/// <param name="name">Name of parameter, can contain a string array.</param>
/// <param name="value">Value</param>
/// <example>
/// <code>
/// ArrayParameterCollection array = new ArrayParameterCollection();
/// array.Add("user[FirstName]", "Jonas");
/// array.Add("user[FirstName]", "Arne");
/// string firstName = array["user"]["FirstName"].Value; // "Arne" is returned
/// foreach (string value in array["user"]["FirstName"])
/// Console.WriteLine(value); // each name is displayed.
/// </code>
/// </example>
public void Add(string name, string value)
{
int pos = name.IndexOf('[');
if (pos != -1)
{
string myName = name.Substring(0, pos);
name = name.Remove(0, pos + 1);
pos = name.IndexOf(']');
name = name.Remove(pos, 1);
ArrayParameterCollection mine = GetItem(myName);
if (mine == null)
{
mine = new ArrayParameterCollection(myName, string.Empty);
_items.Add(myName, mine);
}
mine.Add(name, value);
return;
}
// new parameter
ArrayParameterCollection current = GetItem(name);
if (current == null)
{
current = new ArrayParameterCollection(name, value);
_items.Add(name, current);
return;
}
// existing
current.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);
}
/// <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>
string IParameterCollection.this[string name]
{
get { return GetItem(name).Value; }
}
/// <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();
}
#endregion
}
}