Adding #if DOTNET35 for REST
[castle.git] / MonoRail / Castle.MonoRail.Views.Brail / DslWrapper.cs
blob4b924013b5bca2ac101f014559c99e6f17ee68a8
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 namespace Castle.MonoRail.Views.Brail
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Reflection;
21 using Boo.Lang;
23 public class DslProvider : IQuackFu
25 private readonly IDictionary<string, MethodInfo> extensionMethods = new Dictionary<string, MethodInfo>();
26 private readonly BrailBase view = null;
27 private IDslLanguageExtension currentExtension = null;
29 public DslProvider(BrailBase view)
31 this.view = view;
34 #region IQuackFu Members
36 public object QuackGet(string name, object[] parameters)
38 if (view.IsDefined(name))
40 return view.GetParameter(name);
43 switch(name)
45 case "ScriptDirectory":
46 return view.ScriptDirectory;
47 case "Flash":
48 return view.Flash;
49 case "OutputStream":
50 return view.OutputStream;
51 case "ChildOutput":
52 return view.ChildOutput;
53 case "context":
54 return view.context;
57 return null;
60 public object QuackInvoke(string name, params object[] args)
62 string methodName = CreateMethodKey(name, args);
63 if (extensionMethods.ContainsKey(methodName))
65 MethodInfo method = extensionMethods[methodName];
66 method.Invoke(currentExtension, args);
67 return null;
69 //didn't find it in the hard coded methods, so we want to raise the generic ones.
70 switch(args.Length)
72 case 0:
73 currentExtension.Tag(name);
74 break;
75 case 1:
76 currentExtension.Tag(name, (ICallable) args[0]);
77 break;
78 case 2:
79 currentExtension.Tag(name, (IDictionary) args[0], (ICallable) args[1]);
80 break;
82 return null;
85 public object QuackSet(string name, object[] parameters, object value)
87 throw new Exception("The method or operation is not implemented.");
90 #endregion
92 public void Register(IDslLanguageExtension dslExtension)
94 if (currentExtension == null)
96 foreach(MethodInfo method in dslExtension.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance))
98 if (method.DeclaringType == typeof(object))
99 continue;
100 string name = CreateMethodKey(method.Name, method.GetParameters());
101 extensionMethods.Add(name, method);
103 currentExtension = dslExtension;
107 private string CreateMethodKey(string methodName, object[] parameters)
109 return string.Format("{0}_{1}", methodName, parameters.GetLength(0));