From 88a8debd30c9f31d32c9c8f98a317e43e1da2608 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Wed, 15 Sep 2010 10:09:14 +0000 Subject: [PATCH] RSS design implemented; XML output optimised; encoding bugs fixed --- IISMainHandler/IISMainHandler.csproj | 1 + IISMainHandler/TemplateEngine.cs | 7 +++--- IISMainHandler/WebContext.cs | 12 +++++++--- IISMainHandler/designs/Classic.cs | 7 ++++++ IISMainHandler/designs/IDesign.cs | 4 ++++ IISMainHandler/designs/Lite.cs | 7 ++++++ IISMainHandler/designs/Raw.cs | 7 ++++++ IISMainHandler/designs/Rss.cs | 24 +++++++++++++++++++ IISMainHandler/handlers/AbstractGetHandler.cs | 4 ++-- .../handlers/request/AbstractPostHandler.cs | 4 ++-- 10 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 IISMainHandler/designs/Rss.cs diff --git a/IISMainHandler/IISMainHandler.csproj b/IISMainHandler/IISMainHandler.csproj index b3bcd44..8d9c0a2 100644 --- a/IISMainHandler/IISMainHandler.csproj +++ b/IISMainHandler/IISMainHandler.csproj @@ -144,6 +144,7 @@ + diff --git a/IISMainHandler/TemplateEngine.cs b/IISMainHandler/TemplateEngine.cs index 7614433..f158470 100644 --- a/IISMainHandler/TemplateEngine.cs +++ b/IISMainHandler/TemplateEngine.cs @@ -5,6 +5,7 @@ using System.Text; using System.Xml; using System.Xml.Linq; using System.Xml.Xsl; +using System.IO; namespace FLocal.IISHandler { class TemplateEngine { @@ -32,14 +33,12 @@ namespace FLocal.IISHandler { } - public static string Compile(string templateName, XDocument data) { - StringBuilder builder = new StringBuilder(); - using(XmlWriter writer = XmlWriter.Create(builder, new XmlWriterSettings { Indent = false })) { + public static void WriteCompiled(string templateName, XDocument data, Encoding encoding, TextWriter outStream) { + using(XmlWriter writer = XmlWriter.Create(outStream, new XmlWriterSettings { Indent = false, Encoding = encoding })) { using(XmlReader reader = data.CreateReader()) { TemplateCacher.instance.getCompiledTransform(templateName).Transform(reader, writer); } } - return builder.ToString(); } } diff --git a/IISMainHandler/WebContext.cs b/IISMainHandler/WebContext.cs index 23f9ca6..869ab52 100644 --- a/IISMainHandler/WebContext.cs +++ b/IISMainHandler/WebContext.cs @@ -13,6 +13,8 @@ using System.IO; namespace FLocal.IISHandler { class WebContext : Common.UserContext { + private static readonly Encoding OutputEncoding = Encoding.GetEncoding(1251); + public readonly HttpContext httpcontext; public HttpRequest httprequest { @@ -115,6 +117,9 @@ namespace FLocal.IISHandler { case 447: this.design = new designs.Lite(); break; + case 449: + this.design = new designs.Rss(); + break; case 443: default: this.design = new designs.Classic(); @@ -122,9 +127,10 @@ namespace FLocal.IISHandler { } } - public string Transform(string templateName, System.Xml.Linq.XDocument data) { - //TODO: this should work according to design! - return TemplateEngine.Compile(this.design.GetFSName(templateName), data); + public void WriteTransformResult(string templateName, System.Xml.Linq.XDocument data) { + this.httpresponse.ContentType = this.design.ContentType; + this.httpresponse.ContentEncoding = OutputEncoding; + TemplateEngine.WriteCompiled(this.design.GetFSName(templateName), data, OutputEncoding, this.httpresponse.Output); } public XElement exportSession() { diff --git a/IISMainHandler/designs/Classic.cs b/IISMainHandler/designs/Classic.cs index 3bed8d2..5288557 100644 --- a/IISMainHandler/designs/Classic.cs +++ b/IISMainHandler/designs/Classic.cs @@ -16,5 +16,12 @@ namespace FLocal.IISHandler.designs { Replace("", "\" alt=\""). Replace("", "\"/>"); } + + public string ContentType { + get { + return "text/html"; + } + } + } } diff --git a/IISMainHandler/designs/IDesign.cs b/IISMainHandler/designs/IDesign.cs index fb28876..5e2b179 100644 --- a/IISMainHandler/designs/IDesign.cs +++ b/IISMainHandler/designs/IDesign.cs @@ -8,5 +8,9 @@ namespace FLocal.IISHandler.designs { string GetFSName(string template); + string ContentType { + get; + } + } } diff --git a/IISMainHandler/designs/Lite.cs b/IISMainHandler/designs/Lite.cs index 0a988e2..1ea8912 100644 --- a/IISMainHandler/designs/Lite.cs +++ b/IISMainHandler/designs/Lite.cs @@ -16,5 +16,12 @@ namespace FLocal.IISHandler.designs { Replace("", "\">"). Replace("", ""); } + + public string ContentType { + get { + return "text/html"; + } + } + } } diff --git a/IISMainHandler/designs/Raw.cs b/IISMainHandler/designs/Raw.cs index 52e7c73..61cce64 100644 --- a/IISMainHandler/designs/Raw.cs +++ b/IISMainHandler/designs/Raw.cs @@ -13,5 +13,12 @@ namespace FLocal.IISHandler.designs { string FLocal.Common.IOutputParams.preprocessBodyIntermediate(string bodyIntermediate) { return bodyIntermediate; } + + public string ContentType { + get { + return "application/xml"; + } + } + } } diff --git a/IISMainHandler/designs/Rss.cs b/IISMainHandler/designs/Rss.cs new file mode 100644 index 0000000..89d2db3 --- /dev/null +++ b/IISMainHandler/designs/Rss.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FLocal.IISHandler.designs { + class Rss : IDesign { + + public string GetFSName(string template) { + return System.IO.Path.Combine("Rss", template); + } + + string FLocal.Common.IOutputParams.preprocessBodyIntermediate(string bodyIntermediate) { + return bodyIntermediate; + } + + public string ContentType { + get { + return "application/rss+xml"; + } + } + + } +} diff --git a/IISMainHandler/handlers/AbstractGetHandler.cs b/IISMainHandler/handlers/AbstractGetHandler.cs index 40e858c..c666ff4 100644 --- a/IISMainHandler/handlers/AbstractGetHandler.cs +++ b/IISMainHandler/handlers/AbstractGetHandler.cs @@ -41,14 +41,14 @@ namespace FLocal.IISHandler.handlers { public void Handle(WebContext context) { try { - context.httpresponse.Write(context.Transform(this.templateName, this.getData(context))); + context.WriteTransformResult(this.templateName, this.getData(context)); } catch(RedirectException) { throw; } catch(WrongUrlException) { throw; } catch(Exception e) { context.LogError(e); - context.httpresponse.Write(context.Transform("Exception.xslt", new XDocument(new XElement("root", this.getCommonData(context), e.ToXml())))); + context.WriteTransformResult("Exception.xslt", new XDocument(new XElement("root", this.getCommonData(context), e.ToXml()))); } } diff --git a/IISMainHandler/handlers/request/AbstractPostHandler.cs b/IISMainHandler/handlers/request/AbstractPostHandler.cs index d996d32..6490f83 100644 --- a/IISMainHandler/handlers/request/AbstractPostHandler.cs +++ b/IISMainHandler/handlers/request/AbstractPostHandler.cs @@ -56,14 +56,14 @@ namespace FLocal.IISHandler.handlers.request { if(this.shouldBeGuest && context.session != null) throw new FLocalException("Should be guest"); if(this.shouldBeLoggedIn && context.session == null) throw new FLocalException("Should be anonymous"); - context.httpresponse.Write(context.Transform(this.templateName, this.getData(context))); + context.WriteTransformResult(this.templateName, this.getData(context)); } catch(RedirectException) { throw; } catch(WrongUrlException) { throw; } catch(Exception e) { context.LogError(e); - context.httpresponse.Write(context.Transform("Exception.xslt", new XDocument(new XElement("root", this.getCommonData(context), e.ToXml())))); + context.WriteTransformResult("Exception.xslt", new XDocument(new XElement("root", this.getCommonData(context), e.ToXml()))); } }