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
.Collections
.Generic
;
24 public class RoutingEngine
: IRoutingEngine
26 private readonly IList rules
= ArrayList
.Synchronized(new ArrayList());
27 private readonly Dictionary
<string,IRoutingRule
> name2Rule
= new Dictionary
<string,IRoutingRule
>();
30 /// Adds the specified rule.
32 /// <param name="rule">The rule.</param>
33 public void Add(IRoutingRule rule
)
37 // For really fast access
38 name2Rule
[rule
.RouteName
] = rule
;
44 /// <param name="routeName">Name of the route.</param>
45 /// <param name="hostname">The hostname.</param>
46 /// <param name="virtualPath">The virtual path.</param>
47 /// <param name="parameters">The parameters.</param>
48 /// <returns></returns>
49 public string CreateUrl(string routeName
, string hostname
, string virtualPath
, IDictionary parameters
)
53 if (!name2Rule
.TryGetValue(routeName
, out rule
))
55 throw new RailsException("Could not find named route: " + routeName
);
58 return rule
.CreateUrl(hostname
, virtualPath
, parameters
);
64 /// <param name="hostname"></param>
65 /// <param name="virtualPath"></param>
66 /// <param name="url">The URL.</param>
67 /// <returns></returns>
68 public RouteMatch
FindMatch(string hostname
, string virtualPath
, string url
)
70 if (string.IsNullOrEmpty(url
))
72 throw new ArgumentNullException("url", "url cannot be empty nor null");
75 if (url
[url
.Length
- 1] == '/')
77 url
= url
.Substring(0, url
.Length
- 1);
82 url
= url
.Substring(1);
85 foreach(IRoutingRule rule
in rules
)
87 RouteMatch match
= new RouteMatch(rule
.ControllerType
, rule
.RouteName
, rule
.Action
);
89 if (rule
.Matches(hostname
, virtualPath
, url
, match
))