1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Internal
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
20 using System
.Globalization
;
24 /// Code shared by Helpers/Controllers/Others
26 public static class CommonUtils
29 /// Obtains the entry.
31 /// <param name="attributes">The attributes.</param>
32 /// <param name="key">The key.</param>
33 /// <returns>The generated form element</returns>
34 internal static string ObtainEntry(IDictionary attributes
, string key
)
36 if (attributes
!= null && attributes
.Contains(key
))
38 return (String
)attributes
[key
];
45 /// Obtains the entry.
47 /// <param name="attributes">The attributes.</param>
48 /// <param name="key">The key.</param>
49 /// <param name="defaultValue">The default value.</param>
50 /// <returns>the entry value or the default value</returns>
51 internal static string ObtainEntry(IDictionary attributes
, string key
, string defaultValue
)
53 string value = ObtainEntry(attributes
, key
);
55 return value ?? defaultValue
;
59 /// Obtains the entry and remove it if found.
61 /// <param name="attributes">The attributes.</param>
62 /// <param name="key">The key.</param>
63 /// <param name="defaultValue">The default value.</param>
64 /// <returns>the entry value or the default value</returns>
65 internal static string ObtainEntryAndRemove(IDictionary attributes
, string key
, string defaultValue
)
67 string value = ObtainEntryAndRemove(attributes
, key
);
69 return value ?? defaultValue
;
73 /// Obtains the entry and remove it if found.
75 /// <param name="attributes">The attributes.</param>
76 /// <param name="key">The key.</param>
77 /// <returns>the entry value or null</returns>
78 internal static string ObtainEntryAndRemove(IDictionary attributes
, string key
)
82 if (attributes
!= null && attributes
.Contains(key
))
84 value = (String
)attributes
[key
];
86 attributes
.Remove(key
);
93 /// Obtains the entry and remove it if found.
95 /// <param name="attributes">The attributes.</param>
96 /// <param name="key">The key.</param>
97 /// <returns>the entry value or null</returns>
98 internal static object ObtainObjectEntryAndRemove(IDictionary attributes
, string key
)
102 if (attributes
!= null && attributes
.Contains(key
))
104 value = attributes
[key
];
106 attributes
.Remove(key
);
113 /// Merges <paramref name="userOptions"/> with <paramref name="defaultOptions"/> placing results in
114 /// <paramref name="userOptions"/>.
116 /// <param name="userOptions">The user options.</param>
117 /// <param name="defaultOptions">The default options.</param>
119 /// All <see cref="IDictionary.Values"/> and <see cref="IDictionary.Keys"/> in <paramref name="defaultOptions"/>
120 /// are copied to <paramref name="userOptions"/>. Entries with the same <see cref="DictionaryEntry.Key"/> in
121 /// <paramref name="defaultOptions"/> and <paramref name="userOptions"/> are skipped.
123 internal static void MergeOptions(IDictionary userOptions
, IDictionary defaultOptions
)
125 foreach(DictionaryEntry entry
in defaultOptions
)
127 if (!userOptions
.Contains(entry
.Key
))
129 userOptions
[entry
.Key
] = entry
.Value
;
135 /// Builds a query string.
138 /// Supports multi-value query strings, using any
139 /// <see cref="IEnumerable"/> as a value.
141 /// <param name="parameters">The parameters</param>
142 /// <param name="serverUtil">The server utility instance</param>
143 /// <param name="encodeAmp">if <c>true</c>, the separation of entries will be encoded.</param>
144 public static string BuildQueryString(IServerUtility serverUtil
, NameValueCollection parameters
, bool encodeAmp
)
146 if (parameters
== null || parameters
.Count
== 0) return string.Empty
;
147 if (serverUtil
== null) throw new ArgumentNullException("serverUtil");
149 StringBuilder sb
= new StringBuilder();
151 bool useSeparator
= false;
153 foreach (string key
in parameters
.Keys
)
155 if (key
== null) continue;
157 foreach (string value in parameters
.GetValues(key
))
175 sb
.Append(serverUtil
.UrlEncode(key
))
177 .Append(serverUtil
.UrlEncode(value));
181 return sb
.ToString();
185 /// Builds a query string.
188 /// Supports multi-value query strings, using any
189 /// <see cref="IEnumerable"/> as a value.
191 /// <param name="parameters">The parameters</param>
192 public static string BuildQueryString(NameValueCollection parameters
)
194 if (parameters
== null || parameters
.Count
== 0) return string.Empty
;
196 StringBuilder sb
= new StringBuilder();
198 bool useSeparator
= false;
200 foreach(string key
in parameters
.Keys
)
202 if (key
== null) continue;
204 foreach(string value in parameters
.GetValues(key
))
221 return sb
.ToString();
225 /// Builds a query string.
228 /// Supports multi-value query strings, using any
229 /// <see cref="IEnumerable"/> as a value.
232 /// IDictionary dict = new Hashtable();
233 /// dict.Add("id", 5);
234 /// dict.Add("selectedItem", new int[] { 2, 4, 99 });
235 /// string querystring = BuildQueryString(dict);
236 /// // should result in: "id=5&selectedItem=2&selectedItem=4&selectedItem=99&"
240 /// <param name="parameters">The parameters</param>
241 /// <param name="serverUtil">The server utility instance</param>
242 /// <param name="encodeAmp">if <c>true</c>, the separation of entries will be encoded.</param>
243 public static string BuildQueryString(IServerUtility serverUtil
, IDictionary parameters
, bool encodeAmp
)
245 if (parameters
== null || parameters
.Count
== 0) return string.Empty
;
246 if (serverUtil
== null) throw new ArgumentNullException("serverUtil");
248 Object
[] singleValueEntry
= new Object
[1];
249 StringBuilder sb
= new StringBuilder();
251 bool useSeparator
= false;
253 foreach(DictionaryEntry entry
in parameters
)
255 if (entry
.Value
== null) continue;
257 IEnumerable values
= singleValueEntry
;
259 if (!(entry
.Value
is String
) && (entry
.Value
is IEnumerable
))
261 values
= (IEnumerable
) entry
.Value
;
265 singleValueEntry
[0] = entry
.Value
;
268 foreach(object value in values
)
286 string encoded
= serverUtil
.UrlEncode(Convert
.ToString(value, CultureInfo
.CurrentCulture
));
288 sb
.Append(serverUtil
.UrlEncode(entry
.Key
.ToString())).Append('=').Append(encoded
);
292 return sb
.ToString();