View Javadoc

1   /*
2    * Copyright 2008-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.maven.issues;
17  
18  import java.io.File;
19  import java.io.FileWriter;
20  import java.io.IOException;
21  import java.io.Writer;
22  import java.util.HashMap;
23  import java.util.Locale;
24  import java.util.Map;
25  import java.util.ResourceBundle;
26  
27  import org.apache.commons.lang.LocaleUtils;
28  import org.apache.maven.artifact.factory.ArtifactFactory;
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.artifact.resolver.ArtifactResolver;
31  import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext;
32  import org.apache.maven.doxia.site.decoration.Body;
33  import org.apache.maven.doxia.site.decoration.DecorationModel;
34  import org.apache.maven.doxia.siterenderer.Renderer;
35  import org.apache.maven.doxia.siterenderer.RendererException;
36  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
37  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
38  import org.apache.maven.plugin.MojoExecutionException;
39  import org.apache.maven.plugin.logging.Log;
40  import org.apache.maven.project.MavenProject;
41  import org.apache.maven.reporting.AbstractMavenReport;
42  import org.apache.maven.reporting.MavenReportException;
43  import org.codehaus.plexus.util.StringUtils;
44  
45  import de.smartics.maven.util.LoggingUtils;
46  import de.smartics.maven.util.report.ReportUtils;
47  
48  /**
49   * The issues Mojo reports on issues.
50   *
51   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
52   * @version $Revision:591 $
53   */
54  public abstract class AbstractReportMojo extends AbstractMavenReport
55  {
56    // ********************************* Fields *********************************
57  
58    // --- constants ------------------------------------------------------------
59  
60    // --- members --------------------------------------------------------------
61  
62    // ... plugin infrastructure ................................................
63  
64    /**
65     * The Maven project.
66     *
67     * @parameter expression="${project}"
68     * @required
69     * @readonly
70     */
71    protected MavenProject project;
72  
73    /**
74     * The Doxia site renderer.
75     *
76     * @component
77     * @required
78     * @readonly
79     */
80    protected Renderer siteRenderer;
81  
82    /**
83     * Local Repository.
84     *
85     * @parameter expression="${localRepository}"
86     * @required
87     * @readonly
88     */
89    protected ArtifactRepository localRepository;
90  
91    /**
92     * The resolver for resolving artifacts.
93     *
94     * @component
95     * @required
96     * @readonly
97     */
98    protected ArtifactResolver resolver;
99  
100   /**
101    * The factory to create dependent artifacts.
102    *
103    * @component
104    * @required
105    * @readonly
106    */
107   protected ArtifactFactory factory;
108 
109   // ... report configuration parameters ......................................
110 
111   /**
112    * Specifies the directory where the report will written to. This information
113    * is only used if the report is not part of the site generation process.
114    *
115    * @parameter expression="${project.reporting.outputDirectory}"
116    * @readonly
117    */
118   protected File outputDirectory;
119 
120   /**
121    * Specifies the log level used for this plugin.
122    * <p>
123    * Allowed values are <code>SEVERE</code>, <code>WARNING</code>,
124    * <code>INFO</code> and <code>FINEST</code>.
125    * </p>
126    *
127    * @parameter expression="${issues.logLevel}"
128    * @since 1.0
129    */
130   protected String logLevel;
131 
132   /**
133    * The locale to use regardless of the report. This should be set to the
134    * locale the Javadoc comment is written in. If not set, the Maven provided
135    * locale is used.
136    *
137    * @parameter expression="${issues.locale}"
138    * @since 1.0
139    */
140   protected String locale;
141 
142   // ****************************** Initializer *******************************
143 
144   // ****************************** Constructors ******************************
145 
146   // ****************************** Inner Classes *****************************
147 
148   // ********************************* Methods ********************************
149 
150   // --- init -----------------------------------------------------------------
151 
152   // --- get&set --------------------------------------------------------------
153 
154   // ... plugin infrastructure ................................................
155 
156   /**
157    * {@inheritDoc}
158    *
159    * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
160    */
161   @Override
162   protected MavenProject getProject()
163   {
164     return project;
165   }
166 
167   /**
168    * {@inheritDoc}
169    *
170    * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
171    */
172   @Override
173   protected Renderer getSiteRenderer()
174   {
175     return siteRenderer;
176   }
177 
178   // ... report configuration parameters ......................................
179 
180   // ... basics
181 
182   /**
183    * {@inheritDoc}
184    *
185    * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
186    */
187   @Override
188   protected String getOutputDirectory()
189   {
190     return outputDirectory.getAbsolutePath();
191   }
192 
193   // --- business -------------------------------------------------------------
194 
195   /**
196    * Runs the report generation.
197    *
198    * @throws MojoExecutionException on any problem encountered.
199    */
200   public void execute() throws MojoExecutionException
201   {
202     final Log log = getLog();
203     if (!canGenerateReport())
204     {
205       if (log.isInfoEnabled())
206       {
207         log.info("Report '" + getName(Locale.getDefault())
208                  + "' skipped due to offline mode.");
209       }
210       return;
211     }
212 
213     LoggingUtils.configureLogger(log, logLevel);
214 
215     provideSink();
216   }
217 
218   /**
219    * {@inheritDoc}
220    * <p>
221    * Configures the plugin logger.
222    * </p>
223    *
224    * @see org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
225    */
226   @Override
227   protected void executeReport(final Locale locale) throws MavenReportException
228   {
229     final Log log = getLog();
230     LoggingUtils.configureLogger(log, logLevel);
231   }
232 
233   /**
234    * Ensures that a writeable sink is provided.
235    * <p>
236    * Stolen from the changes plugin.
237    * </p>
238    *
239    * @throws MojoExecutionException if the sink cannot be created.
240    */
241   protected void provideSink() throws MojoExecutionException
242   {
243     final Locale reportLocale = determineLocale();
244 
245     try
246     {
247       final DecorationModel model = new DecorationModel();
248       model.setBody(new Body());
249       final Map<String, String> attributes = new HashMap<String, String>();
250       attributes.put("outputEncoding", "UTF-8"); // TODO correct???
251       final SiteRenderingContext siteContext =
252           siteRenderer.createContextForSkin(ReportUtils.getSkinArtifactFile(
253               project, localRepository, resolver, factory), attributes, model,
254               getName(reportLocale), reportLocale);
255 
256       final RenderingContext context =
257           new RenderingContext(outputDirectory, getOutputName() + ".html");
258 
259       final SiteRendererSink sink = new SiteRendererSink(context);
260       generate(sink, reportLocale);
261 
262       if (!outputDirectory.mkdirs())
263       {
264         throw new IOException("Cannot generate directories '"
265                               + outputDirectory.getPath() + "'");
266       }
267 
268       // The writer will be closed by the renderer
269       // http://maven.apache.org/doxia/doxia-sitetools/doxia-site-renderer/xref/index.html
270       final Writer writer =
271           new FileWriter(new File(outputDirectory, getOutputName() + ".html"));
272       siteRenderer.generateDocument(writer, sink, siteContext);
273 
274       siteRenderer.copyResources(siteContext, new File(project.getBasedir(),
275           "src/site/resources"), outputDirectory);
276     }
277     catch (final RendererException e)
278     {
279       throw new MojoExecutionException(createErrorMessage(reportLocale), e);
280     }
281     catch (final IOException e)
282     {
283       throw new MojoExecutionException(createErrorMessage(reportLocale), e);
284     }
285     catch (final MavenReportException e)
286     {
287       throw new MojoExecutionException(createErrorMessage(reportLocale), e);
288     }
289   }
290 
291   /**
292    * Creates an error message signaling a problem with the report generation.
293    *
294    * @param reportLocale the locale to select the report name.
295    * @return the error message for failed report generation.
296    */
297   private String createErrorMessage(final Locale reportLocale)
298   {
299     return "An error has occurred in " + getName(reportLocale)
300            + " report generation.";
301   }
302 
303   /**
304    * Determines the locale to use. The plugin allows the user to override the
305    * locale provided by Maven.
306    *
307    * @return the locale to use for this report.
308    */
309   private Locale determineLocale()
310   {
311     return StringUtils.isNotBlank(this.locale) ? LocaleUtils
312         .toLocale(this.locale) : Locale.getDefault();
313   }
314 
315   /**
316    * Returns the resource bundle for the given locale.
317    *
318    * @param locale the locale for which the resource bundle is requested.
319    * @return the bundle for the given locale.
320    */
321   protected ResourceBundle getBundle(final Locale locale)
322   {
323     return ResourceBundle.getBundle("de.smartics.maven.issues.IssuesReport",
324         locale);
325   }
326 
327   // --- object basics --------------------------------------------------------
328 
329 }