| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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<string> _css = new List<string>();
- protected List<string> _headers = new List<string>();
- 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<string> CSS_Files {
- get {
- return this._css;
- }
- }
- public string InnerHtml {
- get {
- return this._innerHtml;
- }
- set {
- this._innerHtml = value;
- }
- }
- public List<string> 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 += " <link src='css/" + s + "' type='text/css'>\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 "<!DOCTYPE html>\n" +
- "<head><title>{title}</title>{header}</head><body>{message}</body></html>";
- }
- }
- }
|