[asp.net] Ignore JavaScript blocks enclosed in HTML comments
[mono.git] / mcs / class / System.Web / System.Web.Compilation / RouteUrlExpressionBuilder.cs
1 //
2 // RouteUrlExpressionBuilder.cs
3 //
4 // Authors:
5 //   Marek Habersack (mhabersack@novell.com)
6 //
7 // (C) 2010 Novell, Inc (http://novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.CodeDom;
33 using System.Web.Routing;
34 using System.Web.UI;
35
36 namespace System.Web.Compilation
37 {
38         [ExpressionEditor ("System.Web.UI.Design.RouteUrlExpressionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
39         [ExpressionPrefix ("Routes")]
40         public class RouteUrlExpressionBuilder : ExpressionBuilder
41         {
42                 static readonly char[] expressionSplitChars = { ',' };
43                 static readonly char[] keyValueSplitChars = { '=' };
44                 
45                 public override bool SupportsEvaluate { get { return true; } }
46                 
47                 public RouteUrlExpressionBuilder ()
48                 {
49                 }
50
51                 // This method is used only from within pages that aren't compiled
52                 public override object EvaluateExpression (object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
53                 {
54                         if (entry == null)
55                                 throw new NullReferenceException (".NET emulation (entry == null)");
56
57                         if (context == null)
58                                 throw new NullReferenceException (".NET emulation (context == null)");
59                         
60                         return GetRouteUrl (context.TemplateControl, entry.Expression);
61                 }
62
63                 public override CodeExpression GetCodeExpression (BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
64                 {
65                         if (entry == null)
66                                 throw new NullReferenceException (".NET emulation (entry == null)");
67                         
68                         var ret = new CodeMethodInvokeExpression ();
69                         ret.Method = new CodeMethodReferenceExpression (new CodeTypeReferenceExpression (typeof (RouteUrlExpressionBuilder)), "GetRouteUrl");
70
71                         CodeExpressionCollection parameters = ret.Parameters;
72                         parameters.Add (new CodeThisReferenceExpression ());
73                         parameters.Add (new CodePrimitiveExpression (entry.Expression));
74
75                         return ret;
76                 }
77
78                 public static string GetRouteUrl (Control control, string expression)
79                 {
80                         if (control == null)
81                                 throw new ArgumentNullException ("control");
82                         
83                         string routeName;
84                         var rvd = new RouteValueDictionary ();
85                         
86                         if (!TryParseRouteExpression (expression, rvd, out routeName))
87                                 throw new InvalidOperationException ("Invalid expression, RouteUrlExpressionBuilder expects a string with format: RouteName=route,Key1=Value1,Key2=Value2");
88
89                         return control.GetRouteUrl (routeName, rvd);
90                 }
91
92                 public static bool TryParseRouteExpression (string expression, RouteValueDictionary routeValues, out string routeName)
93                 {
94                         routeName = null;
95                         if (String.IsNullOrEmpty (expression))
96                                 return false;
97
98                         if (routeValues == null)
99                                 throw new NullReferenceException (".NET emulation (routeValues == null)");
100
101                         string[] parts = expression.Split (expressionSplitChars);
102                         foreach (string part in parts) {
103                                 string[] keyval = part.Split (keyValueSplitChars);
104                                 if (keyval.Length != 2)
105                                         return false;
106
107                                 string key = keyval [0].Trim ();
108                                 if (key == String.Empty)
109                                         return false;
110
111                                 if (String.Compare (key, "routename", StringComparison.OrdinalIgnoreCase) == 0) {
112                                         routeName = keyval [1].Trim ();
113                                         continue;
114                                 }
115                                 
116                                 if (routeValues.ContainsKey (key))
117                                         routeValues [key] = keyval [1].Trim ();
118                                 else
119                                         routeValues.Add (key, keyval [1].Trim ());
120                         }
121                         
122                         return true;
123                 }
124         }
125 }
126