Package com.puppycrawl.tools.checkstyle
Class SarifLogger
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.SarifLogger
- All Implemented Interfaces:
AuditListener
,Configurable
,Contextualizable
,EventListener
Simple SARIF logger.
SARIF stands for the static analysis results interchange format.
See reference
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final record
Composite key for uniquely identifying a rule by source name and module ID.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Pattern
A pattern for two backslashes.private final Map<String,
ModuleDetails> Map of all available module metadata by fully qualified name.private static final int
Input stream buffer size.private final boolean
Close output stream in auditFinished.private static final String
The placeholder for column.private static final String
Comma and line separator.private static final String
The placeholder for line.private static final String
The placeholder for message id.private static final String
The placeholder for message.private static final String
The placeholder for message text.private final String
Content for messageStrings.private final String
Content for message with text only.private final String
Content for message with id.private final String
Content for the entire report.private final String
Content for result representing an error without filename or location.private final String
Content for result representing an error with filename only and without source location.private final String
Content for result representing an error with source line and column.private final String
Content for result representing an error with source line only.The results.private static final String
The placeholder for results.private final String
Content for rule.private static final String
The placeholder for rule id.private final Map<SarifLogger.RuleKey,
ModuleDetails> Map to store rule metadata by composite key (sourceName, moduleId).private static final String
The placeholder for rules.private static final String
The placeholder for severity level.private static final String
Two backslashes to not duplicate strings.private static final Pattern
A pattern for two backslashes.private static final int
Unicode escaping upper limit.private static final int
The length of unicode placeholder.private static final String
The placeholder for uri.private static final String
The placeholder for version.private static final Pattern
A pattern to match a file with a Windows drive letter.private final PrintWriter
Helper writer that allows easy encoding and printing. -
Constructor Summary
ConstructorsConstructorDescriptionSarifLogger
(OutputStream outputStream, AbstractAutomaticBean.OutputStreamOptions outputStreamOptions) Creates a newSarifLogger
instance.SarifLogger
(OutputStream outputStream, AutomaticBean.OutputStreamOptions outputStreamOptions) Creates a newSarifLogger
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addError
(AuditEvent event) Notify that an audit error was discovered on a specific file.void
addException
(AuditEvent event, Throwable throwable) Notify that an exception happened while performing audit.void
auditFinished
(AuditEvent event) Notify that the audit is finished.void
auditStarted
(AuditEvent event) Notify that the audit is about to start.private SarifLogger.RuleKey
cacheRuleMetadata
(AuditEvent event) Caches rule metadata for a given audit event.static String
Escape \b, \f, \n, \r, \t, \", \\ and U+0000 through U+001F.private static String
escapeUnicode1F
(char chr) Escape the character between 0x00 to 0x1F in JSON.void
fileFinished
(AuditEvent event) Notify that audit is finished on a specific file.void
fileStarted
(AuditEvent event) Notify that audit is about to start on a specific file.protected void
Provides a hook to finish the part of this component's setup that was not handled by the bean introspection.private String
generateMessage
(SarifLogger.RuleKey ruleKey, AuditEvent event) Generate message for the given rule key and audit event.generateMessageStrings
(ModuleDetails module) Generates message strings for a given module.Generates rules from cached rule metadata.getMessages
(ModuleDetails moduleDetails) Gets a map of message keys to their message strings for a module.private void
Loads all available module metadata from XML files.static String
readResource
(String name) Read string from given resource.private static String
renderFileNameUri
(String fileName) Render the file name URI for the given file name.private static String
renderSeverityLevel
(SeverityLevel severityLevel) Render the severity level into SARIF severity level.private static String
replaceVersionString
(String report) Returns the version string.Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
UNICODE_LENGTH
The length of unicode placeholder.- See Also:
-
UNICODE_ESCAPE_UPPER_LIMIT
Unicode escaping upper limit.- See Also:
-
BUFFER_SIZE
Input stream buffer size.- See Also:
-
MESSAGE_PLACEHOLDER
The placeholder for message.- See Also:
-
MESSAGE_TEXT_PLACEHOLDER
The placeholder for message text.- See Also:
-
MESSAGE_ID_PLACEHOLDER
The placeholder for message id.- See Also:
-
SEVERITY_LEVEL_PLACEHOLDER
The placeholder for severity level.- See Also:
-
URI_PLACEHOLDER
The placeholder for uri.- See Also:
-
LINE_PLACEHOLDER
The placeholder for line.- See Also:
-
COLUMN_PLACEHOLDER
The placeholder for column.- See Also:
-
RULE_ID_PLACEHOLDER
The placeholder for rule id.- See Also:
-
VERSION_PLACEHOLDER
The placeholder for version.- See Also:
-
RESULTS_PLACEHOLDER
The placeholder for results.- See Also:
-
RULES_PLACEHOLDER
The placeholder for rules.- See Also:
-
TWO_BACKSLASHES
Two backslashes to not duplicate strings.- See Also:
-
A_SPACE_PATTERN
A pattern for two backslashes. -
TWO_BACKSLASHES_PATTERN
A pattern for two backslashes. -
WINDOWS_DRIVE_LETTER_PATTERN
A pattern to match a file with a Windows drive letter. -
COMMA_LINE_SEPARATOR
Comma and line separator.- See Also:
-
writer
Helper writer that allows easy encoding and printing. -
closeStream
Close output stream in auditFinished. -
results
The results. -
allModuleMetadata
Map of all available module metadata by fully qualified name. -
ruleMetadata
Map to store rule metadata by composite key (sourceName, moduleId). -
report
Content for the entire report. -
resultLineColumn
Content for result representing an error with source line and column. -
resultLineOnly
Content for result representing an error with source line only. -
resultFileOnly
Content for result representing an error with filename only and without source location. -
resultErrorOnly
Content for result representing an error without filename or location. -
rule
Content for rule. -
messageStrings
Content for messageStrings. -
messageTextOnly
Content for message with text only. -
messageWithId
Content for message with id.
-
-
Constructor Details
-
SarifLogger
public SarifLogger(OutputStream outputStream, AutomaticBean.OutputStreamOptions outputStreamOptions) throws IOException Creates a newSarifLogger
instance.- Parameters:
outputStream
- where to log audit eventsoutputStreamOptions
- ifCLOSE
that should be closed in auditFinished()- Throws:
IllegalArgumentException
- if outputStreamOptions is nullIOException
- if there is reading errors.
-
SarifLogger
public SarifLogger(OutputStream outputStream, AbstractAutomaticBean.OutputStreamOptions outputStreamOptions) throws IOException Creates a newSarifLogger
instance.- Parameters:
outputStream
- where to log audit eventsoutputStreamOptions
- ifCLOSE
that should be closed in auditFinished()- Throws:
IllegalArgumentException
- if outputStreamOptions is nullIOException
- if there is reading errors.
-
-
Method Details
-
loadModuleMetadata
Loads all available module metadata from XML files. -
finishLocalSetup
Description copied from class:AbstractAutomaticBean
Provides 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:
finishLocalSetup
in classAbstractAutomaticBean
-
auditStarted
Description copied from interface:AuditListener
Notify that the audit is about to start.- Specified by:
auditStarted
in interfaceAuditListener
- Parameters:
event
- the event details
-
auditFinished
Description copied from interface:AuditListener
Notify that the audit is finished.- Specified by:
auditFinished
in interfaceAuditListener
- Parameters:
event
- the event details
-
generateRules
Generates rules from cached rule metadata.- Returns:
- list of rules
-
generateMessageStrings
Generates message strings for a given module.- Parameters:
module
- the module- Returns:
- the generated message strings
-
getMessages
Gets a map of message keys to their message strings for a module.- Parameters:
moduleDetails
- the module details- Returns:
- map of message keys to message strings
-
replaceVersionString
Returns the version string.- Parameters:
report
- report content where replace should happen- Returns:
- a version string based on the package implementation version
-
addError
Description copied from interface:AuditListener
Notify that an audit error was discovered on a specific file.- Specified by:
addError
in interfaceAuditListener
- Parameters:
event
- the event details
-
cacheRuleMetadata
Caches rule metadata for a given audit event.- Parameters:
event
- the audit event- Returns:
- the composite key for the rule
-
generateMessage
Generate message for the given rule key and audit event.- Parameters:
ruleKey
- the rule keyevent
- the audit event- Returns:
- the generated message
-
addException
Description copied from interface:AuditListener
Notify that an exception happened while performing audit.- Specified by:
addException
in interfaceAuditListener
- Parameters:
event
- the event detailsthrowable
- details of the exception
-
fileStarted
Description copied from interface:AuditListener
Notify that audit is about to start on a specific file.- Specified by:
fileStarted
in interfaceAuditListener
- Parameters:
event
- the event details
-
fileFinished
Description copied from interface:AuditListener
Notify that audit is finished on a specific file.- Specified by:
fileFinished
in interfaceAuditListener
- Parameters:
event
- the event details
-
renderFileNameUri
Render the file name URI for the given file name.- Parameters:
fileName
- the file name to render the URI for- Returns:
- the rendered URI for the given file name
-
renderSeverityLevel
Render the severity level into SARIF severity level.- Parameters:
severityLevel
- the Severity level.- Returns:
- the rendered severity level in string.
-
escape
Escape \b, \f, \n, \r, \t, \", \\ and U+0000 through U+001F. See reference - 2.5. Strings- Parameters:
value
- the value to escape.- Returns:
- the escaped value if necessary.
-
escapeUnicode1F
Escape the character between 0x00 to 0x1F in JSON.- Parameters:
chr
- the character to be escaped.- Returns:
- the escaped string.
-
readResource
Read string from given resource.- Parameters:
name
- name of the desired resource- Returns:
- the string content from the give resource
- Throws:
IOException
- if there is reading errors
-