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.module.xhtml.decoration.render.RenderingContext;
23  import org.apache.maven.doxia.sink.Sink;
24  import org.apache.maven.doxia.sink.SinkFactory;
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.plugin.logging.Log;
31  import org.apache.maven.reporting.MavenReport;
32  import org.apache.maven.reporting.MavenMultiPageReport;
33  import org.apache.maven.reporting.MavenReportException;
34  
35  import java.io.FileNotFoundException;
36  import java.io.IOException;
37  import java.io.Writer;
38  import java.io.File;
39  import java.io.FileWriter;
40  import java.util.ArrayList;
41  import java.util.Locale;
42  import java.util.List;
43  import java.util.Iterator;
44  
45  /**
46   * Renders a Maven report.
47   *
48   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
49   */
50  public class ReportDocumentRenderer
51      implements DocumentRenderer
52  {
53      private MavenReport report;
54  
55      private RenderingContext renderingContext;
56  
57      private Log log;
58  
59      public ReportDocumentRenderer( MavenReport report, RenderingContext renderingContext, Log log )
60      {
61          this.report = report;
62  
63          this.renderingContext = renderingContext;
64  
65          this.log = log;
66      }
67  
68      private static class MySink extends SiteRendererSink
69      {
70          private File outputDir;
71  
72          private String outputName;
73  
74          public MySink( File outputDir, String outputName, RenderingContext ctx )
75          {
76              super( ctx );
77              this.outputName = outputName;
78              this.outputDir = outputDir;
79          }
80  
81          public String getOutputName()
82          {
83              return outputName;
84          }
85  
86          public File getOutputDir()
87          {
88              return outputDir;
89          }
90  
91      }
92  
93      private static class MySinkFactory implements SinkFactory
94      {
95          private RenderingContext context;
96  
97          private List sinks = new ArrayList();
98  
99          public MySinkFactory( RenderingContext ctx )
100         {
101             this.context = ctx;
102         }
103 
104         public Sink createSink( File outputDir, String outputName )
105         {
106             SiteRendererSink sink = new MySink( outputDir, outputName, context );
107             sinks.add( sink );
108             return sink;
109         }
110 
111         public List sinks()
112         {
113             return sinks;
114         }
115     }
116 
117 
118     public void renderDocument( Writer writer, Renderer renderer, SiteRenderingContext siteRenderingContext )
119         throws RendererException, FileNotFoundException
120     {
121         Locale locale = siteRenderingContext.getLocale();
122         String localReportName = report.getName( locale );
123         log.info( "Generating \"" + localReportName + "\" report." );
124 
125         MySinkFactory sf = new MySinkFactory( renderingContext );
126 
127         SiteRendererSink sink = new SiteRendererSink( renderingContext );
128 
129         try
130         {
131             if ( report instanceof MavenMultiPageReport )
132             {
133                 ( (MavenMultiPageReport) report ).generate( sink, sf, locale );
134             }
135             else
136             {
137                 try
138                 {
139                     report.generate( sink, locale );
140                 }
141                 catch ( NoSuchMethodError e )
142                 {
143                     throw new RendererException( "No method on " + report.getClass(), e );
144                 }
145             }
146         }
147         catch ( MavenReportException e )
148         {
149             throw new RendererException( "Error rendering Maven report: " + e.getMessage(), e );
150         }
151 
152         if ( !report.isExternalReport() )
153         {
154             try
155             {
156                 List sinks = sf.sinks();
157 
158                 log.debug( "Multipage report: " + sinks.size() + " subreports" );
159 
160                 for ( Iterator it = sinks.iterator(); it.hasNext(); )
161                 {
162                     MySink mySink = (MySink) it.next();
163 
164                     log.debug( "  Rendering " +  mySink.getOutputName() );
165 
166                     Writer out = new FileWriter( new File( mySink.getOutputDir(), mySink.getOutputName() ) );
167 
168                     renderer.generateDocument( out, mySink, siteRenderingContext );
169                 }
170             }
171             catch ( IOException e )
172             {
173                 throw new RendererException( "Cannot create writer", e );
174             }
175 
176             renderer.generateDocument( writer, sink, siteRenderingContext );
177         }
178     }
179 
180     public String getOutputName()
181     {
182         return renderingContext.getOutputName();
183     }
184 
185     public RenderingContext getRenderingContext()
186     {
187         return renderingContext;
188     }
189 
190     public boolean isOverwrite()
191     {
192         // TODO: would be nice to query the report to see if it is modified
193         return true;
194     }
195 
196     /**
197      * @return true if the current report is external, false otherwise
198      */
199     public boolean isExternalReport()
200     {
201         return report.isExternalReport();
202     }
203 }