2005-10-24 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System / System.CodeDom.Compiler / IndentedTextWriter.cs
1 //
2 // System.CodeDom.Compiler IndentedTextWriter class
3 //
4 // Author:
5 //   Daniel Stodden (stodden@in.tum.de)
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
30 using System.IO;
31 using System.Security.Permissions;
32 using System.Text;
33
34 namespace System.CodeDom.Compiler {
35         
36         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
37         [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
38         public class IndentedTextWriter
39                 : TextWriter
40         {
41                 private TextWriter writer;
42                 private string tabString;
43                 private int indent;
44                 private bool newline;
45
46                 //
47                 // Constructors
48                 //
49                 public IndentedTextWriter( TextWriter writer )
50                 {
51                         this.writer = writer;
52                         this.tabString = DefaultTabString;
53                         newline = true;
54                 }
55
56                 public IndentedTextWriter( TextWriter writer, string tabString )
57                 {
58                         this.writer = writer;
59                         this.tabString = tabString;
60                         newline = true;
61                 }
62
63                 
64                 //
65                 // Fields
66                 //
67                 public const string DefaultTabString = "    ";
68
69                 //
70                 // Properties
71                 //
72                 public override Encoding Encoding {
73                         get {
74                                 return writer.Encoding;
75                         }
76                 }
77
78                 public int Indent {
79                         get {
80                                 return indent;
81                         }
82                         set {
83                                 indent = value;
84                         }
85                 }
86
87                 public TextWriter InnerWriter {
88                         get {
89                                 return writer;
90                         }
91                 }
92
93                 public override string NewLine {
94                         get {
95                                 return writer.NewLine;
96                         }
97                         set {
98                                 writer.NewLine = value;
99                         }
100                 }
101
102                 //
103                 // Methods
104                 //
105                 public override void Close()
106                 {
107                         writer.Close();
108                 }
109
110                 public override void Flush()
111                 {
112                         writer.Flush();
113                 }
114
115                 public override void Write( bool value )
116                 {
117                         OutputTabs();
118                         writer.Write( value );
119                 }
120
121                 public override void Write( char value )
122                 {
123                         OutputTabs();
124                         writer.Write( value );
125                 }
126                 
127                 public override void Write( char[] value )
128                 {
129                         OutputTabs();
130                         writer.Write( value );
131                 }
132
133                 public override void Write( double value )
134                 {
135                         OutputTabs();
136                         writer.Write( value );
137                 }
138
139                 public override void Write( int value )
140                 {
141                         OutputTabs();
142                         writer.Write( value );
143                 }
144
145                 public override void Write( long value )
146                 {
147                         OutputTabs();
148                         writer.Write( value );
149                 }
150
151                 public override void Write( object value )
152                 {
153                         OutputTabs();
154                         writer.Write( value );
155                 }
156
157                 public override void Write( float value )
158                 {
159                         OutputTabs();
160                         writer.Write( value );
161                 }
162
163                 public override void Write( string value )
164                 {
165                         OutputTabs();
166                         writer.Write( value );
167                 }
168
169                 public override void Write( string format, object arg )
170                 {
171                         OutputTabs();
172                         writer.Write( format, arg );
173                 }
174
175                 public override void Write( string format, params object[] args )
176                 {
177                         OutputTabs();
178                         writer.Write( format, args );
179                 }
180
181                 public override void Write( char[] buffer, int index, int count )
182                 {
183                         OutputTabs();
184                         writer.Write( buffer, index, count );
185                 }
186                 
187                 public override void Write( string format, object arg0, object arg1 )
188                 {
189                         OutputTabs();
190                         writer.Write( format, arg0, arg1 );
191                 }
192
193                 
194                 public override void WriteLine()
195                 {
196                         OutputTabs();
197                         writer.WriteLine();
198                         newline = true;
199                 }
200
201                 public override void WriteLine( bool value )
202                 {
203                         OutputTabs();
204                         writer.WriteLine( value );
205                         newline = true;
206                 }
207
208                 public override void WriteLine( char value )
209                 {
210                         OutputTabs();
211                         writer.WriteLine( value );
212                         newline = true;
213                 }
214
215                 public override void WriteLine( char[] value )
216                 {
217                         OutputTabs();
218                         writer.WriteLine( value );
219                         newline = true;
220                 }
221
222                 public override void WriteLine( double value )
223                 {
224                         OutputTabs();
225                         writer.WriteLine( value );
226                         newline = true;
227                 }
228
229                 public override void WriteLine( int value )
230                 {
231                         OutputTabs();
232                         writer.WriteLine( value );
233                         newline = true;
234                 }
235
236                 public override void WriteLine( long value )
237                 {
238                         OutputTabs();
239                         writer.WriteLine( value );
240                         newline = true;
241                 }
242
243                 public override void WriteLine( object value )
244                 {
245                         OutputTabs();
246                         writer.WriteLine( value );
247                         newline = true;
248                 }
249
250                 public override void WriteLine( float value )
251                 {
252                         OutputTabs();
253                         writer.WriteLine( value );
254                         newline = true;
255                 }
256
257                 public override void WriteLine( string value )
258                 {
259                         OutputTabs();
260                         writer.WriteLine( value );
261                         newline = true;
262                 }
263
264                 [CLSCompliant(false)]
265                 public override void WriteLine( uint value )
266                 {
267                         OutputTabs();
268                         writer.WriteLine( value );
269                         newline = true;
270                 }
271
272                 public override void WriteLine( string format, object arg )
273                 {
274                         OutputTabs();
275                         writer.WriteLine( format, arg );
276                         newline = true;
277                 }
278
279                 public override void WriteLine( string format, params object[] args )
280                 {
281                         OutputTabs();
282                         writer.WriteLine( format, args );
283                         newline = true;
284                 }
285
286                 public override void WriteLine( char[] buffer, int index, int count )
287                 {
288                         OutputTabs();
289                         writer.WriteLine( buffer, index, count );
290                         newline = true;
291                 }
292
293                 public override void WriteLine( string format, object arg0, object arg1 )
294                 {
295                         OutputTabs();
296                         writer.WriteLine( format, arg0, arg1 );
297                         newline = true;
298                 }
299
300
301                 public void WriteLineNoTabs( string value )
302                 {
303                         writer.WriteLine( value );
304                         newline = true;
305                 }
306
307
308                 protected virtual void OutputTabs()
309                 {
310                         if ( newline ) {
311                                 for ( int i = 0; i < indent; ++i )
312                                         writer.Write( tabString );
313                                 newline = false;
314                         }
315                 }
316         }
317 }