Search in sources :

Example 1 with MavenMultiPageReport

use of org.apache.maven.reporting.MavenMultiPageReport in project maven-plugins by apache.

the class ReportDocumentRenderer method renderDocument.

public void renderDocument(Writer writer, Renderer renderer, SiteRenderingContext siteRenderingContext) throws RendererException, FileNotFoundException {
    Locale locale = siteRenderingContext.getLocale();
    String localReportName = report.getName(locale);
    String msg = "Generating \"" + buffer().strong(localReportName) + "\" report";
    // CHECKSTYLE_OFF: MagicNumber
    log.info(reportMojoInfo == null ? (msg + '.') : (StringUtils.rightPad(msg, 40) + buffer().strong(" --- ").mojo(reportMojoInfo)));
    // CHECKSTYLE_ON: MagicNumber
    // main sink
    SiteRendererSink mainSink = new SiteRendererSink(renderingContext);
    // sink factory, for multi-page reports that need sub-sinks
    MultiPageSinkFactory multiPageSinkFactory = new MultiPageSinkFactory(renderingContext);
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (classLoader != null) {
            Thread.currentThread().setContextClassLoader(classLoader);
        }
        if (report instanceof MavenMultiPageReport) {
            // extended multi-page API
            ((MavenMultiPageReport) report).generate(mainSink, multiPageSinkFactory, locale);
        } else if (generateMultiPage(locale, multiPageSinkFactory, mainSink)) {
        // extended multi-page API for Maven 2.2, only accessible by reflection API
        } else {
            // old single-page-only API
            report.generate(mainSink, locale);
        }
    } catch (MavenReportException e) {
        String report = (reportMojoInfo == null) ? ('"' + localReportName + "\" report") : reportMojoInfo;
        throw new RendererException("Error generating " + report + ": " + e.getMessage(), e);
    } catch (LinkageError e) {
        String report = (reportMojoInfo == null) ? ('"' + localReportName + "\" report") : reportMojoInfo;
        log.warn("An issue has occurred with " + report + ", skipping LinkageError " + e.getMessage() + ", please report an issue to Maven dev team.", e);
    } finally {
        if (classLoader != null) {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }
        mainSink.close();
    }
    if (report.isExternalReport()) {
        // external reports are rendered from their own: no Doxia site rendering needed
        return;
    }
    // render main sink
    renderer.generateDocument(writer, mainSink, siteRenderingContext);
    // render sub-sinks, eventually created by multi-page reports
    try {
        List<MultiPageSubSink> sinks = multiPageSinkFactory.sinks();
        log.debug("Multipage report: " + sinks.size() + " subreports");
        for (MultiPageSubSink mySink : sinks) {
            mySink.enableLogging(new MojoLogWrapper(log));
            log.debug("  Rendering " + mySink.getOutputName());
            File outputFile = new File(mySink.getOutputDir(), mySink.getOutputName());
            Writer out = null;
            try {
                out = WriterFactory.newWriter(outputFile, siteRenderingContext.getOutputEncoding());
                renderer.generateDocument(out, mySink, siteRenderingContext);
                mySink.close();
                mySink = null;
                out.close();
                out = null;
            } finally {
                IOUtil.close(out);
                if (mySink != null) {
                    mySink.close();
                }
            }
        }
    } catch (IOException e) {
        throw new RendererException("Cannot create writer", e);
    }
}
Also used : Locale(java.util.Locale) IOException(java.io.IOException) MavenMultiPageReport(org.apache.maven.reporting.MavenMultiPageReport) RendererException(org.apache.maven.doxia.siterenderer.RendererException) MojoLogWrapper(org.apache.maven.doxia.tools.MojoLogWrapper) SiteRendererSink(org.apache.maven.doxia.siterenderer.sink.SiteRendererSink) File(java.io.File) Writer(java.io.Writer) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 Locale (java.util.Locale)1 RendererException (org.apache.maven.doxia.siterenderer.RendererException)1 SiteRendererSink (org.apache.maven.doxia.siterenderer.sink.SiteRendererSink)1 MojoLogWrapper (org.apache.maven.doxia.tools.MojoLogWrapper)1 MavenMultiPageReport (org.apache.maven.reporting.MavenMultiPageReport)1 MavenReportException (org.apache.maven.reporting.MavenReportException)1