Author: hboutemy Date: Sat Jul 26 17:35:04 2014 New Revision: 1613691 URL: http://svn.apache.org/r1613691 Log: [MCHECKSTYLE-148] added category and name of rule causing a violation to output
Added: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java (with props) Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-fail/pom.xml maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportGenerator.java maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-fail/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-fail/pom.xml?rev=1613691&r1=1613690&r2=1613691&view=diff ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-fail/pom.xml (original) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/it/check-fail/pom.xml Sat Jul 26 17:35:04 2014 @@ -40,6 +40,9 @@ <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>@pom.version@</version> + <configuration> + <logViolationsToConsole>true</logViolationsToConsole> + </configuration> <executions> <execution> <id>check</id> Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportGenerator.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportGenerator.java?rev=1613691&r1=1613690&r2=1613691&view=diff ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportGenerator.java (original) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportGenerator.java Sat Jul 26 17:35:04 2014 @@ -373,7 +373,7 @@ public class CheckstyleReportGenerator // column 1: rule category sink.tableCell(); - String category = getRuleCategoryName( lastMatchedEvent ); + String category = RuleUtil.getCategory( lastMatchedEvent ); sink.text( category ); sink.tableCell_(); @@ -473,59 +473,6 @@ public class CheckstyleReportGenerator } /** - * Get the rule name from a violation. - * - * @param event the violation - * @return the rule name, which is the class name without package and removed eventual "Check" suffix - */ - public String getRuleName( AuditEvent event ) - { - String eventSrcName = event.getSourceName(); - - if ( eventSrcName == null ) - { - return null; - } - - if ( eventSrcName.endsWith( "Check" ) ) - { - eventSrcName = eventSrcName.substring( 0, eventSrcName.length() - 5 ); - } - - return eventSrcName.substring( eventSrcName.lastIndexOf( '.' ) + 1 ); - } - - /** - * Get the rule category from a violation. - * - * @param event the violation - * @return the rule category, which is the last package name or "misc" or "extension" - */ - public String getRuleCategoryName( AuditEvent event ) - { - String eventSrcName = event.getSourceName(); - - if ( eventSrcName == null ) - { - return null; - } - - int end = eventSrcName.lastIndexOf( '.' ); - eventSrcName = eventSrcName.substring( 0, end ); - - if ( "com.puppycrawl.tools.checkstyle.checks".equals( eventSrcName ) ) - { - return "misc"; - } - else if ( !eventSrcName.startsWith( "com.puppycrawl.tools.checkstyle.checks" ) ) - { - return "extension"; - } - - return eventSrcName.substring( eventSrcName.lastIndexOf( '.' ) + 1 ); - } - - /** * Check if a violation matches a rule. * * @param event the violation to check @@ -536,7 +483,7 @@ public class CheckstyleReportGenerator */ public boolean matchRule( AuditEvent event, String ruleName, String expectedMessage, String expectedSeverity ) { - if ( !ruleName.equals( getRuleName( event ) ) ) + if ( !ruleName.equals( RuleUtil.getName( event ) ) ) { return false; } @@ -781,7 +728,7 @@ public class CheckstyleReportGenerator sink.tableCell_(); sink.tableCell(); - String category = getRuleCategoryName( event ); + String category = RuleUtil.getCategory( event ); if ( category != null ) { sink.text( category ); @@ -789,7 +736,7 @@ public class CheckstyleReportGenerator sink.tableCell_(); sink.tableCell(); - String ruleName = getRuleName( event ); + String ruleName = RuleUtil.getName( event ); if ( ruleName != null ) { sink.text( ruleName ); Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java?rev=1613691&r1=1613690&r2=1613691&view=diff ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java (original) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java Sat Jul 26 17:35:04 2014 @@ -573,35 +573,46 @@ public class CheckstyleViolationCheckMoj String file = ""; while ( eventType != XmlPullParser.END_DOCUMENT ) { - if ( eventType == XmlPullParser.START_TAG && "file".equals( xpp.getName() ) ) + if ( eventType == XmlPullParser.START_TAG ) { - file = xpp.getAttributeValue( "", "name" ); - file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 ); - } + String severity; - if ( eventType == XmlPullParser.START_TAG && "error".equals( xpp.getName() ) - && isViolation( xpp.getAttributeValue( "", "severity" ) ) ) - { - if ( logViolationsToConsole ) + if ( "file".equals( xpp.getName() ) ) { - final String column = - xpp.getAttributeValue( "", "column" ) == null ? "n/a" : xpp.getAttributeValue( "", "column" ); - final String logMessage = file + '[' + xpp.getAttributeValue( "", "line" ) + ':' + column + "] " - + xpp.getAttributeValue( "", "message" ); - if ( "info".equals( xpp.getAttributeValue( "", "severity" ) ) ) - { - getLog().info( logMessage ); - } - else if ( "warning".equals( xpp.getAttributeValue( "", "severity" ) ) ) - { - getLog().warn( logMessage ); - } - else + file = xpp.getAttributeValue( "", "name" ); + file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 ); + } + else if ( "error".equals( xpp.getName() ) + && isViolation( severity = xpp.getAttributeValue( "", "severity" ) ) ) + { + count++; + + if ( logViolationsToConsole ) { - getLog().error( logMessage ); + String line = xpp.getAttributeValue( "", "line" ); + String column = xpp.getAttributeValue( "", "column" ); + String message = xpp.getAttributeValue( "", "message" ); + String source = xpp.getAttributeValue( "", "source" ); + String rule = RuleUtil.getName( source ); + String category = RuleUtil.getCategory( source ); + + String logMessage = + file + '[' + line + ( ( column == null ) ? "" : ( ':' + column ) ) + "] (" + category + + ") " + rule + ": " + message; + if ( "info".equals( severity ) ) + { + getLog().info( logMessage ); + } + else if ( "warning".equals( severity ) ) + { + getLog().warn( logMessage ); + } + else + { + getLog().error( logMessage ); + } } } - count++; } eventType = xpp.next(); } Added: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java?rev=1613691&view=auto ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java (added) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java Sat Jul 26 17:35:04 2014 @@ -0,0 +1,101 @@ +package org.apache.maven.plugin.checkstyle; + +import com.puppycrawl.tools.checkstyle.api.AuditEvent; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/** + * Tooling for Checkstyle rules conventions: names, categories. + * + * @author Hervé Boutemy + * @since 2.13 + */ +public class RuleUtil +{ + /** + * Get the rule name from an audit event. + * + * @param event the audit event + * @return the rule name, which is the class name without package and removed eventual "Check" suffix + */ + public static String getName( AuditEvent event ) + { + return getName( event.getSourceName() ); + } + /** + * Get the rule name from an audit event source name. + * + * @param eventSrcName the audit event source name + * @return the rule name, which is the class name without package and removed eventual "Check" suffix + */ + public static String getName( String eventSrcName ) + { + if ( eventSrcName == null ) + { + return null; + } + + if ( eventSrcName.endsWith( "Check" ) ) + { + eventSrcName = eventSrcName.substring( 0, eventSrcName.length() - 5 ); + } + + return eventSrcName.substring( eventSrcName.lastIndexOf( '.' ) + 1 ); + } + + /** + * Get the rule category from an audit event. + * + * @param event the audit event + * @return the rule category, which is the last package name or "misc" or "extension" + */ + public static String getCategory( AuditEvent event ) + { + return getCategory( event.getSourceName() ); + } + + /** + * Get the rule category from an audit event source name. + * + * @param eventSrcName the audit event source name + * @return the rule category, which is the last package name or "misc" or "extension" + */ + public static String getCategory( String eventSrcName ) + { + if ( eventSrcName == null ) + { + return null; + } + + int end = eventSrcName.lastIndexOf( '.' ); + eventSrcName = eventSrcName.substring( 0, end ); + + if ( "com.puppycrawl.tools.checkstyle.checks".equals( eventSrcName ) ) + { + return "misc"; + } + else if ( !eventSrcName.startsWith( "com.puppycrawl.tools.checkstyle.checks" ) ) + { + return "extension"; + } + + return eventSrcName.substring( eventSrcName.lastIndexOf( '.' ) + 1 ); + } +} Propchange: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/RuleUtil.java ------------------------------------------------------------------------------ svn:mime-type = text/plain