Adding #if DOTNET35 for REST
[castle.git] / MonoRail / Castle.MonoRail.Framework.Views.NVelocity / NVelocityViewContextAdapter.cs
blobab493fcd0f5114e7b74ce1ca094c640f74c3fe13
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 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
22 using System;
23 using System.Collections;
24 using System.Collections.Specialized;
25 using System.IO;
27 using Castle.MonoRail.Framework.Views.NVelocity.CustomDirectives;
29 /// <summary>
30 /// <see cref="IViewComponentContext"/>'s implementation for
31 /// NVelocity
32 /// </summary>
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;
47 /// <summary>
48 /// Initializes a new instance of the <see cref="NVelocityViewContextAdapter"/> class.
49 /// </summary>
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
64 /// <summary>
65 /// Gets the name of the component.
66 /// </summary>
67 /// <value>The name of the component.</value>
68 public String ComponentName
70 get { return componentName; }
73 /// <summary>
74 /// Gets the dictionary that holds variables for the
75 /// view and for the view component
76 /// </summary>
77 /// <value>The context vars.</value>
78 public IDictionary ContextVars
80 get { return context; }
83 /// <summary>
84 /// Gets the component parameters that the view has passed
85 /// to the component
86 /// </summary>
87 /// <value>The component parameters.</value>
88 public IDictionary ComponentParameters
90 get { return componentParams; }
93 /// <summary>
94 /// Gets or sets the view to render.
95 /// </summary>
96 /// <value>The view to render.</value>
97 public String ViewToRender
99 get { return viewToRender; }
100 set { viewToRender = value; }
103 /// <summary>
104 /// Gets the writer used to render the component
105 /// </summary>
106 /// <value>The writer.</value>
107 public TextWriter Writer
109 get { return writer; }
112 /// <summary>
113 /// Determines whether the current component declaration on the view
114 /// has the specified section.
115 /// </summary>
116 /// <param name="sectionName">Name of the section.</param>
117 /// <returns>
118 /// <c>true</c> if the specified section exists; otherwise, <c>false</c>.
119 /// </returns>
120 public bool HasSection(String sectionName)
122 return sections != null && sections.Contains(sectionName);
125 /// <summary>
126 /// Renders the component body.
127 /// </summary>
128 public void RenderBody()
130 RenderBody(writer);
133 /// <summary>
134 /// Pendent
135 /// </summary>
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);
143 /// <summary>
144 /// Renders the the specified section
145 /// </summary>
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);
157 /// <summary>
158 /// Renders the the specified section
159 /// </summary>
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);
172 /// <summary>
173 /// Renders the body into the specified <see cref="TextWriter"/>
174 /// </summary>
175 /// <param name="writer">The writer.</param>
176 public void RenderBody(TextWriter writer)
178 if (bodyNode == null)
180 throw new MonoRailException("This component does not have a body content to be rendered");
183 bodyNode.Render(context, writer);
186 /// <summary>
187 /// Gets the view engine instance.
188 /// </summary>
189 /// <value>The view engine.</value>
190 public IViewEngine ViewEngine
192 get { return viewEngine; }
195 #endregion
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;