- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / DictHelper.cs
blob9681842e9ad613aedd5460f9de734d8a8deca1a5
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.Helpers
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
21 /// <summary>
22 /// Helper used to create <see cref="IDictionary"/> instances
23 /// </summary>
24 public class DictHelper : AbstractHelper
26 /// <summary>
27 /// Creates an <see cref="IDictionary"/> with entries
28 /// infered from the arguments.
29 /// <code>
30 /// CreateDict( "style=display: none;", "selected" )
31 /// </code>
32 /// </summary>
33 /// <param name="args"></param>
34 /// <returns></returns>
35 public IDictionary CreateDict(params String[] args)
37 return Create(args);
40 /// <summary>
41 /// Creates a dictionary from specified arguments.
42 /// </summary>
43 /// <param name="args">The arguments.</param>
44 /// <returns></returns>
45 public static IDictionary Create(params String[] args)
47 IDictionary dict = new HybridDictionary(true);
49 foreach(String arg in args)
51 String[] parts = arg.Split('=');
53 if (parts.Length == 1)
55 dict[arg] = "";
57 else if (parts.Length == 2)
59 dict[ parts[0] ] = parts[1];
61 else
63 dict[ parts[0] ] = String.Join("=", parts, 1, parts.Length - 1);
67 return dict;
70 /// <summary>
71 /// Creates a dictionary fros a name value collection.
72 /// </summary>
73 /// <param name="collection">The collection.</param>
74 /// <returns></returns>
75 public IDictionary FromNameValueCollection(NameValueCollection collection)
77 IDictionary dict = new HybridDictionary(true);
79 foreach(string key in collection.AllKeys)
81 if (key == null) continue;
82 dict[key] = collection.GetValues(key);
85 return dict;