Coverage Report - org.apache.maven.plugins.site.ReportDocumentRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
ReportDocumentRenderer
0%
0/36
0%
0/6
1,75
ReportDocumentRenderer$MySink
0%
0/6
N/A
1,75
ReportDocumentRenderer$MySinkFactory
0%
0/8
N/A
1,75
 
 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  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  
         // TODO: would be nice to query the report to see if it is modified
 193  0
         return true;
 194  
     }
 195  
 
 196  
     /**
 197  
      * @return true if the current report is external, false otherwise
 198  
      */
 199  
     public boolean isExternalReport()
 200  
     {
 201  0
         return report.isExternalReport();
 202  
     }
 203  
 }