2004-03-09 Juraj Skripsky <juraj@hotfeet.ch>
[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 using System;
12 using System.Collections;
13 using System.ComponentModel;
14 using System.Reflection;
15
16 namespace System.Web.UI {
17
18         public sealed class DataBinder
19         {
20                 public DataBinder ()
21                 {
22                 }
23
24                 private static string FormatResult (object result, string format)
25                 {
26                         if (result == null)
27                                 return String.Empty;
28
29                         if (format == null)
30                                 return result.ToString ();
31
32                         return String.Format (format, result);
33                 }
34                 
35                 public static object Eval (object container, string expression)
36                 {
37                         if (expression == null)
38                                 throw new ArgumentNullException ("expression");
39
40                         object current = container;
41
42                         while (current != null) {
43                                 int dot = expression.IndexOf ('.');
44                                 int size = (dot == -1) ? expression.Length : dot;
45                                 string prop = expression.Substring (0, size);
46                                 if (prop.IndexOf ('[') != -1)
47                                         current = GetIndexedPropertyValue (current, prop);
48                                 else
49                                         current = GetPropertyValue (current, prop);
50
51                                 if (dot == -1)
52                                         break;
53                                 
54                                 expression = expression.Substring (prop.Length + 1);
55                         }
56
57                         return current;
58                 }
59
60                 public static string Eval (object container, string expression, string format)
61                 {
62                         object result = Eval (container, expression);
63                         return FormatResult (result, format);
64                 }
65
66                 public static object GetIndexedPropertyValue (object container, string expr)
67                 {
68                         if (expr == null)
69                                 throw new ArgumentNullException ("expr");
70
71                         int openIdx = expr.IndexOf ('[');
72                         int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
73                         if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
74                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
75
76                         string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
77                         val = val.Trim ();
78                         if (val.Length == 0)
79                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
80
81                         bool is_string = false;
82                         // a quoted val means we have a string
83                         if ((val[0] == '\'' && val[val.Length - 1] == '\'') ||
84                                 (val[0] == '\"' && val[val.Length - 1] == '\"')) {
85                                 is_string = true;
86                                 val = val.Substring(1, val.Length - 2);
87                         } else {
88                                 // if all chars are digits, then we have a int
89                                 for(int i = 0; i < val.Length; i++)
90                                         if (!Char.IsDigit(val[i])) {
91                                                 is_string = true;
92                                                 break;
93                                         }
94                         }
95
96                         int intVal = 0;
97                         if (!is_string) {
98                                 try {
99                                         intVal = Int32.Parse (val);
100                                 } catch {
101                                         throw new ArgumentException (expr + " is not a valid indexed expression.");
102                                 }
103                         }
104
105                         string property = null;
106                         if (openIdx > 0) {
107                                 property = expr.Substring (0, openIdx);
108                                 if (property != null && property != String.Empty)
109                                         container = GetPropertyValue (container, property);
110                         }
111
112                         if (container == null)
113                                 return null;
114
115                         if (container is System.Collections.IList) {
116                                 IList l = (IList) container;
117                                 return l [intVal];
118                         }
119
120                         Type t = container.GetType ();
121                         // MS does not seem to look for any other than "Item"!!!
122                         object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
123                         if (atts.Length != 1)
124                                 throw new ArgumentException (expr + " indexer not found.");
125
126                         property = ((DefaultMemberAttribute) atts [0]).MemberName;
127
128                         Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
129                         PropertyInfo prop = t.GetProperty (property, argTypes);
130                         if (prop == null)
131                                 throw new ArgumentException (expr + " indexer not found.");
132
133                         object [] args = new object [1];
134                         if (is_string)
135                                 args [0] = val;
136                         else
137                                 args [0] = intVal;
138
139                         return prop.GetValue (container, args);
140                 }
141
142                 public static string GetIndexedPropertyValue (object container, string expr, string format)
143                 {
144                         object result = GetIndexedPropertyValue (container, expr);
145                         return FormatResult (result, format);
146                 }
147
148                 public static object GetPropertyValue (object container, string propName)
149                 {
150                         if (propName == null)
151                                 throw new ArgumentNullException ("propName");
152
153                         PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
154                         if (prop == null) {
155                                 throw new HttpException ("Property " + propName + " not found in " +
156                                                          container.GetType ());
157                         }
158
159                         return prop.GetValue (container);
160                 }
161
162                 public static string GetPropertyValue (object container, string propName, string format)
163                 {
164                         object result = GetPropertyValue (container, propName);
165                         return FormatResult (result, format);
166                 }
167
168                 #if NET_1_2
169                 public static object GetDataItem (object container, out bool foundDataItem)
170                 {       
171                         foundDataItem = false;
172                         if (container == null)                  
173                                 return null;
174                         
175                         PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
176                         if (pi == null)
177                                 return null;
178                         
179                         foundDataItem = true;
180                         return pi.GetValue (container, null); 
181                 } 
182                 
183                 
184                 public static object GetDataItem (object container)
185                 {
186                         bool flag;
187                         return GetDataItem (container, out flag); 
188                 }
189                 #endif
190         }
191 }
192