**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI / DataBinder.cs
blob1fad8bd542908ca620861206d1e9601b0387fd19
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 //
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.
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Reflection;
37 namespace System.Web.UI {
39 public sealed class DataBinder
41 public DataBinder ()
45 private static string FormatResult (object result, string format)
47 if (result == null)
48 return String.Empty;
50 if (format == null)
51 return result.ToString ();
53 return String.Format (format, result);
56 public static object Eval (object container, string expression)
58 if (expression == null)
59 throw new ArgumentNullException ("expression");
61 object current = container;
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);
72 if (dot == -1)
73 break;
75 expression = expression.Substring (prop.Length + 1);
78 return current;
81 public static string Eval (object container, string expression, string format)
83 object result = Eval (container, expression);
84 return FormatResult (result, format);
87 public static object GetIndexedPropertyValue (object container, string expr)
89 if (expr == null)
90 throw new ArgumentNullException ("expr");
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.");
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.");
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;
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.");
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);
133 if (container == null)
134 return null;
136 if (container is System.Collections.IList) {
137 IList l = (IList) container;
138 return l [intVal];
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.");
147 property = ((DefaultMemberAttribute) atts [0]).MemberName;
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.");
154 object [] args = new object [1];
155 if (is_string)
156 args [0] = val;
157 else
158 args [0] = intVal;
160 return prop.GetValue (container, args);
163 public static string GetIndexedPropertyValue (object container, string expr, string format)
165 object result = GetIndexedPropertyValue (container, expr);
166 return FormatResult (result, format);
169 public static object GetPropertyValue (object container, string propName)
171 if (propName == null)
172 throw new ArgumentNullException ("propName");
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 ());
180 return prop.GetValue (container);
183 public static string GetPropertyValue (object container, string propName, string format)
185 object result = GetPropertyValue (container, propName);
186 return FormatResult (result, format);
189 #if NET_2_0
190 public static object GetDataItem (object container, out bool foundDataItem)
192 foundDataItem = false;
193 if (container == null)
194 return null;
196 PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
197 if (pi == null)
198 return null;
200 foundDataItem = true;
201 return pi.GetValue (container, null);
205 public static object GetDataItem (object container)
207 bool flag;
208 return GetDataItem (container, out flag);
210 #endif