1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29
30
31
32
33
34 public class AuditEventDefaultFormatter implements AuditEventFormatter {
35
36
37 private static final int LENGTH_OF_ALL_SEPARATORS = 10;
38
39
40 private static final String SUFFIX = "Check";
41
42 @Override
43 public String format(AuditEvent event) {
44 final String fileName = event.getFileName();
45 final String message = event.getMessage();
46
47 final SeverityLevel severityLevel = event.getSeverityLevel();
48 final String severityLevelName;
49 if (severityLevel == SeverityLevel.WARNING) {
50
51
52 severityLevelName = "WARN";
53 }
54 else {
55 severityLevelName = severityLevel.getName().toUpperCase(Locale.US);
56 }
57
58 final StringBuilder sb = initStringBuilderWithOptimalBuffer(event, severityLevelName);
59
60 sb.append('[').append(severityLevelName).append("] ")
61 .append(fileName).append(':').append(event.getLine());
62 if (event.getColumn() > 0) {
63 sb.append(':').append(event.getColumn());
64 }
65 sb.append(": ").append(message).append(" [");
66 if (event.getModuleId() == null) {
67 final String checkShortName = getCheckShortName(event);
68 sb.append(checkShortName);
69 }
70 else {
71 sb.append(event.getModuleId());
72 }
73 sb.append(']');
74
75 return sb.toString();
76 }
77
78
79
80
81
82
83
84
85
86
87
88 private static StringBuilder initStringBuilderWithOptimalBuffer(AuditEvent event,
89 String severityLevelName) {
90 final int bufLen = LENGTH_OF_ALL_SEPARATORS + event.getFileName().length()
91 + event.getMessage().length() + severityLevelName.length()
92 + getCheckShortName(event).length();
93 return new StringBuilder(bufLen);
94 }
95
96
97
98
99
100
101
102 private static String getCheckShortName(AuditEvent event) {
103 final String checkFullName = event.getSourceName();
104 String checkShortName = checkFullName.substring(checkFullName.lastIndexOf('.') + 1);
105 if (checkShortName.endsWith(SUFFIX)) {
106 final int endIndex = checkShortName.length() - SUFFIX.length();
107 checkShortName = checkShortName.substring(0, endIndex);
108 }
109 return checkShortName;
110 }
111
112 }