From ddca90d98fbdd3deb0a61794f2980fa64d0bd54c Mon Sep 17 00:00:00 2001 From: ayende Date: Thu, 4 Oct 2007 19:22:52 +0000 Subject: [PATCH] Fixing MR-312, use the BrailBase.Context when access the IViewComponentFactory git-svn-id: https://svn.castleproject.org/svn/castle/trunk@4374 73e77b4c-caa6-f847-a29a-24ab75ae54b6 --- MonoRail/Castle.MonoRail.Views.Brail/BrailBase.cs | 259 +++++++++++---------- .../Macros/ComponentMacro.cs | 4 +- 2 files changed, 139 insertions(+), 124 deletions(-) diff --git a/MonoRail/Castle.MonoRail.Views.Brail/BrailBase.cs b/MonoRail/Castle.MonoRail.Views.Brail/BrailBase.cs index a773461bb..9e0d1d487 100644 --- a/MonoRail/Castle.MonoRail.Views.Brail/BrailBase.cs +++ b/MonoRail/Castle.MonoRail.Views.Brail/BrailBase.cs @@ -12,16 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System.Diagnostics; -using System.Text; - namespace Castle.MonoRail.Views.Brail { using System; using System.Collections; using System.IO; using System.Web; - using Castle.MonoRail.Framework; + using Framework; + /// ///Base class for all the view scripts, this is the class that is responsible for /// support all the behind the scenes magic such as variable to PropertyBag trasnlation, @@ -29,31 +27,34 @@ namespace Castle.MonoRail.Views.Brail /// public abstract class BrailBase { - private TextWriter outputStream; + protected Controller __controller; /// - /// This is used by layout scripts only, for outputing the child's content + /// Reference to the DSL service /// - protected TextWriter childOutput; - private Hashtable properties; + private DslProvider _dsl; + /// - /// used to hold the ComponentParams from the view, so their views/sections could access them + /// This is used by layout scripts only, for outputing the child's content /// - private IList viewComponentsParameters; + protected TextWriter childOutput; protected IRailsEngineContext context; - protected Controller __controller; - protected BooViewEngine viewEngine; + private TextWriter outputStream; /// /// usually used by the layout to refer to its view, or a subview to its parent /// protected BrailBase parent; - /// - /// Reference to the DSL service - /// - private DslProvider _dsl; + private Hashtable properties; + + /// + /// used to hold the ComponentParams from the view, so their views/sections could access them + /// + private IList viewComponentsParameters; + + protected BooViewEngine viewEngine; /// /// Initializes a new instance of the class. @@ -65,17 +66,12 @@ namespace Castle.MonoRail.Views.Brail public BrailBase(BooViewEngine viewEngine, TextWriter output, IRailsEngineContext context, Controller __controller) { this.viewEngine = viewEngine; - this.outputStream = output; + outputStream = output; this.context = context; this.__controller = __controller; InitProperties(context, __controller); } - /// - /// Runs this instance, this is generated by the script - /// - public abstract void Run(); - /// ///The path of the script, this is filled by AddBrailBaseClassStep @@ -95,34 +91,76 @@ namespace Castle.MonoRail.Views.Brail get { return viewEngine; } } - /// - /// Gets the DSL provider - /// - /// Reference to the current DSL Provider - public DslProvider Dsl - { - get - { - BrailBase view = this; - if (null == view._dsl) - { - view._dsl = new DslProvider(view); - } - - return view._dsl; - //while (view.parent != null) - //{ - // view = view.parent; - //} - - //if (view._dsl == null) - //{ - // view._dsl = new DslProvider(view); - //} - - //return view._dsl; - } - } + /// + /// Gets the DSL provider + /// + /// Reference to the current DSL Provider + public DslProvider Dsl + { + get + { + BrailBase view = this; + if (null == view._dsl) + { + view._dsl = new DslProvider(view); + } + + return view._dsl; + //while (view.parent != null) + //{ + // view = view.parent; + //} + + //if (view._dsl == null) + //{ + // view._dsl = new DslProvider(view); + //} + + //return view._dsl; + } + } + + /// + /// Gets the flash. + /// + /// The flash. + public Flash Flash + { + get { return context.Flash; } + } + + /// + /// Gets the output stream. + /// + /// The output stream. + public TextWriter OutputStream + { + get { return outputStream; } + } + + /// + /// Gets or sets the child output. + /// + /// The child output. + public TextWriter ChildOutput + { + get { return childOutput; } + set { childOutput = value; } + } + + /// + /// Gets the properties. + /// + /// The properties. + public IDictionary Properties + { + get { return properties; } + } + + /// + /// Runs this instance, this is generated by the script + /// + public abstract void Run(); /// /// Output the subview to the client, this is either a relative path "SubView" which @@ -143,7 +181,7 @@ namespace Castle.MonoRail.Views.Brail public string OutputSubView(string subviewName, IDictionary parameters) { OutputSubView(subviewName, outputStream, parameters); - return string.Empty; + return string.Empty; } /// @@ -254,24 +292,6 @@ namespace Castle.MonoRail.Views.Brail } /// - /// Gets the flash. - /// - /// The flash. - public Flash Flash - { - get { return context.Flash; } - } - - /// - /// Gets the output stream. - /// - /// The output stream. - public TextWriter OutputStream - { - get { return outputStream; } - } - - /// /// This is required because we may want to replace the output stream and get the correct /// behavior from components call RenderText() or RenderSection() /// @@ -288,32 +308,13 @@ namespace Castle.MonoRail.Views.Brail /// public void OutputEscaped(object toOutput) { - if(toOutput==null) + if (toOutput == null) return; - string str= toOutput.ToString(); + string str = toOutput.ToString(); OutputStream.Write(HttpUtility.HtmlEncode(str)); } /// - /// Gets or sets the child output. - /// - /// The child output. - public TextWriter ChildOutput - { - get { return childOutput; } - set { childOutput = value; } - } - - /// - /// Gets the properties. - /// - /// The properties. - public IDictionary Properties - { - get { return properties; } - } - - /// /// Note that this will overwrite any existing property. /// public void AddProperty(string name, object item) @@ -345,27 +346,28 @@ namespace Castle.MonoRail.Views.Brail return; viewComponentsParameters.Remove(propertiesToRemove); } - public void RenderComponent(string componentName) - { - RenderComponent(componentName, new Hashtable()); - } - public void RenderComponent(string componentName, IDictionary parameters) - { - BrailViewComponentContext componentContext = - new BrailViewComponentContext(this, null, componentName, OutputStream, - new Hashtable(StringComparer.InvariantCultureIgnoreCase)); - this.AddViewComponentProperties(componentContext.ComponentParameters); - IViewComponentFactory componentFactory = (IViewComponentFactory)this.context.GetService(typeof(IViewComponentFactory)); - ViewComponent component = componentFactory.Create(componentName); - component.Init(this.context, componentContext); - component.Render(); - if (componentContext.ViewToRender != null) - { - this.OutputSubView("/" + componentContext.ViewToRender, componentContext.ComponentParameters); - } - this.RemoveViewComponentProperties(componentContext.ComponentParameters); - - } + + public void RenderComponent(string componentName) + { + RenderComponent(componentName, new Hashtable()); + } + + public void RenderComponent(string componentName, IDictionary parameters) + { + BrailViewComponentContext componentContext = + new BrailViewComponentContext(this, null, componentName, OutputStream, + new Hashtable(StringComparer.InvariantCultureIgnoreCase)); + AddViewComponentProperties(componentContext.ComponentParameters); + IViewComponentFactory componentFactory = (IViewComponentFactory) context.GetService(typeof (IViewComponentFactory)); + ViewComponent component = componentFactory.Create(componentName); + component.Init(context, componentContext); + component.Render(); + if (componentContext.ViewToRender != null) + { + OutputSubView("/" + componentContext.ViewToRender, componentContext.ComponentParameters); + } + RemoveViewComponentProperties(componentContext.ComponentParameters); + } /// /// Initialize all the properties that a script may need @@ -377,8 +379,9 @@ namespace Castle.MonoRail.Views.Brail private void InitProperties(IRailsEngineContext myContext, Controller myController) { properties = new Hashtable(StringComparer.InvariantCultureIgnoreCase); - //properties.Add("dsl", new DslWrapper(this)); + //properties.Add("dsl", new DslWrapper(this)); properties.Add("Controller", myController); + properties.Add("Context", myContext); properties.Add("request", myContext.Request); properties.Add("response", myContext.Response); properties.Add("session", myContext.Session); @@ -407,7 +410,7 @@ namespace Castle.MonoRail.Views.Brail { properties[entry.Key] = entry.Value; } - + foreach (DictionaryEntry entry in myController.Helpers) { properties[entry.Key] = entry.Value; @@ -416,10 +419,18 @@ namespace Castle.MonoRail.Views.Brail properties["siteRoot"] = myContext.ApplicationPath; } + #region Nested type: ParameterSearch + private class ParameterSearch { - private bool found; - private object value; + private readonly bool found; + private readonly object value; + + public ParameterSearch(object value, bool found) + { + this.found = found; + this.value = value; + } public bool Found { @@ -430,14 +441,12 @@ namespace Castle.MonoRail.Views.Brail { get { return value; } } - - public ParameterSearch(object value, bool found) - { - this.found = found; - this.value = value; - } } + #endregion + + #region Nested type: ReturnOutputStreamToInitialWriter + private class ReturnOutputStreamToInitialWriter : IDisposable { private TextWriter initialWriter; @@ -449,10 +458,16 @@ namespace Castle.MonoRail.Views.Brail this.parent = parent; } + #region IDisposable Members + public void Dispose() { parent.outputStream = initialWriter; } + + #endregion } + + #endregion } -} +} \ No newline at end of file diff --git a/MonoRail/Castle.MonoRail.Views.Brail/Macros/ComponentMacro.cs b/MonoRail/Castle.MonoRail.Views.Brail/Macros/ComponentMacro.cs index 31a47fb15..b145fd259 100644 --- a/MonoRail/Castle.MonoRail.Views.Brail/Macros/ComponentMacro.cs +++ b/MonoRail/Castle.MonoRail.Views.Brail/Macros/ComponentMacro.cs @@ -69,9 +69,9 @@ namespace Castle.MonoRail.Views.Brail InternalLocal viewComponentFactoryLocal = CodeBuilder.DeclareLocal(method, "viewComponentFactory", TypeSystemServices.Map( typeof(IViewComponentFactory))); - // viewComponentFactory = MonoRailHttpHandler.CurrentContext.GetService(IViewComponentFactory) + // viewComponentFactory = context.GetService(IViewComponentFactory) MethodInvocationExpression callService = new MethodInvocationExpression( - AstUtil.CreateReferenceExpression("MonoRailHttpHandler.CurrentContext.GetService")); + AstUtil.CreateReferenceExpression("context.GetService")); callService.Arguments.Add(CodeBuilder.CreateTypeofExpression(typeof(IViewComponentFactory))); block.Add(new BinaryExpression(BinaryOperatorType.Assign, -- 2.11.4.GIT