1 // Copyright 2004-2007 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
.Framework
.Internal
18 using Castle
.MonoRail
.Framework
.Helpers
;
21 /// Abstract class that contains the shared logic of JS Generation, separated from
22 /// the various view engines implementations
24 public abstract class JSGeneratorBase
27 /// The generator instance
29 protected readonly IJSGenerator generator
;
32 /// Initializes a new instance of the <see cref="JSGeneratorBase"/> class.
34 /// <param name="generator">The generator.</param>
35 protected JSGeneratorBase(IJSGenerator generator
)
37 this.generator
= generator
;
41 /// Executes an operation (totally late bound)
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
)
50 if (args
== null || args
.Length
!= 1)
52 throw new ArgumentException("el() method must be invoked with the element name as an argument");
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");
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();
93 /// Creates a null generator.
95 /// <returns></returns>
96 protected abstract object CreateNullGenerator();
99 /// Creates a JS collection generator.
101 /// <param name="collectionGenerator">The collection generator.</param>
102 /// <returns></returns>
103 protected abstract object CreateJSCollectionGenerator(IJSCollectionGenerator collectionGenerator
);
106 /// Creates a JS element generator.
108 /// <param name="elementGenerator">The element generator.</param>
109 /// <returns></returns>
110 protected abstract object CreateJSElementGenerator(IJSElementGenerator elementGenerator
);
113 /// Delegates to the generator
116 /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
118 public override string ToString()
120 return generator
.ToString();