KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > CodeGen > CGFileWriter


1 /* $Id: CGFileWriter.java,v 1.2 2004/05/20 14:23:51 bures Exp $ */
2 package SOFA.SOFAnode.Made.CodeGen;
3
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6
7 /** Class for writing files with generated code. Use only <tt>writeln</tt> methods for writing new line!
8   *
9   * @author Petr Hnetynka
10   */

11 public class CGFileWriter extends java.io.FileWriter JavaDoc {
12   private int indent;
13   private boolean doIndent;
14
15
16   public CGFileWriter(String JavaDoc name) throws IOException JavaDoc {
17     super(name);
18     indent = 0;
19     doIndent = true;
20   }
21
22   public CGFileWriter(File JavaDoc file) throws IOException JavaDoc {
23     super(file);
24     indent = 0;
25     doIndent = true;
26   }
27  
28   /** Line separator in files. */
29   public static final String JavaDoc newLineSep = "\n"; // !!! change to property line.separator ??? may be
30

31   /** Number of spaces of one level of indentation */
32   public static final int indentSize = 2;
33
34
35   /** Current indentaion level */
36   public int curIndent() {
37     return indent;
38   }
39
40   /** Increase intentaion level */
41   public void incIndent() {
42     indent++;
43   }
44
45   /** Decrease intentaion level */
46   public void decIndent() {
47     if (indent != 0)
48       indent--;
49   }
50
51   private void makeIndent() throws IOException JavaDoc {
52     if (doIndent) {
53       for (int i=0; i<indent * indentSize; i++) {
54         super.write(' ');
55       }
56       doIndent = false;
57     }
58   }
59
60   /** Writes new line.
61     *
62     * @exception IOException If an I/O error occurs
63     */

64   public void iwriteln() throws IOException JavaDoc {
65     makeIndent();
66     super.write(newLineSep);
67     flush();
68     doIndent = true;
69   }
70
71   /** Writes text and new line.
72     *
73     * @param text string
74     * @exception IOException If an I/O error occurs
75     */

76   public void iwriteln(String JavaDoc text) throws IOException JavaDoc {
77     makeIndent();
78     super.write(text);
79     super.write(newLineSep);
80     flush();
81     doIndent = true;
82   }
83
84   /** Writes spaces.
85     * @param number number of spaces
86     * @exception IOException If an I/O error occurs
87     */

88   public void iwritespace(int number) throws IOException JavaDoc {
89     makeIndent();
90     for (int i=0;i<number;i++) {
91       super.write(" ");
92     }
93   }
94
95   public void iwrite(char[] cbuf, int off, int len) throws IOException JavaDoc {
96     makeIndent();
97     super.write(cbuf, off, len);
98   }
99
100   public void iwrite(int c) throws IOException JavaDoc {
101     makeIndent();
102     super.write(c);
103   }
104
105   public void iwrite(String JavaDoc str, int off, int len) throws IOException JavaDoc {
106     makeIndent();
107     super.write(str, off, len);
108   }
109  
110   public void iwrite(char[] cbuf) throws IOException JavaDoc {
111     makeIndent();
112     super.write(cbuf);
113   }
114
115   public void iwrite(String JavaDoc str) throws IOException JavaDoc {
116     makeIndent();
117     super.write(str);
118   }
119 }
120
Popular Tags