IndentedTextWriter.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // System.CodeDom.Compiler IndentedTextWriter class
  3. //
  4. // Author:
  5. // Daniel Stodden ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc.
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.IO;
  30. using System.Security.Permissions;
  31. using System.Text;
  32. namespace System.CodeDom.Compiler {
  33. [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
  34. [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
  35. public class IndentedTextWriter
  36. : TextWriter
  37. {
  38. private TextWriter writer;
  39. private string tabString;
  40. private int indent;
  41. private bool newline;
  42. //
  43. // Constructors
  44. //
  45. public IndentedTextWriter( TextWriter writer )
  46. {
  47. this.writer = writer;
  48. this.tabString = DefaultTabString;
  49. newline = true;
  50. }
  51. public IndentedTextWriter( TextWriter writer, string tabString )
  52. {
  53. this.writer = writer;
  54. this.tabString = tabString;
  55. newline = true;
  56. }
  57. //
  58. // Fields
  59. //
  60. public const string DefaultTabString = " ";
  61. //
  62. // Properties
  63. //
  64. public override Encoding Encoding {
  65. get {
  66. return writer.Encoding;
  67. }
  68. }
  69. public int Indent {
  70. get {
  71. return indent;
  72. }
  73. set {
  74. indent = value;
  75. }
  76. }
  77. public TextWriter InnerWriter {
  78. get {
  79. return writer;
  80. }
  81. }
  82. public override string NewLine {
  83. get {
  84. return writer.NewLine;
  85. }
  86. set {
  87. writer.NewLine = value;
  88. }
  89. }
  90. //
  91. // Methods
  92. //
  93. public override void Close()
  94. {
  95. writer.Close();
  96. }
  97. public override void Flush()
  98. {
  99. writer.Flush();
  100. }
  101. public override void Write( bool value )
  102. {
  103. OutputTabs();
  104. writer.Write( value );
  105. }
  106. public override void Write( char value )
  107. {
  108. OutputTabs();
  109. writer.Write( value );
  110. }
  111. public override void Write( char[] value )
  112. {
  113. OutputTabs();
  114. writer.Write( value );
  115. }
  116. public override void Write( double value )
  117. {
  118. OutputTabs();
  119. writer.Write( value );
  120. }
  121. public override void Write( int value )
  122. {
  123. OutputTabs();
  124. writer.Write( value );
  125. }
  126. public override void Write( long value )
  127. {
  128. OutputTabs();
  129. writer.Write( value );
  130. }
  131. public override void Write( object value )
  132. {
  133. OutputTabs();
  134. writer.Write( value );
  135. }
  136. public override void Write( float value )
  137. {
  138. OutputTabs();
  139. writer.Write( value );
  140. }
  141. public override void Write( string value )
  142. {
  143. OutputTabs();
  144. writer.Write( value );
  145. }
  146. public override void Write( string format, object arg )
  147. {
  148. OutputTabs();
  149. writer.Write( format, arg );
  150. }
  151. public override void Write( string format, params object[] args )
  152. {
  153. OutputTabs();
  154. writer.Write( format, args );
  155. }
  156. public override void Write( char[] buffer, int index, int count )
  157. {
  158. OutputTabs();
  159. writer.Write( buffer, index, count );
  160. }
  161. public override void Write( string format, object arg0, object arg1 )
  162. {
  163. OutputTabs();
  164. writer.Write( format, arg0, arg1 );
  165. }
  166. public override void WriteLine()
  167. {
  168. OutputTabs();
  169. writer.WriteLine();
  170. newline = true;
  171. }
  172. public override void WriteLine( bool value )
  173. {
  174. OutputTabs();
  175. writer.WriteLine( value );
  176. newline = true;
  177. }
  178. public override void WriteLine( char value )
  179. {
  180. OutputTabs();
  181. writer.WriteLine( value );
  182. newline = true;
  183. }
  184. public override void WriteLine( char[] value )
  185. {
  186. OutputTabs();
  187. writer.WriteLine( value );
  188. newline = true;
  189. }
  190. public override void WriteLine( double value )
  191. {
  192. OutputTabs();
  193. writer.WriteLine( value );
  194. newline = true;
  195. }
  196. public override void WriteLine( int value )
  197. {
  198. OutputTabs();
  199. writer.WriteLine( value );
  200. newline = true;
  201. }
  202. public override void WriteLine( long value )
  203. {
  204. OutputTabs();
  205. writer.WriteLine( value );
  206. newline = true;
  207. }
  208. public override void WriteLine( object value )
  209. {
  210. OutputTabs();
  211. writer.WriteLine( value );
  212. newline = true;
  213. }
  214. public override void WriteLine( float value )
  215. {
  216. OutputTabs();
  217. writer.WriteLine( value );
  218. newline = true;
  219. }
  220. public override void WriteLine( string value )
  221. {
  222. OutputTabs();
  223. writer.WriteLine( value );
  224. newline = true;
  225. }
  226. [CLSCompliant(false)]
  227. public override void WriteLine( uint value )
  228. {
  229. OutputTabs();
  230. writer.WriteLine( value );
  231. newline = true;
  232. }
  233. public override void WriteLine( string format, object arg )
  234. {
  235. OutputTabs();
  236. writer.WriteLine( format, arg );
  237. newline = true;
  238. }
  239. public override void WriteLine( string format, params object[] args )
  240. {
  241. OutputTabs();
  242. writer.WriteLine( format, args );
  243. newline = true;
  244. }
  245. public override void WriteLine( char[] buffer, int index, int count )
  246. {
  247. OutputTabs();
  248. writer.WriteLine( buffer, index, count );
  249. newline = true;
  250. }
  251. public override void WriteLine( string format, object arg0, object arg1 )
  252. {
  253. OutputTabs();
  254. writer.WriteLine( format, arg0, arg1 );
  255. newline = true;
  256. }
  257. public void WriteLineNoTabs( string value )
  258. {
  259. writer.WriteLine( value );
  260. newline = true;
  261. }
  262. protected virtual void OutputTabs()
  263. {
  264. if ( newline ) {
  265. for ( int i = 0; i < indent; ++i )
  266. writer.Write( tabString );
  267. newline = false;
  268. }
  269. }
  270. }
  271. }