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
.Views
.Brail
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
20 using System
.Reflection
;
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
)
34 #region IQuackFu Members
36 public object QuackGet(string name
, object[] parameters
)
38 if (view
.IsDefined(name
))
40 return view
.GetParameter(name
);
45 case "ScriptDirectory":
46 return view
.ScriptDirectory
;
50 return view
.OutputStream
;
52 return view
.ChildOutput
;
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
);
69 //didn't find it in the hard coded methods, so we want to raise the generic ones.
73 currentExtension
.Tag(name
);
76 currentExtension
.Tag(name
, (ICallable
) args
[0]);
79 currentExtension
.Tag(name
, (IDictionary
) args
[0], (ICallable
) args
[1]);
85 public object QuackSet(string name
, object[] parameters
, object value)
87 throw new Exception("The method or operation is not implemented.");
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))
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));