Class SarifLogger

java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.SarifLogger
All Implemented Interfaces:
AuditListener, Configurable, Contextualizable, EventListener

public final class SarifLogger extends AbstractAutomaticBean implements AuditListener
Simple SARIF logger. SARIF stands for the static analysis results interchange format. See reference
  • Field Details

    • UNICODE_LENGTH

      private static final int UNICODE_LENGTH
      The length of unicode placeholder.
      See Also:
    • UNICODE_ESCAPE_UPPER_LIMIT

      private static final int UNICODE_ESCAPE_UPPER_LIMIT
      Unicode escaping upper limit.
      See Also:
    • BUFFER_SIZE

      private static final int BUFFER_SIZE
      Input stream buffer size.
      See Also:
    • MESSAGE_PLACEHOLDER

      private static final String MESSAGE_PLACEHOLDER
      The placeholder for message.
      See Also:
    • MESSAGE_TEXT_PLACEHOLDER

      private static final String MESSAGE_TEXT_PLACEHOLDER
      The placeholder for message text.
      See Also:
    • MESSAGE_ID_PLACEHOLDER

      private static final String MESSAGE_ID_PLACEHOLDER
      The placeholder for message id.
      See Also:
    • SEVERITY_LEVEL_PLACEHOLDER

      private static final String SEVERITY_LEVEL_PLACEHOLDER
      The placeholder for severity level.
      See Also:
    • URI_PLACEHOLDER

      private static final String URI_PLACEHOLDER
      The placeholder for uri.
      See Also:
    • LINE_PLACEHOLDER

      private static final String LINE_PLACEHOLDER
      The placeholder for line.
      See Also:
    • COLUMN_PLACEHOLDER

      private static final String COLUMN_PLACEHOLDER
      The placeholder for column.
      See Also:
    • RULE_ID_PLACEHOLDER

      private static final String RULE_ID_PLACEHOLDER
      The placeholder for rule id.
      See Also:
    • VERSION_PLACEHOLDER

      private static final String VERSION_PLACEHOLDER
      The placeholder for version.
      See Also:
    • RESULTS_PLACEHOLDER

      private static final String RESULTS_PLACEHOLDER
      The placeholder for results.
      See Also:
    • RULES_PLACEHOLDER

      private static final String RULES_PLACEHOLDER
      The placeholder for rules.
      See Also:
    • TWO_BACKSLASHES

      private static final String TWO_BACKSLASHES
      Two backslashes to not duplicate strings.
      See Also:
    • A_SPACE_PATTERN

      private static final Pattern A_SPACE_PATTERN
      A pattern for two backslashes.
    • TWO_BACKSLASHES_PATTERN

      private static final Pattern TWO_BACKSLASHES_PATTERN
      A pattern for two backslashes.
    • WINDOWS_DRIVE_LETTER_PATTERN

      private static final Pattern WINDOWS_DRIVE_LETTER_PATTERN
      A pattern to match a file with a Windows drive letter.
    • COMMA_LINE_SEPARATOR

      private static final String COMMA_LINE_SEPARATOR
      Comma and line separator.
      See Also:
    • writer

      private final PrintWriter writer
      Helper writer that allows easy encoding and printing.
    • closeStream

      private final boolean closeStream
      Close output stream in auditFinished.
    • results

      private final List<String> results
      The results.
    • allModuleMetadata

      private final Map<String, ModuleDetails> allModuleMetadata
      Map of all available module metadata by fully qualified name.
    • ruleMetadata

      private final Map<SarifLogger.RuleKey, ModuleDetails> ruleMetadata
      Map to store rule metadata by composite key (sourceName, moduleId).
    • report

      private final String report
      Content for the entire report.
    • resultLineColumn

      private final String resultLineColumn
      Content for result representing an error with source line and column.
    • resultLineOnly

      private final String resultLineOnly
      Content for result representing an error with source line only.
    • resultFileOnly

      private final String resultFileOnly
      Content for result representing an error with filename only and without source location.
    • resultErrorOnly

      private final String resultErrorOnly
      Content for result representing an error without filename or location.
    • rule

      private final String rule
      Content for rule.
    • messageStrings

      private final String messageStrings
      Content for messageStrings.
    • messageTextOnly

      private final String messageTextOnly
      Content for message with text only.
    • messageWithId

      private final String messageWithId
      Content for message with id.
  • Constructor Details

  • Method Details

    • loadModuleMetadata

      private void loadModuleMetadata()
      Loads all available module metadata from XML files.
    • finishLocalSetup

      protected void 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 class AbstractAutomaticBean
    • auditStarted

      public void auditStarted(AuditEvent event)
      Description copied from interface: AuditListener
      Notify that the audit is about to start.
      Specified by:
      auditStarted in interface AuditListener
      Parameters:
      event - the event details
    • auditFinished

      public void auditFinished(AuditEvent event)
      Description copied from interface: AuditListener
      Notify that the audit is finished.
      Specified by:
      auditFinished in interface AuditListener
      Parameters:
      event - the event details
    • generateRules

      private List<String> generateRules()
      Generates rules from cached rule metadata.
      Returns:
      list of rules
    • generateMessageStrings

      private List<String> generateMessageStrings(ModuleDetails module)
      Generates message strings for a given module.
      Parameters:
      module - the module
      Returns:
      the generated message strings
    • getMessages

      private static Map<String,String> getMessages(ModuleDetails moduleDetails)
      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

      private static String replaceVersionString(String report)
      Returns the version string.
      Parameters:
      report - report content where replace should happen
      Returns:
      a version string based on the package implementation version
    • addError

      public void addError(AuditEvent event)
      Description copied from interface: AuditListener
      Notify that an audit error was discovered on a specific file.
      Specified by:
      addError in interface AuditListener
      Parameters:
      event - the event details
    • cacheRuleMetadata

      private SarifLogger.RuleKey cacheRuleMetadata(AuditEvent event)
      Caches rule metadata for a given audit event.
      Parameters:
      event - the audit event
      Returns:
      the composite key for the rule
    • generateMessage

      private String generateMessage(SarifLogger.RuleKey ruleKey, AuditEvent event)
      Generate message for the given rule key and audit event.
      Parameters:
      ruleKey - the rule key
      event - the audit event
      Returns:
      the generated message
    • addException

      public void addException(AuditEvent event, Throwable throwable)
      Description copied from interface: AuditListener
      Notify that an exception happened while performing audit.
      Specified by:
      addException in interface AuditListener
      Parameters:
      event - the event details
      throwable - details of the exception
    • fileStarted

      public void fileStarted(AuditEvent event)
      Description copied from interface: AuditListener
      Notify that audit is about to start on a specific file.
      Specified by:
      fileStarted in interface AuditListener
      Parameters:
      event - the event details
    • fileFinished

      public void fileFinished(AuditEvent event)
      Description copied from interface: AuditListener
      Notify that audit is finished on a specific file.
      Specified by:
      fileFinished in interface AuditListener
      Parameters:
      event - the event details
    • renderFileNameUri

      private static String renderFileNameUri(String fileName)
      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

      private static 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 String escape(String value)
      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

      private static 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 String readResource(String name) throws IOException
      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