KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beansdev > gen > GenBuffer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.schema2beansdev.gen;
21
22 import java.util.*;
23 import java.io.*;
24
25 public class GenBuffer extends Writer {
26     protected int INITIAL_BUFFER_CAPACITY = 512;
27     protected int curOut;
28     protected StringBuffer JavaDoc listOut[];
29     protected int bufferCount;
30     protected Stack selectStack = new Stack();
31     private boolean first = false;
32     private String JavaDoc separator = null;
33
34     /**
35      * @param bufferCount is the number of buffers to create.
36      */

37     public GenBuffer(int bufferCount) {
38         this.bufferCount = bufferCount;
39         listOut = new StringBuffer JavaDoc[bufferCount];
40         privateInit();
41     }
42     /**
43      * @return a fresh GenBuffer with the configuration duplicated
44      * (number of buffers, etc). The buffers are NOT
45      * duplicated.
46      */

47     public GenBuffer(GenBuffer source) {
48         bufferCount = source.bufferCount;
49         listOut = new StringBuffer JavaDoc[bufferCount];
50         INITIAL_BUFFER_CAPACITY = source.INITIAL_BUFFER_CAPACITY;
51         curOut = source.curOut;
52         first = source.first;
53         separator = source.separator;
54         privateInit();
55     }
56
57     /**
58      * Reset the buffers so that you can use it again.
59      */

60     public void reset() {
61         privateInit();
62     }
63     
64     private void privateInit() {
65         for (int i = 0; i < bufferCount; i++) {
66             listOut[i] = new StringBuffer JavaDoc();
67             listOut[i].ensureCapacity(INITIAL_BUFFER_CAPACITY);
68         }
69     }
70
71     /**
72      * Insert some additional buffers.
73      * Previous buffers are not adjusted automatically.
74      * select() should be called afterwards to reestablish current buffer.
75      */

76     public void insertAdditionalBuffers(int offset, int count) {
77         StringBuffer JavaDoc[] newListOut = new StringBuffer JavaDoc[bufferCount + count];
78         // copy before and including offset
79
System.arraycopy(listOut, 0, newListOut, 0, offset+1);
80         // copy after offset
81
System.arraycopy(listOut, offset+1, newListOut, offset + 1 + count, bufferCount - offset - 1);
82         // init the new elements
83
for (int i = 0; i < count; ++i) {
84             newListOut[offset + 1 + i] = new StringBuffer JavaDoc();
85             newListOut[offset + 1 + i].ensureCapacity(INITIAL_BUFFER_CAPACITY);
86         }
87         bufferCount += count;
88         listOut = newListOut;
89     }
90     
91     /**
92      * This method has no effect.
93      */

94     public void close() {
95     }
96     
97     /**
98      * This does nothing as we're all in memory.
99      */

100     public void flush() {
101     }
102
103     /**
104      * Select the current buffer to use as output.
105      * Valid range is 0 <= @param bufferNum < bufferCount
106      */

107     public void select(int bufferNum) {
108         if (bufferNum >= bufferCount || bufferNum < 0)
109             throw new IllegalArgumentException JavaDoc("Invalid bufferNum "+bufferNum+" out of "+bufferCount);
110         curOut = bufferNum;
111     }
112
113     public void pushSelect(int bufferNum) {
114         int prevOut = curOut;
115         // do the select before the push, in case the select throws an exception
116
select(bufferNum);
117         selectStack.push(new Integer JavaDoc(prevOut));
118     }
119
120     public void popSelect() {
121         curOut = ((Integer JavaDoc) selectStack.pop()).intValue();
122     }
123
124     /**
125      * This method will get called before any write occurs.
126      */

127     protected void beforeWriteHook() {}
128     
129     /**
130      * Append the parameter to the current buffer.
131      */

132     public void write(boolean b) throws IOException {
133         beforeWriteHook();
134         listOut[curOut].append(b);
135     }
136
137     /**
138      * Append the parameter to the current buffer.
139      */

140     public void write(char c) throws IOException {
141         beforeWriteHook();
142         listOut[curOut].append(c);
143     }
144
145     /**
146      * Append the parameter to the current buffer.
147      */

148     public void write(char[] str) throws IOException {
149         beforeWriteHook();
150         listOut[curOut].append(str);
151     }
152
153     /**
154      * @see Writer
155      */

156     public void write(char[] cbuf, int off, int len) throws IOException {
157         beforeWriteHook();
158         listOut[curOut].append(cbuf, off, len);
159     }
160
161     /**
162      * Append the parameter to the current buffer.
163      */

164     public void write(double d) throws IOException {
165         beforeWriteHook();
166         listOut[curOut].append(d);
167     }
168
169     /**
170      * Append the parameter to the current buffer.
171      */

172     public void write(float f) throws IOException {
173         beforeWriteHook();
174         listOut[curOut].append(f);
175     }
176
177     private CharArrayWriter caw = null;
178     /**
179      * Append the parameter to the current buffer *as a character*.
180      */

181     public void write(int i) throws IOException {
182         // A CharArrayWriter is used, because that was the only way I could
183
// figure out how to convert an int into a String.
184
if (caw == null)
185             caw = new CharArrayWriter(2);
186         caw.write(i);
187         beforeWriteHook();
188         listOut[curOut].append(caw.toString());
189         caw.reset();
190     }
191
192     /**
193      * Append the parameter to the current buffer *as a character*.
194      */

195     public void write(long l) throws IOException {
196         write((int)l);
197     }
198
199     /**
200      * Append the parameter to the current buffer.
201      * @see StringBuffer
202      */

203     public void write(Object JavaDoc obj) throws IOException {
204         beforeWriteHook();
205         listOut[curOut].append(obj);
206     }
207
208     /**
209      * write @param s to the current buffer
210      */

211     public void write(String JavaDoc s) throws IOException {
212         beforeWriteHook();
213         listOut[curOut].append(s);
214     }
215
216     /**
217      * write @param s to the current buffer
218      */

219     public void write(StringBuffer JavaDoc s) throws IOException {
220         beforeWriteHook();
221         listOut[curOut].append(s);
222     }
223
224     /**
225      * write @param s1 and @param s2 to the current buffer just as if
226      * 2 separate writes were done.
227      */

228     public void write(String JavaDoc s1, String JavaDoc s2) throws IOException {
229         beforeWriteHook();
230         listOut[curOut].append(s1);
231         listOut[curOut].append(s2);
232     }
233
234     /**
235      * write @param s1, @param s2, and @param s3 to the current buffer
236      * just as if 3 separate writes were done.
237      */

238     public void write(String JavaDoc s1, String JavaDoc s2, String JavaDoc s3) throws IOException {
239         beforeWriteHook();
240         listOut[curOut].append(s1);
241         listOut[curOut].append(s2);
242         listOut[curOut].append(s3);
243     }
244
245     /**
246      * write @param s1, @param s2, @param s3, and @param s4 to the current buffer
247      * just as if 3 separate writes were done.
248      */

249     public void write(String JavaDoc s1, String JavaDoc s2, String JavaDoc s3, String JavaDoc s4) throws IOException {
250         beforeWriteHook();
251         listOut[curOut].append(s1);
252         listOut[curOut].append(s2);
253         listOut[curOut].append(s3);
254         listOut[curOut].append(s4);
255     }
256
257     public void write(String JavaDoc str, int bufferNum) throws IOException {
258         if (bufferNum >= bufferCount || bufferNum < 0)
259             throw new IllegalArgumentException JavaDoc("Invalid bufferNum "+bufferNum+" out of "+bufferCount);
260         beforeWriteHook();
261         listOut[bufferNum].append(str);
262     }
263
264     /**
265      * setFirst and writeNext work in together to allow easier generation
266      * or lists where the items in the list are separated by some text
267      * between each of them.
268      * For instance,
269      * setFirst(", ");
270      * if (doBlue) writeNext("blue");
271      * if (doGreen) writeNext("green");
272      * if (doRed) writeNext("red");
273      */

274     public void setFirst(String JavaDoc separator) {
275         first = true;
276         this.separator = separator;
277     }
278
279     /**
280      * Write the next text in the sequence.
281      */

282     public void writeNext(String JavaDoc msg) throws IOException {
283         writeNext();
284         write(msg);
285     }
286
287     /**
288      * Write the next text in the sequence.
289      */

290     public void writeNext(String JavaDoc msg1, String JavaDoc msg2) throws IOException {
291         writeNext();
292         write(msg1);
293         write(msg2);
294     }
295
296     /**
297      * Write the next text in the sequence.
298      */

299     public void writeNext(String JavaDoc msg1, String JavaDoc msg2, String JavaDoc msg3) throws IOException {
300         writeNext();
301         write(msg1);
302         write(msg2);
303         write(msg3);
304     }
305
306     /**
307      * Begin the next in the sequence.
308      * Equivalent to writeNext(""), where we'll write out the separator.
309      */

310     public void writeNext() throws IOException {
311         if (first)
312             first = false;
313         else
314             write(separator);
315     }
316
317     /**
318      * Send buffers to @param out
319      */

320     public void writeTo(Writer out) throws IOException {
321         for (int i = 0; i < bufferCount; i++)
322             out.write(listOut[i].toString());
323     }
324
325     /**
326      * Send buffers to @param out
327      */

328     public void writeTo(OutputStream out) throws IOException {
329         for (int i = 0; i < bufferCount; i++)
330             out.write(listOut[i].toString().getBytes());
331     }
332
333     public void writeTo(StringBuffer JavaDoc out) {
334         for (int i = 0; i < bufferCount; i++)
335             out.append(listOut[i]);
336     }
337
338     public void writeTo(GenBuffer out) {
339         int minInCommonBufferCount = bufferCount;
340         if (out.bufferCount < bufferCount)
341             minInCommonBufferCount = out.bufferCount;
342         for (int i = 0; i < minInCommonBufferCount; i++)
343             out.listOut[i].append(listOut[i]);
344         if (out.bufferCount < bufferCount) {
345             // We've got more buffers than our destination. Put all
346
// of our "extra" ones at the end.
347
for (int i = minInCommonBufferCount; i < bufferCount; i++)
348                 out.listOut[minInCommonBufferCount-1].append(listOut[i]);
349         } else {
350             out.curOut = curOut;
351         }
352         out.first = first;
353         out.separator = separator;
354     }
355
356     /**
357      * Has anything actually been written here?
358      */

359     public boolean anyContent() {
360         for (int i = 0; i < bufferCount; i++)
361             if (listOut[i].length() > 0)
362                 return true;
363         return false;
364     }
365
366     public int getCurrentPosition() {
367         return listOut[curOut].length();
368     }
369
370     public void truncateAtPosition(int pos) {
371         listOut[curOut].setLength(pos);
372     }
373
374     /**
375      * Return the active StringBuffer
376      */

377     public StringBuffer JavaDoc getBuffer() {
378         return listOut[curOut];
379     }
380
381     /**
382      * Ensures the capacity of every buffer is at least @param minimumCapacity.
383      */

384     public void ensureCapacity(int minimumCapacity) {
385         for (int i = 0; i < bufferCount; i++)
386             listOut[i].ensureCapacity(minimumCapacity);
387     }
388 }
389
Popular Tags