- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / DslWrapper.cs
blobc4e343b4621a8cc6ed88c5733d8c073f3b5d9a19
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using Boo.Lang;
5 using System.Reflection;
6 using Castle.MonoRail.Framework;
8 namespace Castle.MonoRail.Views.Brail
10 public class DslProvider : IQuackFu
12 private readonly BrailBase view = null;
13 public DslProvider(BrailBase view)
15 this.view = view;
16 foreach (PropertyInfo prop in typeof(BrailBase).GetProperties(BindingFlags.Public | BindingFlags.Instance))
18 if (prop.Name == "Dsl" || prop.Name == "ViewEngine" || prop.Name == "Properties" || prop.CanRead == false)
20 continue;
23 viewProperties.Add(prop.Name, prop.GetGetMethod());
27 private IDslLanguageExtension currentExtension = null;
28 private readonly IDictionary<string, MethodInfo> extensionMethods = new Dictionary<string, MethodInfo>();
29 private readonly IDictionary<string, MethodInfo> viewProperties = new Dictionary<string, MethodInfo>();
31 public void Register(IDslLanguageExtension dslExtension)
33 if (currentExtension == null)
35 foreach (MethodInfo method in dslExtension.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance))
37 if(method.DeclaringType==typeof(object))
38 continue;
39 string name = CreateMethodKey(method.Name, method.GetParameters());
40 extensionMethods.Add(name, method);
42 currentExtension = dslExtension;
46 private string CreateMethodKey(string methodName, object[] parameters)
48 return string.Format("{0}_{1}", methodName, parameters.GetLength(0));
51 #region IQuackFu Members
53 public object QuackGet(string name, object[] parameters)
55 if (view.IsDefined(name))
57 return view.GetParameter(name);
60 if (viewProperties.ContainsKey(name))
62 return viewProperties[name].Invoke(view, null);
65 return null;
68 public object QuackInvoke(string name, params object[] args)
70 string methodName = CreateMethodKey(name, args);
71 if (extensionMethods.ContainsKey(methodName))
73 MethodInfo method = extensionMethods[methodName];
74 method.Invoke(currentExtension, args);
75 return null;
77 //didn't find it in the hard coded methods, so we want to raise the generic ones.
78 switch (args.Length)
80 case 0:
81 currentExtension.Tag(name);
82 break;
83 case 1:
84 currentExtension.Tag(name, (ICallable)args[0]);
85 break;
86 case 2:
87 currentExtension.Tag(name, (IDictionary)args[0],(ICallable)args[1]);
88 break;
90 return null;
93 public object QuackSet(string name, object[] parameters, object value)
95 throw new Exception("The method or operation is not implemented.");
98 #endregion