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