1 | |
package org.apache.maven.plugins.site; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
49 | |
|
50 | |
|
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 | 0 | { |
63 | 0 | this.report = report; |
64 | |
|
65 | 0 | this.renderingContext = renderingContext; |
66 | |
|
67 | 0 | this.log = log; |
68 | 0 | } |
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 | 0 | super( ctx ); |
80 | 0 | this.outputName = outputName; |
81 | 0 | this.outputDir = outputDir; |
82 | 0 | } |
83 | |
|
84 | |
public String getOutputName() |
85 | |
{ |
86 | 0 | return outputName; |
87 | |
} |
88 | |
|
89 | |
public File getOutputDir() |
90 | |
{ |
91 | 0 | return outputDir; |
92 | |
} |
93 | |
|
94 | |
} |
95 | |
|
96 | |
private static class MySinkFactory |
97 | |
implements SinkFactory |
98 | |
{ |
99 | |
private RenderingContext context; |
100 | |
|
101 | 0 | private List sinks = new ArrayList(); |
102 | |
|
103 | |
public MySinkFactory( RenderingContext ctx ) |
104 | 0 | { |
105 | 0 | this.context = ctx; |
106 | 0 | } |
107 | |
|
108 | |
public Sink createSink( File outputDir, String outputName ) |
109 | |
{ |
110 | 0 | SiteRendererSink sink = new MySink( outputDir, outputName, context ); |
111 | 0 | sinks.add( sink ); |
112 | 0 | return sink; |
113 | |
} |
114 | |
|
115 | |
public Sink createSink( File arg0, String arg1, String arg2 ) |
116 | |
throws IOException |
117 | |
{ |
118 | |
|
119 | 0 | return null; |
120 | |
} |
121 | |
|
122 | |
public Sink createSink( OutputStream arg0 ) |
123 | |
throws IOException |
124 | |
{ |
125 | |
|
126 | 0 | return null; |
127 | |
} |
128 | |
|
129 | |
public Sink createSink( OutputStream arg0, String arg1 ) |
130 | |
throws IOException |
131 | |
{ |
132 | |
|
133 | 0 | return null; |
134 | |
} |
135 | |
|
136 | |
public List sinks() |
137 | |
{ |
138 | 0 | return sinks; |
139 | |
} |
140 | |
} |
141 | |
|
142 | |
public void renderDocument( Writer writer, Renderer renderer, SiteRenderingContext siteRenderingContext ) |
143 | |
throws RendererException, FileNotFoundException |
144 | |
{ |
145 | 0 | Locale locale = siteRenderingContext.getLocale(); |
146 | 0 | String localReportName = report.getName( locale ); |
147 | 0 | log.info( "Generating \"" + localReportName + "\" report." ); |
148 | |
|
149 | 0 | MySinkFactory sf = new MySinkFactory( renderingContext ); |
150 | |
|
151 | 0 | SiteRendererSink sink = new SiteRendererSink( renderingContext ); |
152 | 0 | sink.enableLogging( new MojoLogWrapper( log ) ); |
153 | |
|
154 | |
try |
155 | |
{ |
156 | 0 | if ( report instanceof MavenMultiPageReport ) |
157 | |
{ |
158 | 0 | ( (MavenMultiPageReport) report ).generate( sink, sf, locale ); |
159 | |
} |
160 | |
else |
161 | |
{ |
162 | |
try |
163 | |
{ |
164 | 0 | report.generate( sink, locale ); |
165 | |
} |
166 | 0 | catch ( NoSuchMethodError e ) |
167 | |
{ |
168 | 0 | throw new RendererException( "No method on " + report.getClass(), e ); |
169 | 0 | } |
170 | |
} |
171 | |
} |
172 | 0 | catch ( MavenReportException e ) |
173 | |
{ |
174 | 0 | throw new RendererException( "Error rendering Maven report: " + e.getMessage(), e ); |
175 | |
} |
176 | |
finally |
177 | |
{ |
178 | 0 | sink.close(); |
179 | 0 | } |
180 | |
|
181 | 0 | if ( !report.isExternalReport() ) |
182 | |
{ |
183 | |
try |
184 | |
{ |
185 | 0 | List sinks = sf.sinks(); |
186 | |
|
187 | 0 | log.debug( "Multipage report: " + sinks.size() + " subreports" ); |
188 | |
|
189 | 0 | for ( Iterator it = sinks.iterator(); it.hasNext(); ) |
190 | |
{ |
191 | 0 | MySink mySink = (MySink) it.next(); |
192 | 0 | mySink.enableLogging( new MojoLogWrapper( log ) ); |
193 | |
|
194 | 0 | log.debug( " Rendering " + mySink.getOutputName() ); |
195 | |
|
196 | 0 | Writer out = new FileWriter( new File( mySink.getOutputDir(), mySink.getOutputName() ) ); |
197 | |
|
198 | |
try |
199 | |
{ |
200 | 0 | renderer.generateDocument( out, mySink, siteRenderingContext ); |
201 | |
} |
202 | |
finally |
203 | |
{ |
204 | 0 | mySink.close(); |
205 | 0 | } |
206 | 0 | } |
207 | |
} |
208 | 0 | catch ( IOException e ) |
209 | |
{ |
210 | 0 | throw new RendererException( "Cannot create writer", e ); |
211 | 0 | } |
212 | |
|
213 | 0 | renderer.generateDocument( writer, sink, siteRenderingContext ); |
214 | |
} |
215 | 0 | } |
216 | |
|
217 | |
public String getOutputName() |
218 | |
{ |
219 | 0 | return renderingContext.getOutputName(); |
220 | |
} |
221 | |
|
222 | |
public RenderingContext getRenderingContext() |
223 | |
{ |
224 | 0 | return renderingContext; |
225 | |
} |
226 | |
|
227 | |
public boolean isOverwrite() |
228 | |
{ |
229 | |
|
230 | 0 | return true; |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
public boolean isExternalReport() |
237 | |
{ |
238 | 0 | return report.isExternalReport(); |
239 | |
} |
240 | |
} |