diff --git a/Common/Config.cs b/Common/Config.cs index 9e64226..2c3bac8 100644 --- a/Common/Config.cs +++ b/Common/Config.cs @@ -12,9 +12,12 @@ namespace FLocal.Common { public readonly Core.DB.IDBConnection mainConnection; + public readonly string dataDir; + protected Config(NameValueCollection data) : base(data) { this.InitTime = DateTime.Now.ToLongTimeString(); - this.mainConnection = new MySQLConnector.Connection(data["connectionString"]); + this.mainConnection = new MySQLConnector.Connection(data["ConnectionString"]); + this.dataDir = data["DataDir"]; } public static void Init(NameValueCollection data) { diff --git a/Common/dataobjects/Board.cs b/Common/dataobjects/Board.cs index bdb2078..69b1273 100644 --- a/Common/dataobjects/Board.cs +++ b/Common/dataobjects/Board.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Xml.Linq; namespace FLocal.Common.dataobjects { public class Board : SqlObject { @@ -58,5 +59,12 @@ namespace FLocal.Common.dataobjects { this._categoryId = int.Parse(data["categoryId"]); } + public XElement exportToXml() { + return new XElement("board", + new XElement("name", this.name), + new XElement("description", this.description) + ); + } + } } diff --git a/IISMainHandler/HandlersFactory.cs b/IISMainHandler/HandlersFactory.cs index 0c6cb0b..e4bbe83 100644 --- a/IISMainHandler/HandlersFactory.cs +++ b/IISMainHandler/HandlersFactory.cs @@ -3,15 +3,24 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; +using FLocal.Core; namespace FLocal.IISHandler { class HandlersFactory { public static ISpecificHandler getHandler(WebContext context) { + if(!context.httprequest.Path.EndsWith("/")) { + return new handlers.WrongUrlHandler(); +// throw new FLocalException("Malformed url"); + } string[] requestParts = context.httprequest.Path.Split("/", StringSplitOptions.RemoveEmptyEntries); if(requestParts.Length < 1) return new handlers.RootHandler(); - return new handlers.DebugHandler(requestParts[0]); - //return new handlers.WrongUrlHandler(); + switch(requestParts[0]) { + case "boards": + return new handlers.BoardsHandler(); + default: + return new handlers.DebugHandler(requestParts[0]); + } } } diff --git a/IISMainHandler/IISMainHandler.csproj b/IISMainHandler/IISMainHandler.csproj index da6309c..4011236 100644 --- a/IISMainHandler/IISMainHandler.csproj +++ b/IISMainHandler/IISMainHandler.csproj @@ -53,6 +53,7 @@ + diff --git a/IISMainHandler/TemplateEngine.cs b/IISMainHandler/TemplateEngine.cs index 341a531..16b9fb8 100644 --- a/IISMainHandler/TemplateEngine.cs +++ b/IISMainHandler/TemplateEngine.cs @@ -2,13 +2,44 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Xml; using System.Xml.Linq; +using System.Xml.Xsl; namespace FLocal.IISHandler { class TemplateEngine { - public static string Compile(string pathToTemplate, XDocument data) { - throw new NotImplementedException(); + private class TemplateCacher { + + public static TemplateCacher instance = new TemplateCacher(); + + private object locker = new object(); + + private Dictionary cache = new Dictionary(); + + public XslCompiledTransform getCompiledTransform(string templateName) { + if(!this.cache.ContainsKey(templateName)) { + lock(this.locker) { + if(!this.cache.ContainsKey(templateName)) { + XslCompiledTransform xslt = new XslCompiledTransform(); + xslt.Load(FLocal.Common.Config.instance.dataDir + templateName); + this.cache[templateName] = xslt; + } + } + } + return this.cache[templateName]; + } + + } + + public static string Compile(string templateName, XDocument data) { + XDocument result = new XDocument(); + using(XmlWriter writer = result.CreateWriter()) { + using(XmlReader reader = data.CreateReader()) { + TemplateCacher.instance.getCompiledTransform(templateName).Transform(reader, writer); + } + } + return result.ToString(); } } diff --git a/IISMainHandler/handlers/BoardsHandler.cs b/IISMainHandler/handlers/BoardsHandler.cs new file mode 100644 index 0000000..df8dc3c --- /dev/null +++ b/IISMainHandler/handlers/BoardsHandler.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Web; +using System.Xml.Linq; +using FLocal.Common; +using FLocal.Common.dataobjects; + +namespace FLocal.IISHandler.handlers { + + class BoardsHandler : AbstractGetHandler { + + override protected string templateName { + get { + return "Boards.xslt"; + } + } + + override protected XDocument getData(WebContext context) { + Board board1 = Board.LoadById(1); + Board board2 = Board.LoadById(2); + Board board3 = Board.LoadById(3); + return new XDocument( + new XElement("root", + new XElement("title", Config.instance.AppInfo), + new XElement("categories", + new XElement("category", + new XElement("name", board1.category.name), + new XElement("boards", + board1.exportToXml(), + board2.exportToXml() + ) + ), + new XElement("category", + new XElement("name", board3.category.name), + new XElement("boards", + board3.exportToXml() + ) + ) + ) + ) + ); + } + + } + +} \ No newline at end of file diff --git a/IISMainHandler/handlers/DebugHandler.cs b/IISMainHandler/handlers/DebugHandler.cs index 1c95c85..695f6c6 100644 --- a/IISMainHandler/handlers/DebugHandler.cs +++ b/IISMainHandler/handlers/DebugHandler.cs @@ -26,7 +26,7 @@ namespace FLocal.IISHandler.handlers { context.httpresponse.WriteLine(transaction.GetHashCode().ToString()); } } - if(context.httprequest.Path == "/boards/") { + if(context.httprequest.Path == "/test2/") { Board board = Board.LoadById(1); context.httpresponse.WriteLine("name: " + board.name); context.httpresponse.WriteLine("description: " + board.description); diff --git a/IISMainHandler/handlers/WrongUrlHandler.cs b/IISMainHandler/handlers/WrongUrlHandler.cs index df63cbf..85aacc4 100644 --- a/IISMainHandler/handlers/WrongUrlHandler.cs +++ b/IISMainHandler/handlers/WrongUrlHandler.cs @@ -8,7 +8,7 @@ namespace FLocal.IISHandler.handlers { class WrongUrlHandler : ISpecificHandler { public void Handle(WebContext context) { - throw new HttpException(404, "page not found"); + throw new HttpException(400, "wrong url"); } }