New test.
[mono.git] / mcs / class / System.Data / Mainsoft.Data.Configuration.jvm / KeyMapperBase.cs
1 //
2 // System.Data.OleDb.OleDbConnection
3 //
4 // Authors:
5 //      Konstantin Triger <kostat@mainsoft.com>
6 //      Boris Kirzner <borisk@mainsoft.com>
7 //      
8 // (C) 2006 Mainsoft Corporation (http://www.mainsoft.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Text;
36 using System.Globalization;
37 using System.IO;
38
39 namespace Mainsoft.Data.Configuration
40 {
41         public class ConnectionStringDictionary: IConnectionStringDictionary
42         {
43                 #region Fields
44
45                 private readonly IDictionary _dictionary;
46                 private readonly NameValueCollection _mapping;
47                 private readonly NameValueCollection _actualKeys;
48                 const string DataDirectoryPlaceholder = "|DataDirectory|";
49
50                 #endregion // Fields
51
52                 #region Constructors
53
54                 public ConnectionStringDictionary(string connectionString, NameValueCollection defaultMapping)
55                 {
56                         _actualKeys = new NameValueCollection();
57                         _dictionary = Parse (connectionString);
58                         _mapping = defaultMapping;
59                 }
60
61                 #endregion // Constructors
62
63                 #region Methods
64
65                 public string GetConnectionStringKey (string key)
66                 {
67                         string cached = _actualKeys [key];
68                         if (cached != null)
69                                 return cached;
70
71                         if (_mapping != null)
72                         for(int i = 0, c = _mapping.Keys.Count; i < c; i++) {
73                                 if (string.Compare(key, _mapping.Keys[i], true,
74                                         CultureInfo.InvariantCulture) == 0) {
75                                         string[] values = _mapping.GetValues(i);
76                                         for(int j = 0; j < values.Length; j++) {
77                                                 string actualKey = values[j];
78                                                 if (_dictionary.Contains (actualKey)) {
79                                                         _actualKeys.Add (key, actualKey);
80                                                         return actualKey;
81                                                 }
82                                         }
83                                 }
84                         }
85
86                         if (_dictionary.Contains(key))
87                                 return key;
88
89                         return null;
90                 }
91
92                 public static IDictionary Parse (string connectionString)
93                 {
94                         IDictionary userParameters = CollectionsUtil.CreateCaseInsensitiveHashtable();
95
96                         if (connectionString == null || connectionString.Length == 0) {
97                                 return userParameters;
98                         }
99                         //connectionString += ";";
100
101                         if (connectionString.IndexOf (DataDirectoryPlaceholder, StringComparison.Ordinal) >= 0) {
102                                 string dataDirectory = (string) AppDomain.CurrentDomain.GetData ("DataDirectory");
103                                 if (dataDirectory != null && dataDirectory.Length > 0) {
104                                         char lastChar = dataDirectory [dataDirectory.Length - 1];
105                                         if (lastChar != Path.DirectorySeparatorChar &&
106                                                 lastChar != Path.AltDirectorySeparatorChar)
107                                                 dataDirectory += '/';
108                                 }
109                                 connectionString = connectionString.Replace (DataDirectoryPlaceholder, dataDirectory);
110                         }
111
112                         bool inQuote = false;
113                         bool inDQuote = false;
114                         bool inName = true;
115
116                         string name = String.Empty;
117                         string value = String.Empty;
118                         StringBuilder sb = new StringBuilder (connectionString.Length);
119
120                         for (int i = 0; i < connectionString.Length; i ++) {
121                                 char c = connectionString [i];
122                                 char peek;
123                                 if (i == connectionString.Length - 1)
124                                         peek = '\0';
125                                 else
126                                         peek = connectionString [i + 1];
127
128                                 switch (c) {
129                                         case '\'':
130                                                 if (inDQuote)
131                                                         sb.Append (c);
132                                                 else if (peek == c) {
133                                                         sb.Append(c);
134                                                         i ++;
135                                                 }
136                                                 else
137                                                         inQuote = !inQuote;
138                                                 break;
139                                         case '"':
140                                                 if (inQuote)
141                                                         sb.Append(c);
142                                                 else if (peek == c) {
143                                                         sb.Append(c);
144                                                         i ++;
145                                                 }
146                                                 else
147                                                         inDQuote = !inDQuote;
148                                                 break;
149                                         case ';':
150                                                 if (inDQuote || inQuote)
151                                                         sb.Append(c);
152                                                 else {
153                                                         if (name != null && name.Length > 0) {
154                                                                 value = sb.ToString();
155                                                                 userParameters [name.Trim()] = value.Trim();
156                                                         }
157                                                         inName = true;
158                                                         name = String.Empty;
159                                                         value = String.Empty;
160                                                         sb.Length = 0;
161                                                 }
162                                                 break;
163                                         case '=':
164                                                 if (inDQuote || inQuote || !inName)
165                                                         sb.Append (c);
166                                                 else if (peek == c) {
167                                                         sb.Append (c);
168                                                         i += 1;
169                                                 }
170                                                 else {
171                                                         name = sb.ToString();
172                                                         sb.Length = 0;
173                                                         inName = false;
174                                                 }
175                                                 break;
176                                         case ' ':
177                                                 if (inQuote || inDQuote)
178                                                         sb.Append(c);
179                                                 else if (sb.Length > 0 && peek != ';')
180                                                         sb.Append(c);
181                                                 break;
182                                         default:
183                                                 sb.Append(c);
184                                                 break;
185                                 }
186                         }
187
188                         if (inDQuote || inQuote)
189                                 throw new ArgumentException("connectionString");
190
191                         if (name != null && name.Length > 0) {
192                                 value = sb.ToString();
193                                 userParameters [name.Trim()] = value.Trim();
194                         }
195
196                         return userParameters;
197                 }
198
199
200                 #endregion // Methods
201
202                 #region IDictionary Members
203
204                 public virtual bool IsFixedSize
205                 {
206                         get { return _dictionary.IsFixedSize; }
207                 }
208
209                 public virtual bool IsReadOnly
210                 {
211                         get { return _dictionary.IsReadOnly; }
212                 }
213
214                 public virtual ICollection Keys
215                 {
216                         get { 
217                                 return _dictionary.Keys; 
218                         }
219                 }
220
221                 public virtual object this [object key] {
222                         get { 
223
224                                 if (!(key is String))
225                                         throw new ArgumentException("key is not string");
226
227                                 string skey = (string)key;
228
229                                 skey = GetConnectionStringKey(skey);
230                                 if (skey == null)
231                                         return null;
232
233                                 return _dictionary[skey];
234                         }
235
236                         set {
237                                 if (!(key is String))
238                                         throw new ArgumentException("key is not string");
239
240                                 string skey = (string)key;
241
242                                 skey = GetConnectionStringKey(skey);
243                                 if (skey == null)
244                                         skey = (string)key;
245
246                                 _dictionary[skey] = value;
247                         }
248                 }
249
250                 public virtual ICollection Values
251                 {
252                         get { return _dictionary.Values; }
253                 }
254
255                 public virtual void Add (object key, object value)
256                 {
257                         _dictionary.Add ((string)key, (string)value);
258                 }
259
260                 public virtual void Clear ()
261                 {
262                         _dictionary.Clear ();
263                 }
264
265                 public virtual bool Contains (object key)
266                 {
267                         return _dictionary.Contains (key);
268                 }
269
270                 public virtual IDictionaryEnumerator GetEnumerator ()
271                 {
272                         return _dictionary.GetEnumerator ();
273                 }
274
275                 public virtual void Remove (object key)
276                 {
277                         _dictionary.Remove ((string)key);
278                 }
279
280                 #endregion // IDictionary Members
281
282                 #region IEnumerable Members
283
284                 IEnumerator IEnumerable.GetEnumerator () 
285                 {
286                         return this.GetEnumerator();
287                 }
288
289                 #endregion // IEnumerable Members
290
291                 #region ICollection Members
292
293                 public virtual bool IsSynchronized 
294                 {
295                         get { return ((ICollection)_dictionary).IsSynchronized; }
296                 }
297
298                 public virtual int Count 
299                 {
300                         get { return _dictionary.Count; }
301                 }
302
303                 public virtual void CopyTo (Array array, int index) 
304                 {
305                         _dictionary.CopyTo (array, index);
306                 }
307
308                 public virtual object SyncRoot 
309                 {
310                         get {return ((ICollection)_dictionary).SyncRoot; }
311                 }
312
313                 #endregion // ICollection Members
314         }
315 }