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 using Directive
= NVelocity
.Runtime
.Directive
.Directive
;
16 using IInternalContextAdapter
= NVelocity
.Context
.IInternalContextAdapter
;
17 using INode
= NVelocity
.Runtime
.Parser
.Node
.INode
;
18 using InternalContextAdapter
= NVelocity
.Context
.IInternalContextAdapter
;
20 namespace Castle
.MonoRail
.Framework
.Views
.NVelocity
23 using System
.Collections
;
24 using System
.Collections
.Specialized
;
27 using Castle
.MonoRail
.Framework
.Views
.NVelocity
.CustomDirectives
;
30 /// <see cref="IViewComponentContext"/>'s implementation for
33 public class NVelocityViewContextAdapter
: IViewComponentContext
35 private readonly String componentName
;
36 private readonly INode parentNode
;
37 private readonly IViewEngine viewEngine
;
38 private readonly IViewRenderer renderer
;
40 private String viewToRender
;
41 private TextWriter writer
;
42 private INode bodyNode
;
43 private IDictionary sections
;
44 private IDictionary componentParams
;
45 private InternalContextAdapter context
;
48 /// Initializes a new instance of the <see cref="NVelocityViewContextAdapter"/> class.
50 /// <param name="componentName">Name of the component.</param>
51 /// <param name="parentNode">The parent node.</param>
52 /// <param name="viewEngine">The view engine.</param>
53 /// <param name="renderer">The view renderer.</param>
54 public NVelocityViewContextAdapter(String componentName
, INode parentNode
, IViewEngine viewEngine
, IViewRenderer renderer
)
56 this.componentName
= componentName
;
57 this.parentNode
= parentNode
;
58 this.viewEngine
= viewEngine
;
59 this.renderer
= renderer
;
62 #region IViewComponentContext
65 /// Gets the name of the component.
67 /// <value>The name of the component.</value>
68 public String ComponentName
70 get { return componentName; }
74 /// Gets the dictionary that holds variables for the
75 /// view and for the view component
77 /// <value>The context vars.</value>
78 public IDictionary ContextVars
80 get { return context; }
84 /// Gets the component parameters that the view has passed
87 /// <value>The component parameters.</value>
88 public IDictionary ComponentParameters
90 get { return componentParams; }
94 /// Gets or sets the view to render.
96 /// <value>The view to render.</value>
97 public String ViewToRender
99 get { return viewToRender; }
100 set { viewToRender = value; }
104 /// Gets the writer used to render the component
106 /// <value>The writer.</value>
107 public TextWriter Writer
109 get { return writer; }
113 /// Determines whether the current component declaration on the view
114 /// has the specified section.
116 /// <param name="sectionName">Name of the section.</param>
118 /// <c>true</c> if the specified section exists; otherwise, <c>false</c>.
120 public bool HasSection(String sectionName
)
122 return sections
!= null && sections
.Contains(sectionName
);
126 /// Renders the component body.
128 public void RenderBody()
136 /// <param name="name"></param>
137 /// <param name="writer"></param>
138 public void RenderView(string name
, TextWriter writer
)
140 renderer
.RenderComponentView(context
, name
, writer
, this);
144 /// Renders the the specified section
146 /// <param name="sectionName">Name of the section.</param>
147 public void RenderSection(String sectionName
)
149 if (HasSection(sectionName
))
151 Directive directive
= (Directive
) sections
[sectionName
];
153 directive
.Render(context
, writer
, parentNode
);
158 /// Renders the the specified section
160 /// <param name="sectionName">Name of the section.</param>
161 /// <param name="writer">The writer.</param>
162 public void RenderSection(string sectionName
, TextWriter writer
)
164 if (HasSection(sectionName
))
166 Directive directive
= (Directive
)sections
[sectionName
];
168 directive
.Render(context
, writer
, parentNode
);
173 /// Renders the body into the specified <see cref="TextWriter"/>
175 /// <param name="writer">The writer.</param>
176 public void RenderBody(TextWriter writer
)
178 if (bodyNode
== null)
180 throw new RailsException("This component does not have a body content to be rendered");
183 bodyNode
.Render(context
, writer
);
187 /// Gets the view engine instance.
189 /// <value>The view engine.</value>
190 public IViewEngine ViewEngine
192 get { return viewEngine; }
197 internal IInternalContextAdapter Context
199 get { return context; }
200 set { context = value; }
203 internal INode BodyNode
205 get { return bodyNode; }
206 set { bodyNode = value; }
209 internal IDictionary ComponentParams
211 get { return componentParams; }
212 set { componentParams = value; }
215 internal TextWriter TextWriter
217 set { writer = value; }
220 internal void RegisterSection(SubSectionDirective section
)
222 if (sections
== null)
224 sections
= new HybridDictionary(true);
227 sections
[section
.Name
] = section
;