html_response.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace System.Net.ApiService {
  6. public class html_response {
  7. protected string _title;
  8. protected string _cacheName = "";
  9. protected string _template = "main.html";
  10. protected List<string> _css = new List<string>();
  11. protected List<string> _headers = new List<string>();
  12. protected string _innerHtml = "";
  13. public html_response(string Title) {
  14. this._title = Title;
  15. }
  16. public html_response(string Title, string CacheName) {
  17. this._title = Title;
  18. this._cacheName = CacheName;
  19. }
  20. public html_response(string Title, string Template, string CacheName) {
  21. this._title = Title;
  22. this._template = Template;
  23. this._cacheName = CacheName;
  24. }
  25. public List<string> CSS_Files {
  26. get {
  27. return this._css;
  28. }
  29. }
  30. public string InnerHtml {
  31. get {
  32. return this._innerHtml;
  33. }
  34. set {
  35. this._innerHtml = value;
  36. }
  37. }
  38. public List<string> HeaderLines {
  39. get {
  40. return this._headers;
  41. }
  42. }
  43. public string Title {
  44. get {
  45. return this._title;
  46. }
  47. set {
  48. this._title = value;
  49. }
  50. }
  51. public string Template {
  52. get {
  53. return this._template;
  54. }
  55. set {
  56. this._template = value;
  57. }
  58. }
  59. public bool AllowCache {
  60. get {
  61. return this._cacheName != "";
  62. }
  63. }
  64. public string CacheName {
  65. get {
  66. return this._cacheName;
  67. }
  68. set {
  69. this._cacheName = value;
  70. }
  71. }
  72. public override string ToString() {
  73. return this.getHtml();
  74. }
  75. protected string getHtml() {
  76. if (Directory.Exists("cache/") && this.AllowCache && File.Exists("cache/" + this._cacheName)) {
  77. System.Diagnostics.Debug.WriteLine("Read from Cache: 'cache/" + this._cacheName);
  78. return File.ReadAllText("cache/" + this._cacheName);
  79. }
  80. System.Diagnostics.Debug.WriteLine("Get Template");
  81. string res = this.getTemplate();
  82. res = res.Replace("{title}", this._title);
  83. res = res.Replace("{header}", this.getHeader());
  84. res = res.Replace("{message}", this.getBody());
  85. if (this.AllowCache) {
  86. File.WriteAllText("cache/" + this._cacheName, res);
  87. }
  88. return res;
  89. }
  90. protected string getBody() {
  91. return this._innerHtml;
  92. }
  93. protected string getHeader() {
  94. string res = "";
  95. res += this.getCSS();
  96. res += this.getHeaderLines();
  97. return res;
  98. }
  99. protected string getCSS() {
  100. string res = "";
  101. if (this._css.Count > 0) {
  102. foreach (string s in this._css) {
  103. res += " <link src='css/" + s + "' type='text/css'>\n";
  104. }
  105. }
  106. return res;
  107. }
  108. protected string getHeaderLines() {
  109. string res = "";
  110. if (this._css.Count > 0) {
  111. foreach (string s in this._headers) {
  112. res += " "+s+"\n";
  113. }
  114. }
  115. return res;
  116. }
  117. protected string getTemplate() {
  118. if (Directory.Exists("template/") && File.Exists("template/" + this._template) && this._template != "") {
  119. return File.ReadAllText("template/" + this._template);
  120. }
  121. return "<!DOCTYPE html>\n" +
  122. "<head><title>{title}</title>{header}</head><body>{message}</body></html>";
  123. }
  124. }
  125. }