KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSharpType.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 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code.csharp;
35
36 import java.io.PrintWriter JavaDoc;
37
38 import net.percederberg.grammatica.code.CodeElement;
39 import net.percederberg.grammatica.code.CodeElementContainer;
40 import net.percederberg.grammatica.code.CodeStyle;
41
42 /**
43  * An abstract superclass for the various C# type code generators.
44  *
45  * @author Per Cederberg, <per at percederberg dot net>
46  * @version 1.0
47  */

48 abstract class CSharpType extends CodeElementContainer {
49
50     /**
51      * The type modifier flags.
52      */

53     private int modifiers;
54
55     /**
56      * The type name.
57      */

58     private String JavaDoc name;
59
60     /**
61      * The name of the type that this type extends and/or implements.
62      */

63     private String JavaDoc[] extendTypes;
64
65     /**
66      * The type comment.
67      */

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

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

99     protected CSharpType(int modifiers, String JavaDoc name, String JavaDoc[] extendTypes) {
100         this.modifiers = modifiers;
101         this.name = name;
102         this.extendTypes = extendTypes;
103     }
104
105     /**
106      * Returns the type name.
107      *
108      * @return the type name
109      */

110     public String JavaDoc toString() {
111         return name;
112     }
113
114     /**
115      * Sets the type comment. This method will remove any previous
116      * type comment.
117      *
118      * @param comment the new type comment
119      */

120     public void addComment(CSharpComment comment) {
121         this.comment = comment;
122     }
123
124     /**
125      * Prints the type to the specified stream.
126      *
127      * @param out the output stream
128      * @param style the code style to use
129      * @param indent the indentation level
130      * @param type the type name
131      */

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

177     protected void printSeparator(PrintWriter JavaDoc out,
178                                   CodeStyle style,
179                                   CodeElement prev,
180                                   CodeElement next) {
181
182         if (prev == null || next == null) {
183             // Do nothing
184
} else {
185             out.println();
186         }
187     }
188 }
189
Popular Tags