2002-10-29 Gonzalo Paniagua Javier <gonzalo@ximian.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Tue, 29 Oct 2002 20:45:43 +0000 (20:45 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Tue, 29 Oct 2002 20:45:43 +0000 (20:45 -0000)
* DataBinder.cs: implemented Eval and GetIndexedPropertyValue methods.

svn path=/trunk/mcs/; revision=8666

mcs/class/System.Web/System.Web.UI/ChangeLog
mcs/class/System.Web/System.Web.UI/DataBinder.cs

index 312a22e9460010a15296e424574237db7ae7262b..8fec58e3b95ac580f0c2928f5b08f6e5e56c8534 100644 (file)
@@ -1,3 +1,7 @@
+2002-10-29  Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+       * DataBinder.cs: implemented Eval and GetIndexedPropertyValue methods.
+
 2002-10-27  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
        * LosFormatter.cs: Use WebEncoding.Encoding.
index 37bb71adf517ed34c484f953d27d71735d8b94ca..a5ab521c5d235ffff31a9ae5d4d16af19df0f6ef 100755 (executable)
@@ -19,26 +19,121 @@ namespace System.Web.UI {
                {
                }
 
+               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)
                {
-                       return GetPropertyValue (container, expression);
+                       if (container == null)
+                               throw new ArgumentNullException ("container");
+
+                       if (expression == null)
+                               throw new ArgumentNullException ("expression");
+
+                       object current = container;
+
+                       while (true) {
+                               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)
                {
-                       return GetPropertyValue (container, expression, format);
+                       object result = Eval (container, expression);
+                       return FormatResult (result, format);
                }
 
-               [MonoTODO]
                public static object GetIndexedPropertyValue (object container, string expr)
                {
-                       throw new NotImplementedException ();
+                       if (container == null)
+                               throw new ArgumentNullException ("container");
+
+                       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 = DataBinder.GetPropertyValue (container, property);
+                       }
+
+                       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);
                }
 
-               [MonoTODO]
                public static string GetIndexedPropertyValue (object container, string expr, string format)
                {
-                       throw new NotImplementedException ();
+                       object result = GetIndexedPropertyValue (container, expr);
+                       return FormatResult (result, format);
                }
 
                public static object GetPropertyValue (object container, string propName)
@@ -61,16 +156,8 @@ namespace System.Web.UI {
 
                public static string GetPropertyValue (object container, string propName, string format)
                {
-                       object result;
-
-                       result = GetPropertyValue (container, propName);
-                       if (result == null)
-                               return String.Empty;
-
-                       if (format == null)
-                               return result.ToString ();
-
-                       return String.Format (format, result);
+                       object result = GetPropertyValue (container, propName);
+                       return FormatResult (result, format);
                }               
        }
 }