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.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 | |
|
47 | |
|
48 | |
|
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 | 0 | { |
61 | 0 | this.report = report; |
62 | |
|
63 | 0 | this.renderingContext = renderingContext; |
64 | |
|
65 | 0 | this.log = log; |
66 | 0 | } |
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 | 0 | super( ctx ); |
77 | 0 | this.outputName = outputName; |
78 | 0 | this.outputDir = outputDir; |
79 | 0 | } |
80 | |
|
81 | |
public String getOutputName() |
82 | |
{ |
83 | 0 | return outputName; |
84 | |
} |
85 | |
|
86 | |
public File getOutputDir() |
87 | |
{ |
88 | 0 | return outputDir; |
89 | |
} |
90 | |
|
91 | |
} |
92 | |
|
93 | |
private static class MySinkFactory implements SinkFactory |
94 | |
{ |
95 | |
private RenderingContext context; |
96 | |
|
97 | 0 | private List sinks = new ArrayList(); |
98 | |
|
99 | |
public MySinkFactory( RenderingContext ctx ) |
100 | 0 | { |
101 | 0 | this.context = ctx; |
102 | 0 | } |
103 | |
|
104 | |
public Sink createSink( File outputDir, String outputName ) |
105 | |
{ |
106 | 0 | SiteRendererSink sink = new MySink( outputDir, outputName, context ); |
107 | 0 | sinks.add( sink ); |
108 | 0 | return sink; |
109 | |
} |
110 | |
|
111 | |
public List sinks() |
112 | |
{ |
113 | 0 | return sinks; |
114 | |
} |
115 | |
} |
116 | |
|
117 | |
|
118 | |
public void renderDocument( Writer writer, Renderer renderer, SiteRenderingContext siteRenderingContext ) |
119 | |
throws RendererException, FileNotFoundException |
120 | |
{ |
121 | 0 | Locale locale = siteRenderingContext.getLocale(); |
122 | 0 | String localReportName = report.getName( locale ); |
123 | 0 | log.info( "Generating \"" + localReportName + "\" report." ); |
124 | |
|
125 | 0 | MySinkFactory sf = new MySinkFactory( renderingContext ); |
126 | |
|
127 | 0 | SiteRendererSink sink = new SiteRendererSink( renderingContext ); |
128 | |
|
129 | |
try |
130 | |
{ |
131 | 0 | if ( report instanceof MavenMultiPageReport ) |
132 | |
{ |
133 | 0 | ( (MavenMultiPageReport) report ).generate( sink, sf, locale ); |
134 | |
} |
135 | |
else |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 0 | report.generate( sink, locale ); |
140 | |
} |
141 | 0 | catch ( NoSuchMethodError e ) |
142 | |
{ |
143 | 0 | throw new RendererException( "No method on " + report.getClass(), e ); |
144 | 0 | } |
145 | |
} |
146 | |
} |
147 | 0 | catch ( MavenReportException e ) |
148 | |
{ |
149 | 0 | throw new RendererException( "Error rendering Maven report: " + e.getMessage(), e ); |
150 | 0 | } |
151 | |
|
152 | 0 | if ( !report.isExternalReport() ) |
153 | |
{ |
154 | |
try |
155 | |
{ |
156 | 0 | List sinks = sf.sinks(); |
157 | |
|
158 | 0 | log.debug( "Multipage report: " + sinks.size() + " subreports" ); |
159 | |
|
160 | 0 | for ( Iterator it = sinks.iterator(); it.hasNext(); ) |
161 | |
{ |
162 | 0 | MySink mySink = (MySink) it.next(); |
163 | |
|
164 | 0 | log.debug( " Rendering " + mySink.getOutputName() ); |
165 | |
|
166 | 0 | Writer out = new FileWriter( new File( mySink.getOutputDir(), mySink.getOutputName() ) ); |
167 | |
|
168 | 0 | renderer.generateDocument( out, mySink, siteRenderingContext ); |
169 | |
} |
170 | |
} |
171 | 0 | catch ( IOException e ) |
172 | |
{ |
173 | 0 | throw new RendererException( "Cannot create writer", e ); |
174 | 0 | } |
175 | |
|
176 | 0 | renderer.generateDocument( writer, sink, siteRenderingContext ); |
177 | |
} |
178 | 0 | } |
179 | |
|
180 | |
public String getOutputName() |
181 | |
{ |
182 | 0 | return renderingContext.getOutputName(); |
183 | |
} |
184 | |
|
185 | |
public RenderingContext getRenderingContext() |
186 | |
{ |
187 | 0 | return renderingContext; |
188 | |
} |
189 | |
|
190 | |
public boolean isOverwrite() |
191 | |
{ |
192 | |
|
193 | 0 | return true; |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
public boolean isExternalReport() |
200 | |
{ |
201 | 0 | return report.isExternalReport(); |
202 | |
} |
203 | |
} |