001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
042: */package org.exolab.javasource;
043:
044: import java.io.Writer;
045:
046: /**
047: * The writer used by the javasource classes.
048: *
049: * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
050: * @version $Revision: 6669 $ $Date: 2005-03-30 03:29:24 -0700 (Wed, 30 Mar 2005) $
051: */
052: public final class JSourceWriter extends Writer {
053: //--------------------------------------------------------------------------
054:
055: /** The default character to use for indentation. */
056: public static final char DEFAULT_CHAR = ' ';
057:
058: /** The default indentation size. */
059: public static final short DEFAULT_SIZE = 4;
060:
061: //--------------------------------------------------------------------------
062:
063: /** The line separator to use for the writeln methods. */
064: private String _lineSeparator = System
065: .getProperty("line.separator");
066:
067: /** Flag for indicating whether we need to add the whitespace to beginning
068: * of next write call. */
069: private boolean _addIndentation = true;
070:
071: /** A flag indicating whether this JSourceWriter should perform autoflush at
072: * the end of a new line. */
073: private boolean _autoflush = false;
074:
075: /** The tab (indentation) size. */
076: private short _tabSize = DEFAULT_SIZE;
077:
078: /** The tab representation. */
079: private char[] _tab;
080:
081: /** The character to use for indentation. */
082: private char _tabChar = DEFAULT_CHAR;
083:
084: /** The current tab level. */
085: private short _tabLevel = 0;
086:
087: /** The writer to send all output to. */
088: private Writer _out = null;
089:
090: //--------------------------------------------------------------------------
091:
092: /**
093: * Creates a new JSourceWriter.
094: *
095: * @param out The Writer to write the actual output to.
096: */
097: public JSourceWriter(final Writer out) {
098: this (out, DEFAULT_SIZE, DEFAULT_CHAR, false);
099: }
100:
101: /**
102: * Creates a new JSourceWriter.
103: *
104: * @param out The Writer to write the actual output to.
105: * @param autoflush A boolean indicating whether or not to perform automatic
106: * flush at the end of a line.
107: */
108: public JSourceWriter(final Writer out, final boolean autoflush) {
109: this (out, DEFAULT_SIZE, DEFAULT_CHAR, autoflush);
110: }
111:
112: /**
113: * Creates a new JSourceWriter.
114: *
115: * @param out The Writer to write the actual output to.
116: * @param tabSize The size of each indentation.
117: * @param autoflush A boolean indicating whether or not to perform automatic
118: * flush at the end of a line.
119: */
120: public JSourceWriter(final Writer out, final short tabSize,
121: final boolean autoflush) {
122: this (out, tabSize, DEFAULT_CHAR, autoflush);
123: }
124:
125: /**
126: * Creates a new JSourceWriter.
127: *
128: * @param out The Writer to write the actual output to.
129: * @param tabSize The size of each indentation.
130: * @param tabChar The character to use for indentation.
131: * @param autoflush A boolean indicating whether or not to perform an automatic
132: * flush at the end of each line.
133: */
134: public JSourceWriter(final Writer out, final short tabSize,
135: final char tabChar, final boolean autoflush) {
136: _out = out;
137: _autoflush = autoflush;
138: _tabChar = tabChar;
139: _tabSize = tabSize;
140: createTab();
141: }
142:
143: //--------------------------------------------------------------------------
144:
145: /**
146: * Returns the line separator being used by this JSourceWriter.
147: *
148: * @return The line separator being used by this JSourceWriter.
149: */
150: public String getLineSeparator() {
151: return _lineSeparator;
152: }
153:
154: /**
155: * Increases the indentation level by 1.
156: */
157: public void indent() {
158: ++_tabLevel;
159: }
160:
161: /**
162: * Checks to see if the cursor is positioned on a new line.
163: *
164: * @return True if the cursor is at the start of a new line, otherwise false.
165: */
166: public boolean isNewline() {
167: //-- if we need to add indentation, we are on a new line
168: return _addIndentation;
169: }
170:
171: /**
172: * Sets the line separator to use at the end of each line. Typically a line
173: * separator will be one of the following:
174: * <ul>
175: * <li>"\r\n" for MS Windows</li>
176: * <li>"\n" for UNIX</li>
177: * <li>"\r" for Macintosh</li>
178: * </ul>
179: *
180: * @param lineSeparator The String to use as a line separator.
181: */
182: public void setLineSeparator(final String lineSeparator) {
183: _lineSeparator = lineSeparator;
184: }
185:
186: /**
187: * Decreases the indentation level by 1.
188: */
189: public void unindent() {
190: if (_tabLevel > 0) {
191: --_tabLevel;
192: }
193: }
194:
195: /**
196: * Returns the current indentation level.
197: *
198: * @return The current indentation level.
199: */
200: protected short getIndentLevel() {
201: return _tabLevel;
202: }
203:
204: /**
205: * Returns the current indent size (getIndentLevel()*tabSize).
206: *
207: * @return The current indent size.
208: */
209: protected short getIndentSize() {
210: return (short) (_tabLevel * _tabSize);
211: }
212:
213: /**
214: * Returns the current character used for indentation.
215: *
216: * @return The current character used for indentation.
217: */
218: protected char getIndentChar() {
219: return _tabChar;
220: }
221:
222: /**
223: * Always applies the current indentation.
224: */
225: protected void writeIndent() {
226: try {
227: for (int i = 0; i < _tabLevel; i++) {
228: _out.write(_tab);
229: }
230: } catch (java.io.IOException ioe) {
231: // ignore
232: }
233: }
234:
235: /**
236: * If indentation has not already been applied, then applies it.
237: */
238: private void ensureIndent() {
239: if (_addIndentation) {
240: writeIndent();
241: _addIndentation = false;
242: }
243: }
244:
245: /**
246: * Writes the line separator character to the writer.
247: */
248: private void linefeed() {
249: try {
250: _out.write(_lineSeparator);
251: } catch (java.io.IOException ioe) {
252: // ignore
253: }
254: }
255:
256: /**
257: * Creates the tab from the tabSize and the tabChar.
258: */
259: private void createTab() {
260: _tab = new char[_tabSize];
261: for (int i = 0; i < _tabSize; i++) {
262: _tab[i] = _tabChar;
263: }
264: }
265:
266: //--------------------------------------------------------------------------
267:
268: public void write(final float f) {
269: write(String.valueOf(f));
270: }
271:
272: public void write(final long l) {
273: write(String.valueOf(l));
274: }
275:
276: public void write(final double d) {
277: write(String.valueOf(d));
278: }
279:
280: public void write(final Object obj) {
281: write(obj.toString());
282: }
283:
284: public void write(final boolean b) {
285: write(String.valueOf(b));
286: }
287:
288: //--------------------------------------------------------------------------
289:
290: public void writeln() {
291: synchronized (lock) {
292: linefeed();
293: _addIndentation = true;
294: }
295: }
296:
297: public void writeln(final float f) {
298: synchronized (lock) {
299: ensureIndent();
300: try {
301: _out.write(String.valueOf(f));
302: } catch (java.io.IOException ioe) {
303: // ignore
304: }
305: linefeed();
306: _addIndentation = true;
307: }
308: }
309:
310: public void writeln(final long l) {
311: synchronized (lock) {
312: ensureIndent();
313: try {
314: _out.write(String.valueOf(l));
315: } catch (java.io.IOException ioe) {
316: // ignore
317: }
318: linefeed();
319: _addIndentation = true;
320: }
321: }
322:
323: public void writeln(final int i) {
324: synchronized (lock) {
325: ensureIndent();
326: try {
327: _out.write(String.valueOf(i));
328: } catch (java.io.IOException ioe) {
329: // ignore
330: }
331: linefeed();
332: _addIndentation = true;
333: }
334: }
335:
336: public void writeln(final double d) {
337: synchronized (lock) {
338: ensureIndent();
339: try {
340: _out.write(String.valueOf(d));
341: } catch (java.io.IOException ioe) {
342: // ignore
343: }
344: linefeed();
345: _addIndentation = true;
346: }
347: }
348:
349: public void writeln(final Object obj) {
350: synchronized (lock) {
351: ensureIndent();
352: try {
353: _out.write(obj.toString());
354: } catch (java.io.IOException ioe) {
355: // ignore
356: }
357: linefeed();
358: _addIndentation = true;
359: }
360: }
361:
362: public void writeln(final String string) {
363: synchronized (lock) {
364: ensureIndent();
365: try {
366: _out.write(string);
367: } catch (java.io.IOException ioe) {
368: // ignore
369: }
370: linefeed();
371: _addIndentation = true;
372: }
373: }
374:
375: public void writeln(final char[] chars) {
376: synchronized (lock) {
377: ensureIndent();
378: try {
379: _out.write(chars);
380: } catch (java.io.IOException ioe) {
381: // ignore
382: }
383: linefeed();
384: _addIndentation = true;
385: }
386: }
387:
388: public void writeln(final boolean b) {
389: synchronized (lock) {
390: ensureIndent();
391: try {
392: _out.write(String.valueOf(b));
393: } catch (java.io.IOException ioe) {
394: // ignore
395: }
396: linefeed();
397: _addIndentation = true;
398: }
399: }
400:
401: public void writeln(final char c) {
402: synchronized (lock) {
403: ensureIndent();
404: try {
405: _out.write(c);
406: } catch (java.io.IOException ioe) {
407: // ignore
408: }
409: linefeed();
410: _addIndentation = true;
411: }
412: }
413:
414: //--------------------------------------------------------------------------
415:
416: /**
417: * {@inheritDoc}
418: */
419: public void close() {
420: try {
421: _out.close();
422: } catch (java.io.IOException ioe) {
423: // ignore
424: }
425: }
426:
427: /**
428: * {@inheritDoc}
429: */
430: public void flush() {
431: try {
432: _out.flush();
433: } catch (java.io.IOException ioe) {
434: // ignore
435: }
436: }
437:
438: /**
439: * {@inheritDoc}
440: */
441: public void write(final String s, final int off, final int len) {
442: synchronized (lock) {
443: ensureIndent();
444: try {
445: _out.write(s, off, len);
446: } catch (java.io.IOException ioe) {
447: // ignore
448: }
449: if (_autoflush) {
450: flush();
451: }
452: }
453: }
454:
455: /**
456: * {@inheritDoc}
457: */
458: public void write(final String s) {
459: synchronized (lock) {
460: ensureIndent();
461: try {
462: _out.write(s);
463: } catch (java.io.IOException ioe) {
464: // ignore
465: }
466: if (_autoflush) {
467: flush();
468: }
469: }
470: }
471:
472: /**
473: * {@inheritDoc}
474: */
475: public void write(final char[] buf) {
476: synchronized (lock) {
477: ensureIndent();
478: try {
479: _out.write(buf);
480: } catch (java.io.IOException ioe) {
481: // ignore
482: }
483: if (_autoflush) {
484: flush();
485: }
486: }
487: }
488:
489: /**
490: * {@inheritDoc}
491: */
492: public void write(final int c) {
493: synchronized (lock) {
494: ensureIndent();
495: try {
496: _out.write(c);
497: } catch (java.io.IOException ioe) {
498: // ignore
499: }
500: if (_autoflush) {
501: flush();
502: }
503: }
504: }
505:
506: /**
507: * {@inheritDoc}
508: */
509: public void write(final char[] buf, final int off, final int len) {
510: synchronized (lock) {
511: ensureIndent();
512: try {
513: _out.write(buf, off, len);
514: } catch (java.io.IOException ioe) {
515: // ignore
516: }
517: if (_autoflush) {
518: flush();
519: }
520: }
521: }
522:
523: //--------------------------------------------------------------------------
524: }
|