Fix the build.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Routing / RoutingEngine.cs
blob393040784a1e8bb721f72203a98bdc0764ebc6fd
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.Collections.Generic;
21 /// <summary>
22 /// Pendent
23 /// </summary>
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>();
29 /// <summary>
30 /// Adds the specified rule.
31 /// </summary>
32 /// <param name="rule">The rule.</param>
33 public void Add(IRoutingRule rule)
35 rules.Add(rule);
37 // For really fast access
38 name2Rule[rule.RouteName] = rule;
41 /// <summary>
42 /// Creates the URL.
43 /// </summary>
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)
51 IRoutingRule rule;
53 if (!name2Rule.TryGetValue(routeName, out rule))
55 throw new RailsException("Could not find named route: " + routeName);
58 return rule.CreateUrl(hostname, virtualPath, parameters);
61 /// <summary>
62 /// Finds the match.
63 /// </summary>
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);
80 if (url[0] == '/')
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))
91 return match;
95 return null;