2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 //
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.ComponentModel;
35 using System.Reflection;
36
37 namespace System.Web.UI {
38
39         public sealed class DataBinder
40         {
41                 public DataBinder ()
42                 {
43                 }
44
45                 private 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)
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 (expr == null)
90                                 throw new ArgumentNullException ("expr");
91
92                         int openIdx = expr.IndexOf ('[');
93                         int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
94                         if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
95                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
96
97                         string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
98                         val = val.Trim ();
99                         if (val.Length == 0)
100                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
101
102                         bool is_string = false;
103                         // a quoted val means we have a string
104                         if ((val[0] == '\'' && val[val.Length - 1] == '\'') ||
105                                 (val[0] == '\"' && val[val.Length - 1] == '\"')) {
106                                 is_string = true;
107                                 val = val.Substring(1, val.Length - 2);
108                         } else {
109                                 // if all chars are digits, then we have a int
110                                 for(int i = 0; i < val.Length; i++)
111                                         if (!Char.IsDigit(val[i])) {
112                                                 is_string = true;
113                                                 break;
114                                         }
115                         }
116
117                         int intVal = 0;
118                         if (!is_string) {
119                                 try {
120                                         intVal = Int32.Parse (val);
121                                 } catch {
122                                         throw new ArgumentException (expr + " is not a valid indexed expression.");
123                                 }
124                         }
125
126                         string property = null;
127                         if (openIdx > 0) {
128                                 property = expr.Substring (0, openIdx);
129                                 if (property != null && property != String.Empty)
130                                         container = GetPropertyValue (container, property);
131                         }
132
133                         if (container == null)
134                                 return null;
135
136                         if (container is System.Collections.IList) {
137                                 IList l = (IList) container;
138                                 return l [intVal];
139                         }
140
141                         Type t = container.GetType ();
142                         // MS does not seem to look for any other than "Item"!!!
143                         object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
144                         if (atts.Length != 1)
145                                 throw new ArgumentException (expr + " indexer not found.");
146
147                         property = ((DefaultMemberAttribute) atts [0]).MemberName;
148
149                         Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
150                         PropertyInfo prop = t.GetProperty (property, argTypes);
151                         if (prop == null)
152                                 throw new ArgumentException (expr + " indexer not found.");
153
154                         object [] args = new object [1];
155                         if (is_string)
156                                 args [0] = val;
157                         else
158                                 args [0] = intVal;
159
160                         return prop.GetValue (container, args);
161                 }
162
163                 public static string GetIndexedPropertyValue (object container, string expr, string format)
164                 {
165                         object result = GetIndexedPropertyValue (container, expr);
166                         return FormatResult (result, format);
167                 }
168
169                 public static object GetPropertyValue (object container, string propName)
170                 {
171                         if (propName == null)
172                                 throw new ArgumentNullException ("propName");
173
174                         PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
175                         if (prop == null) {
176                                 throw new HttpException ("Property " + propName + " not found in " +
177                                                          container.GetType ());
178                         }
179
180                         return prop.GetValue (container);
181                 }
182
183                 public static string GetPropertyValue (object container, string propName, string format)
184                 {
185                         object result = GetPropertyValue (container, propName);
186                         return FormatResult (result, format);
187                 }
188
189                 #if NET_2_0
190                 public static object GetDataItem (object container, out bool foundDataItem)
191                 {       
192                         foundDataItem = false;
193                         if (container == null)                  
194                                 return null;
195                         
196                         PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
197                         if (pi == null)
198                                 return null;
199                         
200                         foundDataItem = true;
201                         return pi.GetValue (container, null); 
202                 } 
203                 
204                 
205                 public static object GetDataItem (object container)
206                 {
207                         bool flag;
208                         return GetDataItem (container, out flag); 
209                 }
210                 #endif
211         }
212 }
213