KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > visualbasic > VisualBasicConstructor


1 /*
2  * VisualBasicConstructor.java
3  *
4  * This work is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This work is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  *
31  * Copyright (c) 2004 Adrian Moore. All rights reserved.
32  * Copyright (c) 2004 Per Cederberg. All rights reserved.
33  */

34
35 package net.percederberg.grammatica.code.visualbasic;
36
37 import java.io.PrintWriter JavaDoc;
38 import java.util.LinkedList JavaDoc;
39
40 import net.percederberg.grammatica.code.CodeElement;
41 import net.percederberg.grammatica.code.CodeStyle;
42
43 /**
44  * A class generating a Visual Basic constructor declaration.
45  *
46  * @author Adrian Moore, <adrianrob at hotmail dot com>
47  * @author Per Cederberg, <per at percederberg dot net>
48  * @version 1.5
49  * @since 1.5
50  */

51 public class VisualBasicConstructor extends CodeElement {
52
53     /**
54      * The public access modifier constant.
55      */

56     public static final int PUBLIC = VisualBasicModifier.PUBLIC;
57
58     /**
59      * The protected friend access modifier constant.
60      */

61     public static final int PROTECTED_FRIEND =
62         VisualBasicModifier.PROTECTED_FRIEND;
63
64     /**
65      * The protected access modifier constant.
66      */

67     public static final int PROTECTED = VisualBasicModifier.PROTECTED;
68
69     /**
70      * The friend access modifier constant.
71      */

72     public static final int FRIEND = VisualBasicModifier.FRIEND;
73
74     /**
75      * The private access modifier constant.
76      */

77     public static final int PRIVATE = VisualBasicModifier.PRIVATE;
78
79     /**
80      * The shared modifier constant.
81      */

82     public static final int SHARED = VisualBasicModifier.SHARED;
83
84     /**
85      * The modifier flags.
86      */

87     private int modifiers;
88
89     /**
90      * The class to construct.
91      */

92     private VisualBasicClass cls;
93
94     /**
95      * The argument list.
96      */

97     private String JavaDoc args;
98
99     /**
100      * The implementing code.
101      */

102     private LinkedList JavaDoc code;
103
104     /**
105      * The constructor comment.
106      */

107     private VisualBasicComment comment;
108
109     /**
110      * Creates a new empty constructor.
111      */

112     public VisualBasicConstructor() {
113         this("");
114     }
115
116     /**
117      * Creates a new constructor with the specified arguments.
118      *
119      * @param args the argument list, excluding parenthesis
120      */

121     public VisualBasicConstructor(String JavaDoc args) {
122         this(PUBLIC, args);
123     }
124
125     /**
126      * Creates a new constructor with the specified arguments.
127      *
128      * @param modifiers the modifier flags
129      * @param args the argument list, excluding parenthesis
130      */

131     public VisualBasicConstructor(int modifiers, String JavaDoc args) {
132         this.modifiers = modifiers;
133         this.cls = null;
134         this.args = args;
135         this.code = new LinkedList JavaDoc();
136         this.comment = null;
137     }
138
139     /**
140      * Returns the class for this constructor, or null.
141      *
142      * @return the class for this constructor, or
143      * null if none has been assigned
144      */

145     public VisualBasicClass getVisualBasicClass() {
146         return this.cls;
147     }
148
149     /**
150      * Sets the class for this constructor.
151      *
152      * @param cls the class to add the constructor to
153      */

154     void setVisualBasicClass(VisualBasicClass cls) {
155         this.cls = cls;
156     }
157
158     /**
159      * Adds one or more lines of actual code.
160      *
161      * @param codeLines the lines of Java code to add
162      */

163     public void addCode(String JavaDoc codeLines) {
164         int pos;
165
166         pos = codeLines.indexOf('\n');
167         while (pos >= 0) {
168             this.code.add(codeLines.substring(0, pos));
169             codeLines = codeLines.substring(pos + 1);
170             pos = codeLines.indexOf('\n');
171         }
172         this.code.add(codeLines);
173     }
174
175     /**
176      * Sets a comment for this constructor.
177      *
178      * @param comment the new constructor comment
179      */

180     public void addComment(VisualBasicComment comment) {
181         this.comment = comment;
182     }
183
184     /**
185      * Returns a numeric category number for the code element. A lower
186      * category number implies that the code element should be placed
187      * before code elements with a higher category number within a
188      * declaration.
189      *
190      * @return the category number
191      */

192     public int category() {
193         return 7;
194     }
195
196     /**
197      * Prints the code element to the specified output stream.
198      *
199      * @param out the output stream
200      * @param style the code style to use
201      * @param indent the indentation level
202      */

203     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
204         String JavaDoc indentStr = style.getIndent(indent);
205         String JavaDoc codeIndentStr = style.getIndent(indent + 1);
206         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
207
208         // Print comment
209
if (comment != null) {
210             comment.print(out, style, indent);
211         }
212
213         // Handle declaration
214
res.append(indentStr);
215         res.append(VisualBasicModifier.createModifierDecl(modifiers));
216         res.append("Sub New(");
217         res.append(args);
218         res.append(")");
219
220         // Handle code
221
for (int i = 0; i < code.size(); i++) {
222             if (code.get(i).toString().length() > 0) {
223                 res.append(codeIndentStr);
224                 res.append(code.get(i).toString());
225                 res.append("\n");
226             } else {
227                 res.append("\n");
228             }
229         }
230         res.append(indentStr);
231         res.append("End Sub");
232
233         // Print method
234
out.println(res.toString());
235     }
236 }
237
Popular Tags