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 using Directive
= NVelocity
.Runtime
.Directive
.Directive
;
16 using DirectiveManager
= NVelocity
.Runtime
.Directive
.DirectiveManager
;
18 namespace Castle
.MonoRail
.Framework
.Views
.NVelocity
21 using System
.Collections
;
22 using Castle
.MonoRail
.Framework
.Views
.NVelocity
.CustomDirectives
;
27 public class CustomDirectiveManager
: DirectiveManager
29 private static readonly String DirectiveSuffix
= "directive";
31 private Hashtable directives
= new Hashtable();
34 /// Initializes a new instance of the <see cref="CustomDirectiveManager"/> class.
36 public CustomDirectiveManager()
38 RegisterCustomDirectives();
41 public override bool Contains(string name
)
43 return directives
.Contains(name
) ? true : base.Contains(name
);
46 public override Directive
Create(string name
, Stack directiveStack
)
48 Type customDirective
= directives
[name
] as Type
;
50 if (customDirective
!= null)
54 if (typeof(AbstractComponentDirective
).IsAssignableFrom(customDirective
))
56 args
= new object[] { GetViewComponentFactory(), GetViewEngine() }
;
59 return Activator
.CreateInstance(customDirective
, args
) as Directive
;
63 return base.Create(name
, directiveStack
);
67 protected void RegisterCustomDirectives()
69 RegisterCustomDirective(typeof(BlockComponentDirective
));
70 RegisterCustomDirective(typeof(ComponentDirective
));
71 RegisterCustomDirective(typeof(CaptureForDirective
));
74 private void RegisterCustomDirective(Type type
)
76 if (!typeof(Directive
).IsAssignableFrom(type
))
78 throw new RailsException("{0} is not a subclass of directive", type
.FullName
);
81 String name
= type
.Name
.ToLower(System
.Globalization
.CultureInfo
.InvariantCulture
);
83 if (name
.EndsWith(DirectiveSuffix
))
85 name
= name
.Substring(0, name
.Length
- DirectiveSuffix
.Length
);
88 directives
.Add(name
, type
);
91 private IViewComponentFactory
GetViewComponentFactory()
93 IViewComponentFactory compFactory
= (IViewComponentFactory
)
94 MonoRailHttpHandler
.CurrentContext
.GetService(typeof(IViewComponentFactory
));
96 if (compFactory
== null)
98 throw new RailsException("NVelocityViewEngine: Could not obtain ViewComponentFactory instance");
104 private IViewEngine
GetViewEngine()
106 IViewEngine viewEngine
= (IViewEngine
)
107 MonoRailHttpHandler
.CurrentContext
.GetService(typeof(IViewEngine
));
109 if (viewEngine
== null)
111 throw new RailsException("NVelocityViewEngine: could not obtain ViewEngine instance");