Implemented boardslist page; implemented templateengine

main
Inga 🏳‍🌈 15 years ago
parent f17bd5136c
commit 2584b10d30
  1. 5
      Common/Config.cs
  2. 8
      Common/dataobjects/Board.cs
  3. 13
      IISMainHandler/HandlersFactory.cs
  4. 1
      IISMainHandler/IISMainHandler.csproj
  5. 35
      IISMainHandler/TemplateEngine.cs
  6. 48
      IISMainHandler/handlers/BoardsHandler.cs
  7. 2
      IISMainHandler/handlers/DebugHandler.cs
  8. 2
      IISMainHandler/handlers/WrongUrlHandler.cs

@ -12,9 +12,12 @@ namespace FLocal.Common {
public readonly Core.DB.IDBConnection mainConnection; public readonly Core.DB.IDBConnection mainConnection;
public readonly string dataDir;
protected Config(NameValueCollection data) : base(data) { protected Config(NameValueCollection data) : base(data) {
this.InitTime = DateTime.Now.ToLongTimeString(); 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) { public static void Init(NameValueCollection data) {

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Xml.Linq;
namespace FLocal.Common.dataobjects { namespace FLocal.Common.dataobjects {
public class Board : SqlObject<Board> { public class Board : SqlObject<Board> {
@ -58,5 +59,12 @@ namespace FLocal.Common.dataobjects {
this._categoryId = int.Parse(data["categoryId"]); this._categoryId = int.Parse(data["categoryId"]);
} }
public XElement exportToXml() {
return new XElement("board",
new XElement("name", this.name),
new XElement("description", this.description)
);
}
} }
} }

@ -3,15 +3,24 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Web; using System.Web;
using FLocal.Core;
namespace FLocal.IISHandler { namespace FLocal.IISHandler {
class HandlersFactory { class HandlersFactory {
public static ISpecificHandler getHandler(WebContext context) { 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); string[] requestParts = context.httprequest.Path.Split("/", StringSplitOptions.RemoveEmptyEntries);
if(requestParts.Length < 1) return new handlers.RootHandler(); if(requestParts.Length < 1) return new handlers.RootHandler();
return new handlers.DebugHandler(requestParts[0]); switch(requestParts[0]) {
//return new handlers.WrongUrlHandler(); case "boards":
return new handlers.BoardsHandler();
default:
return new handlers.DebugHandler(requestParts[0]);
}
} }
} }

@ -53,6 +53,7 @@
<Compile Include="Extensions.cs" /> <Compile Include="Extensions.cs" />
<Compile Include="HandlersFactory.cs" /> <Compile Include="HandlersFactory.cs" />
<Compile Include="handlers\AbstractGetHandler.cs" /> <Compile Include="handlers\AbstractGetHandler.cs" />
<Compile Include="handlers\BoardsHandler.cs" />
<Compile Include="handlers\DebugHandler.cs" /> <Compile Include="handlers\DebugHandler.cs" />
<Compile Include="handlers\RootHandler.cs" /> <Compile Include="handlers\RootHandler.cs" />
<Compile Include="handlers\WrongUrlHandler.cs" /> <Compile Include="handlers\WrongUrlHandler.cs" />

@ -2,13 +2,44 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Xml;
using System.Xml.Linq; using System.Xml.Linq;
using System.Xml.Xsl;
namespace FLocal.IISHandler { namespace FLocal.IISHandler {
class TemplateEngine { class TemplateEngine {
public static string Compile(string pathToTemplate, XDocument data) { private class TemplateCacher {
throw new NotImplementedException();
public static TemplateCacher instance = new TemplateCacher();
private object locker = new object();
private Dictionary<string, XslCompiledTransform> cache = new Dictionary<string,XslCompiledTransform>();
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();
} }
} }

@ -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()
)
)
)
)
);
}
}
}

@ -26,7 +26,7 @@ namespace FLocal.IISHandler.handlers {
context.httpresponse.WriteLine(transaction.GetHashCode().ToString()); context.httpresponse.WriteLine(transaction.GetHashCode().ToString());
} }
} }
if(context.httprequest.Path == "/boards/") { if(context.httprequest.Path == "/test2/") {
Board board = Board.LoadById(1); Board board = Board.LoadById(1);
context.httpresponse.WriteLine("name: " + board.name); context.httpresponse.WriteLine("name: " + board.name);
context.httpresponse.WriteLine("description: " + board.description); context.httpresponse.WriteLine("description: " + board.description);

@ -8,7 +8,7 @@ namespace FLocal.IISHandler.handlers {
class WrongUrlHandler : ISpecificHandler { class WrongUrlHandler : ISpecificHandler {
public void Handle(WebContext context) { public void Handle(WebContext context) {
throw new HttpException(404, "page not found"); throw new HttpException(400, "wrong url");
} }
} }

Loading…
Cancel
Save