Minor style changes
[castle.git] / MonoRail / Castle.MonoRail.Framework / Routing / RegexRule.cs
blob8c30de6fa82e7a5554d4fa37fce3056edb553d69
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Routing
17 using System;
18 using System.Collections;
19 using System.Text.RegularExpressions;
21 /// <summary>
22 ///
23 /// </summary>
24 public class RegexRule : IRoutingRule
26 private readonly string routeName;
27 private readonly string action;
28 private readonly Type controllerType;
29 private readonly Regex regExp;
31 /// <summary>
32 /// Initializes a new instance of the <see cref="RegexRule"/> class.
33 /// </summary>
34 /// <param name="routeName">Name of the rule.</param>
35 /// <param name="regExp">The reg exp.</param>
36 /// <param name="controllerType">Type of the controller.</param>
37 /// <param name="action">The action.</param>
38 private RegexRule(string routeName, Regex regExp, Type controllerType, string action)
40 this.routeName = routeName;
41 this.regExp = regExp;
42 this.controllerType = controllerType;
43 this.action = action;
46 /// <summary>
47 /// Gets the name of the rule.
48 /// </summary>
49 /// <value>The name of the rule.</value>
50 public string RouteName
52 get { return routeName; }
55 /// <summary>
56 /// Gets the type of the controller.
57 /// </summary>
58 /// <value>The type of the controller.</value>
59 public Type ControllerType
61 get { return controllerType; }
64 /// <summary>
65 /// Gets the action.
66 /// </summary>
67 /// <value>The action.</value>
68 public string Action
70 get { return action; }
73 /// <summary>
74 /// Determines if the specified URL matches the
75 /// routing rule.
76 /// </summary>
77 /// <param name="hostname"></param>
78 /// <param name="virtualPath"></param>
79 /// <param name="url">The URL.</param>
80 /// <param name="match">The match.</param>
81 /// <returns></returns>
82 public bool Matches(string hostname, string virtualPath, string url, RouteMatch match)
84 Match regExpMatch = regExp.Match(url);
86 int index = 0;
88 foreach(Group group in regExpMatch.Groups)
90 if (!group.Success)
92 return false;
95 string name = regExp.GroupNameFromNumber(index++);
97 match.AddNamed(name, group.Value);
100 return regExpMatch.Success;
103 /// <summary>
104 /// Pendent
105 /// </summary>
106 /// <param name="hostname">The hostname.</param>
107 /// <param name="virtualPath">The virtual path.</param>
108 /// <param name="parameters">The parameters.</param>
109 /// <returns></returns>
110 public string CreateUrl(string hostname, string virtualPath, IDictionary parameters)
112 throw new NotImplementedException("CreateUrl not implemented for regexp routes");
115 /// <summary>
116 /// Builds the specified rule name.
117 /// </summary>
118 /// <param name="ruleName">Name of the rule.</param>
119 /// <param name="expression">The expression.</param>
120 /// <param name="controllerType">Type of the controller.</param>
121 /// <param name="action">The action.</param>
122 /// <returns></returns>
123 public static RegexRule Build(string ruleName, string expression, Type controllerType, string action)
125 if (string.IsNullOrEmpty(ruleName))
127 throw new ArgumentNullException("ruleName");
129 if (string.IsNullOrEmpty(expression))
131 throw new ArgumentNullException("expression");
133 if (string.IsNullOrEmpty(action))
135 throw new ArgumentNullException("action");
137 if (controllerType.IsAssignableFrom(typeof(Controller)))
139 throw new ArgumentException("The specified type does not inherit from the Controller class", "controllerType");
142 Regex regExp = new Regex(expression, RegexOptions.Compiled|RegexOptions.Singleline);
144 return new RegexRule(ruleName, regExp, controllerType, action);