001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.schema2beansdev.gen;
043:
044: import java.util.*;
045: import java.io.*;
046:
047: public class GenBuffer extends Writer {
048: protected int INITIAL_BUFFER_CAPACITY = 512;
049: protected int curOut;
050: protected StringBuffer listOut[];
051: protected int bufferCount;
052: protected Stack selectStack = new Stack();
053: private boolean first = false;
054: private String separator = null;
055:
056: /**
057: * @param bufferCount is the number of buffers to create.
058: */
059: public GenBuffer(int bufferCount) {
060: this .bufferCount = bufferCount;
061: listOut = new StringBuffer[bufferCount];
062: privateInit();
063: }
064:
065: /**
066: * @return a fresh GenBuffer with the configuration duplicated
067: * (number of buffers, etc). The buffers are NOT
068: * duplicated.
069: */
070: public GenBuffer(GenBuffer source) {
071: bufferCount = source.bufferCount;
072: listOut = new StringBuffer[bufferCount];
073: INITIAL_BUFFER_CAPACITY = source.INITIAL_BUFFER_CAPACITY;
074: curOut = source.curOut;
075: first = source.first;
076: separator = source.separator;
077: privateInit();
078: }
079:
080: /**
081: * Reset the buffers so that you can use it again.
082: */
083: public void reset() {
084: privateInit();
085: }
086:
087: private void privateInit() {
088: for (int i = 0; i < bufferCount; i++) {
089: listOut[i] = new StringBuffer();
090: listOut[i].ensureCapacity(INITIAL_BUFFER_CAPACITY);
091: }
092: }
093:
094: /**
095: * Insert some additional buffers.
096: * Previous buffers are not adjusted automatically.
097: * select() should be called afterwards to reestablish current buffer.
098: */
099: public void insertAdditionalBuffers(int offset, int count) {
100: StringBuffer[] newListOut = new StringBuffer[bufferCount
101: + count];
102: // copy before and including offset
103: System.arraycopy(listOut, 0, newListOut, 0, offset + 1);
104: // copy after offset
105: System.arraycopy(listOut, offset + 1, newListOut, offset + 1
106: + count, bufferCount - offset - 1);
107: // init the new elements
108: for (int i = 0; i < count; ++i) {
109: newListOut[offset + 1 + i] = new StringBuffer();
110: newListOut[offset + 1 + i]
111: .ensureCapacity(INITIAL_BUFFER_CAPACITY);
112: }
113: bufferCount += count;
114: listOut = newListOut;
115: }
116:
117: /**
118: * This method has no effect.
119: */
120: public void close() {
121: }
122:
123: /**
124: * This does nothing as we're all in memory.
125: */
126: public void flush() {
127: }
128:
129: /**
130: * Select the current buffer to use as output.
131: * Valid range is 0 <= @param bufferNum < bufferCount
132: */
133: public void select(int bufferNum) {
134: if (bufferNum >= bufferCount || bufferNum < 0)
135: throw new IllegalArgumentException("Invalid bufferNum "
136: + bufferNum + " out of " + bufferCount);
137: curOut = bufferNum;
138: }
139:
140: public void pushSelect(int bufferNum) {
141: int prevOut = curOut;
142: // do the select before the push, in case the select throws an exception
143: select(bufferNum);
144: selectStack.push(new Integer(prevOut));
145: }
146:
147: public void popSelect() {
148: curOut = ((Integer) selectStack.pop()).intValue();
149: }
150:
151: /**
152: * This method will get called before any write occurs.
153: */
154: protected void beforeWriteHook() {
155: }
156:
157: /**
158: * Append the parameter to the current buffer.
159: */
160: public void write(boolean b) throws IOException {
161: beforeWriteHook();
162: listOut[curOut].append(b);
163: }
164:
165: /**
166: * Append the parameter to the current buffer.
167: */
168: public void write(char c) throws IOException {
169: beforeWriteHook();
170: listOut[curOut].append(c);
171: }
172:
173: /**
174: * Append the parameter to the current buffer.
175: */
176: public void write(char[] str) throws IOException {
177: beforeWriteHook();
178: listOut[curOut].append(str);
179: }
180:
181: /**
182: * @see Writer
183: */
184: public void write(char[] cbuf, int off, int len) throws IOException {
185: beforeWriteHook();
186: listOut[curOut].append(cbuf, off, len);
187: }
188:
189: /**
190: * Append the parameter to the current buffer.
191: */
192: public void write(double d) throws IOException {
193: beforeWriteHook();
194: listOut[curOut].append(d);
195: }
196:
197: /**
198: * Append the parameter to the current buffer.
199: */
200: public void write(float f) throws IOException {
201: beforeWriteHook();
202: listOut[curOut].append(f);
203: }
204:
205: private CharArrayWriter caw = null;
206:
207: /**
208: * Append the parameter to the current buffer *as a character*.
209: */
210: public void write(int i) throws IOException {
211: // A CharArrayWriter is used, because that was the only way I could
212: // figure out how to convert an int into a String.
213: if (caw == null)
214: caw = new CharArrayWriter(2);
215: caw.write(i);
216: beforeWriteHook();
217: listOut[curOut].append(caw.toString());
218: caw.reset();
219: }
220:
221: /**
222: * Append the parameter to the current buffer *as a character*.
223: */
224: public void write(long l) throws IOException {
225: write((int) l);
226: }
227:
228: /**
229: * Append the parameter to the current buffer.
230: * @see StringBuffer
231: */
232: public void write(Object obj) throws IOException {
233: beforeWriteHook();
234: listOut[curOut].append(obj);
235: }
236:
237: /**
238: * write @param s to the current buffer
239: */
240: public void write(String s) throws IOException {
241: beforeWriteHook();
242: listOut[curOut].append(s);
243: }
244:
245: /**
246: * write @param s to the current buffer
247: */
248: public void write(StringBuffer s) throws IOException {
249: beforeWriteHook();
250: listOut[curOut].append(s);
251: }
252:
253: /**
254: * write @param s1 and @param s2 to the current buffer just as if
255: * 2 separate writes were done.
256: */
257: public void write(String s1, String s2) throws IOException {
258: beforeWriteHook();
259: listOut[curOut].append(s1);
260: listOut[curOut].append(s2);
261: }
262:
263: /**
264: * write @param s1, @param s2, and @param s3 to the current buffer
265: * just as if 3 separate writes were done.
266: */
267: public void write(String s1, String s2, String s3)
268: throws IOException {
269: beforeWriteHook();
270: listOut[curOut].append(s1);
271: listOut[curOut].append(s2);
272: listOut[curOut].append(s3);
273: }
274:
275: /**
276: * write @param s1, @param s2, @param s3, and @param s4 to the current buffer
277: * just as if 3 separate writes were done.
278: */
279: public void write(String s1, String s2, String s3, String s4)
280: throws IOException {
281: beforeWriteHook();
282: listOut[curOut].append(s1);
283: listOut[curOut].append(s2);
284: listOut[curOut].append(s3);
285: listOut[curOut].append(s4);
286: }
287:
288: public void write(String str, int bufferNum) throws IOException {
289: if (bufferNum >= bufferCount || bufferNum < 0)
290: throw new IllegalArgumentException("Invalid bufferNum "
291: + bufferNum + " out of " + bufferCount);
292: beforeWriteHook();
293: listOut[bufferNum].append(str);
294: }
295:
296: /**
297: * setFirst and writeNext work in together to allow easier generation
298: * or lists where the items in the list are separated by some text
299: * between each of them.
300: * For instance,
301: * setFirst(", ");
302: * if (doBlue) writeNext("blue");
303: * if (doGreen) writeNext("green");
304: * if (doRed) writeNext("red");
305: */
306: public void setFirst(String separator) {
307: first = true;
308: this .separator = separator;
309: }
310:
311: /**
312: * Write the next text in the sequence.
313: */
314: public void writeNext(String msg) throws IOException {
315: writeNext();
316: write(msg);
317: }
318:
319: /**
320: * Write the next text in the sequence.
321: */
322: public void writeNext(String msg1, String msg2) throws IOException {
323: writeNext();
324: write(msg1);
325: write(msg2);
326: }
327:
328: /**
329: * Write the next text in the sequence.
330: */
331: public void writeNext(String msg1, String msg2, String msg3)
332: throws IOException {
333: writeNext();
334: write(msg1);
335: write(msg2);
336: write(msg3);
337: }
338:
339: /**
340: * Begin the next in the sequence.
341: * Equivalent to writeNext(""), where we'll write out the separator.
342: */
343: public void writeNext() throws IOException {
344: if (first)
345: first = false;
346: else
347: write(separator);
348: }
349:
350: /**
351: * Send buffers to @param out
352: */
353: public void writeTo(Writer out) throws IOException {
354: for (int i = 0; i < bufferCount; i++)
355: out.write(listOut[i].toString());
356: }
357:
358: /**
359: * Send buffers to @param out
360: */
361: public void writeTo(OutputStream out) throws IOException {
362: for (int i = 0; i < bufferCount; i++)
363: out.write(listOut[i].toString().getBytes());
364: }
365:
366: public void writeTo(StringBuffer out) {
367: for (int i = 0; i < bufferCount; i++)
368: out.append(listOut[i]);
369: }
370:
371: public void writeTo(GenBuffer out) {
372: int minInCommonBufferCount = bufferCount;
373: if (out.bufferCount < bufferCount)
374: minInCommonBufferCount = out.bufferCount;
375: for (int i = 0; i < minInCommonBufferCount; i++)
376: out.listOut[i].append(listOut[i]);
377: if (out.bufferCount < bufferCount) {
378: // We've got more buffers than our destination. Put all
379: // of our "extra" ones at the end.
380: for (int i = minInCommonBufferCount; i < bufferCount; i++)
381: out.listOut[minInCommonBufferCount - 1]
382: .append(listOut[i]);
383: } else {
384: out.curOut = curOut;
385: }
386: out.first = first;
387: out.separator = separator;
388: }
389:
390: /**
391: * Has anything actually been written here?
392: */
393: public boolean anyContent() {
394: for (int i = 0; i < bufferCount; i++)
395: if (listOut[i].length() > 0)
396: return true;
397: return false;
398: }
399:
400: public int getCurrentPosition() {
401: return listOut[curOut].length();
402: }
403:
404: public void truncateAtPosition(int pos) {
405: listOut[curOut].setLength(pos);
406: }
407:
408: /**
409: * Return the active StringBuffer
410: */
411: public StringBuffer getBuffer() {
412: return listOut[curOut];
413: }
414:
415: /**
416: * Ensures the capacity of every buffer is at least @param minimumCapacity.
417: */
418: public void ensureCapacity(int minimumCapacity) {
419: for (int i = 0; i < bufferCount; i++)
420: listOut[i].ensureCapacity(minimumCapacity);
421: }
422: }
|