Package com.puppycrawl.tools.checkstyle
Class SarifLogger
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.SarifLogger
-
- All Implemented Interfaces:
AuditListener,Configurable,Contextualizable,java.util.EventListener
public class SarifLogger extends AutomaticBean implements AuditListener
Simple SARIF logger. SARIF stands for the static analysis results interchange format. Reference: https://sarifweb.azurewebsites.net/
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private static intBUFFER_SIZEInput stream buffer size.private booleancloseStreamClose output stream in auditFinished.private static java.lang.StringCOLUMN_PLACEHOLDERThe placeholder for column.private static java.lang.StringLINE_PLACEHOLDERThe placeholder for line.private static java.lang.StringMESSAGE_PLACEHOLDERThe placeholder for message.private java.lang.StringreportContent for the entire report.private java.lang.StringresultErrorOnlyContent for result representing an error without filename or location.private java.lang.StringresultFileOnlyContent for result representing an error with filename only and without source location.private java.lang.StringresultLineColumnContent for result representing an error with source line and column.private java.lang.StringresultLineOnlyContent for result representing an error with source line only.private java.util.List<java.lang.String>resultsThe results.private static java.lang.StringRESULTS_PLACEHOLDERThe placeholder for results.private static java.lang.StringRULE_ID_PLACEHOLDERThe placeholder for rule id.private static java.lang.StringSEVERITY_LEVEL_PLACEHOLDERThe placeholder for severity level.private static intUNICODE_ESCAPE_UPPER_LIMITUnicode escaping upper limit.private static intUNICODE_LENGTHThe length of unicode placeholder.private static java.lang.StringURI_PLACEHOLDERThe placeholder for uri.private static java.lang.StringVERSION_PLACEHOLDERThe placeholder for version.private java.io.PrintWriterwriterHelper writer that allows easy encoding and printing.
-
Constructor Summary
Constructors Constructor Description SarifLogger(java.io.OutputStream outputStream, AutomaticBean.OutputStreamOptions outputStreamOptions)Creates a newSarifLoggerinstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddError(AuditEvent event)Notify that an audit error was discovered on a specific file.voidaddException(AuditEvent event, java.lang.Throwable throwable)Notify that an exception happened while performing audit.voidauditFinished(AuditEvent event)Notify that the audit is finished.voidauditStarted(AuditEvent event)Notify that the audit is about to start.static java.lang.Stringescape(java.lang.String value)Escape \b, \f, \n, \r, \t, \", \\ and U+0000 through U+001F.private static java.lang.StringescapeUnicode1F(char chr)Escape the character between 0x00 to 0x1F in JSON.voidfileFinished(AuditEvent event)Notify that audit is finished on a specific file.voidfileStarted(AuditEvent event)Notify that audit is about to start on a specific file.protected voidfinishLocalSetup()Provides a hook to finish the part of this component's setup that was not handled by the bean introspection.static java.lang.StringreadResource(java.lang.String name)Read string from given resource.private static java.lang.StringrenderSeverityLevel(SeverityLevel severityLevel)Render the severity level into SARIF severity level.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
UNICODE_LENGTH
private static final int UNICODE_LENGTH
The length of unicode placeholder.- See Also:
- Constant Field Values
-
UNICODE_ESCAPE_UPPER_LIMIT
private static final int UNICODE_ESCAPE_UPPER_LIMIT
Unicode escaping upper limit.- See Also:
- Constant Field Values
-
BUFFER_SIZE
private static final int BUFFER_SIZE
Input stream buffer size.- See Also:
- Constant Field Values
-
MESSAGE_PLACEHOLDER
private static final java.lang.String MESSAGE_PLACEHOLDER
The placeholder for message.- See Also:
- Constant Field Values
-
SEVERITY_LEVEL_PLACEHOLDER
private static final java.lang.String SEVERITY_LEVEL_PLACEHOLDER
The placeholder for severity level.- See Also:
- Constant Field Values
-
URI_PLACEHOLDER
private static final java.lang.String URI_PLACEHOLDER
The placeholder for uri.- See Also:
- Constant Field Values
-
LINE_PLACEHOLDER
private static final java.lang.String LINE_PLACEHOLDER
The placeholder for line.- See Also:
- Constant Field Values
-
COLUMN_PLACEHOLDER
private static final java.lang.String COLUMN_PLACEHOLDER
The placeholder for column.- See Also:
- Constant Field Values
-
RULE_ID_PLACEHOLDER
private static final java.lang.String RULE_ID_PLACEHOLDER
The placeholder for rule id.- See Also:
- Constant Field Values
-
VERSION_PLACEHOLDER
private static final java.lang.String VERSION_PLACEHOLDER
The placeholder for version.- See Also:
- Constant Field Values
-
RESULTS_PLACEHOLDER
private static final java.lang.String RESULTS_PLACEHOLDER
The placeholder for results.- See Also:
- Constant Field Values
-
writer
private final java.io.PrintWriter writer
Helper writer that allows easy encoding and printing.
-
closeStream
private final boolean closeStream
Close output stream in auditFinished.
-
results
private final java.util.List<java.lang.String> results
The results.
-
report
private final java.lang.String report
Content for the entire report.
-
resultLineColumn
private final java.lang.String resultLineColumn
Content for result representing an error with source line and column.
-
resultLineOnly
private final java.lang.String resultLineOnly
Content for result representing an error with source line only.
-
resultFileOnly
private final java.lang.String resultFileOnly
Content for result representing an error with filename only and without source location.
-
resultErrorOnly
private final java.lang.String resultErrorOnly
Content for result representing an error without filename or location.
-
-
Constructor Detail
-
SarifLogger
public SarifLogger(java.io.OutputStream outputStream, AutomaticBean.OutputStreamOptions outputStreamOptions) throws java.io.IOException
Creates a newSarifLoggerinstance.- Parameters:
outputStream- where to log audit eventsoutputStreamOptions- ifCLOSEthat should be closed in auditFinished()- Throws:
java.lang.IllegalArgumentException- if outputStreamOptions is nulljava.io.IOException- if there is reading errors.
-
-
Method Detail
-
finishLocalSetup
protected void finishLocalSetup()
Description copied from class:AutomaticBeanProvides a hook to finish the part of this component's setup that was not handled by the bean introspection.The default implementation does nothing.
- Specified by:
finishLocalSetupin classAutomaticBean
-
auditStarted
public void auditStarted(AuditEvent event)
Description copied from interface:AuditListenerNotify that the audit is about to start.- Specified by:
auditStartedin interfaceAuditListener- Parameters:
event- the event details
-
auditFinished
public void auditFinished(AuditEvent event)
Description copied from interface:AuditListenerNotify that the audit is finished.- Specified by:
auditFinishedin interfaceAuditListener- Parameters:
event- the event details
-
addError
public void addError(AuditEvent event)
Description copied from interface:AuditListenerNotify that an audit error was discovered on a specific file.- Specified by:
addErrorin interfaceAuditListener- Parameters:
event- the event details
-
addException
public void addException(AuditEvent event, java.lang.Throwable throwable)
Description copied from interface:AuditListenerNotify that an exception happened while performing audit.- Specified by:
addExceptionin interfaceAuditListener- Parameters:
event- the event detailsthrowable- details of the exception
-
fileStarted
public void fileStarted(AuditEvent event)
Description copied from interface:AuditListenerNotify that audit is about to start on a specific file.- Specified by:
fileStartedin interfaceAuditListener- Parameters:
event- the event details
-
fileFinished
public void fileFinished(AuditEvent event)
Description copied from interface:AuditListenerNotify that audit is finished on a specific file.- Specified by:
fileFinishedin interfaceAuditListener- Parameters:
event- the event details
-
renderSeverityLevel
private static java.lang.String renderSeverityLevel(SeverityLevel severityLevel)
Render the severity level into SARIF severity level.- Parameters:
severityLevel- the Severity level.- Returns:
- the rendered severity level in string.
-
escape
public static java.lang.String escape(java.lang.String value)
Escape \b, \f, \n, \r, \t, \", \\ and U+0000 through U+001F. Reference: https://www.ietf.org/rfc/rfc4627.txt - 2.5. Strings- Parameters:
value- the value to escape.- Returns:
- the escaped value if necessary.
-
escapeUnicode1F
private static java.lang.String escapeUnicode1F(char chr)
Escape the character between 0x00 to 0x1F in JSON.- Parameters:
chr- the character to be escaped.- Returns:
- the escaped string.
-
readResource
public static java.lang.String readResource(java.lang.String name) throws java.io.IOException
Read string from given resource.- Parameters:
name- name of the desired resource- Returns:
- the string content from the give resource
- Throws:
java.io.IOException- if there is reading errors
-
-