View Javadoc

1   /*
2    * Copyright 2011-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Base implementation for tag cloud mojos.
37   *
38   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
39   * @version $Revision:591 $
40   */
41  public abstract class AbstractTagCloudMojo extends AbstractMojo
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    // --- members --------------------------------------------------------------
48  
49    /**
50     * The Maven project.
51     *
52     * @parameter expression="${project}"
53     * @required
54     * @readonly
55     * @since 1.0
56     */
57    protected MavenProject project;
58  
59    /**
60     * The base directory to scan files.
61     *
62     * @parameter expression="${project.build.sourceDirectory}
63     * @since 1.0
64     */
65    protected String basedir;
66  
67    /**
68     * The filter for files to include to be passed to the tag collection.
69     *
70     * @parameter
71     * @since 1.0
72     */
73    protected String[] includes;
74  
75    /**
76     * The filter for files to exclude to be passed to the tag collection..
77     *
78     * @parameter
79     * @since 1.0
80     */
81    protected String[] excludes;
82  
83    /**
84     * A simple flag to skip the generation of the XML file. If set on the command
85     * line use <code>-Dtagcloud.skip</code>.
86     *
87     * @parameter expression="${tagcloud.skip}" default-value="false"
88     * @since 1.0
89     */
90    protected boolean skip;
91  
92    /**
93     * Specifies the log level used for this plugin.
94     * <p>
95     * Allowed values are <code>SEVERE</code>, <code>WARNING</code>,
96     * <code>INFO</code> and <code>FINEST</code>.
97     * </p>
98     *
99     * @parameter expression="${tagcloud.logLevel}"
100    * @since 1.0
101    */
102   protected String logLevel;
103 
104   /**
105    * Specifies the encoding to be used to read the source files.
106    *
107    * @parameter expression="${project.build.sourceEncoding}"
108    * @since 1.0
109    */
110   protected String encoding;
111 
112   /**
113    * The filter removes Java reserved words from the tag cloud.
114    *
115    * @parameter default-value="true"
116    * @since 1.0
117    */
118   protected boolean useJavaReservedWordsFilter;
119 
120   /**
121    * The filter removes usual Java words from the tag cloud.
122    *
123    * @parameter default-value="false"
124    * @since 1.0
125    */
126   protected boolean useUsualWordsFilter;
127 
128   /**
129    * The list of words to be removed from the tag cloud. This allows an
130    * individual configuration.
131    *
132    * @parameter
133    * @since 1.0
134    */
135   protected String[] wordsToFilter;
136 
137   // ****************************** Initializer *******************************
138 
139   // ****************************** Constructors ******************************
140 
141   // ****************************** Inner Classes *****************************
142 
143   // ********************************* Methods ********************************
144 
145   // --- init -----------------------------------------------------------------
146 
147   // --- get&set --------------------------------------------------------------
148 
149   // --- business -------------------------------------------------------------
150 
151   /**
152    * {@inheritDoc}
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    * Subclasses will get the generated cloud passed by overriding this method.
193    *
194    * @param tagCloud the generated tag cloud.
195    * @throws MojoExecutionException on any problem encountered.
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   // --- object basics --------------------------------------------------------
265 
266 }