Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / JSGeneration / DynamicDispatching / JSGeneratorDispatcherBase.cs
blob65a7aafff92b63c1bf1d4f018db6e6e0cfc38331
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.Framework.JSGeneration.DynamicDispatching
17 using System;
19 /// <summary>
20 /// Abstract class that contains the shared logic of JS Generation, separated from
21 /// the various view engines implementations
22 /// </summary>
23 public abstract class JSGeneratorDispatcherBase : DynamicDispatcher
25 private readonly IJSCodeGenerator codeGen;
26 private readonly IJSGenerator generator;
27 private readonly object[] elementExtensions;
29 /// <summary>
30 /// Initializes a new instance of the <see cref="JSGeneratorDispatcherBase"/> class.
31 /// </summary>
32 /// <param name="codeGen">The code gen.</param>
33 /// <param name="generator">The generator.</param>
34 /// <param name="extensions">The extensions.</param>
35 /// <param name="elementExtensions">The element extensions.</param>
36 protected JSGeneratorDispatcherBase(IJSCodeGenerator codeGen, IJSGenerator generator, object[] extensions, object[] elementExtensions)
37 : base(generator, extensions)
39 this.codeGen = codeGen;
40 this.generator = generator;
41 this.elementExtensions = elementExtensions;
44 /// <summary>
45 /// Gets the javascript code generator.
46 /// </summary>
47 /// <value>The code gen.</value>
48 public IJSCodeGenerator CodeGen
50 get { return codeGen; }
53 /// <summary>
54 /// Executes an operation (totally late bound)
55 /// </summary>
56 /// <param name="method">The method.</param>
57 /// <param name="args">The args.</param>
58 /// <returns></returns>
59 protected object InternalInvoke(string method, params object[] args)
61 if (method == "el")
63 AssertValidElementAccessor(args);
65 string root = args[0].ToString();
67 return CreateJSElementGeneratorProxy(codeGen, generator.CreateElementGenerator(root), elementExtensions);
70 if (HasMethod(method))
72 return Dispatch(method, args);
75 return CreateNullGenerator();
78 /// <summary>
79 /// Creates a null generator.
80 /// </summary>
81 /// <returns></returns>
82 protected abstract object CreateNullGenerator();
84 /// <summary>
85 /// Creates a JS element generator.
86 /// </summary>
87 /// <param name="codeGen">The code gen.</param>
88 /// <param name="elementGenerator">The element generator.</param>
89 /// <param name="elementExtensions">The element extensions.</param>
90 /// <returns></returns>
91 protected abstract object CreateJSElementGeneratorProxy(IJSCodeGenerator codeGen, IJSElementGenerator elementGenerator, object[] elementExtensions);
93 private static void AssertValidElementAccessor(object[] args)
95 if (args == null || args.Length != 1)
97 throw new ArgumentException("el() method must be invoked with the element name as an argument");
99 if (args[0] == null)
101 throw new ArgumentException("el() method invoked with a null argument");