View Javadoc

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