1 // Copyright 2004-2008 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
.Helpers
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
22 /// Helper used to create <see cref="IDictionary"/> instances
24 public class DictHelper
: AbstractHelper
26 #region MonoRailDictionary inner class
28 /// Helper (for the Helper) used to create <see cref="IDictionary"/> instances.
31 public class MonoRailDictionary
: HybridDictionary
34 /// Initializes a new instance of the <see cref="MonoRailDictionary"/> class.
35 /// Forces case Insensitivty.
37 public MonoRailDictionary() : base(true) { }
40 // Removed. See explaination in outer class.
41 //public MonoRailDictionary N<T>(string key, T value)
43 // this[key] = value.ToString();
48 /// Adds the specified key & value to the collection.
50 /// <remarks>Usuable in placed where the generic version is not available.</remarks>
51 /// <param name="key">The key.</param>
52 /// <param name="value">The value.</param>
53 /// <returns>itself, to allow for chaning.</returns>
54 public MonoRailDictionary
N(string key
, object value)
56 this[key
] = value.ToString();
61 /// Adds the specified key to the collection.
63 /// <param name="key">The key.</param>
64 /// <returns>itself, to allow for chaning.</returns>
65 public MonoRailDictionary
N(string key
)
75 /// Initializes a new instance of the <see cref="DictHelper"/> class.
77 public DictHelper() { }
79 /// Initializes a new instance of the <see cref="DictHelper"/> class.
81 /// <param name="engineContext">The engine context.</param>
82 public DictHelper(IEngineContext engineContext
) : base(engineContext
) { }
86 /// Creates an <see cref="IDictionary"/> with entries
87 /// infered from the arguments.
91 /// helper.CreateDict( "style=display: none;", "selected" )
93 /// <param name="args"></param>
94 /// <returns>an <see cref="IDictionary"/></returns>
95 public MonoRailDictionary
CreateDict(params String
[] args
)
101 /// Creates a dictionary from specified arguments.
103 /// <remarks> Returns a MonoRailDictionary object,
104 /// which is an IDictionary like previous versions of this method.
108 /// DictHelper.Create( "style=display: none;", "selected" )
109 /// </code></example>
110 /// <param name="args">The arguments.</param>
111 /// <returns>an <see cref="IDictionary"/></returns>
112 public static MonoRailDictionary
Create(params String
[] args
)
114 MonoRailDictionary dict
= new MonoRailDictionary();
115 foreach (String arg
in args
)
117 int pos
= arg
.IndexOf('=');
125 dict
[arg
.Substring(0, pos
)] = arg
.Substring(pos
+ 1);
132 ///// Create a new collections and adds the specified key & value.
134 ///// <typeparam name="T"></typeparam>
135 ///// <param name="key">The key.</param>
136 ///// <param name="value">The value.</param>
137 ///// <returns></returns>
139 // Generic version can't be used in an nVelocity template,
140 // and the non-generic, object version (below) would need a
141 // different name to avoid conflict, so I figured I'd
142 // just get rid of it, even thought it's slightly faster.
143 //public MonoRailDictionary N<T>(string key, T value)
145 // MonoRailDictionary helper = new MonoRailDictionary();
146 // helper[key] = value.ToString();
151 /// Creates an <see cref="IDictionary"/> and adds the
152 /// specified key & value to the collection.
154 /// <param name="name">The name.</param>
155 /// <param name="value">The value.</param>
156 /// <returns>an <see cref="IDictionary"/></returns>
157 public MonoRailDictionary
N(string name
, object value)
159 return CreateN(name
, value);
163 /// Creates an <see cref="IDictionary"/> and adds the
164 /// specified key & value to the collection.
166 /// <param name="key">The key.</param>
167 /// <param name="value">The value.</param>
168 /// <returns>an <see cref="IDictionary"/></returns>
169 public static MonoRailDictionary
CreateN(string key
, object value)
171 MonoRailDictionary helper
= new MonoRailDictionary();
172 helper
[key
] = value.ToString();
177 /// Creates a dictionary froms a name value collection.
179 /// <param name="collection">The collection.</param>
180 /// <returns></returns>
181 public IDictionary
FromNameValueCollection(NameValueCollection collection
)
183 IDictionary dict
= new MonoRailDictionary();
185 foreach(string key
in collection
.AllKeys
)
189 dict
[key
] = collection
.GetValues(key
);