1 |
| package org.apache.maven.jalopy; |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| import de.hunsicker.jalopy.Jalopy; |
21 |
| import de.hunsicker.jalopy.storage.Convention; |
22 |
| import de.hunsicker.jalopy.storage.History; |
23 |
| |
24 |
| import java.io.File; |
25 |
| import java.io.FileNotFoundException; |
26 |
| import java.io.IOException; |
27 |
| import java.io.InputStream; |
28 |
| |
29 |
| import org.apache.commons.logging.Log; |
30 |
| import org.apache.commons.logging.LogFactory; |
31 |
| |
32 |
| import org.codehaus.plexus.util.DirectoryScanner; |
33 |
| import org.codehaus.plexus.util.StringUtils; |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| public class JalopyBean |
46 |
| { |
47 |
| |
48 |
| private static final Log LOG = LogFactory.getLog( JalopyBean.class ); |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| private String fileFormat; |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| private String historyPolicy; |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| private boolean failOnError; |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| private boolean inspect; |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| private boolean backup; |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| private boolean force; |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| private File sourceDirectory; |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| private File testSourceDirectory; |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| private File convention; |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| private String srcIncludesPattern; |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| private String srcExcludesPattern; |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| private String testIncludesPattern; |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| private String testExcludesPattern; |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
1
| public void execute() throws Exception
|
131 |
| { |
132 |
| |
133 |
1
| LOG.info("[jalopy] Jalopy Java Source Code Formatter " + Jalopy.getVersion());
|
134 |
| |
135 |
1
| try
|
136 |
| { |
137 |
1
| if ( getSourceDirectory().exists() )
|
138 |
| { |
139 |
1
| String[] filesToFormat = getIncludedFiles( getSourceDirectory(), getSrcIncludesPattern(),
|
140 |
| getSrcExcludesPattern() ); |
141 |
| |
142 |
1
| LOG.info( "[jalopy] Format " + filesToFormat.length + " source files in " + getSourceDirectory().toString() );
|
143 |
1
| formatFiles( getSourceDirectory(), filesToFormat );
|
144 |
1
| LOG.info( "[jalopy] " + filesToFormat.length + " source files formatted" );
|
145 |
| } |
146 |
| |
147 |
1
| if ( getTestSourceDirectory().exists() )
|
148 |
| { |
149 |
1
| String[] filesToFormat = getIncludedFiles( getTestSourceDirectory(), getTestIncludesPattern(),
|
150 |
| getTestExcludesPattern() ); |
151 |
| |
152 |
1
| LOG.info( "[jalopy] Format " + filesToFormat.length + " source files in " + getTestSourceDirectory().toString() );
|
153 |
1
| formatFiles( getTestSourceDirectory(), filesToFormat );
|
154 |
1
| LOG.info( "[jalopy] " + filesToFormat.length + " source files formatted" );
|
155 |
| } |
156 |
| } |
157 |
| catch ( Exception e ) |
158 |
| { |
159 |
0
| e.printStackTrace();
|
160 |
| } |
161 |
| } |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
2
| private String[] getIncludedFiles( File directory, String includes, String excludes )
|
172 |
| { |
173 |
2
| DirectoryScanner scanner = new DirectoryScanner();
|
174 |
| |
175 |
2
| scanner.setBasedir( directory );
|
176 |
| |
177 |
2
| scanner.setIncludes( StringUtils.split( includes, "," ) );
|
178 |
| |
179 |
2
| scanner.setExcludes( StringUtils.split( excludes, "," ) );
|
180 |
| |
181 |
2
| scanner.scan();
|
182 |
| |
183 |
2
| String[] filesToFormat = scanner.getIncludedFiles();
|
184 |
| |
185 |
2
| return filesToFormat;
|
186 |
| } |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
2
| private void formatFiles( File directory, String[] filesToFormat )
|
197 |
| throws FileNotFoundException, Exception |
198 |
| { |
199 |
2
| Jalopy jalopy = createJalopy();
|
200 |
| |
201 |
2
| for ( int i = 0; i < filesToFormat.length; i++ )
|
202 |
| { |
203 |
4
| File currentFile = new File( directory, filesToFormat[i] );
|
204 |
| |
205 |
4
| jalopy.setInput( currentFile );
|
206 |
| |
207 |
4
| jalopy.setOutput( currentFile );
|
208 |
| |
209 |
4
| if ( isBackup() )
|
210 |
| { |
211 |
0
| jalopy.setBackupDirectory( currentFile.getParentFile() );
|
212 |
| } |
213 |
| |
214 |
4
| jalopy.format();
|
215 |
| |
216 |
4
| logMessage( jalopy, currentFile );
|
217 |
| } |
218 |
| } |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
4
| private void logMessage( Jalopy jalopy, File currentFile )
|
228 |
| throws Exception |
229 |
| { |
230 |
| |
231 |
4
| if ( jalopy.getState() == Jalopy.State.OK )
|
232 |
| { |
233 |
4
| LOG.debug( "[jalopy] " + currentFile + " formatted correctly." );
|
234 |
| } |
235 |
0
| else if ( jalopy.getState() == Jalopy.State.WARN )
|
236 |
| { |
237 |
0
| LOG.warn( "[jalopy] " + currentFile + " formatted with warnings." );
|
238 |
| } |
239 |
0
| else if ( jalopy.getState() == Jalopy.State.ERROR )
|
240 |
| { |
241 |
0
| LOG.error( "[jalopy] " + currentFile + " could not be formatted." );
|
242 |
| |
243 |
0
| if ( isFailOnError() )
|
244 |
| { |
245 |
0
| throw new Exception( currentFile + " could not be formatted." );
|
246 |
| } |
247 |
| } |
248 |
| else |
249 |
| { |
250 |
0
| LOG.warn( "[jalopy] " + currentFile + " formatted with unknown state." );
|
251 |
| } |
252 |
| } |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
2
| private Jalopy createJalopy()
|
260 |
| { |
261 |
2
| Jalopy jalopy = new Jalopy();
|
262 |
| |
263 |
2
| try
|
264 |
| { |
265 |
2
| if ( ( convention != null ) && convention.exists() )
|
266 |
| { |
267 |
2
| LOG.debug( "[jalopy] Using convention file : " + convention.getAbsoluteFile() );
|
268 |
2
| Jalopy.setConvention( convention );
|
269 |
| } |
270 |
| else |
271 |
| { |
272 |
0
| LOG.warn( "[jalopy] Convention file does not exist: " + convention.getAbsoluteFile() );
|
273 |
0
| LOG.warn( "[jalopy] Using default convention : jalopy.xml");
|
274 |
0
| InputStream in = this.getClass().getClassLoader().getResourceAsStream( "plugin-resources/jalopy.xml" );
|
275 |
0
| Convention.importSettings( in, Convention.EXTENSION_XML );
|
276 |
| } |
277 |
| } |
278 |
| catch ( IOException ex ) |
279 |
| { |
280 |
0
| ex.printStackTrace();
|
281 |
| } |
282 |
| |
283 |
2
| jalopy.setFileFormat( getFileFormat() );
|
284 |
| |
285 |
2
| jalopy.setInspect( isInspect() );
|
286 |
| |
287 |
2
| if ( !getHistoryPolicy().equalsIgnoreCase( "none" ) )
|
288 |
| { |
289 |
0
| jalopy.setHistoryPolicy( History.Policy.valueOf( getHistoryPolicy() ) );
|
290 |
| } |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
2
| jalopy.setBackup( isBackup() );
|
299 |
| |
300 |
2
| jalopy.setForce( isForce() );
|
301 |
| |
302 |
2
| return jalopy;
|
303 |
| } |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
3
| public String getFileFormat()
|
311 |
| { |
312 |
3
| return fileFormat;
|
313 |
| } |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
2
| public void setFileFormat( String newfileFormat )
|
321 |
| { |
322 |
2
| this.fileFormat = newfileFormat;
|
323 |
| } |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
1
| public boolean isFailOnError()
|
331 |
| { |
332 |
1
| return failOnError;
|
333 |
| } |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
2
| public void setFailOnError( boolean newfailOnError )
|
341 |
| { |
342 |
2
| this.failOnError = newfailOnError;
|
343 |
| } |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
2
| public boolean isInspect()
|
351 |
| { |
352 |
2
| return inspect;
|
353 |
| } |
354 |
| |
355 |
| |
356 |
| |
357 |
| |
358 |
| |
359 |
| |
360 |
1
| public void setInspect( boolean newinspect )
|
361 |
| { |
362 |
1
| this.inspect = newinspect;
|
363 |
| } |
364 |
| |
365 |
| |
366 |
| |
367 |
| |
368 |
| |
369 |
| |
370 |
6
| public boolean isBackup()
|
371 |
| { |
372 |
6
| return backup;
|
373 |
| } |
374 |
| |
375 |
| |
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
1
| public void setBackup( boolean newbackup )
|
381 |
| { |
382 |
1
| this.backup = newbackup;
|
383 |
| } |
384 |
| |
385 |
| |
386 |
| |
387 |
| |
388 |
| |
389 |
| |
390 |
2
| public boolean isForce()
|
391 |
| { |
392 |
2
| return force;
|
393 |
| } |
394 |
| |
395 |
| |
396 |
| |
397 |
| |
398 |
| |
399 |
| |
400 |
1
| public void setForce( boolean newforce )
|
401 |
| { |
402 |
1
| this.force = newforce;
|
403 |
| } |
404 |
| |
405 |
| |
406 |
| |
407 |
| |
408 |
| |
409 |
| |
410 |
5
| public File getSourceDirectory()
|
411 |
| { |
412 |
5
| return sourceDirectory;
|
413 |
| } |
414 |
| |
415 |
| |
416 |
| |
417 |
| |
418 |
| |
419 |
| |
420 |
2
| public void setSourceDirectory( File newsourceDirectory )
|
421 |
| { |
422 |
2
| this.sourceDirectory = newsourceDirectory;
|
423 |
| } |
424 |
| |
425 |
| |
426 |
| |
427 |
| |
428 |
| |
429 |
| |
430 |
2
| public String getSrcIncludesPattern()
|
431 |
| { |
432 |
2
| return srcIncludesPattern;
|
433 |
| } |
434 |
| |
435 |
| |
436 |
| |
437 |
| |
438 |
| |
439 |
| |
440 |
2
| public void setSrcIncludesPattern( String newsrcIncludesPattern )
|
441 |
| { |
442 |
2
| this.srcIncludesPattern = newsrcIncludesPattern;
|
443 |
| } |
444 |
| |
445 |
| |
446 |
| |
447 |
| |
448 |
| |
449 |
| |
450 |
2
| public String getSrcExcludesPattern()
|
451 |
| { |
452 |
2
| return srcExcludesPattern;
|
453 |
| } |
454 |
| |
455 |
| |
456 |
| |
457 |
| |
458 |
| |
459 |
| |
460 |
2
| public void setSrcExcludesPattern( String newsrcExcludesPattern )
|
461 |
| { |
462 |
2
| this.srcExcludesPattern = newsrcExcludesPattern;
|
463 |
| } |
464 |
| |
465 |
| |
466 |
| |
467 |
| |
468 |
| |
469 |
| |
470 |
3
| public String getHistoryPolicy()
|
471 |
| { |
472 |
3
| return historyPolicy;
|
473 |
| } |
474 |
| |
475 |
| |
476 |
| |
477 |
| |
478 |
| |
479 |
| |
480 |
2
| public void setHistoryPolicy( String newhistoryPolicy )
|
481 |
| { |
482 |
2
| this.historyPolicy = newhistoryPolicy;
|
483 |
| } |
484 |
| |
485 |
| |
486 |
| |
487 |
| |
488 |
| |
489 |
| |
490 |
0
| public File getConvention()
|
491 |
| { |
492 |
0
| return convention;
|
493 |
| } |
494 |
| |
495 |
| |
496 |
| |
497 |
| |
498 |
| |
499 |
| |
500 |
1
| public void setConvention( File newconvention )
|
501 |
| { |
502 |
1
| this.convention = newconvention;
|
503 |
| } |
504 |
| |
505 |
| |
506 |
| |
507 |
| |
508 |
| |
509 |
| |
510 |
4
| public File getTestSourceDirectory()
|
511 |
| { |
512 |
4
| return testSourceDirectory;
|
513 |
| } |
514 |
| |
515 |
| |
516 |
| |
517 |
| |
518 |
| |
519 |
| |
520 |
1
| public void setTestSourceDirectory( File newtestSourceDirectory )
|
521 |
| { |
522 |
1
| this.testSourceDirectory = newtestSourceDirectory;
|
523 |
| } |
524 |
| |
525 |
| |
526 |
| |
527 |
| |
528 |
| |
529 |
| |
530 |
1
| public String getTestIncludesPattern()
|
531 |
| { |
532 |
1
| return testIncludesPattern;
|
533 |
| } |
534 |
| |
535 |
| |
536 |
| |
537 |
| |
538 |
| |
539 |
| |
540 |
1
| public void setTestIncludesPattern( String newtestIncludesPattern )
|
541 |
| { |
542 |
1
| this.testIncludesPattern = newtestIncludesPattern;
|
543 |
| } |
544 |
| |
545 |
| |
546 |
| |
547 |
| |
548 |
| |
549 |
| |
550 |
1
| public String getTestExcludesPattern()
|
551 |
| { |
552 |
1
| return testExcludesPattern;
|
553 |
| } |
554 |
| |
555 |
| |
556 |
| |
557 |
| |
558 |
| |
559 |
| |
560 |
1
| public void setTestExcludesPattern( String newtestExcludesPattern )
|
561 |
| { |
562 |
1
| this.testExcludesPattern = newtestExcludesPattern;
|
563 |
| } |
564 |
| } |