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 package net.sourceforge.plantumldependency.cli.main.option.output;
26
27 import static java.util.logging.Level.INFO;
28 import static java.util.logging.Level.SEVERE;
29 import static java.util.logging.Logger.getLogger;
30 import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.READING_SOURCE_FILE_ERROR;
31 import static net.sourceforge.plantumldependency.cli.constants.log.InfoConstants.TREATED_DEPENDENCY_INFO;
32 import static net.sourceforge.plantumldependency.common.utils.file.FileUtils.readFileIntoString;
33 import static net.sourceforge.plantumldependency.common.utils.file.FileUtils.writeIntoFile;
34 import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
35
36 import java.io.File;
37 import java.util.Iterator;
38 import java.util.Set;
39 import java.util.TreeSet;
40 import java.util.logging.Logger;
41 import java.util.regex.Pattern;
42
43 import net.sourceforge.plantumldependency.cli.exception.PlantUMLDependencyException;
44 import net.sourceforge.plantumldependency.cli.generic.GenericDependency;
45 import net.sourceforge.plantumldependency.cli.main.option.display.type.argument.DisplayType;
46 import net.sourceforge.plantumldependency.cli.main.option.programminglanguage.argument.ProgrammingLanguage;
47 import net.sourceforge.plantumldependency.cli.main.option.programminglanguage.context.ProgrammingLanguageContext;
48 import net.sourceforge.plantumldependency.cli.plantumldiagram.PlantUMLDiagram;
49 import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
50 import net.sourceforge.plantumldependency.commoncli.option.execution.AbstractOptionExecution;
51 import net.sourceforge.plantumldependency.commoncli.option.execution.OptionExecution;
52
53 import org.apache.tools.ant.types.FileSet;
54 import org.apache.tools.ant.types.resources.FileResource;
55
56
57
58
59
60
61
62
63
64 public class PlantUMLDependencyOutputOptionExecution extends AbstractOptionExecution {
65
66
67 private static final long serialVersionUID = 2237748681247499173L;
68
69
70 private static final transient Logger LOGGER = getLogger(PlantUMLDependencyOutputOptionExecution.class.getName());
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 private static ProgrammingLanguageContext readDependenciesContextFromFiles(final ProgrammingLanguage language,
97 final FileSet includeExcludeFiles, final Set < DisplayType > displayTypesOpts,
98 final Pattern displayPackageNamePattern, final Pattern displayNamePattern) {
99 final ProgrammingLanguageContext programmingLanguageContext = language.createNewContext(displayTypesOpts,
100 displayPackageNamePattern, displayNamePattern);
101
102 final Iterator < FileResource > iter = includeExcludeFiles.iterator();
103 while (iter.hasNext()) {
104 final FileResource fileResource = iter.next();
105
106 try {
107 readDependencyFromFile(fileResource.getFile(), programmingLanguageContext, language);
108 } catch (final PlantUMLDependencyException e) {
109 LOGGER.log(SEVERE, buildLogString(READING_SOURCE_FILE_ERROR, fileResource.getFile()));
110 LOGGER.log(INFO, e.getMessage(), e);
111 }
112 }
113
114 programmingLanguageContext.removeAllPotentialJavaLangSeenDependencyAndChangePackageToJavaLang();
115 return programmingLanguageContext;
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 private static GenericDependency readDependencyFromFile(final File file,
138 final ProgrammingLanguageContext programmingLanguageContext, final ProgrammingLanguage language)
139 throws PlantUMLDependencyException {
140 final String sourceFileContent = readFileIntoString(file);
141 return language.readDependencyFromFile(sourceFileContent, programmingLanguageContext);
142 }
143
144
145 private File outputFile;
146
147
148
149
150
151
152 private transient FileSet inputFileSet;
153
154
155 private ProgrammingLanguage programmingLanguage;
156
157
158 private Set < DisplayType > displayTypesOptions;
159
160
161
162
163
164 private Pattern displayPackageNamePattern;
165
166
167
168
169
170 private Pattern displayNamePattern;
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public PlantUMLDependencyOutputOptionExecution(final File file, final ProgrammingLanguage language,
198 final FileSet includeExcludeFiles, final Set < DisplayType > displayTypesOpts,
199 final Pattern displayPackageNamePatternOpt, final Pattern displayNamePatternOpt, final int optionPriority) {
200 super(optionPriority);
201 setOutputFile(file);
202 setInputFileSet(includeExcludeFiles);
203 setProgrammingLanguage(language);
204 setDisplayTypesOptions(displayTypesOpts);
205 setDisplayPackageNamePattern(displayPackageNamePatternOpt);
206 setDisplayNamePattern(displayNamePatternOpt);
207 }
208
209
210
211
212
213
214 @Override
215 public OptionExecution deepClone() {
216 final PlantUMLDependencyOutputOptionExecution p = (PlantUMLDependencyOutputOptionExecution) super.deepClone();
217 p.outputFile = new File(outputFile.getAbsolutePath());
218 p.inputFileSet = (FileSet) inputFileSet.clone();
219 p.displayTypesOptions = new TreeSet < DisplayType >(displayTypesOptions);
220 p.displayPackageNamePattern = displayPackageNamePattern;
221 p.displayNamePattern = displayNamePattern;
222 return p;
223 }
224
225
226
227
228
229
230 @Override
231 public void execute() throws CommandLineException {
232 final ProgrammingLanguageContext programmingLanguageContext = readDependenciesContextFromFiles(
233 getProgrammingLanguage(), getInputFileSet(), getDisplayTypesOptions(), getDisplayPackageNamePattern(),
234 getDisplayNamePattern());
235 final PlantUMLDiagram plantUMLDiagram = programmingLanguageContext.getPlantUMLClassesDiagram();
236 writeIntoFile(plantUMLDiagram.getPlantUMLTextDescription(), getOutputFile());
237 LOGGER.log(INFO,
238 buildLogString(TREATED_DEPENDENCY_INFO, programmingLanguageContext.getParsedDependencies().size()));
239 }
240
241
242
243
244
245
246
247
248 private Pattern getDisplayNamePattern() {
249 return displayNamePattern;
250 }
251
252
253
254
255
256
257
258
259 private Pattern getDisplayPackageNamePattern() {
260 return displayPackageNamePattern;
261 }
262
263
264
265
266
267
268
269
270 private Set < DisplayType > getDisplayTypesOptions() {
271 return displayTypesOptions;
272 }
273
274
275
276
277
278
279
280
281 private FileSet getInputFileSet() {
282 return inputFileSet;
283 }
284
285
286
287
288
289
290
291
292 private File getOutputFile() {
293 return outputFile;
294 }
295
296
297
298
299
300
301
302
303 private ProgrammingLanguage getProgrammingLanguage() {
304 return programmingLanguage;
305 }
306
307
308
309
310
311
312
313
314
315 private void setDisplayNamePattern(final Pattern value) {
316 displayNamePattern = value;
317 }
318
319
320
321
322
323
324
325
326
327 private void setDisplayPackageNamePattern(final Pattern value) {
328 displayPackageNamePattern = value;
329 }
330
331
332
333
334
335
336
337
338
339 private void setDisplayTypesOptions(final Set < DisplayType > value) {
340 displayTypesOptions = value;
341 }
342
343
344
345
346
347
348
349
350
351 private void setInputFileSet(final FileSet value) {
352 inputFileSet = value;
353 }
354
355
356
357
358
359
360
361
362
363 private void setOutputFile(final File value) {
364 outputFile = value;
365 }
366
367
368
369
370
371
372
373
374
375 private void setProgrammingLanguage(final ProgrammingLanguage value) {
376 programmingLanguage = value;
377 }
378 }