Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DataBinder.cs « System.Web.UI « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ede3e8a8b81423581a8915054744d97e179c7acc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// System.Web.UI.DataBinder.cs
//
// Authors:
// 	Duncan Mak  (duncan@ximian.com)
// 	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2002 Ximian, Inc. (http://www.ximian.com)
//

using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;

namespace System.Web.UI {

	public sealed class DataBinder
	{
		public DataBinder ()
		{
		}

		private static string FormatResult (object result, string format)
		{
			if (result == null)
				return String.Empty;

			if (format == null)
				return result.ToString ();

			return String.Format (format, result);
		}
		
		public static object Eval (object container, string expression)
		{
			if (expression == null)
				throw new ArgumentNullException ("expression");

			object current = container;

			while (current != null) {
				int dot = expression.IndexOf ('.');
				int size = (dot == -1) ? expression.Length : dot;
				string prop = expression.Substring (0, size);
				if (prop.IndexOf ('[') != -1)
					current = GetIndexedPropertyValue (current, prop);
				else
					current = GetPropertyValue (current, prop);

				if (dot == -1)
					break;
				
				expression = expression.Substring (prop.Length + 1);
			}

			return current;
		}

		public static string Eval (object container, string expression, string format)
		{
			object result = Eval (container, expression);
			return FormatResult (result, format);
		}

		public static object GetIndexedPropertyValue (object container, string expr)
		{
			if (expr == null)
				throw new ArgumentNullException ("expr");

			int openIdx = expr.IndexOf ('[');
			int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
			if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
				throw new ArgumentException (expr + " is not a valid indexed expression.");

			string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
			val = val.Trim ();
			int valLength = val.Length;
			if (valLength == 0)
				throw new ArgumentException (expr + " is not a valid indexed expression.");

			int intVal = 0;
			bool is_string;
			char first = val [0];
			if (first >= '0' && first <= '9') {
				is_string = false;
				try {
					intVal = Int32.Parse (val);
				} catch {
					throw new ArgumentException (expr + " is not a valid indexed expression.");
				}

			} else if (first == '"' && val [valLength - 1] == '"') {
				is_string = true;
				val = val.Substring (0, val.Length - 1).Substring (1);
			} else {
				throw new ArgumentException (expr + " is not a valid indexed expression.");
			}

			string property = null;
			if (openIdx > 0) {
				property = expr.Substring (0, openIdx);
				if (property != null && property != String.Empty)
					container = GetPropertyValue (container, property);
			}

                        if (container == null)
                                return null;

			if (container is System.Collections.IList) {
				IList l = (IList) container;
				return l [intVal];
			}

			Type t = container.GetType ();
			// MS does not seem to look for any other than "Item"!!!
			object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
			if (atts.Length != 1)
				throw new ArgumentException (expr + " indexer not found.");

			property = ((DefaultMemberAttribute) atts [0]).MemberName;

			Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
			PropertyInfo prop = t.GetProperty (property, argTypes);
			if (prop == null)
				throw new ArgumentException (expr + " indexer not found.");

			object [] args = new object [1];
			if (is_string)
				args [0] = val;
			else
				args [0] = intVal;

			return prop.GetValue (container, args);
		}

		public static string GetIndexedPropertyValue (object container, string expr, string format)
		{
			object result = GetIndexedPropertyValue (container, expr);
			return FormatResult (result, format);
		}

		public static object GetPropertyValue (object container, string propName)
		{
			if (propName == null)
				throw new ArgumentNullException ("propName");

			PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
			if (prop == null) {
				throw new HttpException ("Property " + propName + " not found in " +
							 container.GetType ());
			}

			return prop.GetValue (container);
		}

		public static string GetPropertyValue (object container, string propName, string format)
		{
			object result = GetPropertyValue (container, propName);
			return FormatResult (result, format);
		}

		#if NET_1_2
		public static object GetDataItem (object container, out bool foundDataItem)
		{	
			foundDataItem = false;
			if (container == null)			
				return null;
			
			PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
			if (pi == null)
				return null;
			
			foundDataItem = true;
			return pi.GetValue (container, null); 
		} 
		
		
		public static object GetDataItem (object container)
		{
			bool flag;
			return GetDataItem (container, out flag); 
		}
		#endif
	}
}