1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.tagcloud.maven;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.ServiceLoader;
22
23 import org.apache.commons.io.FileUtils;
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.project.MavenProject;
28 import org.codehaus.plexus.util.DirectoryScanner;
29
30 import de.smartics.maven.util.LoggingUtils;
31 import de.smartics.tagcloud.TagCloud;
32 import de.smartics.tagcloud.TagCloudFactory;
33 import de.smartics.tagcloud.collector.TokenizerTagCollector;
34
35
36
37
38
39
40
41 public abstract class AbstractTagCloudMojo extends AbstractMojo
42 {
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 protected MavenProject project;
58
59
60
61
62
63
64
65 protected String basedir;
66
67
68
69
70
71
72
73 protected String[] includes;
74
75
76
77
78
79
80
81 protected String[] excludes;
82
83
84
85
86
87
88
89
90 protected boolean skip;
91
92
93
94
95
96
97
98
99
100
101
102 protected String logLevel;
103
104
105
106
107
108
109
110 protected String encoding;
111
112
113
114
115
116
117
118 protected boolean useJavaReservedWordsFilter;
119
120
121
122
123
124
125
126 protected boolean useUsualWordsFilter;
127
128
129
130
131
132
133
134
135 protected String[] wordsToFilter;
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 @Override
155 public void execute() throws MojoExecutionException, MojoFailureException
156 {
157 if (!skip)
158 {
159 if (canBuild())
160 {
161 runExecution();
162 }
163 else
164 {
165 getLog()
166 .debug(
167 "Skipping tag cloud since no test classes are provided by this project.");
168 }
169 }
170 else
171 {
172 getLog().info("Skipping tag cloud XML generation since skip=true.");
173 }
174 }
175
176 private void runExecution() throws MojoExecutionException
177 {
178 init();
179
180 try
181 {
182 final TagCloud tagCloud = createTagCloud();
183 runWith(tagCloud);
184 }
185 catch (final IOException e)
186 {
187 throw new MojoExecutionException("Cannot generate tag cloud XML file.", e);
188 }
189 }
190
191
192
193
194
195
196
197 protected abstract void runWith(TagCloud tagCloud)
198 throws MojoExecutionException;
199
200 private TagCloud createTagCloud() throws MojoExecutionException, IOException
201 {
202 final String[] sourceFileNames = calcSourceFiles();
203
204 final TagCloudFactory factory = createFactoryInstance();
205 final TagCloud tagCloud = factory.createJavaCloud();
206 final TokenizerTagCollector tagCollector =
207 new TokenizerTagCollector(encoding, tagCloud);
208 for (final String sourceFileName : sourceFileNames)
209 {
210 final File sourceFile = new File(basedir, sourceFileName);
211 final InputStream input = FileUtils.openInputStream(sourceFile);
212 tagCollector.collect(input);
213 }
214 return tagCloud;
215 }
216
217 protected TagCloudFactory createFactoryInstance()
218 throws MojoExecutionException
219 {
220 final ServiceLoader<TagCloudFactory> loader =
221 ServiceLoader.load(TagCloudFactory.class);
222 for (TagCloudFactory factory : loader)
223 {
224 factory.setUseJavaReservedWordsFilter(useJavaReservedWordsFilter);
225 factory.setUseUsualWordsFilter(useUsualWordsFilter);
226 factory.setWordsToFilter(wordsToFilter);
227 return factory;
228 }
229 throw new MojoExecutionException(
230 "No TagCloudFactory implementation registered.");
231 }
232
233 private String[] calcSourceFiles()
234 {
235 final DirectoryScanner scanner = new DirectoryScanner();
236 if (includes != null)
237 {
238 scanner.setIncludes(includes);
239 }
240 else
241 {
242 scanner.setIncludes(new String[]
243 { "**/*.java" });
244 }
245 scanner.setExcludes(excludes);
246 scanner.setBasedir(basedir);
247 scanner.scan();
248 final String[] sourceFiles = scanner.getIncludedFiles();
249 return sourceFiles;
250 }
251
252 private boolean canBuild()
253 {
254 final String packaging = project.getPackaging();
255 return !("pom".equals(packaging) || !new File(project.getBuild()
256 .getSourceDirectory()).exists());
257 }
258
259 private void init() throws MojoExecutionException
260 {
261 LoggingUtils.configureLogger(getLog(), logLevel);
262 }
263
264
265
266 }