KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > plugins > proxygen > SourceWriter


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: SourceWriter.java,v 1.1 2004/11/26 01:51:14 tanderson Exp $
44  */

45 package org.exolab.jms.plugins.proxygen;
46
47 import java.io.BufferedWriter JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.Writer JavaDoc;
50
51
52 /**
53  * Helper class for generating formatted Java source
54  *
55  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:14 $
56  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
57  */

58 public class SourceWriter extends BufferedWriter JavaDoc {
59
60     /**
61      * The current indent
62      */

63     private int _indent = 0;
64
65     /**
66      * The number of spaces to indent
67      */

68     private final int _spaces = 4;
69
70     /**
71      * Determines if at the start of a new line
72      */

73     private boolean _startOfLine = true;
74
75
76     /**
77      * Construct a new <code>SourceWriter</code>
78      *
79      * @param writer the writer to write to
80      */

81     public SourceWriter(Writer JavaDoc writer) {
82         super(writer);
83     }
84
85     /**
86      * Increments the indentation
87      */

88     public void incIndent() {
89         ++_indent;
90     }
91
92     /**
93      * Decrements the indentation
94      */

95     public void decIndent() {
96         --_indent;
97     }
98
99     /**
100      * Write a single character
101      *
102      * @param ch the character to write
103      * @throws IOException for any I/O error
104      */

105     public void write(int ch) throws IOException JavaDoc {
106         indent();
107         super.write(ch);
108     }
109
110     /**
111      * Write a portion of an array of characters.
112      *
113      * @param buffer the buffer to write
114      * @param offset the offset into <code>buffer</code>
115      * @param length the no. of characters to write
116      * @throws IOException for any I/O error
117      */

118     public void write(char[] buffer, int offset, int length)
119         throws IOException JavaDoc {
120         indent();
121         super.write(buffer, offset, length);
122     }
123
124     /**
125      * Write a portion of a String.
126      *
127      * @param string the String to write
128      * @param offset the offset into <code>string</code>
129      * @param length the no. of characters to write
130      * @throws IOException for any I/O error
131      */

132     public void write(String JavaDoc string, int offset, int length)
133         throws IOException JavaDoc {
134         indent();
135         super.write(string, offset, length);
136     }
137
138     /**
139      * Write a line separator
140      *
141      * @throws IOException for any I/O error
142      */

143     public void writeln() throws IOException JavaDoc {
144         newLine();
145     }
146
147     /**
148      * Write a String terminated by a new line
149      *
150      * @param string the String to write
151      * @throws IOException for any I/O error
152      */

153     public void writeln(String JavaDoc string) throws IOException JavaDoc {
154         write(string);
155         newLine();
156     }
157
158     /**
159      * Write a new line, and increase the indent
160      *
161      * @throws IOException for any I/O error
162      */

163     public void writelnInc() throws IOException JavaDoc {
164         newLine();
165         incIndent();
166     }
167
168     /**
169      * Write a String terminated by a new line, and increase the indent
170      *
171      * @param string the String to write
172      * @throws IOException for any I/O error
173      */

174     public void writelnInc(String JavaDoc string) throws IOException JavaDoc {
175         writeln(string);
176         incIndent();
177     }
178
179     /**
180      * Write a new line, and decrease the indent
181      *
182      * @throws IOException for any I/O error
183      */

184     public void writelnDec() throws IOException JavaDoc {
185         newLine();
186         decIndent();
187     }
188
189     /**
190      * Write a String terminated by a new line, and decrease the indent
191      *
192      * @param string the String to write
193      * @throws IOException for any I/O error
194      */

195     public void writelnDec(String JavaDoc string) throws IOException JavaDoc {
196         writeln(string);
197         decIndent();
198     }
199
200     /**
201      * Write a line separator
202      *
203      * @throws IOException for any error
204      */

205     public void newLine() throws IOException JavaDoc {
206         super.newLine();
207         _startOfLine = true;
208     }
209
210     /**
211      * Indents iff at the start of the line
212      *
213      * @throws IOException for any I/O error
214      */

215     protected void indent() throws IOException JavaDoc {
216         if (_startOfLine) {
217             int count = _indent * _spaces;
218             for (int i = 0; i < count; ++i) {
219                 super.write(' ');
220             }
221             _startOfLine = false;
222         }
223     }
224
225 }
226
Popular Tags