Fix the build.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Internal / CommonUtils.cs
blob25b9ca50ada2048bac0014b6cd550a5bb6832e65
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.Globalization;
21 using System.Text;
23 /// <summary>
24 /// Code shared by Helpers/Controllers/Others
25 /// </summary>
26 public static class CommonUtils
28 /// <summary>
29 /// Obtains the entry.
30 /// </summary>
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];
41 return null;
44 /// <summary>
45 /// Obtains the entry.
46 /// </summary>
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;
58 /// <summary>
59 /// Obtains the entry and remove it if found.
60 /// </summary>
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;
72 /// <summary>
73 /// Obtains the entry and remove it if found.
74 /// </summary>
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)
80 string value = null;
82 if (attributes != null && attributes.Contains(key))
84 value = (String)attributes[key];
86 attributes.Remove(key);
89 return value;
92 /// <summary>
93 /// Obtains the entry and remove it if found.
94 /// </summary>
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)
100 object value = null;
102 if (attributes != null && attributes.Contains(key))
104 value = attributes[key];
106 attributes.Remove(key);
109 return value;
112 /// <summary>
113 /// Merges <paramref name="userOptions"/> with <paramref name="defaultOptions"/> placing results in
114 /// <paramref name="userOptions"/>.
115 /// </summary>
116 /// <param name="userOptions">The user options.</param>
117 /// <param name="defaultOptions">The default options.</param>
118 /// <remarks>
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.
122 /// </remarks>
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;
134 /// <summary>
135 /// Builds a query string.
136 /// </summary>
137 /// <remarks>
138 /// Supports multi-value query strings, using any
139 /// <see cref="IEnumerable"/> as a value.
140 /// </remarks>
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))
159 if (useSeparator)
161 if (encodeAmp)
163 sb.Append("&amp;");
165 else
167 sb.Append("&");
170 else
172 useSeparator = true;
175 sb.Append(serverUtil.UrlEncode(key))
176 .Append('=')
177 .Append(serverUtil.UrlEncode(value));
181 return sb.ToString();
184 /// <summary>
185 /// Builds a query string.
186 /// </summary>
187 /// <remarks>
188 /// Supports multi-value query strings, using any
189 /// <see cref="IEnumerable"/> as a value.
190 /// </remarks>
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))
206 if (useSeparator)
208 sb.Append("&");
210 else
212 useSeparator = true;
215 sb.Append(key)
216 .Append('=')
217 .Append(value);
221 return sb.ToString();
224 /// <summary>
225 /// Builds a query string.
226 /// </summary>
227 /// <remarks>
228 /// Supports multi-value query strings, using any
229 /// <see cref="IEnumerable"/> as a value.
230 /// <example>
231 /// <code>
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&amp;selectedItem=2&amp;selectedItem=4&amp;selectedItem=99&amp;"
237 /// </code>
238 /// </example>
239 /// </remarks>
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;
263 else
265 singleValueEntry[0] = entry.Value;
268 foreach(object value in values)
270 if (useSeparator)
272 if (encodeAmp)
274 sb.Append("&amp;");
276 else
278 sb.Append("&");
281 else
283 useSeparator = true;
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();