2002-12-15 Gonzalo Paniagua Javier <gonzalo@ximian.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 //
10
11 using System;
12 using System.Reflection;
13
14 namespace System.Web.UI {
15
16         public sealed class DataBinder
17         {
18                 public DataBinder ()
19                 {
20                 }
21
22                 private static string FormatResult (object result, string format)
23                 {
24                         if (result == null)
25                                 return String.Empty;
26
27                         if (format == null)
28                                 return result.ToString ();
29
30                         return String.Format (format, result);
31                 }
32                 
33                 public static object Eval (object container, string expression)
34                 {
35                         if (container == null)
36                                 throw new ArgumentNullException ("container");
37
38                         if (expression == null)
39                                 throw new ArgumentNullException ("expression");
40
41                         object current = container;
42
43                         while (true) {
44                                 int dot = expression.IndexOf ('.');
45                                 int size = (dot == -1) ? expression.Length : dot;
46                                 string prop = expression.Substring (0, size);
47                                 if (prop.IndexOf ('[') != -1)
48                                         current = GetIndexedPropertyValue (current, prop);
49                                 else
50                                         current = GetPropertyValue (current, prop);
51
52                                 if (dot == -1)
53                                         break;
54                                 
55                                 expression = expression.Substring (prop.Length + 1);
56                         }
57
58                         return current;
59                 }
60
61                 public static string Eval (object container, string expression, string format)
62                 {
63                         object result = Eval (container, expression);
64                         return FormatResult (result, format);
65                 }
66
67                 public static object GetIndexedPropertyValue (object container, string expr)
68                 {
69                         if (container == null)
70                                 throw new ArgumentNullException ("container");
71
72                         if (expr == null)
73                                 throw new ArgumentNullException ("expr");
74
75                         int openIdx = expr.IndexOf ('[');
76                         int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
77                         if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
78                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
79
80                         string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
81                         val = val.Trim ();
82                         int valLength = val.Length;
83                         if (valLength == 0)
84                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
85
86                         int intVal = 0;
87                         bool is_string;
88                         char first = val [0];
89                         if (first >= '0' && first <= '9') {
90                                 is_string = false;
91                                 try {
92                                         intVal = Int32.Parse (val);
93                                 } catch {
94                                         throw new ArgumentException (expr + " is not a valid indexed expression.");
95                                 }
96                                 
97                         } else if (first == '"' && val [valLength - 1] == '"') {
98                                 is_string = true;
99                                 val = val.Substring (0, val.Length - 1).Substring (1);
100                         } else {
101                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
102                         }
103
104                         string property = null;
105                         if (openIdx > 0) {
106                                 property = expr.Substring (0, openIdx);
107                                 if (property != null && property != String.Empty)
108                                         container = DataBinder.GetPropertyValue (container, property);
109                         }
110
111                         Type t = container.GetType ();
112                         // MS does not seem to look for any other than "Item"!!!
113                         object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
114                         if (atts.Length != 1)
115                                 throw new ArgumentException (expr + " indexer not found.");
116                                 
117                         property = ((DefaultMemberAttribute) atts [0]).MemberName;
118
119                         Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
120                         PropertyInfo prop = t.GetProperty (property, argTypes);
121                         if (prop == null)
122                                 throw new ArgumentException (expr + " indexer not found.");
123
124                         object [] args = new object [1];
125                         if (is_string)
126                                 args [0] = val;
127                         else
128                                 args [0] = intVal;
129
130                         return prop.GetValue (container, args);
131                 }
132
133                 public static string GetIndexedPropertyValue (object container, string expr, string format)
134                 {
135                         object result = GetIndexedPropertyValue (container, expr);
136                         return FormatResult (result, format);
137                 }
138
139                 public static object GetPropertyValue (object container, string propName)
140                 {
141                         if (container == null || propName == null)
142                                 throw new ArgumentException ();
143
144                         Type type = container.GetType ();
145                         PropertyInfo prop = type.GetProperty (propName);
146                         if (prop == null) {
147                                 try {
148                                         return GetIndexedPropertyValue (container, "[\"" + propName + "\"]");
149                                 } catch {
150                                         throw new HttpException ("Property " + propName + " not found in " +
151                                                                  type.ToString ());
152                                 }
153                         }
154
155
156                         MethodInfo getm = prop.GetGetMethod ();
157                         if (getm == null)
158                                 throw new HttpException ("Cannot find get accessor for " + propName +
159                                                          " in " + type.ToString ());
160                         
161                         return getm.Invoke (container, null);
162                 }
163
164                 public static string GetPropertyValue (object container, string propName, string format)
165                 {
166                         object result = GetPropertyValue (container, propName);
167                         return FormatResult (result, format);
168                 }               
169         }
170 }
171