KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > csharp > CSharpConstructor


1 /*
2  * CSharpConstructor.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) 2003-2004 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code.csharp;
35
36 import java.io.PrintWriter JavaDoc;
37 import java.util.LinkedList JavaDoc;
38
39 import net.percederberg.grammatica.code.CodeElement;
40 import net.percederberg.grammatica.code.CodeStyle;
41
42 /**
43  * A class generating a C# constructor declaration.
44  *
45  * @author Per Cederberg, <per at percederberg dot net>
46  * @version 1.5
47  */

48 public class CSharpConstructor extends CodeElement {
49
50     /**
51      * The public access modifier constant.
52      */

53     public static final int PUBLIC = CSharpModifier.PUBLIC;
54
55     /**
56      * The protected internal access modifier constant.
57      */

58     public static final int PROTECTED_INTERNAL =
59         CSharpModifier.PROTECTED_INTERNAL;
60
61     /**
62      * The protected access modifier constant.
63      */

64     public static final int PROTECTED = CSharpModifier.PROTECTED;
65
66     /**
67      * The internal access modifier constant.
68      */

69     public static final int INTERNAL = CSharpModifier.INTERNAL;
70
71     /**
72      * The private access modifier constant.
73      */

74     public static final int PRIVATE = CSharpModifier.PRIVATE;
75
76     /**
77      * The extern modifier constant.
78      */

79     public static final int EXTERN = CSharpModifier.EXTERN;
80
81     /**
82      * The modifier flags.
83      */

84     private int modifiers;
85
86     /**
87      * The class to construct.
88      */

89     private CSharpClass cls;
90
91     /**
92      * The argument list.
93      */

94     private String JavaDoc args;
95
96     /**
97      * The initializer call code.
98      */

99     private String JavaDoc initializer;
100
101     /**
102      * The implementing code.
103      */

104     private LinkedList JavaDoc code;
105
106     /**
107      * The constructor comment.
108      */

109     private CSharpComment comment;
110
111     /**
112      * Creates a new empty constructor.
113      */

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

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

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

148     public CSharpClass getCSharpClass() {
149         return this.cls;
150     }
151
152     /**
153      * Sets the class for this constructor.
154      *
155      * @param cls the class to add the constructor to
156      */

157     void setCSharpClass(CSharpClass cls) {
158         this.cls = cls;
159     }
160
161     /**
162      * Adds an initializer call, i.e. a call to another constructor.
163      *
164      * @param initializer the initializer call
165      */

166     public void addInitializer(String JavaDoc initializer) {
167         this.initializer = initializer;
168     }
169
170     /**
171      * Adds one or more lines of actual code.
172      *
173      * @param codeLines the lines of Java code to add
174      */

175     public void addCode(String JavaDoc codeLines) {
176         int pos;
177
178         pos = codeLines.indexOf('\n');
179         while (pos >= 0) {
180             this.code.add(codeLines.substring(0, pos));
181             codeLines = codeLines.substring(pos + 1);
182             pos = codeLines.indexOf('\n');
183         }
184         this.code.add(codeLines);
185     }
186
187     /**
188      * Sets a comment for this constructor.
189      *
190      * @param comment the new constructor comment
191      */

192     public void addComment(CSharpComment comment) {
193         this.comment = comment;
194     }
195
196     /**
197      * Returns a numeric category number for the code element. A lower
198      * category number implies that the code element should be placed
199      * before code elements with a higher category number within a
200      * declaration.
201      *
202      * @return the category number
203      */

204     public int category() {
205         return 7;
206     }
207
208     /**
209      * Prints the code element to the specified output stream.
210      *
211      * @param out the output stream
212      * @param style the code style to use
213      * @param indent the indentation level
214      */

215     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
216         String JavaDoc indentStr = style.getIndent(indent);
217         String JavaDoc codeIndentStr = style.getIndent(indent + 1);
218         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
219
220         // Print comment
221
if (comment != null) {
222             comment.print(out, style, indent);
223         }
224
225         // Handle declaration
226
res.append(indentStr);
227         res.append(CSharpModifier.createModifierDecl(modifiers));
228         res.append(cls.toString());
229         res.append("(");
230         res.append(args);
231         res.append(")");
232
233         // Handle initializer
234
if (initializer != null) {
235             res.append("\n");
236             res.append(codeIndentStr);
237             res.append(": ");
238             res.append(initializer);
239         }
240         res.append(" {\n");
241
242         // Handle code
243
if (initializer != null && code.size() > 0) {
244             res.append("\n");
245         }
246         for (int i = 0; i < code.size(); i++) {
247             if (code.get(i).toString().length() > 0) {
248                 res.append(codeIndentStr);
249                 res.append(code.get(i).toString());
250                 res.append("\n");
251             } else {
252                 res.append("\n");
253             }
254         }
255         res.append(indentStr);
256         res.append("}");
257
258         // Print method
259
out.println(res.toString());
260     }
261 }
262
Popular Tags