Fix the build.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Internal / JSGeneratorBase.cs
blob29fc8218bbbef1fc1e272f32b25be78e646392fa
1 // Copyright 2004-2007 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.Internal
17 using System;
18 using Castle.MonoRail.Framework.Helpers;
20 /// <summary>
21 /// Abstract class that contains the shared logic of JS Generation, separated from
22 /// the various view engines implementations
23 /// </summary>
24 public abstract class JSGeneratorBase
26 /// <summary>
27 /// The generator instance
28 /// </summary>
29 protected readonly IJSGenerator generator;
31 /// <summary>
32 /// Initializes a new instance of the <see cref="JSGeneratorBase"/> class.
33 /// </summary>
34 /// <param name="generator">The generator.</param>
35 protected JSGeneratorBase(IJSGenerator generator)
37 this.generator = generator;
40 /// <summary>
41 /// Executes an operation (totally late bound)
42 /// </summary>
43 /// <param name="method">The method.</param>
44 /// <param name="args">The args.</param>
45 /// <returns></returns>
46 protected object InternalInvoke(string method, params object[] args)
48 if (method == "el")
50 if (args == null || args.Length != 1)
52 throw new ArgumentException("el() method must be invoked with the element name as an argument");
54 if (args[0] == null)
56 throw new ArgumentNullException("el() method invoked with a null argument");
59 return CreateJSElementGenerator(
60 generator.CreateElementGenerator(args[0].ToString()));
62 else if (method == "select")
64 if (args == null || args.Length != 1)
66 throw new ArgumentException(
67 "select() method must be invoked with the element/css selection rule name as an argument");
69 if (args[0] == null)
71 throw new ArgumentNullException("select() method invoked with a null argument");
74 return CreateJSCollectionGenerator(
75 generator.CreateCollectionGenerator(args[0].ToString()));
78 DynamicDispatchSupport dispInterface = generator as DynamicDispatchSupport;
79 if (dispInterface == null)
81 throw new MonoRail.Framework.RailsException("JS Generators must inherit DynamicDispatchSupport");
84 if (dispInterface.IsGeneratorMethod(method))
86 return dispInterface.Dispatch(method, args);
89 return CreateNullGenerator();
92 /// <summary>
93 /// Creates a null generator.
94 /// </summary>
95 /// <returns></returns>
96 protected abstract object CreateNullGenerator();
98 /// <summary>
99 /// Creates a JS collection generator.
100 /// </summary>
101 /// <param name="collectionGenerator">The collection generator.</param>
102 /// <returns></returns>
103 protected abstract object CreateJSCollectionGenerator(IJSCollectionGenerator collectionGenerator);
105 /// <summary>
106 /// Creates a JS element generator.
107 /// </summary>
108 /// <param name="elementGenerator">The element generator.</param>
109 /// <returns></returns>
110 protected abstract object CreateJSElementGenerator(IJSElementGenerator elementGenerator);
112 /// <summary>
113 /// Delegates to the generator
114 /// </summary>
115 /// <returns>
116 /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
117 /// </returns>
118 public override string ToString()
120 return generator.ToString();