Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mcs / class / System.Web.Mvc3 / Mvc / ExpressionHelper.cs
1 namespace System.Web.Mvc {
2     using System;
3     using System.Collections.Generic;
4     using System.Globalization;
5     using System.Linq;
6     using System.Linq.Expressions;
7     using System.Reflection;
8     using System.Web.Mvc.Resources;
9
10     public static class ExpressionHelper {
11         public static string GetExpressionText(string expression) {
12             return
13                 String.Equals(expression, "model", StringComparison.OrdinalIgnoreCase)
14                     ? String.Empty    // If it's exactly "model", then give them an empty string, to replicate the lambda behavior
15                     : expression;
16         }
17
18         public static string GetExpressionText(LambdaExpression expression) {
19             // Split apart the expression string for property/field accessors to create its name
20             Stack<string> nameParts = new Stack<string>();
21             Expression part = expression.Body;
22
23             while (part != null) {
24                 if (part.NodeType == ExpressionType.Call) {
25                     MethodCallExpression methodExpression = (MethodCallExpression)part;
26
27                     if (!IsSingleArgumentIndexer(methodExpression)) {
28                         break;
29                     }
30
31                     nameParts.Push(
32                         GetIndexerInvocation(
33                             methodExpression.Arguments.Single(),
34                             expression.Parameters.ToArray()
35                         )
36                     );
37
38                     part = methodExpression.Object;
39                 }
40                 else if (part.NodeType == ExpressionType.ArrayIndex) {
41                     BinaryExpression binaryExpression = (BinaryExpression)part;
42
43                     nameParts.Push(
44                         GetIndexerInvocation(
45                             binaryExpression.Right,
46                             expression.Parameters.ToArray()
47                         )
48                     );
49
50                     part = binaryExpression.Left;
51                 }
52                 else if (part.NodeType == ExpressionType.MemberAccess) {
53                     MemberExpression memberExpressionPart = (MemberExpression)part;
54                     nameParts.Push("." + memberExpressionPart.Member.Name);
55                     part = memberExpressionPart.Expression;
56                 }
57                 else if (part.NodeType == ExpressionType.Parameter) {
58                     // Dev10 Bug #907611
59                     // When the expression is parameter based (m => m.Something...), we'll push an empty
60                     // string onto the stack and stop evaluating. The extra empty string makes sure that
61                     // we don't accidentally cut off too much of m => m.Model.
62                     nameParts.Push(String.Empty);
63                     part = null;
64                 }
65                 else {
66                     break;
67                 }
68             }
69
70             // If it starts with "model", then strip that away
71             if (nameParts.Count > 0 && String.Equals(nameParts.Peek(), ".model", StringComparison.OrdinalIgnoreCase)) {
72                 nameParts.Pop();
73             }
74
75             if (nameParts.Count > 0) {
76                 return nameParts.Aggregate((left, right) => left + right).TrimStart('.');
77             }
78
79             return String.Empty;
80         }
81
82         private static string GetIndexerInvocation(Expression expression, ParameterExpression[] parameters) {
83             Expression converted = Expression.Convert(expression, typeof(object));
84             ParameterExpression fakeParameter = Expression.Parameter(typeof(object), null);
85             Expression<Func<object, object>> lambda = Expression.Lambda<Func<object, object>>(converted, fakeParameter);
86             Func<object, object> func;
87
88             try {
89                 func = ExpressionUtil.CachedExpressionCompiler.Process(lambda);
90             }
91             catch (InvalidOperationException ex) {
92                 throw new InvalidOperationException(
93                     String.Format(
94                         CultureInfo.CurrentCulture,
95                         MvcResources.ExpressionHelper_InvalidIndexerExpression,
96                         expression,
97                         parameters[0].Name
98                     ),
99                     ex
100                 );
101             }
102
103             return "[" + Convert.ToString(func(null), CultureInfo.InvariantCulture) + "]";
104         }
105
106         internal static bool IsSingleArgumentIndexer(Expression expression) {
107             MethodCallExpression methodExpression = expression as MethodCallExpression;
108             if (methodExpression == null || methodExpression.Arguments.Count != 1) {
109                 return false;
110             }
111
112             return methodExpression.Method
113                                    .DeclaringType
114                                    .GetDefaultMembers()
115                                    .OfType<PropertyInfo>()
116                                    .Any(p => p.GetGetMethod() == methodExpression.Method);
117         }
118     }
119 }