1 // Copyright 2004-2008 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
.Services
.Utils
18 using Castle
.MonoRail
.Framework
.Descriptors
;
21 /// Utilities methods to inspect the controller Type
22 /// and gathers its name and area.
24 public static class ControllerInspectionUtil
27 /// Creates a <see cref="ControllerDescriptor"/> based on the conventions
28 /// and possible attributes found for the Controller Type specified
30 /// <param name="controllerType">The controller type</param>
31 /// <returns>A controller descriptor</returns>
32 public static ControllerDescriptor
Inspect(Type controllerType
)
34 ControllerDescriptor descriptor
;
36 if (controllerType
.IsDefined(typeof(ControllerDetailsAttribute
), true))
38 object[] attrs
= controllerType
.GetCustomAttributes(
39 typeof(ControllerDetailsAttribute
), true);
41 ControllerDetailsAttribute details
= (ControllerDetailsAttribute
) attrs
[0];
43 descriptor
= new ControllerDescriptor(controllerType
,
44 ObtainControllerName(details
.Name
, controllerType
),
45 details
.Area
, details
.Sessionless
);
49 descriptor
= new ControllerDescriptor(controllerType
,
50 ObtainControllerName(null, controllerType
), String
.Empty
, false);
57 /// Obtains the name of the controller.
59 /// <param name="name">The name.</param>
60 /// <param name="controller">The controller.</param>
61 /// <returns></returns>
62 private static String
ObtainControllerName(String name
, Type controller
)
64 if (name
== null || name
.Length
== 0)
66 return Strip(controller
.Name
);
72 /// Strips the specified name.
74 /// <param name="name">The name.</param>
75 /// <returns></returns>
76 private static String
Strip(String name
)
78 if (name
.EndsWith("Controller"))
80 return name
.Substring(0, name
.IndexOf("Controller"));