View Javadoc

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 Intalio, Inc.  For written permission,
18   *    please contact info@codehaus.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 Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.codehaus.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. 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   * INTALIO, INC. 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 1999 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  
46  package org.codehaus.modello.plugin.java.javasource;
47  
48  /*
49   * Copyright (c) 2004, Codehaus.org
50   *
51   * Permission is hereby granted, free of charge, to any person obtaining a copy of
52   * this software and associated documentation files (the "Software"), to deal in
53   * the Software without restriction, including without limitation the rights to
54   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
55   * of the Software, and to permit persons to whom the Software is furnished to do
56   * so, subject to the following conditions:
57   *
58   * The above copyright notice and this permission notice shall be included in all
59   * copies or substantial portions of the Software.
60   *
61   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
66   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
67   * SOFTWARE.
68   */
69  
70  import java.io.Writer;
71  
72  /**
73   * The writer used by the modello classes
74   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
75   * @version $Revision$ $Date$
76   **/
77  public class JSourceWriter extends Writer
78  {
79  
80      /**
81       * The default character to use for indentation
82       **/
83      public static final char DEFAULT_CHAR = ' ';
84  
85      /**
86       * The default indentation size
87       **/
88      public static final short DEFAULT_SIZE = 4;
89  
90      /**
91       * The line separator to use for the writeln methods
92       **/
93      private String lineSeparator = System.getProperty( "line.separator" );
94  
95      /**
96       * Flag for indicating whether we need to add
97       * the whitespace to beginning of next write
98       * call
99       **/
100     private boolean addIndentation = true;
101 
102     /**
103      * A flag indicating whether this JSourceWriter should perform
104      * autoflush at the end of a new line
105      **/
106     private boolean autoflush = false;
107 
108     /**
109      * The tab (indentation) size
110      **/
111     private short tabSize = DEFAULT_SIZE;
112 
113     /**
114      * The tab representation
115      **/
116     private char[] tab;
117 
118     /**
119      * The character to use for indentation
120      **/
121     private char tabChar = DEFAULT_CHAR;
122 
123     /**
124      * The current tab level
125      **/
126     private short tabLevel = 0;
127 
128     /**
129      * The writer to send all output to
130      **/
131     private Writer out = null;
132 
133 
134     /**
135      * Creates a new JSourceWriter
136      * @param out the Writer to write the actual output to
137      **/
138     public JSourceWriter( Writer out )
139     {
140         this( out, DEFAULT_SIZE, DEFAULT_CHAR, false );
141     } //-- JSourceWriter
142 
143     /**
144      * Creates a new JSourceWriter
145      * @param out the Writer to write the actual output to
146      * @param autoflush a boolean indicating whether or not to
147      * perform automatic flush at the end of a line
148      **/
149     public JSourceWriter( Writer out, boolean autoflush )
150     {
151         this( out, DEFAULT_SIZE, DEFAULT_CHAR, autoflush );
152     } //-- JSourceWriter
153 
154     /**
155      * Creates a new JSourceWriter
156      * @param out the Writer to write the actual output to
157      * @param tabSize the size of each indentation
158      * @param autoflush a boolean indicating whether or not to
159      * perform automatic flush at the end of a line
160      **/
161     public JSourceWriter
162         ( Writer out, short tabSize, boolean autoflush )
163     {
164         this( out, tabSize, DEFAULT_CHAR, autoflush );
165     } //-- JSourceWriter
166 
167     /**
168      * Creates a new JSourceWriter
169      * @param out the Writer to write the actual output to
170      * @param tabSize the size of each indentation
171      * @param tabChar the character to use for indentation
172      * @param autoflush a boolean indicating whether or not to
173      * perform automatic flush at the end of a line
174      **/
175     public JSourceWriter
176         ( Writer out, short tabSize, char tabChar, boolean autoflush )
177     {
178         this.out = out;
179         this.autoflush = autoflush;
180         this.tabChar = tabChar;
181         this.tabSize = tabSize;
182         createTab();
183     } //-- JSourceWriter
184 
185     /**
186      * Returns the line separator being used by this JSourceWriter
187      * @return the line separator being used by this JSourceWriter
188      **/
189     public String getLineSeparator()
190     {
191         return lineSeparator;
192     } //-- getLineSeparator
193 
194 
195     /**
196      * Increases the indentation level by 1
197      **/
198     public void indent()
199     {
200         ++tabLevel;
201     } //-- increaseIndent
202 
203     /**
204      * Checks to see if the cursor is positioned on a new line
205      * @return true if the cursor is at the start of a new line, otherwise false
206      **/
207     public boolean isNewline()
208     {
209         //-- if we need to add indentation, we are on a new line
210         return addIndentation;
211     } //--  isNewline
212 
213     /**
214      * Sets the line separator to use at the end of each line
215      * @param lineSeparator the String to use as a line
216      * separator.
217      * <BR>
218      * Typically a line separator will be one of the following:
219      * <BR>
220      * "\r\n" for MS Windows<BR>
221      * "\n"   for UNIX<BR>
222      * "\r"   for Macintosh
223      **/
224     public void setLineSeparator( String lineSeparator )
225     {
226 
227         this.lineSeparator = lineSeparator;
228 
229     } //-- setLineSeparator
230 
231     /**
232      * Decreases the indentation level by 1
233      **/
234     public void unindent()
235     {
236         if ( tabLevel > 0 ) --tabLevel;
237     } //-- decreaseIndent
238 
239 
240     //----------------------------/
241     //- Additional write methods -/
242     //----------------------------/
243 
244     public void write( float f )
245     {
246         write( String.valueOf( f ) );
247     } //-- write(float)
248 
249     public void write( long l )
250     {
251         write( String.valueOf( l ) );
252     } //-- write(long)
253 
254     public void write( double d )
255     {
256         write( String.valueOf( d ) );
257     } //-- write(double)
258 
259     public void write( Object obj )
260     {
261         write( obj.toString() );
262     } //-- write(Object)
263 
264     public void write( boolean b )
265     {
266         write( String.valueOf( b ) );
267     } //-- write(boolean)
268 
269     //- writeln() methods
270 
271     public void writeln()
272     {
273         synchronized ( lock )
274         {
275             linefeed();
276             addIndentation = true;
277         }
278     } //-- writeln
279 
280     public void writeln( float f )
281     {
282         synchronized ( lock )
283         {
284             ensureIndent();
285             try
286             {
287                 out.write( String.valueOf( f ) );
288             }
289             catch ( java.io.IOException ioe )
290             {
291             }
292             ;
293             linefeed();
294             addIndentation = true;
295         }
296     } //-- writeln(float)
297 
298     public void writeln( long l )
299     {
300         synchronized ( lock )
301         {
302             ensureIndent();
303             try
304             {
305                 out.write( String.valueOf( l ) );
306             }
307             catch ( java.io.IOException ioe )
308             {
309             }
310             ;
311             linefeed();
312             addIndentation = true;
313         }
314     } //-- writeln(long)
315 
316     public void writeln( int i )
317     {
318         synchronized ( lock )
319         {
320             ensureIndent();
321             try
322             {
323                 out.write( String.valueOf( i ) );
324             }
325             catch ( java.io.IOException ioe )
326             {
327             }
328             ;
329             linefeed();
330             addIndentation = true;
331         }
332     } //-- writeln(int)
333 
334     public void writeln( double d )
335     {
336         synchronized ( lock )
337         {
338             ensureIndent();
339             try
340             {
341                 out.write( String.valueOf( d ) );
342             }
343             catch ( java.io.IOException ioe )
344             {
345             }
346             ;
347             linefeed();
348             addIndentation = true;
349         }
350     } //-- writeln(double)
351 
352 
353     public void writeln( Object obj )
354     {
355         synchronized ( lock )
356         {
357             ensureIndent();
358             try
359             {
360                 out.write( obj.toString() );
361             }
362             catch ( java.io.IOException ioe )
363             {
364             }
365             ;
366             linefeed();
367             addIndentation = true;
368         }
369     } //-- writeln(Object)
370 
371     public void writeln( String string )
372     {
373         synchronized ( lock )
374         {
375             if ( string.length() > 0 )
376             {
377                 ensureIndent();
378                 try
379                 {
380                     out.write( string );
381                 }
382                 catch ( java.io.IOException ioe )
383                 {
384                 }
385             }
386 
387             linefeed();
388             addIndentation = true;
389         }
390     } //-- writeln(String)
391 
392     public void writeln( char[] chars )
393     {
394         synchronized ( lock )
395         {
396             ensureIndent();
397             try
398             {
399                 out.write( chars );
400             }
401             catch ( java.io.IOException ioe )
402             {
403             }
404             ;
405             linefeed();
406             addIndentation = true;
407         }
408     } //-- writeln(char[])
409 
410     public void writeln( boolean b )
411     {
412         synchronized ( lock )
413         {
414             ensureIndent();
415             try
416             {
417                 out.write( String.valueOf( b ) );
418             }
419             catch ( java.io.IOException ioe )
420             {
421             }
422             ;
423             linefeed();
424             addIndentation = true;
425         }
426     } //-- writeln(boolean)
427 
428     public void writeln( char c )
429     {
430         synchronized ( lock )
431         {
432             ensureIndent();
433             try
434             {
435                 out.write( c );
436             }
437             catch ( java.io.IOException ioe )
438             {
439             }
440             ;
441             linefeed();
442             addIndentation = true;
443         }
444     } //-- writeln(char)
445 
446 
447     //-----------------------/
448     //- Methods from Writer -/
449     //-----------------------/
450 
451     public void close()
452     {
453         try
454         {
455             out.close();
456         }
457         catch ( java.io.IOException ioe )
458         {
459         }
460         ;
461     } //-- close
462 
463 
464     public void flush()
465     {
466         try
467         {
468             out.flush();
469         }
470         catch ( java.io.IOException ioe )
471         {
472         }
473         ;
474     } //-- flush
475 
476 
477     public void write( String s, int off, int len )
478     {
479         synchronized ( lock )
480         {
481             ensureIndent();
482             try
483             {
484                 out.write( s, off, len );
485             }
486             catch ( java.io.IOException ioe )
487             {
488             }
489             ;
490             if ( autoflush ) flush();
491         }
492     } //-- write
493 
494 
495     public void write( String s )
496     {
497         synchronized ( lock )
498         {
499             ensureIndent();
500             try
501             {
502                 out.write( s );
503             }
504             catch ( java.io.IOException ioe )
505             {
506             }
507             ;
508             if ( autoflush ) flush();
509         }
510     } //-- write
511 
512     public void write( char[] buf )
513     {
514         synchronized ( lock )
515         {
516             ensureIndent();
517             try
518             {
519                 out.write( buf );
520             }
521             catch ( java.io.IOException ioe )
522             {
523             }
524             ;
525 
526             if ( autoflush ) flush();
527         }
528     } //-- write
529 
530 
531     public void write( int c )
532     {
533         synchronized ( lock )
534         {
535             ensureIndent();
536             try
537             {
538                 out.write( c );
539             }
540             catch ( java.io.IOException ioe )
541             {
542             }
543             ;
544             if ( autoflush ) flush();
545         }
546     } //-- write
547 
548 
549     public void write( char[] buf, int off, int len )
550     {
551         synchronized ( lock )
552         {
553             ensureIndent();
554             try
555             {
556                 out.write( buf, off, len );
557             }
558             catch ( java.io.IOException ioe )
559             {
560             }
561             ;
562             if ( autoflush ) flush();
563         }
564     } //-- write
565 
566     //---------------------/
567     //- Protected Methods -/
568     //---------------------/
569 
570     protected short getIndentLevel()
571     {
572         return tabLevel;
573     }
574 
575     /**
576      * Returns the current indent size (getIndentLevel()*tabSize);
577      * @return the current indent size
578      **/
579     protected short getIndentSize()
580     {
581         return (short) ( tabLevel * tabSize );
582     } //-- getIndentSize
583 
584     protected char getIndentChar()
585     {
586         return tabChar;
587     }
588 
589     protected void writeIndent()
590     {
591 
592         try
593         {
594             for ( int i = 0; i < tabLevel; i++ ) out.write( tab );
595         }
596         catch ( java.io.IOException ioe )
597         {
598         }
599         ;
600 
601     } //-- writeIndent
602 
603     //-------------------/
604     //- Private Methods -/
605     //-------------------/
606 
607     private void ensureIndent()
608     {
609         if ( addIndentation )
610         {
611             writeIndent();
612             addIndentation = false;
613         }
614     } //-- ensureIndent
615 
616 
617     /**
618      * writes the line separator character to the writer
619      **/
620     private void linefeed()
621     {
622         try
623         {
624             out.write( lineSeparator );
625         }
626         catch ( java.io.IOException ioe )
627         {
628         }
629         ;
630     } //-- linefeed
631 
632     /**
633      * Creates the tab from the tabSize and the tabChar
634      **/
635     private void createTab()
636     {
637         tab = new char[tabSize];
638         for ( int i = 0; i < tabSize; i++ )
639         {
640             tab[i] = tabChar;
641         }
642     } //-- createTab
643 
644 } //-- JSourceWriter