HtmlWriter.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.IO;
  3. using System.Diagnostics;
  4. using System.Web.UI;
  5. using System.Reflection;
  6. namespace Helper {
  7. public class HtmlWriter : HtmlTextWriter {
  8. bool full_trace;
  9. TextWriter output;
  10. int call_count;
  11. int NextIndex ()
  12. {
  13. return call_count++;
  14. }
  15. public HtmlWriter (TextWriter writer) : this (writer, DefaultTabString)
  16. {
  17. }
  18. public HtmlWriter (TextWriter writer, string tabString) : base (writer, tabString)
  19. {
  20. full_trace = (Environment.GetEnvironmentVariable ("HTMLWRITER_FULLTRACE") == "yes");
  21. string file = Environment.GetEnvironmentVariable ("HTMLWRITER_FILE");
  22. Console.WriteLine ("file: '{0}' (null? {1})", file, file == null);
  23. if (file != null && file != "") {
  24. output = new StreamWriter (new FileStream (file, FileMode.OpenOrCreate | FileMode.Append));
  25. Console.WriteLine ("Sending log to '{0}'.", file);
  26. } else {
  27. output = Console.Out;
  28. }
  29. }
  30. void WriteTrace (StackTrace trace)
  31. {
  32. int n = trace.FrameCount;
  33. for (int i = 0; i < n; i++) {
  34. StackFrame frame = trace.GetFrame (i);
  35. Type type = frame.GetMethod ().DeclaringType;
  36. string ns = type.Namespace;
  37. if (ns != "Helper" && !ns.StartsWith ("System.Web.UI"))
  38. break;
  39. output.Write ("\t{0}.{1}", type.Name, frame);
  40. }
  41. output.WriteLine ();
  42. }
  43. public override void AddAttribute (HtmlTextWriterAttribute key, string value, bool fEncode)
  44. {
  45. output.WriteLine ("{0:###0} AddAttribute ({1}, {2}, {3}))", NextIndex (), key, value, fEncode);
  46. if (full_trace)
  47. WriteTrace (new StackTrace ());
  48. base.AddAttribute (key, value, fEncode);
  49. }
  50. public override void AddAttribute (string name, string value, bool fEncode)
  51. {
  52. output.WriteLine ("{0:###0} AddAttribute ({1}, {2}, {3}))", NextIndex (), name, value, fEncode);
  53. if (full_trace)
  54. WriteTrace (new StackTrace ());
  55. if (fEncode)
  56. ; // FIXME
  57. base.AddAttribute (name, value, (HtmlTextWriterAttribute) 0);
  58. }
  59. protected override void AddAttribute (string name, string value, HtmlTextWriterAttribute key)
  60. {
  61. output.WriteLine ("{0:###0} AddAttribute ({1}, {2}, {3}))", NextIndex (), name, value, key);
  62. if (full_trace)
  63. WriteTrace (new StackTrace ());
  64. base.AddAttribute (name, value, key);
  65. }
  66. protected override void AddStyleAttribute (string name, string value, HtmlTextWriterStyle key)
  67. {
  68. output.WriteLine ("{0:###0} AddStyleAttribute ({1}, {2}, {3}))", NextIndex (), name, value, key);
  69. if (full_trace)
  70. WriteTrace (new StackTrace ());
  71. base.AddStyleAttribute (name, value, key);
  72. }
  73. public override void Close ()
  74. {
  75. output.WriteLine ("{0:###0} Close ()", NextIndex ());
  76. if (full_trace)
  77. WriteTrace (new StackTrace ());
  78. if (output != Console.Out)
  79. output.Close ();
  80. base.Close ();
  81. }
  82. protected override string EncodeAttributeValue (HtmlTextWriterAttribute attrKey, string value)
  83. {
  84. output.WriteLine ("{0:###0} EncodeAttributeValue ({1}, {2})", NextIndex (), attrKey, value);
  85. if (full_trace)
  86. WriteTrace (new StackTrace ());
  87. return base.EncodeAttributeValue (attrKey, value);
  88. }
  89. protected override void FilterAttributes ()
  90. {
  91. output.WriteLine ("{0:###0} FilterAttributes ()", NextIndex ());
  92. if (full_trace)
  93. WriteTrace (new StackTrace ());
  94. base.FilterAttributes ();
  95. }
  96. public override void Flush ()
  97. {
  98. output.WriteLine ("{0:###0} Flush ()", NextIndex ());
  99. if (full_trace)
  100. WriteTrace (new StackTrace ());
  101. base.Flush ();
  102. }
  103. protected override HtmlTextWriterTag GetTagKey (string tagName)
  104. {
  105. output.WriteLine ("{0:###0} GetTagKey ({1})", NextIndex (), tagName);
  106. if (full_trace)
  107. WriteTrace (new StackTrace ());
  108. return base.GetTagKey (tagName);
  109. }
  110. protected override string GetTagName (HtmlTextWriterTag tagKey)
  111. {
  112. output.WriteLine ("{0:###0} GetTagName ({1})", NextIndex (), tagKey);
  113. if (full_trace)
  114. WriteTrace (new StackTrace ());
  115. return base.GetTagName (tagKey);
  116. }
  117. protected override bool OnAttributeRender (string name, string value, HtmlTextWriterAttribute key)
  118. {
  119. output.WriteLine ("{0:###0} OnAttributeRender ({1}, {2}, {3})", NextIndex (), name, value, key);
  120. if (full_trace)
  121. WriteTrace (new StackTrace ());
  122. return base.OnAttributeRender (name, value, key);
  123. }
  124. protected override bool OnStyleAttributeRender (string name, string value, HtmlTextWriterStyle key)
  125. {
  126. output.WriteLine ("{0:###0} OnStyleAttributeRender ({1}, {2}, {3})", NextIndex (), name, value, key);
  127. if (full_trace)
  128. WriteTrace (new StackTrace ());
  129. return base.OnStyleAttributeRender (name, value, key);
  130. }
  131. protected override bool OnTagRender (string name, HtmlTextWriterTag key)
  132. {
  133. output.WriteLine ("{0:###0} OnTagRender ({1}, {2})", NextIndex (), name, key);
  134. if (full_trace)
  135. WriteTrace (new StackTrace ());
  136. return base.OnTagRender (name, key);
  137. }
  138. protected override void OutputTabs ()
  139. {
  140. output.WriteLine ("{0:###0} OutputTabs ()", NextIndex ());
  141. if (full_trace)
  142. WriteTrace (new StackTrace ());
  143. base.OutputTabs ();
  144. }
  145. protected override string RenderAfterContent ()
  146. {
  147. output.WriteLine ("{0:###0} RenderAfterContent ()", NextIndex ());
  148. if (full_trace)
  149. WriteTrace (new StackTrace ());
  150. return null;
  151. }
  152. protected override string RenderAfterTag ()
  153. {
  154. output.WriteLine ("{0:###0} RenderAfterTag ()", NextIndex ());
  155. if (full_trace)
  156. WriteTrace (new StackTrace ());
  157. return null;
  158. }
  159. protected override string RenderBeforeContent ()
  160. {
  161. output.WriteLine ("{0:###0} RenderBeforeContent ()", NextIndex ());
  162. if (full_trace)
  163. WriteTrace (new StackTrace ());
  164. return null;
  165. }
  166. protected override string RenderBeforeTag ()
  167. {
  168. output.WriteLine ("{0:###0} RenderBeforeTag ()", NextIndex ());
  169. if (full_trace)
  170. WriteTrace (new StackTrace ());
  171. return null;
  172. }
  173. public override void RenderBeginTag (string tagName)
  174. {
  175. output.WriteLine ("{0:###0} RenderBeginTag ({1})", NextIndex (), tagName);
  176. if (full_trace)
  177. WriteTrace (new StackTrace ());
  178. base.RenderBeginTag (tagName);
  179. }
  180. public override void RenderBeginTag (HtmlTextWriterTag tagKey)
  181. {
  182. output.WriteLine ("{0:###0} RenderBeginTag ({1})", NextIndex (), tagKey);
  183. if (full_trace)
  184. WriteTrace (new StackTrace ());
  185. base.RenderBeginTag (tagKey);
  186. }
  187. public override void RenderEndTag ()
  188. {
  189. output.WriteLine ("{0:###0} RenderEndTag ()", NextIndex ());
  190. if (full_trace)
  191. WriteTrace (new StackTrace ());
  192. base.RenderEndTag ();
  193. }
  194. public override void WriteAttribute (string name, string value, bool fEncode)
  195. {
  196. output.WriteLine ("{0:###0} WriteAttribute ({1}, {2}, {3})", NextIndex (), name, value, fEncode);
  197. if (full_trace)
  198. WriteTrace (new StackTrace ());
  199. base.WriteAttribute (name, value, fEncode);
  200. }
  201. public override void WriteBeginTag (string tagName)
  202. {
  203. output.WriteLine ("{0:###0} WriteBeginTag ({1})", NextIndex (), tagName);
  204. if (full_trace)
  205. WriteTrace (new StackTrace ());
  206. base.WriteBeginTag (tagName);
  207. }
  208. public override void WriteEndTag (string tagName)
  209. {
  210. output.WriteLine ("{0:###0} WriteEndTag ({1})", NextIndex (), tagName);
  211. if (full_trace)
  212. WriteTrace (new StackTrace ());
  213. base.WriteEndTag (tagName);
  214. }
  215. public override void WriteFullBeginTag (string tagName)
  216. {
  217. output.WriteLine ("{0:###0} WriteFullBeginTag ({1})", NextIndex (), tagName);
  218. if (full_trace)
  219. WriteTrace (new StackTrace ());
  220. base.WriteFullBeginTag (tagName);
  221. }
  222. public override void WriteStyleAttribute (string name, string value, bool fEncode)
  223. {
  224. output.WriteLine ("{0:###0} WriteStyleAttribute ({1}, {2}, {3})", NextIndex (), name, value, fEncode);
  225. if (full_trace)
  226. WriteTrace (new StackTrace ());
  227. base.WriteStyleAttribute (name, value, fEncode);
  228. }
  229. }
  230. }