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