KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * VisualBasicType.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
39 import net.percederberg.grammatica.code.CodeElement;
40 import net.percederberg.grammatica.code.CodeElementContainer;
41 import net.percederberg.grammatica.code.CodeStyle;
42
43 /**
44  * An abstract superclass for the various Visual Basic type code
45  * generators.
46  *
47  * @author Adrian Moore, <adrianrob at hotmail dot com>
48  * @author Per Cederberg, <per at percederberg dot net>
49  * @version 1.5
50  * @since 1.5
51  */

52 public abstract class VisualBasicType extends CodeElementContainer {
53
54     /**
55      * The type modifier flags.
56      */

57     protected int modifiers;
58
59     /**
60      * The type name.
61      */

62     protected String JavaDoc name;
63
64     /**
65      * The name of the type that this type extends and/or implements.
66      */

67     protected String JavaDoc[] extendTypes;
68
69     /**
70      * The type comment.
71      */

72     protected VisualBasicComment comment = null;
73
74     /**
75      * Creates a new type code generator with the specified access
76      * modifier that extends a specified type. If the extend type
77      * null or "" is specified, no extends declaration will be
78      * printed.
79      *
80      * @param modifiers the modifier flag constants
81      * @param name the type name
82      * @param extendType the type to extend and/or implement
83      */

84     protected VisualBasicType(int modifiers, String JavaDoc name, String JavaDoc extendType) {
85         this.modifiers = modifiers;
86         this.name = name;
87         if (extendType == null || extendType.equals("")) {
88             this.extendTypes = new String JavaDoc[0];
89         } else {
90             this.extendTypes = new String JavaDoc[1];
91             this.extendTypes[0] = extendType;
92         }
93     }
94
95     /**
96      * Creates a new type code generator with the specified access
97      * modifier that extends a specified type.
98      *
99      * @param modifiers the modifier flag constants
100      * @param name the type name
101      * @param extendTypes the types to extend and/or implement
102      */

103     protected VisualBasicType(int modifiers,
104                               String JavaDoc name,
105                               String JavaDoc[] extendTypes) {
106
107         this.modifiers = modifiers;
108         this.name = name;
109         this.extendTypes = extendTypes;
110     }
111
112     /**
113      * Returns the type name.
114      *
115      * @return the type name
116      */

117     public String JavaDoc toString() {
118         return name;
119     }
120
121     /**
122      * Sets the type comment. This method will remove any previous
123      * type comment.
124      *
125      * @param comment the new type comment
126      */

127     public void addComment(VisualBasicComment comment) {
128         this.comment = comment;
129     }
130
131     /**
132      * Prints the type to the specified stream.
133      *
134      * @param out the output stream
135      * @param style the code style to use
136      * @param indent the indentation level
137      * @param type the type name
138      */

139     protected void print(PrintWriter JavaDoc out,
140                          CodeStyle style,
141                          int indent,
142                          String JavaDoc type) {
143
144         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
145         String JavaDoc indentStr = style.getIndent(indent);
146
147         // Print type comment
148
if (comment != null) {
149             comment.print(out, style, indent);
150         }
151
152         // Print type declaration
153
buf.append(indentStr);
154         buf.append(VisualBasicModifier.createModifierDecl(modifiers));
155         buf.append(type);
156         buf.append(" ");
157         buf.append(name);
158         buf.append("\n");
159         for (int i = 0; i < extendTypes.length; i++) {
160             buf.append(style.getIndent(indent+1));
161             buf.append("Inherits ");
162             buf.append(extendTypes[i]);
163             buf.append("\n");
164         }
165         out.print(buf.toString());
166
167         // Print type contents
168
printContents(out, style, indent + 1);
169
170         // Print end of type
171
out.println(indentStr + "End " + type);
172     }
173
174     /**
175      * Prints the lines separating two elements.
176      *
177      * @param out the output stream
178      * @param style the code style to use
179      * @param prev the previous element, or null if first
180      * @param next the next element, or null if last
181      */

182     protected void printSeparator(PrintWriter JavaDoc out,
183                                   CodeStyle style,
184                                   CodeElement prev,
185                                   CodeElement next) {
186
187         if (prev == null || next == null) {
188             // Do nothing
189
} else {
190             out.println();
191         }
192     }
193 }
194
Popular Tags