1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.exolab.javasource;
44
45 import java.io.File;
46 import java.io.FileWriter;
47 import java.util.Enumeration;
48 import java.util.SortedSet;
49 import java.util.TreeSet;
50 import java.util.Vector;
51
52
53
54
55
56
57
58
59
60
61 public final class JCompUnit {
62
63
64
65
66 private static final int INITIAL_STRING_BUILDER_SIZE = 32;
67
68
69
70 private static final String DEFAULT_HEADER = "$" + "Id$";
71
72
73 private static final String[] PUBLIC_HEADER = {
74 " //-----------------------------/",
75 " //- Public Class / Interface -/",
76 "//-----------------------------/", };
77
78
79 private static final String[] NON_PUBLIC_HEADER = {
80 " //-------------------------------------/",
81 " //- Non-Public Classes / Interfaces -/",
82 "//-------------------------------------/", };
83
84
85 private JComment _header = null;
86
87
88 private String _packageName = null;
89
90
91 private String _fileName = null;
92
93
94 private Vector<JClass> _classes = new Vector<JClass>();
95
96
97 private Vector<JInterface> _interfaces = new Vector<JInterface>();
98
99
100
101
102
103
104
105
106 public JCompUnit(final String packageName, final String fileName) {
107 _packageName = packageName;
108 _fileName = fileName;
109 }
110
111
112
113
114
115
116
117
118 public JCompUnit(final JClass jClass) {
119 _packageName = jClass.getPackageName();
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 String filePrefix = jClass.getLocalName();
139
140 _fileName = filePrefix + ".java";
141 _classes.add(jClass);
142 }
143
144
145
146
147
148
149
150 public JCompUnit(final JInterface jInterface) {
151 _packageName = jInterface.getPackageName();
152 _fileName = jInterface.getLocalName() + ".java";
153 _interfaces.add(jInterface);
154 }
155
156
157
158
159
160
161 public void setHeader(final JComment comment) {
162 _header = comment;
163 }
164
165
166
167
168
169
170
171 public void addStructure(final JStructure jStructure) {
172 if (jStructure instanceof JInterface) {
173 addInterface((JInterface) jStructure);
174 } else if (jStructure instanceof JClass) {
175 addClass((JClass) jStructure);
176 } else {
177 String err = "Unknown JStructure subclass '"
178 + jStructure.getClass().getName() + "'.";
179 throw new IllegalArgumentException(err);
180 }
181 }
182
183
184
185
186
187
188 public void addClass(final JClass jClass) {
189 _classes.add(jClass);
190 }
191
192
193
194
195
196
197 public void addInterface(final JInterface jInterface) {
198 _interfaces.add(jInterface);
199 }
200
201
202
203
204
205
206
207
208 public SortedSet<String> getImports() {
209 SortedSet<String> allImports = new TreeSet<String>();
210
211
212 for (int i = 0; i < _classes.size(); ++i) {
213 JClass jClass = _classes.get(i);
214
215 Enumeration<String> enumeration = jClass.getImports();
216 while (enumeration.hasMoreElements()) {
217 allImports.add(enumeration.nextElement());
218 }
219 }
220
221 for (int i = 0; i < _interfaces.size(); ++i) {
222 JInterface jInterface = _interfaces.get(i);
223 Enumeration<String> enumeration = jInterface.getImports();
224 while (enumeration.hasMoreElements()) {
225 allImports.add(enumeration.nextElement());
226 }
227 }
228
229 return allImports;
230 }
231
232
233
234
235
236
237
238
239
240 public String getFilename(final String destDir) {
241 String filename = new String(_fileName);
242
243
244 String javaPackagePath = "";
245 if ((_packageName != null) && (_packageName.length() > 0)) {
246 javaPackagePath = _packageName.replace('.', File.separatorChar);
247 }
248
249
250 File pathFile;
251 if (destDir == null) {
252 pathFile = new File(javaPackagePath);
253 } else {
254 pathFile = new File(destDir, javaPackagePath);
255 }
256 if (!pathFile.exists()) {
257 pathFile.mkdirs();
258 }
259
260
261 if (pathFile.toString().length() > 0) {
262 filename = pathFile.toString() + File.separator + filename;
263 }
264
265 return filename;
266 }
267
268
269
270
271
272
273
274 public String getPackageName() {
275 return _packageName;
276 }
277
278
279
280
281
282
283
284 public void print() {
285 print(null, null);
286 }
287
288
289
290
291
292
293
294
295
296
297 public void print(final String destDir) {
298 print(destDir, null);
299 }
300
301
302
303
304
305
306
307
308
309
310 public void print(final String destDir, final String lineSeparator) {
311
312 String filename = getFilename(destDir);
313
314 File file = new File(filename);
315 JSourceWriter jsw = null;
316 try {
317 jsw = new JSourceWriter(new FileWriter(file));
318 } catch (java.io.IOException ioe) {
319 System.out.println("unable to create compilation unit file: "
320 + filename);
321 return;
322 }
323
324 if (lineSeparator == null) {
325 jsw.setLineSeparator(System.getProperty("line.separator"));
326 } else {
327 jsw.setLineSeparator(lineSeparator);
328 }
329 print(jsw);
330 jsw.flush();
331 jsw.close();
332 }
333
334
335
336
337
338
339 public void print(final JSourceWriter jsw) {
340
341
342
343 StringBuilder buffer = new StringBuilder(INITIAL_STRING_BUILDER_SIZE);
344
345
346 if (_header != null) {
347 _header.print(jsw);
348 } else {
349 jsw.writeln("/*");
350 jsw.writeln(" * " + DEFAULT_HEADER);
351 jsw.writeln("*/");
352 }
353 jsw.writeln();
354 jsw.flush();
355
356
357 if ((_packageName != null) && (_packageName.length() > 0)) {
358 buffer.setLength(0);
359 buffer.append("package ");
360 buffer.append(_packageName);
361 buffer.append(';');
362 jsw.writeln(buffer.toString());
363 jsw.writeln();
364 }
365
366
367 jsw.writeln(" //---------------------------------------------/");
368 jsw.writeln(" //- Imported classes, interfaces and packages -/");
369 jsw.writeln("//---------------------------------------------/");
370 jsw.writeln();
371 SortedSet<String> allImports = getImports();
372 String compUnitPackage = getPackageName();
373 for (String importName : allImports) {
374 String importsPackage = JNaming.getPackageFromClassName(importName);
375 if ((importsPackage != null) && !importsPackage.equals(compUnitPackage)) {
376 jsw.write("import ");
377 jsw.write(importName);
378 jsw.writeln(';');
379 }
380 }
381 jsw.writeln();
382
383
384
385
386 printStructures(jsw, true);
387
388
389 printStructures(jsw, false);
390
391 jsw.flush();
392 }
393
394
395
396
397
398
399
400
401 public void printStructures(final JSourceWriter jsw, final boolean printPublic) {
402
403
404
405 boolean isFirst = true;
406
407
408 for (JInterface jInterface : _interfaces) {
409 if (jInterface.getModifiers().isPublic() == printPublic) {
410 if (isFirst) {
411 String[] header = printPublic ? PUBLIC_HEADER : NON_PUBLIC_HEADER;
412 for (int j = 0; j < header.length; ++j) {
413 jsw.writeln(header[j]);
414 }
415 jsw.writeln();
416 isFirst = false;
417 }
418 jInterface.print(jsw, true);
419 jsw.writeln();
420 }
421 }
422
423
424 for (JClass jClass : _classes) {
425 if (jClass.getModifiers().isPublic() == printPublic) {
426 if (isFirst) {
427 String[] header = printPublic ? PUBLIC_HEADER : NON_PUBLIC_HEADER;
428 for (int j = 0; j < header.length; ++j) {
429 jsw.writeln(header[j]);
430 }
431 jsw.writeln();
432 isFirst = false;
433 }
434 jClass.print(jsw, true);
435 jsw.writeln();
436 }
437 }
438 }
439
440 }