* TagAttribute.cs: attributes can be stored as encoded html so we
[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                         int valLength = val.Length;
79                         if (valLength == 0)
80                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
81
82                         int intVal = 0;
83                         bool is_string;
84                         char first = val [0];
85                         if (first >= '0' && first <= '9') {
86                                 is_string = false;
87                                 try {
88                                         intVal = Int32.Parse (val);
89                                 } catch {
90                                         throw new ArgumentException (expr + " is not a valid indexed expression.");
91                                 }
92
93                         } else if (first == '"' && val [valLength - 1] == '"') {
94                                 is_string = true;
95                                 val = val.Substring (0, val.Length - 1).Substring (1);
96                         } else {
97                                 throw new ArgumentException (expr + " is not a valid indexed expression.");
98                         }
99
100                         string property = null;
101                         if (openIdx > 0) {
102                                 property = expr.Substring (0, openIdx);
103                                 if (property != null && property != String.Empty)
104                                         container = GetPropertyValue (container, property);
105                         }
106
107                         if (container == null)
108                                 return null;
109
110                         if (container is System.Collections.IList) {
111                                 IList l = (IList) container;
112                                 return l [intVal];
113                         }
114
115                         Type t = container.GetType ();
116                         // MS does not seem to look for any other than "Item"!!!
117                         object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
118                         if (atts.Length != 1)
119                                 throw new ArgumentException (expr + " indexer not found.");
120
121                         property = ((DefaultMemberAttribute) atts [0]).MemberName;
122
123                         Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
124                         PropertyInfo prop = t.GetProperty (property, argTypes);
125                         if (prop == null)
126                                 throw new ArgumentException (expr + " indexer not found.");
127
128                         object [] args = new object [1];
129                         if (is_string)
130                                 args [0] = val;
131                         else
132                                 args [0] = intVal;
133
134                         return prop.GetValue (container, args);
135                 }
136
137                 public static string GetIndexedPropertyValue (object container, string expr, string format)
138                 {
139                         object result = GetIndexedPropertyValue (container, expr);
140                         return FormatResult (result, format);
141                 }
142
143                 public static object GetPropertyValue (object container, string propName)
144                 {
145                         if (propName == null)
146                                 throw new ArgumentNullException ("propName");
147
148                         PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
149                         if (prop == null) {
150                                 throw new HttpException ("Property " + propName + " not found in " +
151                                                          container.GetType ());
152                         }
153
154                         return prop.GetValue (container);
155                 }
156
157                 public static string GetPropertyValue (object container, string propName, string format)
158                 {
159                         object result = GetPropertyValue (container, propName);
160                         return FormatResult (result, format);
161                 }
162
163                 #if NET_1_2
164                 public static object GetDataItem (object container, out bool foundDataItem)
165                 {       
166                         foundDataItem = false;
167                         if (container == null)                  
168                                 return null;
169                         
170                         PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
171                         if (pi == null)
172                                 return null;
173                         
174                         foundDataItem = true;
175                         return pi.GetValue (container, null); 
176                 } 
177                 
178                 
179                 public static object GetDataItem (object container)
180                 {
181                         bool flag;
182                         return GetDataItem (container, out flag); 
183                 }
184                 #endif
185         }
186 }
187