From 616ab98fa4325827ac14ebf2dc83421cb1c9d23f Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Tue, 11 Oct 2005 09:34:09 +0000 Subject: [PATCH] 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 --- .../plugin/checkstyle/CheckstyleReport.java | 24 +- .../checkstyle/CheckstyleReportGenerator.java | 313 ++++++++++++++++++ .../checkstyle/CheckstyleReportListener.java | 267 +++++++-------- .../resources/checkstyle-report_en.properties | 2 + 4 files changed, 445 insertions(+), 161 deletions(-) create mode 100644 maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportGenerator.java diff --git a/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java b/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java index 6eb3bcf335..f7a3327f8a 100644 --- a/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java +++ b/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java @@ -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 Emmanuel Venisse @@ -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) diff --git a/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportGenerator.java b/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportGenerator.java new file mode 100644 index 0000000000..5cc83dc8df --- /dev/null +++ b/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportGenerator.java @@ -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; + } +} diff --git a/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java b/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java index 9df94de116..e278da9e6b 100644 --- a/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java +++ b/maven-plugins/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java @@ -1,155 +1,112 @@ -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 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; - -/** - * @author Emmanuel Venisse - * @version $Id: DependenciesReport.java,v 1.2 2005/02/23 00:08:02 brett Exp $ - */ -public class CheckstyleReportListener - extends AutomaticBean - implements AuditListener -{ - private Sink sink; - - private String sourceDirectory; - - private String currentFilename; - - private boolean fileInitialized; - - private ResourceBundle bundle; - - public CheckstyleReportListener( Sink sink, String sourceDirectory, ResourceBundle bundle ) - { - this.sink = sink; - this.sourceDirectory = sourceDirectory; - this.bundle = bundle; - } - - private String getTitle() - { - return bundle.getString( "report.checkstyle.title" ); - } - - 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 - } - - public void auditFinished( AuditEvent event ) - { - sink.section1_(); - sink.body_(); - sink.flush(); - sink.close(); - } - - public void fileStarted( AuditEvent event ) - { - currentFilename = StringUtils.substring( event.getFileName(), sourceDirectory.length() + 1 ); - currentFilename = StringUtils.replace( currentFilename, "\\", "/" ); - fileInitialized = false; - } - - public void fileFinished( AuditEvent event ) - { - if ( fileInitialized ) - { - sink.table_(); - sink.section2_(); - } - } - - public void addError( AuditEvent event ) - { - if ( !SeverityLevel.IGNORE.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_(); - } - } - - public void addException( AuditEvent event, Throwable throwable ) - { - //Do Nothing - } -} - +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.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 Emmanuel Venisse + * @version $Id: DependenciesReport.java,v 1.2 2005/02/23 00:08:02 brett Exp $ + */ +public class CheckstyleReportListener + extends AutomaticBean + implements AuditListener +{ + private String sourceDirectory; + + private TreeMap files; + + private String currentFile; + + private LinkedList events; + + private SeverityLevel severityLevel; + + public CheckstyleReportListener( String sourceDirectory ) + { + this.sourceDirectory = sourceDirectory; + } + + public void setSeverityLevelFilter( SeverityLevel severityLevel ) + { + this.severityLevel = severityLevel; + } + + public SeverityLevel getSeverityLevelFilter() + { + return severityLevel; + } + + public void auditStarted( AuditEvent event ) + { + setFiles( new TreeMap() ); + } + + public void auditFinished( AuditEvent event ) + { + //do nothing + } + + public void fileStarted( AuditEvent event ) + { + 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 ) + { + getFiles().put( currentFile, events ); + currentFile = null; + } + + public void addError( AuditEvent event ) + { + if ( SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) ) return; + + if ( severityLevel == null || severityLevel.equals( event.getSeverityLevel() ) ) + { + events.add( event ); + } + } + + public void addException( AuditEvent event, Throwable throwable ) + { + //Do Nothing + } + + public TreeMap getFiles() + { + return files; + } + + public void setFiles( TreeMap files ) + { + this.files = files; + } +} + diff --git a/maven-plugins/maven-checkstyle-plugin/src/main/resources/checkstyle-report_en.properties b/maven-plugins/maven-checkstyle-plugin/src/main/resources/checkstyle-report_en.properties index 5dec5a67ad..96a27680a0 100644 --- a/maven-plugins/maven-checkstyle-plugin/src/main/resources/checkstyle-report_en.properties +++ b/maven-plugins/maven-checkstyle-plugin/src/main/resources/checkstyle-report_en.properties @@ -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 \ No newline at end of file