Coverage Report - com.puppycrawl.tools.checkstyle.AuditEventDefaultFormatter
 
Classes in this File Line Coverage Branch Coverage Complexity
AuditEventDefaultFormatter
100%
35/35
100%
12/12
3
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 the original author or authors.
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 
 20  
 package com.puppycrawl.tools.checkstyle;
 21  
 
 22  
 import java.util.Locale;
 23  
 
 24  
 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
 25  
 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
 26  
 
 27  
 /**
 28  
  * Represents the default formatter for log message.
 29  
  * Default log message format is:
 30  
  * [SEVERITY LEVEL] filePath:lineNo:columnNo: message. [CheckName]
 31  
  * When the module id of the message has been set, the format is:
 32  
  * [SEVERITY LEVEL] filePath:lineNo:columnNo: message. [ModuleId]
 33  
  * @author Andrei Selkin
 34  
  */
 35  55
 public class AuditEventDefaultFormatter implements AuditEventFormatter {
 36  
 
 37  
     /** Length of all separators. */
 38  
     private static final int LENGTH_OF_ALL_SEPARATORS = 10;
 39  
 
 40  
     /** Suffix of module names like XXXXCheck. */
 41  
     private static final String SUFFIX = "Check";
 42  
 
 43  
     @Override
 44  
     public String format(AuditEvent event) {
 45  22
         final String fileName = event.getFileName();
 46  22
         final String message = event.getMessage();
 47  
 
 48  22
         final SeverityLevel severityLevel = event.getSeverityLevel();
 49  
         final String severityLevelName;
 50  22
         if (severityLevel == SeverityLevel.WARNING) {
 51  
             // We change the name of severity level intentionally
 52  
             // to shorten the length of the log message.
 53  13
             severityLevelName = "WARN";
 54  
         }
 55  
         else {
 56  9
             severityLevelName = severityLevel.getName().toUpperCase(Locale.US);
 57  
         }
 58  
 
 59  
         // Avoid StringBuffer.expandCapacity
 60  22
         final int bufLen = calculateBufferLength(event, severityLevelName.length());
 61  22
         final StringBuilder sb = new StringBuilder(bufLen);
 62  
 
 63  22
         sb.append('[').append(severityLevelName).append("] ")
 64  22
             .append(fileName).append(':').append(event.getLine());
 65  22
         if (event.getColumn() > 0) {
 66  9
             sb.append(':').append(event.getColumn());
 67  
         }
 68  22
         sb.append(": ").append(message).append(" [");
 69  22
         if (event.getModuleId() == null) {
 70  20
             final String checkShortName = getCheckShortName(event);
 71  20
             sb.append(checkShortName);
 72  20
         }
 73  
         else {
 74  2
             sb.append(event.getModuleId());
 75  
         }
 76  22
         sb.append(']');
 77  
 
 78  22
         return sb.toString();
 79  
     }
 80  
 
 81  
     /**
 82  
      * Returns the length of the buffer for StringBuilder.
 83  
      * bufferLength = fileNameLength + messageLength + lengthOfAllSeparators +
 84  
      * + severityNameLength + checkNameLength.
 85  
      * @param event audit event.
 86  
      * @param severityLevelNameLength length of severity level name.
 87  
      * @return the length of the buffer for StringBuilder.
 88  
      */
 89  
     private static int calculateBufferLength(AuditEvent event, int severityLevelNameLength) {
 90  46
         return LENGTH_OF_ALL_SEPARATORS + event.getFileName().length()
 91  23
             + event.getMessage().length() + severityLevelNameLength
 92  23
             + getCheckShortName(event).length();
 93  
     }
 94  
 
 95  
     /**
 96  
      * Returns check name without 'Check' suffix.
 97  
      * @param event audit event.
 98  
      * @return check name without 'Check' suffix.
 99  
      */
 100  
     private static String getCheckShortName(AuditEvent event) {
 101  43
         final String checkFullName = event.getSourceName();
 102  
         final String checkShortName;
 103  43
         final int lastDotIndex = checkFullName.lastIndexOf('.');
 104  43
         if (lastDotIndex == -1) {
 105  4
             if (checkFullName.endsWith(SUFFIX)) {
 106  2
                 checkShortName = checkFullName.substring(0, checkFullName.lastIndexOf(SUFFIX));
 107  
             }
 108  
             else {
 109  2
                 checkShortName = checkFullName.substring(0, checkFullName.length());
 110  
             }
 111  
         }
 112  
         else {
 113  39
             if (checkFullName.endsWith(SUFFIX)) {
 114  66
                 checkShortName = checkFullName.substring(lastDotIndex + 1,
 115  33
                     checkFullName.lastIndexOf(SUFFIX));
 116  
             }
 117  
             else {
 118  6
                 checkShortName = checkFullName.substring(lastDotIndex + 1, checkFullName.length());
 119  
             }
 120  
         }
 121  43
         return checkShortName;
 122  
     }
 123  
 }