1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using System
.Text
.RegularExpressions
;
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
;
32 /// Initializes a new instance of the <see cref="RegexRule"/> class.
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
;
42 this.controllerType
= controllerType
;
47 /// Gets the name of the rule.
49 /// <value>The name of the rule.</value>
50 public string RouteName
52 get { return routeName; }
56 /// Gets the type of the controller.
58 /// <value>The type of the controller.</value>
59 public Type ControllerType
61 get { return controllerType; }
67 /// <value>The action.</value>
70 get { return action; }
74 /// Determines if the specified URL matches the
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
);
88 foreach(Group
group in regExpMatch
.Groups
)
95 string name
= regExp
.GroupNameFromNumber(index
++);
97 match
.AddNamed(name
, group.Value
);
100 return regExpMatch
.Success
;
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");
116 /// Builds the specified rule name.
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
);