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-2001 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  
46  package org.codehaus.modello.plugin.java.javasource;
47  
48  
49  /*
50   * Copyright (c) 2004, Codehaus.org
51   *
52   * Permission is hereby granted, free of charge, to any person obtaining a copy of
53   * this software and associated documentation files (the "Software"), to deal in
54   * the Software without restriction, including without limitation the rights to
55   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
56   * of the Software, and to permit persons to whom the Software is furnished to do
57   * so, subject to the following conditions:
58   *
59   * The above copyright notice and this permission notice shall be included in all
60   * copies or substantial portions of the Software.
61   *
62   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
63   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
64   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
65   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
66   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
67   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
68   * SOFTWARE.
69   */
70  
71  import org.codehaus.modello.ModelloRuntimeException;
72  
73  import java.util.Vector;
74  
75  /**
76   * A class for holding in-memory Java source code.
77   *
78   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
79   * @version $Revision$ $Date$
80   **/
81  public class JSourceCode
82  {
83  
84  
85      /**
86       * A list of JCodeStatements
87       **/
88      private Vector<JCodeStatement> source = null;
89  
90      /**
91       * The indent size
92       **/
93      private short indentSize = 4;
94  
95      /**
96       * The current indent size
97       **/
98      private short currentIndent = indentSize;
99  
100     /**
101      * Creates an empty JSourceCode
102      **/
103     public JSourceCode()
104     {
105         super();
106         source = new Vector<JCodeStatement>();
107     } //-- JSourceCode
108 
109     /**
110      * Creates a JSourceCode and adds the given String
111      * to it's contents
112      * @param sourceCode the source to add
113      **/
114     public JSourceCode( String sourceCode )
115     {
116         this();
117         this.source.addElement( new JCodeStatement( sourceCode, currentIndent ) );
118     } //-- JSourceCode
119 
120     /**
121      * Adds the given statement to this JSourceCode. The statement
122      * will be added on a new line.
123      * @param statement the statement to add
124      **/
125     public void add( String statement )
126     {
127         JCodeStatement jcs = new JCodeStatement( statement, currentIndent );
128         source.addElement( jcs );
129     } //-- add
130 
131     /**
132      * Adds the given statement to this JSourceCode. The statement
133      * will be added on a new line.
134      * @param statement the statement to add
135      * @param the indentSize is the size of the indentation to use
136      * when printing this JSourceCode
137      * @see #print
138      * @deprecated this method is not here any mode in castor codegen 1.3rc1
139      **/
140     public void add( String statement, short indentSize )
141     {
142         JCodeStatement jcs = new JCodeStatement( statement, indentSize );
143         source.addElement( jcs );
144     } //-- add
145 
146     /**
147      * Adds the given statement to this JSourceCode. The statement
148      * will be added on a new line and added with increased indent.
149      * This is a convenience method for the sequence
150      * <code>
151      * indent();
152      * add(statement);
153      * unindent();
154      * </code>
155      * @param statement the statement to add
156      **/
157     public void addIndented( String statement )
158     {
159         indent();
160         JCodeStatement jcs = new JCodeStatement( statement, currentIndent );
161         source.addElement( jcs );
162         unindent();
163     } //-- add
164 
165     /**
166      * Appends the given String to the last line in this
167      * JSourceCode
168      * @param segment the String to append
169      **/
170     public void append( String segment )
171     {
172 
173         if ( source.isEmpty() )
174             add( segment );
175         else
176         {
177             JCodeStatement jcs = (JCodeStatement) source.lastElement();
178             jcs.append( segment );
179         }
180     } //-- append(String)
181 
182     /**
183      * Clears all the code statements from this JSourceCode
184      **/
185     public void clear()
186     {
187         source.removeAllElements();
188     } //-- clear();
189 
190     /**
191      * Copies the contents of this JSourceCode into the given JSourceCode
192      * @param jsc the JSourceCode to copy this JSourceCode into
193      **/
194     public void copyInto( JSourceCode jsc )
195     {
196         for ( int i = 0; i < source.size(); i++ )
197         {
198             jsc.addCodeStatement( (JCodeStatement) source.elementAt( i ) );
199         }
200     } //-- copyInto
201 
202     /**
203      * Increases the current indent level by 1
204      **/
205     public void indent()
206     {
207         currentIndent += indentSize;
208     } //-- indent();
209 
210     /**
211      * Returns true if this JSourceCode is empty (ie. no source).
212      * @return true if this JSourceCode is empty.
213      **/
214     public boolean isEmpty()
215     {
216         return source.isEmpty();
217     } //-- isEmpty
218 
219     /**
220      * Prints this JSourceCode to the given JSourceWriter
221      * @param jsw the JSourceWriter to print to
222      **/
223     public void print( JSourceWriter jsw )
224     {
225         for ( int i = 0; i < source.size(); i++ )
226             jsw.writeln( source.elementAt( i ).toString() );
227     } //-- print
228 
229     /**
230      * Decreases the indent level by 1
231      **/
232     public void unindent()
233     {
234         if ( currentIndent == 0 )
235         {
236             throw new ModelloRuntimeException( "Cannot unindent: current indent is 0." );
237         }
238         currentIndent -= indentSize;
239     } //-- unindent
240 
241 
242     /**
243      * Returns the String representation of this JSourceCode
244      * @return the String representation of this JSourceCode
245      **/
246     public String toString()
247     {
248         StringBuffer sb = new StringBuffer();
249         String lineSeparator = System.getProperty( "line.separator" );
250         for ( int i = 0; i < source.size(); i++ )
251         {
252             sb.append( source.elementAt( i ).toString() );
253             sb.append( lineSeparator );
254         }
255         return sb.toString();
256     } //-- toString
257 
258     /**
259      * Adds the given JCodeStatement to this JSourceCode
260      * @param jcs the JCodeStatement to add
261      **/
262     private void addCodeStatement( JCodeStatement jcs )
263     {
264         short indent = (short) ( jcs.getIndent() + currentIndent - JCodeStatement.DEFAULT_INDENTSIZE );
265         source.addElement( new JCodeStatement( jcs.getStatement(), indent ) );
266     } //-- addCodeStatement(JCodeStatement)
267 
268 } //-- JSourceCode
269 
270 /**
271  * Represents a line of code, used by JSourceCode class
272  * @author <a href="kvisco@intalio.com">Keith Visco</a>
273  **/
274 class JCodeStatement
275 {
276 
277     private StringBuffer value = null;
278     static public short DEFAULT_INDENTSIZE = 4;
279     private short indentSize = DEFAULT_INDENTSIZE;
280 
281     JCodeStatement()
282     {
283         super();
284         value = new StringBuffer();
285     } //-- JCodeStatement
286 
287     JCodeStatement( String statement )
288     {
289         this();
290         this.value.append( statement );
291     } //-- JCodeStatement
292 
293     JCodeStatement( String statement, short indentSize )
294     {
295         this( statement );
296         this.indentSize = indentSize;
297     } //-- JCodeStatement
298 
299 
300     void append( String segment )
301     {
302         value.append( segment );
303     }
304 
305     short getIndent()
306     {
307         return indentSize;
308     } //-- getIndent
309 
310     String getStatement()
311     {
312         return value.toString();
313     } //-- getStatement
314 
315     public String toString()
316     {
317         if ( value.length() == 0 )
318         {
319             return "";
320         }
321 
322         StringBuffer sb = new StringBuffer( indentSize + value.length() );
323         for ( int i = 0; i < indentSize; i++ ) sb.append( ' ' );
324         sb.append( value.toString() );
325         return sb.toString();
326     }
327 } //-- JCodeStatement