mirror of https://github.com/apache/maven.git
PR: MNG-1003
Submitted by: Edwin Punzalan refactoring checkstyle report git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@312847 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
07d3b5eb62
commit
616ab98fa4
|
@ -33,8 +33,8 @@ import org.apache.maven.reporting.AbstractMavenReport;
|
|||
import org.apache.maven.reporting.MavenReportException;
|
||||
import org.codehaus.doxia.site.renderer.SiteRenderer;
|
||||
import org.codehaus.plexus.util.FileUtils;
|
||||
import org.codehaus.plexus.util.StringUtils;
|
||||
import org.codehaus.plexus.util.StringOutputStream;
|
||||
import org.codehaus.plexus.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
@ -46,6 +46,7 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
|
||||
|
@ -119,8 +120,7 @@ public class CheckstyleReport
|
|||
|
||||
/**
|
||||
* If null, the checkstyle task will display violations on stdout. Otherwise, the text file will be
|
||||
* created with the violations. Note: This is in addition to the XML result file (containing
|
||||
* the violations in XML format which is always created.
|
||||
* created with the violations.
|
||||
*
|
||||
* @parameter
|
||||
*/
|
||||
|
@ -237,6 +237,16 @@ public class CheckstyleReport
|
|||
*/
|
||||
public void executeReport( Locale locale )
|
||||
throws MavenReportException
|
||||
{
|
||||
Map files = executeCheckstyle();
|
||||
|
||||
CheckstyleReportGenerator generator = new CheckstyleReportGenerator( getSink(), getBundle( locale ) );
|
||||
|
||||
generator.generateReport( files );
|
||||
}
|
||||
|
||||
private Map executeCheckstyle()
|
||||
throws MavenReportException
|
||||
{
|
||||
File[] files = getFilesToProcess( includes, excludes );
|
||||
|
||||
|
@ -252,8 +262,8 @@ public class CheckstyleReport
|
|||
|
||||
try
|
||||
{
|
||||
Configuration config = ConfigurationLoader
|
||||
.loadConfiguration( configFile, new PropertiesExpander( overridingProperties ) );
|
||||
Configuration config = ConfigurationLoader.loadConfiguration( configFile, new PropertiesExpander(
|
||||
overridingProperties ) );
|
||||
|
||||
checker = new Checker();
|
||||
|
||||
|
@ -283,7 +293,7 @@ public class CheckstyleReport
|
|||
|
||||
checker.addListener( getConsoleListener() );
|
||||
|
||||
AuditListener sinkListener = new CheckstyleReportListener( getSink(), sourceDirectory, getBundle( locale ) );
|
||||
CheckstyleReportListener sinkListener = new CheckstyleReportListener( sourceDirectory );
|
||||
|
||||
checker.addListener( sinkListener );
|
||||
|
||||
|
@ -300,6 +310,8 @@ public class CheckstyleReport
|
|||
{
|
||||
throw new MavenReportException( "There are " + nbErrors + " formatting errors." );
|
||||
}
|
||||
|
||||
return sinkListener.getFiles();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
|
|
@ -0,0 +1,313 @@
|
|||
package org.apache.maven.plugin.checkstyle;
|
||||
|
||||
/*
|
||||
* Copyright 2004-2005 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
|
||||
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.Map;
|
||||
|
||||
import org.codehaus.doxia.sink.Sink;
|
||||
|
||||
public class CheckstyleReportGenerator
|
||||
{
|
||||
private ResourceBundle bundle;
|
||||
|
||||
private Sink sink;
|
||||
|
||||
private SeverityLevel severityLevel;
|
||||
|
||||
public CheckstyleReportGenerator( Sink sink, ResourceBundle bundle )
|
||||
{
|
||||
this.bundle = bundle;
|
||||
|
||||
this.sink = sink;
|
||||
}
|
||||
|
||||
private String getTitle()
|
||||
{
|
||||
String title;
|
||||
|
||||
if ( getSeverityLevel() == null )
|
||||
title = bundle.getString( "report.checkstyle.title" );
|
||||
else
|
||||
title = bundle.getString( "report.checkstyle.severity_title" ) + severityLevel.getName();
|
||||
|
||||
return title;
|
||||
}
|
||||
|
||||
public void generateReport( Map files )
|
||||
{
|
||||
doHeading();
|
||||
|
||||
if ( getSeverityLevel() == null )
|
||||
{
|
||||
doSeveritySummary( files );
|
||||
|
||||
doFilesSummary( files );
|
||||
}
|
||||
|
||||
doDetails( files );
|
||||
}
|
||||
|
||||
private void doHeading()
|
||||
{
|
||||
sink.head();
|
||||
sink.title();
|
||||
sink.text( getTitle() );
|
||||
sink.title_();
|
||||
sink.head_();
|
||||
|
||||
sink.body();
|
||||
|
||||
sink.section1();
|
||||
sink.sectionTitle1();
|
||||
sink.text( getTitle() );
|
||||
sink.sectionTitle1_();
|
||||
|
||||
sink.paragraph();
|
||||
sink.text( bundle.getString( "report.checkstyle.checkstylelink" ) + " " );
|
||||
sink.link( "http://checkstyle.sourceforge.net/" );
|
||||
sink.text( "Checkstyle" );
|
||||
sink.link_();
|
||||
sink.paragraph_();
|
||||
}
|
||||
|
||||
private void doSeveritySummary( Map files )
|
||||
{
|
||||
sink.section1();
|
||||
sink.sectionTitle1();
|
||||
sink.text( bundle.getString( "report.checkstyle.summary" ) );
|
||||
sink.sectionTitle1_();
|
||||
|
||||
sink.table();
|
||||
|
||||
sink.tableRow();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( bundle.getString( "report.checkstyle.files" ) );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( "Infos" );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( "Warnings" );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( "Errors" );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableRow_();
|
||||
|
||||
sink.tableRow();
|
||||
sink.tableCell();
|
||||
sink.text( String.valueOf( files.size() ) );
|
||||
sink.tableCell_();
|
||||
sink.tableCell();
|
||||
sink.text( countSeverity( files.values().iterator(), SeverityLevel.INFO ) );
|
||||
sink.tableCell_();
|
||||
sink.tableCell();
|
||||
sink.text( countSeverity( files.values().iterator(), SeverityLevel.WARNING ) );
|
||||
sink.tableCell_();
|
||||
sink.tableCell();
|
||||
sink.text( countSeverity( files.values().iterator(), SeverityLevel.ERROR ) );
|
||||
sink.tableCell_();
|
||||
sink.tableRow_();
|
||||
|
||||
sink.table_();
|
||||
|
||||
sink.section1_();
|
||||
}
|
||||
|
||||
private String countSeverity( Iterator files, SeverityLevel level )
|
||||
{
|
||||
long count = 0;
|
||||
|
||||
while ( files.hasNext() )
|
||||
{
|
||||
List errors = (List) files.next();
|
||||
|
||||
for( Iterator error = errors.iterator(); error.hasNext(); )
|
||||
{
|
||||
AuditEvent event = (AuditEvent) error.next();
|
||||
|
||||
if ( event.getSeverityLevel().equals( level ) ) count++;
|
||||
}
|
||||
}
|
||||
|
||||
return String.valueOf( count );
|
||||
}
|
||||
|
||||
private void doFilesSummary( Map filesMap )
|
||||
{
|
||||
sink.section1();
|
||||
sink.sectionTitle1();
|
||||
sink.text( bundle.getString( "report.checkstyle.files" ) );
|
||||
sink.sectionTitle1_();
|
||||
|
||||
sink.table();
|
||||
|
||||
sink.tableRow();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( bundle.getString( "report.checkstyle.files" ) );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( "I" );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( "W" );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( "E" );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableRow_();
|
||||
|
||||
for( Iterator files = filesMap.keySet().iterator(); files.hasNext(); )
|
||||
{
|
||||
String filename = (String) files.next();
|
||||
List errors = (List) filesMap.get( filename );
|
||||
|
||||
sink.tableRow();
|
||||
|
||||
sink.tableCell();
|
||||
sink.link( "#" + filename );
|
||||
sink.text( filename );
|
||||
sink.link_();
|
||||
sink.tableCell_();
|
||||
|
||||
sink.tableCell();
|
||||
sink.text( countSeverity( Collections.singletonList( errors ).iterator(), SeverityLevel.INFO ) );
|
||||
sink.tableCell_();
|
||||
|
||||
sink.tableCell();
|
||||
sink.text( countSeverity( Collections.singletonList( errors ).iterator(), SeverityLevel.WARNING ) );
|
||||
sink.tableCell_();
|
||||
|
||||
sink.tableCell();
|
||||
sink.text( countSeverity( Collections.singletonList( errors ).iterator(), SeverityLevel.ERROR ) );
|
||||
sink.tableCell_();
|
||||
|
||||
sink.tableRow_();
|
||||
}
|
||||
|
||||
sink.table_();
|
||||
sink.section1_();
|
||||
}
|
||||
|
||||
private void doDetails( Map filesMap )
|
||||
{
|
||||
Iterator files = filesMap.keySet().iterator();
|
||||
|
||||
while ( files.hasNext() )
|
||||
{
|
||||
String file = (String) files.next();
|
||||
List eventList = (List) filesMap.get( file );
|
||||
|
||||
sink.section1();
|
||||
sink.sectionTitle1();
|
||||
sink.anchor( file );
|
||||
sink.text( file );
|
||||
sink.anchor_();
|
||||
sink.sectionTitle1_();
|
||||
|
||||
sink.table();
|
||||
sink.tableRow();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( bundle.getString( "report.checkstyle.column.violation" ) );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( "Message" );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( "Line" );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableRow_();
|
||||
|
||||
doFileEvents( eventList);
|
||||
|
||||
sink.table_();
|
||||
sink.section1_();
|
||||
}
|
||||
}
|
||||
|
||||
private void doFileEvents( List eventList )
|
||||
{
|
||||
Iterator events = eventList.iterator();
|
||||
while ( events.hasNext() )
|
||||
{
|
||||
AuditEvent event = (AuditEvent) events.next();
|
||||
SeverityLevel level = event.getSeverityLevel();
|
||||
|
||||
if ( getSeverityLevel() != null )
|
||||
if ( !getSeverityLevel().equals( level ) ) continue;
|
||||
|
||||
sink.tableRow();
|
||||
|
||||
sink.tableCell();
|
||||
sink.figure();
|
||||
sink.figureCaption();
|
||||
sink.text( level.getName() );
|
||||
sink.figureCaption_();
|
||||
|
||||
if ( SeverityLevel.INFO.equals( level ) )
|
||||
sink.figureGraphics( "images/icon_info_sml.gif" );
|
||||
else if ( SeverityLevel.WARNING.equals( level ) )
|
||||
sink.figureGraphics( "images/icon_warning_sml.gif" );
|
||||
else if ( SeverityLevel.ERROR.equals( level ) )
|
||||
sink.figureGraphics( "images/icon_error_sml.gif" );
|
||||
|
||||
sink.figure_();
|
||||
sink.tableCell_();
|
||||
|
||||
sink.tableCell();
|
||||
sink.text( event.getMessage() );
|
||||
sink.tableCell_();
|
||||
|
||||
sink.tableCell();
|
||||
sink.text( String.valueOf( event.getLine() ) );
|
||||
sink.tableCell_();
|
||||
|
||||
sink.tableRow_();
|
||||
}
|
||||
}
|
||||
|
||||
private Iterator getCheckstyleEvents( Iterator events, SeverityLevel level )
|
||||
{
|
||||
LinkedList filtered = new LinkedList();
|
||||
|
||||
while( events.hasNext() )
|
||||
{
|
||||
AuditEvent event = (AuditEvent) events.next();
|
||||
|
||||
if ( event.getSeverityLevel().equals( level ) ) filtered.add( event );
|
||||
}
|
||||
|
||||
return filtered.iterator();
|
||||
}
|
||||
|
||||
public SeverityLevel getSeverityLevel()
|
||||
{
|
||||
return severityLevel;
|
||||
}
|
||||
|
||||
public void setSeverityLevel(SeverityLevel severityLevel)
|
||||
{
|
||||
this.severityLevel = severityLevel;
|
||||
}
|
||||
}
|
|
@ -16,15 +16,13 @@ package org.apache.maven.plugin.checkstyle;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.codehaus.doxia.sink.Sink;
|
||||
import org.codehaus.plexus.util.StringUtils;
|
||||
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
|
||||
import com.puppycrawl.tools.checkstyle.api.AuditListener;
|
||||
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
|
||||
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
|
||||
import java.util.LinkedList;
|
||||
import java.util.TreeMap;
|
||||
import org.codehaus.plexus.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
|
||||
|
@ -34,116 +32,65 @@ public class CheckstyleReportListener
|
|||
extends AutomaticBean
|
||||
implements AuditListener
|
||||
{
|
||||
private Sink sink;
|
||||
|
||||
private String sourceDirectory;
|
||||
|
||||
private String currentFilename;
|
||||
private TreeMap files;
|
||||
|
||||
private boolean fileInitialized;
|
||||
private String currentFile;
|
||||
|
||||
private ResourceBundle bundle;
|
||||
private LinkedList events;
|
||||
|
||||
public CheckstyleReportListener( Sink sink, String sourceDirectory, ResourceBundle bundle )
|
||||
private SeverityLevel severityLevel;
|
||||
|
||||
public CheckstyleReportListener( String sourceDirectory )
|
||||
{
|
||||
this.sink = sink;
|
||||
this.sourceDirectory = sourceDirectory;
|
||||
this.bundle = bundle;
|
||||
}
|
||||
|
||||
private String getTitle()
|
||||
public void setSeverityLevelFilter( SeverityLevel severityLevel )
|
||||
{
|
||||
return bundle.getString( "report.checkstyle.title" );
|
||||
this.severityLevel = severityLevel;
|
||||
}
|
||||
|
||||
public SeverityLevel getSeverityLevelFilter()
|
||||
{
|
||||
return severityLevel;
|
||||
}
|
||||
|
||||
public void auditStarted( AuditEvent event )
|
||||
{
|
||||
sink.head();
|
||||
sink.title();
|
||||
sink.text( getTitle() );
|
||||
sink.title_();
|
||||
sink.head_();
|
||||
|
||||
sink.body();
|
||||
|
||||
sink.section1();
|
||||
sink.sectionTitle1();
|
||||
sink.text( getTitle() );
|
||||
sink.sectionTitle1_();
|
||||
|
||||
sink.paragraph();
|
||||
sink.text( bundle.getString( "report.checkstyle.checkstylelink" ) + " " );
|
||||
sink.link( "http://checkstyle.sourceforge.net/" );
|
||||
sink.text( "Checkstyle" );
|
||||
sink.link_();
|
||||
sink.paragraph_();
|
||||
|
||||
// TODO overall summary
|
||||
|
||||
sink.section1_();
|
||||
sink.sectionTitle1();
|
||||
sink.text( bundle.getString( "report.checkstyle.files" ) );
|
||||
sink.sectionTitle1_();
|
||||
|
||||
// TODO files summary
|
||||
setFiles( new TreeMap() );
|
||||
}
|
||||
|
||||
public void auditFinished( AuditEvent event )
|
||||
{
|
||||
sink.section1_();
|
||||
sink.body_();
|
||||
sink.flush();
|
||||
sink.close();
|
||||
//do nothing
|
||||
}
|
||||
|
||||
public void fileStarted( AuditEvent event )
|
||||
{
|
||||
currentFilename = StringUtils.substring( event.getFileName(), sourceDirectory.length() + 1 );
|
||||
currentFilename = StringUtils.replace( currentFilename, "\\", "/" );
|
||||
fileInitialized = false;
|
||||
currentFile = StringUtils.substring( event.getFileName(), sourceDirectory.length() + 1 );
|
||||
currentFile = StringUtils.replace( currentFile, "\\", "/" );
|
||||
|
||||
if ( !getFiles().containsKey( currentFile ) )
|
||||
getFiles().put( currentFile, new LinkedList() );
|
||||
|
||||
events = (LinkedList) getFiles().get( currentFile );
|
||||
}
|
||||
|
||||
public void fileFinished( AuditEvent event )
|
||||
{
|
||||
if ( fileInitialized )
|
||||
{
|
||||
sink.table_();
|
||||
sink.section2_();
|
||||
}
|
||||
getFiles().put( currentFile, events );
|
||||
currentFile = null;
|
||||
}
|
||||
|
||||
public void addError( AuditEvent event )
|
||||
{
|
||||
if ( !SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) )
|
||||
if ( SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) ) return;
|
||||
|
||||
if ( severityLevel == null || severityLevel.equals( event.getSeverityLevel() ) )
|
||||
{
|
||||
if ( !fileInitialized )
|
||||
{
|
||||
sink.section2();
|
||||
sink.sectionTitle2();
|
||||
sink.text( currentFilename );
|
||||
sink.sectionTitle2_();
|
||||
|
||||
sink.table();
|
||||
sink.tableRow();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( bundle.getString( "report.checkstyle.column.violation" ) );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableHeaderCell();
|
||||
sink.text( bundle.getString( "report.checkstyle.column.line" ) );
|
||||
sink.tableHeaderCell_();
|
||||
sink.tableRow_();
|
||||
|
||||
fileInitialized = true;
|
||||
}
|
||||
|
||||
sink.tableRow();
|
||||
sink.tableCell();
|
||||
sink.text( event.getMessage() );
|
||||
sink.tableCell_();
|
||||
sink.tableCell();
|
||||
sink.text( String.valueOf( event.getLine() ) );
|
||||
sink.tableCell_();
|
||||
sink.tableRow_();
|
||||
events.add( event );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -151,5 +98,15 @@ public class CheckstyleReportListener
|
|||
{
|
||||
//Do Nothing
|
||||
}
|
||||
|
||||
public TreeMap getFiles()
|
||||
{
|
||||
return files;
|
||||
}
|
||||
|
||||
public void setFiles( TreeMap files )
|
||||
{
|
||||
this.files = files;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,3 +5,5 @@ report.checkstyle.column.violation=Violation
|
|||
report.checkstyle.column.line=Line
|
||||
report.checkstyle.checkstylelink=The following document contains the results of
|
||||
report.checkstyle.files=Files
|
||||
report.checkstyle.summary=Summary
|
||||
report.checkstyle.severity_title=Checkstyle errors in severity
|
Loading…
Reference in New Issue