Update Castle.MicroKernel-vs2005.csproj with missing files.
[castle.git] / MonoRail / Castle.MonoRail.Framework.Views.NVelocity / CustomDirectiveManager.cs
bloba7464b3460b106eca77edbfd323c03104be6dfcc
1 // Copyright 2004-2008 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 using Directive = NVelocity.Runtime.Directive.Directive;
16 using DirectiveManager = NVelocity.Runtime.Directive.DirectiveManager;
18 namespace Castle.MonoRail.Framework.Views.NVelocity
20 using System;
21 using System.Collections;
22 using Castle.MonoRail.Framework.Views.NVelocity.CustomDirectives;
24 /// <summary>
25 /// Pendent
26 /// </summary>
27 public class CustomDirectiveManager : DirectiveManager
29 private static readonly String DirectiveSuffix = "directive";
31 private Hashtable directives = new Hashtable();
33 /// <summary>
34 /// Initializes a new instance of the <see cref="CustomDirectiveManager"/> class.
35 /// </summary>
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)
52 object[] args = null;
54 if (typeof(AbstractComponentDirective).IsAssignableFrom(customDirective))
56 args = new object[] { GetViewComponentFactory(), GetViewEngine() };
59 return Activator.CreateInstance(customDirective, args) as Directive;
61 else
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 MonoRailException("{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 MonoRailHttpHandlerFactory.CurrentEngineContext.GetService(typeof(IViewComponentFactory));
96 if (compFactory == null)
98 throw new MonoRailException("NVelocityViewEngine: Could not obtain ViewComponentFactory instance");
101 return compFactory;
104 private IViewEngine GetViewEngine()
106 IViewEngine viewEngine = (IViewEngine)
107 MonoRailHttpHandlerFactory.CurrentEngineContext.GetService(typeof(IViewEngine));
109 if (viewEngine == null)
111 throw new MonoRailException("NVelocityViewEngine: could not obtain ViewEngine instance");
114 return viewEngine;