2008-07-09 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / DataBinder.cs
1 //
2 // System.Web.UI.DataBinder.cs
3 //
4 // Authors:
5 //      Duncan Mak  (duncan@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Reflection;
34 using System.Security.Permissions;
35 #if NET_2_0
36 using System.Collections.Generic;
37 #endif
38
39 namespace System.Web.UI {
40
41         // CAS - no InheritanceDemand here as the class is sealed
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public sealed class DataBinder {
44                 public DataBinder ()
45                 {
46                 }
47
48                 internal static string FormatResult (object result, string format)
49                 {
50                         if (result == null)
51                                 return String.Empty;
52
53                         if (format == null || format.Length == 0)
54                                 return result.ToString ();
55
56                         return String.Format (format, result);
57                 }
58                 
59                 public static object Eval (object container, string expression)
60                 {
61                         if ((expression == null) || (expression.Length == 0))
62                                 throw new ArgumentNullException ("expression");
63
64                         object current = container;
65
66                         while (current != null) {
67                                 int dot = expression.IndexOf ('.');
68                                 int size = (dot == -1) ? expression.Length : dot;
69                                 string prop = expression.Substring (0, size);
70                                 if (prop.IndexOf ('[') != -1)
71                                         current = GetIndexedPropertyValue (current, prop);
72                                 else
73                                         current = GetPropertyValue (current, prop);
74
75                                 if (dot == -1)
76                                         break;
77                                 
78                                 expression = expression.Substring (prop.Length + 1);
79                         }
80
81                         return current;
82                 }
83
84                 public static string Eval (object container, string expression, string format)
85                 {
86                         object result = Eval (container, expression);
87                         return FormatResult (result, format);
88                 }
89
90                 public static object GetIndexedPropertyValue (object container, string expr)
91                 {
92                         if (container == null)
93                                 throw new ArgumentNullException ("container");
94                         if ((expr == null) || (expr.Length == 0))
95                                 throw new ArgumentNullException ("expr");
96
97                         int openIdx = expr.IndexOf ('[');
98                         int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
99                         if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
100                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
101
102                         string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
103                         val = val.Trim ();
104                         if (val.Length == 0)
105                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
106
107                         bool is_string = false;
108                         // a quoted val means we have a string
109                         if ((val[0] == '\'' && val[val.Length - 1] == '\'') ||
110                                 (val[0] == '\"' && val[val.Length - 1] == '\"')) {
111                                 is_string = true;
112                                 val = val.Substring(1, val.Length - 2);
113                         } else {
114                                 // if all chars are digits, then we have a int
115                                 for(int i = 0; i < val.Length; i++)
116                                         if (!Char.IsDigit(val[i])) {
117                                                 is_string = true;
118                                                 break;
119                                         }
120                         }
121
122                         int intVal = 0;
123                         if (!is_string) {
124                                 try {
125                                         intVal = Int32.Parse (val);
126                                 } catch {
127                                         throw new ArgumentException (expr + " is not a valid indexed expression.");
128                                 }
129                         }
130
131                         string property = null;
132                         if (openIdx > 0) {
133                                 property = expr.Substring (0, openIdx);
134                                 if (property != null && property != String.Empty)
135                                         container = GetPropertyValue (container, property);
136                         }
137
138                         if (container == null)
139                                 return null;
140
141                         if (container is System.Collections.IList) {
142                                 IList l = (IList) container;
143                                 return l [intVal];
144                         }
145
146                         Type t = container.GetType ();
147                         // MS does not seem to look for any other than "Item"!!!
148                         object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
149                         if (atts.Length != 1)
150                                 throw new ArgumentException (expr + " indexer not found.");
151
152                         property = ((DefaultMemberAttribute) atts [0]).MemberName;
153
154                         Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
155                         PropertyInfo prop = t.GetProperty (property, argTypes);
156                         if (prop == null)
157                                 throw new ArgumentException (expr + " indexer not found.");
158
159                         object [] args = new object [1];
160                         if (is_string)
161                                 args [0] = val;
162                         else
163                                 args [0] = intVal;
164
165                         return prop.GetValue (container, args);
166                 }
167
168                 public static string GetIndexedPropertyValue (object container, string expr, string format)
169                 {
170                         object result = GetIndexedPropertyValue (container, expr);
171                         return FormatResult (result, format);
172                 }
173
174                 public static object GetPropertyValue (object container, string propName)
175                 {
176                         if (container == null)
177                                 throw new ArgumentNullException ("container");
178                         if (propName == null)
179                                 throw new ArgumentNullException ("propName");
180
181                         PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
182                         if (prop == null) {
183                                 throw new HttpException ("Property " + propName + " not found in " +
184                                                          container.GetType ());
185                         }
186
187                         return prop.GetValue (container);
188                 }
189
190                 public static string GetPropertyValue (object container, string propName, string format)
191                 {
192                         object result = GetPropertyValue (container, propName);
193                         return FormatResult (result, format);
194                 }
195
196                 #if NET_2_0
197                 [ThreadStatic]
198                 static Dictionary<Type, PropertyInfo> dataItemCache;
199         
200                 public static object GetDataItem (object container, out bool foundDataItem)
201                 {       
202                         foundDataItem = false;
203                         if (container == null)                  
204                                 return null;
205                         
206                         if (container is IDataItemContainer) {
207                                 foundDataItem = true;
208                                 return ((IDataItemContainer)container).DataItem;
209                         }
210                         
211                         PropertyInfo pi = null;
212                         if (dataItemCache == null)
213                                 dataItemCache = new Dictionary<Type, PropertyInfo> ();
214                         
215                         Type type = container.GetType ();
216                         if (!dataItemCache.TryGetValue (type, out pi)) {
217                                 pi = type.GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
218                                 dataItemCache [type] = pi;
219                         }
220
221                         if (pi == null)
222                                 return null;
223                         
224                         foundDataItem = true;
225                         return pi.GetValue (container, null); 
226                 } 
227                 
228                 
229                 public static object GetDataItem (object container)
230                 {
231                         bool flag;
232                         return GetDataItem (container, out flag); 
233                 }
234                 #endif
235         }
236 }
237