2009-06-05 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / CapabilitiesResult.cs
1 #if NET_2_0
2 /*
3 Used to determine Browser Capabilities by the Browsers UserAgent String and related
4 Browser supplied Headers.
5 Copyright (C) 2002-Present  Owen Brady (Ocean at owenbrady dot net) 
6 and Dean Brettle (dean at brettle dot com)
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy 
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights 
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
12 copies of the Software, and to permit persons to whom the Software is furnished
13 to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all 
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
20 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 namespace System.Web.Configuration
26 {
27         using System;
28         using System.Collections;
29         using System.Collections.Generic;
30         using System.Text;
31         using System.Reflection;
32         using System.IO;
33
34         internal class CapabilitiesResult : System.Web.HttpBrowserCapabilities
35         {
36                 /// <summary>
37                 /// Initializes a new instance of the Result class.
38                 /// </summary>
39                 /// <param name="items">
40                 /// This is the data which this class will be handle request made though this class.
41                 /// </param>
42                 internal CapabilitiesResult(System.Collections.IDictionary items)
43                         : base()
44                 {
45                         base.Capabilities = items;
46                         Capabilities ["browsers"] = new ArrayList ();
47                 }
48
49                 /// <summary>
50                 /// 
51                 /// </summary>
52                 /// <param name="name"></param>
53                 /// <param name="value"></param>
54                 internal void AddCapabilities(string name, string value)
55                 {
56                         this.Capabilities[name] = value;
57                 }
58
59                 internal void AddMatchingBrowserId (string id)
60                 {
61                         ArrayList al = Capabilities ["browsers"] as ArrayList;
62                         if (al != null && !al.Contains (id))
63                                 al.Add (id);
64                 }
65                 
66                 internal virtual string Replace(string item)
67                 {
68                         if (item.IndexOf('$') > -1)
69                         {
70                                 //nasty hack to convert regular expression replacement text into  Capability item
71                                 //which we can use to replace with the actual values they are looking for.
72                                 System.Text.RegularExpressions.MatchCollection regxmatch;
73                                 regxmatch = System.Text.RegularExpressions.Regex.Matches(item, @"\$\{(?'Capability'\w*)\}");
74                                 if (regxmatch.Count == 0)
75                                 {
76                                         return item;
77                                 }
78                                 for (int i = 0;i <= regxmatch.Count - 1;i++)
79                                 {
80                                         if (regxmatch[i].Success == true)
81                                         {
82                                                 string c = regxmatch[i].Result("${Capability}");
83                                                 item = item.Replace("${" + c + "}", this[c]);
84                                         }
85                                 }
86                         }
87                         if (item.IndexOf('%') > -1)
88                         {
89                                 //nasty hack to convert regular expression replacement text into  Capability item
90                                 //which we can use to replace with the actual values they are looking for.
91                                 System.Text.RegularExpressions.MatchCollection regxmatch;
92                                 regxmatch = System.Text.RegularExpressions.Regex.Matches(item, @"\%\{(?'Capability'\w*)\}");
93                                 if (regxmatch.Count == 0)
94                                 {
95                                         return item;
96                                 }
97                                 for (int i = 0;i <= regxmatch.Count - 1;i++)
98                                 {
99                                         if (regxmatch[i].Success == true)
100                                         {
101                                                 string c = regxmatch[i].Result("${Capability}");
102                                                 item = item.Replace("%{" + c + "}", this[c]);
103                                         }
104                                 }
105                         }
106                         return item;
107                 }
108                 /// <summary>
109                 /// Gets the keys returned from processing.
110                 /// </summary>
111                 public System.Collections.Specialized.StringCollection Keys
112                 {
113                         get
114                         {
115                                 string[] a = new string[this.Capabilities.Keys.Count];
116                                 this.Capabilities.Keys.CopyTo(a, 0);
117                                 System.Array.Sort(a);
118                                 System.Collections.Specialized.StringCollection l;
119                                 l = new System.Collections.Specialized.StringCollection();
120                                 l.AddRange(a);
121                                 return l;
122                         }
123                 }
124                 public string UserAgent
125                 {
126                         get
127                         {
128                                 return this[""];
129                         }
130                 }
131         }
132 }
133 #endif