using System; using System.Collections.Generic; using System.Text; using System.IO; namespace System.Net.ApiService { public class html_response { protected string _title; protected string _cacheName = ""; protected string _template = "main.html"; protected List _css = new List(); protected List _headers = new List(); protected string _innerHtml = ""; public html_response(string Title) { this._title = Title; } public html_response(string Title, string CacheName) { this._title = Title; this._cacheName = CacheName; } public html_response(string Title, string Template, string CacheName) { this._title = Title; this._template = Template; this._cacheName = CacheName; } public List CSS_Files { get { return this._css; } } public string InnerHtml { get { return this._innerHtml; } set { this._innerHtml = value; } } public List HeaderLines { get { return this._headers; } } public string Title { get { return this._title; } set { this._title = value; } } public string Template { get { return this._template; } set { this._template = value; } } public bool AllowCache { get { return this._cacheName != ""; } } public string CacheName { get { return this._cacheName; } set { this._cacheName = value; } } public override string ToString() { return this.getHtml(); } protected string getHtml() { if (Directory.Exists("cache/") && this.AllowCache && File.Exists("cache/" + this._cacheName)) { System.Diagnostics.Debug.WriteLine("Read from Cache: 'cache/" + this._cacheName); return File.ReadAllText("cache/" + this._cacheName); } System.Diagnostics.Debug.WriteLine("Get Template"); string res = this.getTemplate(); res = res.Replace("{title}", this._title); res = res.Replace("{header}", this.getHeader()); res = res.Replace("{message}", this.getBody()); if (this.AllowCache) { File.WriteAllText("cache/" + this._cacheName, res); } return res; } protected string getBody() { return this._innerHtml; } protected string getHeader() { string res = ""; res += this.getCSS(); res += this.getHeaderLines(); return res; } protected string getCSS() { string res = ""; if (this._css.Count > 0) { foreach (string s in this._css) { res += " \n"; } } return res; } protected string getHeaderLines() { string res = ""; if (this._css.Count > 0) { foreach (string s in this._headers) { res += " "+s+"\n"; } } return res; } protected string getTemplate() { if (Directory.Exists("template/") && File.Exists("template/" + this._template) && this._template != "") { return File.ReadAllText("template/" + this._template); } return "\n" + "{title}{header}{message}"; } } }