View Javadoc

1   package org.apache.maven.plugins.site;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.doxia.sink.Sink;
23  import org.apache.maven.doxia.sink.SinkFactory;
24  import org.apache.maven.doxia.sink.render.RenderingContext;
25  import org.apache.maven.doxia.siterenderer.DocumentRenderer;
26  import org.apache.maven.doxia.siterenderer.Renderer;
27  import org.apache.maven.doxia.siterenderer.RendererException;
28  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
29  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
30  import org.apache.maven.doxia.tools.MojoLogWrapper;
31  import org.apache.maven.plugin.logging.Log;
32  import org.apache.maven.reporting.MavenReport;
33  import org.apache.maven.reporting.MavenMultiPageReport;
34  import org.apache.maven.reporting.MavenReportException;
35  
36  import java.io.FileNotFoundException;
37  import java.io.IOException;
38  import java.io.OutputStream;
39  import java.io.Writer;
40  import java.io.File;
41  import java.io.FileWriter;
42  import java.util.ArrayList;
43  import java.util.Locale;
44  import java.util.List;
45  import java.util.Iterator;
46  
47  /**
48   * Renders a Maven report.
49   *
50   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
51   */
52  public class ReportDocumentRenderer
53      implements DocumentRenderer
54  {
55      private MavenReport report;
56  
57      private RenderingContext renderingContext;
58  
59      private Log log;
60  
61      public ReportDocumentRenderer( MavenReport report, RenderingContext renderingContext, Log log )
62      {
63          this.report = report;
64  
65          this.renderingContext = renderingContext;
66  
67          this.log = log;
68      }
69  
70      private static class MySink
71          extends SiteRendererSink
72      {
73          private File outputDir;
74  
75          private String outputName;
76  
77          public MySink( File outputDir, String outputName, RenderingContext ctx )
78          {
79              super( ctx );
80              this.outputName = outputName;
81              this.outputDir = outputDir;
82          }
83  
84          public String getOutputName()
85          {
86              return outputName;
87          }
88  
89          public File getOutputDir()
90          {
91              return outputDir;
92          }
93  
94      }
95  
96      private static class MySinkFactory
97          implements SinkFactory
98      {
99          private RenderingContext context;
100 
101         private List sinks = new ArrayList();
102 
103         public MySinkFactory( RenderingContext ctx )
104         {
105             this.context = ctx;
106         }
107 
108         public Sink createSink( File outputDir, String outputName )
109         {
110             SiteRendererSink sink = new MySink( outputDir, outputName, context );
111             sinks.add( sink );
112             return sink;
113         }
114 
115         public Sink createSink( File arg0, String arg1, String arg2 )
116             throws IOException
117         {
118             // Not used
119             return null;
120         }
121 
122         public Sink createSink( OutputStream arg0 )
123             throws IOException
124         {
125             // Not used
126             return null;
127         }
128 
129         public Sink createSink( OutputStream arg0, String arg1 )
130             throws IOException
131         {
132             // Not used
133             return null;
134         }
135 
136         public List sinks()
137         {
138             return sinks;
139         }
140     }
141 
142     public void renderDocument( Writer writer, Renderer renderer, SiteRenderingContext siteRenderingContext )
143         throws RendererException, FileNotFoundException
144     {
145         Locale locale = siteRenderingContext.getLocale();
146         String localReportName = report.getName( locale );
147         log.info( "Generating \"" + localReportName + "\" report." );
148 
149         MySinkFactory sf = new MySinkFactory( renderingContext );
150 
151         SiteRendererSink sink = new SiteRendererSink( renderingContext );
152         sink.enableLogging( new MojoLogWrapper( log ) );
153 
154         try
155         {
156             if ( report instanceof MavenMultiPageReport )
157             {
158                 ( (MavenMultiPageReport) report ).generate( sink, sf, locale );
159             }
160             else
161             {
162                 try
163                 {
164                     report.generate( sink, locale );
165                 }
166                 catch ( NoSuchMethodError e )
167                 {
168                     throw new RendererException( "No method on " + report.getClass(), e );
169                 }
170             }
171         }
172         catch ( MavenReportException e )
173         {
174             throw new RendererException( "Error rendering Maven report: " + e.getMessage(), e );
175         }
176         finally
177         {
178             sink.close();
179         }
180 
181         if ( !report.isExternalReport() )
182         {
183             try
184             {
185                 List sinks = sf.sinks();
186 
187                 log.debug( "Multipage report: " + sinks.size() + " subreports" );
188 
189                 for ( Iterator it = sinks.iterator(); it.hasNext(); )
190                 {
191                     MySink mySink = (MySink) it.next();
192                     mySink.enableLogging( new MojoLogWrapper( log ) );
193 
194                     log.debug( "  Rendering " + mySink.getOutputName() );
195 
196                     Writer out = new FileWriter( new File( mySink.getOutputDir(), mySink.getOutputName() ) );
197 
198                     try
199                     {
200                         renderer.generateDocument( out, mySink, siteRenderingContext );
201                     }
202                     finally
203                     {
204                         mySink.close();
205                     }
206                 }
207             }
208             catch ( IOException e )
209             {
210                 throw new RendererException( "Cannot create writer", e );
211             }
212 
213             renderer.generateDocument( writer, sink, siteRenderingContext );
214         }
215     }
216 
217     public String getOutputName()
218     {
219         return renderingContext.getOutputName();
220     }
221 
222     public RenderingContext getRenderingContext()
223     {
224         return renderingContext;
225     }
226 
227     public boolean isOverwrite()
228     {
229         // TODO: would be nice to query the report to see if it is modified
230         return true;
231     }
232 
233     /**
234      * @return true if the current report is external, false otherwise
235      */
236     public boolean isExternalReport()
237     {
238         return report.isExternalReport();
239     }
240 }