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 package com.helger.jcodemodel.writer;
42
43 import java.io.BufferedOutputStream;
44 import java.io.File;
45 import java.io.IOException;
46 import java.io.OutputStream;
47 import java.nio.charset.Charset;
48 import java.nio.charset.StandardCharsets;
49 import java.util.Collection;
50 import java.util.List;
51
52 import javax.annotation.Nonnull;
53 import javax.annotation.Nullable;
54
55 import com.helger.jcodemodel.IJFormatter;
56 import com.helger.jcodemodel.JAnnotationUse;
57 import com.helger.jcodemodel.JCodeModel;
58 import com.helger.jcodemodel.JDefinedClass;
59 import com.helger.jcodemodel.JDocComment;
60 import com.helger.jcodemodel.JPackage;
61 import com.helger.jcodemodel.SourcePrintWriter;
62 import com.helger.jcodemodel.fmt.AbstractJResourceFile;
63 import com.helger.jcodemodel.util.JCValueEnforcer;
64 import com.helger.jcodemodel.writer.ProgressCodeWriter.IProgressTracker;
65
66
67
68
69
70
71
72 public class JCMWriter
73 {
74
75 public static final String DEFAULT_INDENT_STRING = " ";
76
77
78 private static String s_sDefaultNewLine;
79
80 @Nonnull
81 public static String getDefaultNewLine ()
82 {
83 String ret = s_sDefaultNewLine;
84 if (ret == null)
85 {
86 try
87 {
88 ret = s_sDefaultNewLine = System.getProperty ("line.separator");
89 }
90 catch (final Exception ex)
91 {
92
93 }
94
95
96 if (ret == null || ret.length () == 0)
97 ret = s_sDefaultNewLine = "\n";
98 }
99 return ret;
100 }
101
102 private final JCodeModel m_aCM;
103
104
105 private Charset m_aCharset = StandardCharsets.UTF_8;
106
107
108 private String m_sNewLine = getDefaultNewLine ();
109
110
111
112
113 private String m_sIndentString = DEFAULT_INDENT_STRING;
114
115 public JCMWriter (@Nonnull final JCodeModel aCM)
116 {
117 m_aCM = aCM;
118 }
119
120
121
122
123
124 @Nullable
125 public Charset getCharset ()
126 {
127 return m_aCharset;
128 }
129
130
131
132
133
134
135
136
137
138 @Nonnull
139 public JCMWriter setCharset (@Nullable final Charset aCharset)
140 {
141 m_aCharset = aCharset;
142 return this;
143 }
144
145
146
147
148 @Nonnull
149 public String getNewLine ()
150 {
151 return m_sNewLine;
152 }
153
154
155
156
157
158
159
160
161
162 @Nonnull
163 public JCMWriter setNewLine (@Nonnull final String sNewLine)
164 {
165 JCValueEnforcer.notEmpty (sNewLine, "NewLine");
166 m_sNewLine = sNewLine;
167 return this;
168 }
169
170 @Nonnull
171 public String getIndentString ()
172 {
173 return m_sIndentString;
174 }
175
176 @Nonnull
177 public JCMWriter setIndentString (@Nonnull final String sIndentString)
178 {
179 JCValueEnforcer.notNull (sIndentString, "IndentString");
180 m_sIndentString = sIndentString;
181 return this;
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public void build (@Nonnull final File aDestDir, @Nullable final IProgressTracker aStatusPT) throws IOException
197 {
198 build (aDestDir, aDestDir, aStatusPT);
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public void build (@Nonnull final File aSrcDir,
216 @Nonnull final File aResourceDir,
217 @Nullable final IProgressTracker aStatusPT) throws IOException
218 {
219 AbstractCodeWriter aSrcWriter = new FileCodeWriter (aSrcDir, m_aCharset, m_sNewLine);
220 AbstractCodeWriter aResWriter = new FileCodeWriter (aResourceDir, m_aCharset, m_sNewLine);
221 if (aStatusPT != null)
222 {
223 aSrcWriter = new ProgressCodeWriter (aSrcWriter, aStatusPT);
224 aResWriter = new ProgressCodeWriter (aResWriter, aStatusPT);
225 }
226 build (aSrcWriter, aResWriter);
227 }
228
229
230
231
232
233
234
235
236
237 public void build (@Nonnull final File aDestDir) throws IOException
238 {
239 build (aDestDir, System.out::println);
240 }
241
242
243
244
245
246
247
248
249
250
251
252 public void build (@Nonnull final File aSrcDir, @Nonnull final File aResourceDir) throws IOException
253 {
254 build (aSrcDir, aResourceDir, System.out::println);
255 }
256
257
258
259
260
261
262
263
264
265 public void build (@Nonnull final AbstractCodeWriter aWriter) throws IOException
266 {
267 build (aWriter, aWriter);
268 }
269
270
271
272
273
274
275
276
277
278
279
280 public void build (@Nonnull final AbstractCodeWriter aSourceWriter,
281 @Nonnull final AbstractCodeWriter aResourceWriter) throws IOException
282 {
283 try
284 {
285
286 final List <JPackage> aPackages = m_aCM.getAllPackages ();
287 for (final JPackage aPackage : aPackages)
288 buildPackage (aSourceWriter, aResourceWriter, aPackage);
289 }
290 finally
291 {
292 aSourceWriter.close ();
293 aResourceWriter.close ();
294 }
295 }
296
297 @Nonnull
298 private JFormatter _createJavaSourceFileWriter (@Nonnull final AbstractCodeWriter aSrcWriter,
299 @Nonnull final JPackage aPackage,
300 @Nonnull final String sClassName) throws IOException
301 {
302 final SourcePrintWriter aWriter = aSrcWriter.openSource (aPackage, sClassName + ".java");
303 final JFormatterJFormatter.html#JFormatter">JFormatter ret = new JFormatter (aWriter, m_sIndentString);
304
305 ret.addDontImportClasses (m_aCM.getAllDontImportClasses ());
306 return ret;
307 }
308
309 public void buildPackage (@Nonnull final AbstractCodeWriter aSrcWriter,
310 @Nonnull final AbstractCodeWriter aResWriter,
311 @Nonnull final JPackage aPackage) throws IOException
312 {
313
314 for (final JDefinedClass c : aPackage.classes ())
315 {
316 if (c.isHidden ())
317 {
318
319 continue;
320 }
321
322 try (final JFormatter f = _createJavaSourceFileWriter (aSrcWriter, aPackage, c.name ()))
323 {
324 f.writeClassFull (c);
325 }
326 }
327
328
329 final Collection <JAnnotationUse> aAnnotations = aPackage.annotations ();
330 final JDocComment aJavaDoc = aPackage.javadoc ();
331 if (!aAnnotations.isEmpty () || !aJavaDoc.isEmpty ())
332 {
333 try (final IJFormatter f = _createJavaSourceFileWriter (aSrcWriter, aPackage, "package-info"))
334 {
335 if (!aJavaDoc.isEmpty ())
336 f.generable (aJavaDoc);
337
338
339 for (final JAnnotationUse a : aAnnotations)
340 f.generable (a).newline ();
341
342 f.declaration (aPackage);
343 }
344 }
345
346
347 for (final AbstractJResourceFile rsrc : aPackage.getAllResourceFiles ())
348 {
349 final AbstractCodeWriter cw = rsrc.isResource () ? aResWriter : aSrcWriter;
350 try (final OutputStream os = new BufferedOutputStream (cw.openBinary (aPackage, rsrc.name ())))
351 {
352 rsrc.build (os);
353 }
354 }
355 }
356 }