New test.
[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
36 namespace System.Web.UI {
37
38         // CAS - no InheritanceDemand here as the class is sealed
39         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public sealed class DataBinder {
41                 public DataBinder ()
42                 {
43                 }
44
45                 internal static string FormatResult (object result, string format)
46                 {
47                         if (result == null)
48                                 return String.Empty;
49
50                         if (format == null)
51                                 return result.ToString ();
52
53                         return String.Format (format, result);
54                 }
55                 
56                 public static object Eval (object container, string expression)
57                 {
58                         if ((expression == null) || (expression.Length == 0))
59                                 throw new ArgumentNullException ("expression");
60
61                         object current = container;
62
63                         while (current != null) {
64                                 int dot = expression.IndexOf ('.');
65                                 int size = (dot == -1) ? expression.Length : dot;
66                                 string prop = expression.Substring (0, size);
67                                 if (prop.IndexOf ('[') != -1)
68                                         current = GetIndexedPropertyValue (current, prop);
69                                 else
70                                         current = GetPropertyValue (current, prop);
71
72                                 if (dot == -1)
73                                         break;
74                                 
75                                 expression = expression.Substring (prop.Length + 1);
76                         }
77
78                         return current;
79                 }
80
81                 public static string Eval (object container, string expression, string format)
82                 {
83                         object result = Eval (container, expression);
84                         return FormatResult (result, format);
85                 }
86
87                 public static object GetIndexedPropertyValue (object container, string expr)
88                 {
89                         if (container == null)
90                                 throw new ArgumentNullException ("container");
91                         if ((expr == null) || (expr.Length == 0))
92                                 throw new ArgumentNullException ("expr");
93
94                         int openIdx = expr.IndexOf ('[');
95                         int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
96                         if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
97                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
98
99                         string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
100                         val = val.Trim ();
101                         if (val.Length == 0)
102                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
103
104                         bool is_string = false;
105                         // a quoted val means we have a string
106                         if ((val[0] == '\'' && val[val.Length - 1] == '\'') ||
107                                 (val[0] == '\"' && val[val.Length - 1] == '\"')) {
108                                 is_string = true;
109                                 val = val.Substring(1, val.Length - 2);
110                         } else {
111                                 // if all chars are digits, then we have a int
112                                 for(int i = 0; i < val.Length; i++)
113                                         if (!Char.IsDigit(val[i])) {
114                                                 is_string = true;
115                                                 break;
116                                         }
117                         }
118
119                         int intVal = 0;
120                         if (!is_string) {
121                                 try {
122                                         intVal = Int32.Parse (val);
123                                 } catch {
124                                         throw new ArgumentException (expr + " is not a valid indexed expression.");
125                                 }
126                         }
127
128                         string property = null;
129                         if (openIdx > 0) {
130                                 property = expr.Substring (0, openIdx);
131                                 if (property != null && property != String.Empty)
132                                         container = GetPropertyValue (container, property);
133                         }
134
135                         if (container == null)
136                                 return null;
137
138                         if (container is System.Collections.IList) {
139                                 IList l = (IList) container;
140                                 return l [intVal];
141                         }
142
143                         Type t = container.GetType ();
144                         // MS does not seem to look for any other than "Item"!!!
145                         object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
146                         if (atts.Length != 1)
147                                 throw new ArgumentException (expr + " indexer not found.");
148
149                         property = ((DefaultMemberAttribute) atts [0]).MemberName;
150
151                         Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
152                         PropertyInfo prop = t.GetProperty (property, argTypes);
153                         if (prop == null)
154                                 throw new ArgumentException (expr + " indexer not found.");
155
156                         object [] args = new object [1];
157                         if (is_string)
158                                 args [0] = val;
159                         else
160                                 args [0] = intVal;
161
162                         return prop.GetValue (container, args);
163                 }
164
165                 public static string GetIndexedPropertyValue (object container, string expr, string format)
166                 {
167                         object result = GetIndexedPropertyValue (container, expr);
168                         return FormatResult (result, format);
169                 }
170
171                 public static object GetPropertyValue (object container, string propName)
172                 {
173                         if (container == null)
174                                 throw new ArgumentNullException ("container");
175                         if (propName == null)
176                                 throw new ArgumentNullException ("propName");
177
178                         PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
179                         if (prop == null) {
180                                 throw new HttpException ("Property " + propName + " not found in " +
181                                                          container.GetType ());
182                         }
183
184                         return prop.GetValue (container);
185                 }
186
187                 public static string GetPropertyValue (object container, string propName, string format)
188                 {
189                         object result = GetPropertyValue (container, propName);
190                         return FormatResult (result, format);
191                 }
192
193                 #if NET_2_0
194                 public static object GetDataItem (object container, out bool foundDataItem)
195                 {       
196                         foundDataItem = false;
197                         if (container == null)                  
198                                 return null;
199                         
200                         if (container is IDataItemContainer) {
201                                 foundDataItem = true;
202                                 return ((IDataItemContainer)container).DataItem;
203                         }
204                         
205                         PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
206                         if (pi == null)
207                                 return null;
208                         
209                         foundDataItem = true;
210                         return pi.GetValue (container, null); 
211                 } 
212                 
213                 
214                 public static object GetDataItem (object container)
215                 {
216                         bool flag;
217                         return GetDataItem (container, out flag); 
218                 }
219                 #endif
220         }
221 }
222