The Labs \ Source Viewer \ SSCLI \ System.Xml.Serialization \ IndentedWriter

  1. //------------------------------------------------------------------------------
  2. // <copyright file="IndentedWriter.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. // <owner current="true" primary="true">ElenaK</owner>
  15. //------------------------------------------------------------------------------
  16. namespace System.Xml.Serialization
  17. {
  18.    
  19.     using System.IO;
  20.    
  21.     /// <include file='doc\IndentedWriter.uex' path='docs/doc[@for="IndentedWriter"]/*' />
  22.     /// <devdoc>
  23.     /// This class will write to a stream and manage indentation.
  24.     /// </devdoc>
  25.     internal class IndentedWriter
  26.     {
  27.         TextWriter writer;
  28.         bool needIndent;
  29.         int indentLevel;
  30.         bool compact;
  31.        
  32.         internal IndentedWriter(TextWriter writer, bool compact)
  33.         {
  34.             this.writer = writer;
  35.             this.compact = compact;
  36.         }
  37.        
  38.         internal int Indent {
  39.             get { return indentLevel; }
  40.             set { indentLevel = value; }
  41.         }
  42.        
  43.         internal void Write(string s)
  44.         {
  45.             if (needIndent)
  46.                 WriteIndent();
  47.             writer.Write(s);
  48.         }
  49.        
  50.         internal void Write(char c)
  51.         {
  52.             if (needIndent)
  53.                 WriteIndent();
  54.             writer.Write(c);
  55.         }
  56.        
  57.         internal void WriteLine(string s)
  58.         {
  59.             if (needIndent)
  60.                 WriteIndent();
  61.             writer.WriteLine(s);
  62.             needIndent = true;
  63.         }
  64.        
  65.         internal void WriteLine()
  66.         {
  67.             writer.WriteLine();
  68.             needIndent = true;
  69.         }
  70.        
  71.         internal void WriteIndent()
  72.         {
  73.             needIndent = false;
  74.             if (!compact) {
  75.                 for (int i = 0; i < indentLevel; i++) {
  76.                     writer.Write(" ");
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }

Developer Fusion