1 package org.andromda.beautifier.plugin;
2
3
4
5
6
7
8
9
10
11 import de.plushnikov.doctorjim.ImportProcessor;
12 import de.plushnikov.doctorjim.javaparser.ParseException;
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.util.Collection;
17 import org.apache.commons.io.FileUtils;
18 import org.apache.log4j.Logger;
19 import org.apache.maven.plugin.AbstractMojo;
20 import org.apache.maven.plugin.MojoExecutionException;
21
22
23
24
25
26
27
28 public class ImportBeautifierMojo
29 extends AbstractMojo
30 {
31 private static final Logger LOG = Logger.getLogger(ImportBeautifierMojo.class);
32
33
34
35
36
37
38
39 private File inputDirectory;
40
41
42
43
44
45
46
47 private File outputDirectory;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 private boolean skipProcessing = false;
70
71
72
73
74 public void execute()
75 throws MojoExecutionException
76 {
77 if (this.skipProcessing)
78 {
79 getLog().info("process-sources:beautify-imports skipProcessing");
80 return;
81 }
82 getLog().info("process-sources:beautify-imports on " + this.inputDirectory);
83 File file = this.inputDirectory;
84 if ( !file.exists() )
85 {
86 throw new MojoExecutionException("Beautifier format input directory does not exist: " + this.inputDirectory);
87 }
88
89 if (this.outputDirectory==null)
90 {
91 this.outputDirectory = this.inputDirectory;
92 }
93 else
94 {
95 File outputFile = this.outputDirectory;
96 if ( !outputFile.exists() )
97 {
98 throw new MojoExecutionException("Beautifier format output directory does not exist: "
99 + this.outputDirectory);
100 }
101 }
102 String directoryString = null;
103
104 try
105 {
106 directoryString = file.getCanonicalPath();
107 String[] extensions = {"java"};
108 Collection<File> files = FileUtils.listFiles(file, extensions, true);
109 ImportProcessor processor = new ImportProcessor();
110 for (File formatFile : files)
111 {
112 try
113 {
114 LOG.info("Beautifying imports on " + formatFile.getPath());
115 String output = processor.organizeImports(FileUtils.readFileToString(formatFile));
116 FileUtils.writeStringToFile(formatFile, output);
117 }
118 catch (ParseException e)
119 {
120
121 LOG.error("Beautifier error on " + formatFile.getCanonicalPath() + ": " + e.getMessage());
122 }
123 }
124 }
125 catch ( FileNotFoundException e )
126 {
127 throw new MojoExecutionException( "FileNotFound creating beautifier output: " + directoryString, e );
128 }
129 catch ( IOException e )
130 {
131 throw new MojoExecutionException( "Error creating beautifier output: " + directoryString, e );
132 }
133 }
134 }