Class InlineConfigUtils

java.lang.Object
com.puppycrawl.tools.checkstyle.utils.InlineConfigUtils

public final class InlineConfigUtils extends Object
Utility class for inline configuration parsing shared between ExampleMacro and InlineConfigParser.

Supports multiple config-delimiter conventions so that legacy examples using the historical /*xml Java-comment style keep working regardless of target file extension, while newer examples for non-Java targets (.xml, .properties) that need a genuinely valid comment in their own file format can opt into a type-appropriate delimiter instead.

  • Field Details

    • JAVA_CONFIG_PREFIX

      public static final String JAVA_CONFIG_PREFIX
      Legacy/default config comment prefix — a loose Java block comment.
      See Also:
    • JAVA_XML_CONFIG_START

      public static final String JAVA_XML_CONFIG_START
      Legacy/default config start delimiter for XML-style (module) config.
      See Also:
    • JAVA_CONFIG_END

      public static final String JAVA_CONFIG_END
      Legacy/default config end delimiter.
      See Also:
    • XML_TARGET_CONFIG_START

      public static final String XML_TARGET_CONFIG_START
      Config start delimiter for XML target files that need well-formed XML content.
      See Also:
    • XML_TARGET_CONFIG_END

      public static final String XML_TARGET_CONFIG_END
      Config end delimiter for XML target files.
      See Also:
    • PROPERTIES_COMMENT_PREFIX

      public static final String PROPERTIES_COMMENT_PREFIX
      Comment prefix for .properties target files.
      See Also:
    • XML_FILE_EXTENSION

      private static final String XML_FILE_EXTENSION
      File extension for XML files.
      See Also:
    • PROPERTIES_FILE_EXTENSION

      private static final String PROPERTIES_FILE_EXTENSION
      File extension for properties files.
      See Also:
    • DELIMITER_SEPARATOR

      private static final String DELIMITER_SEPARATOR
      Separator for delimiter descriptions.
      See Also:
  • Constructor Details

    • InlineConfigUtils

      private InlineConfigUtils()
      Prevent instantiation.
  • Method Details

    • matchDelimiter

      public static InlineConfigUtils.MatchedDelimiter matchDelimiter(List<String> lines, String filePath)
      Matches the first line of a file against every delimiter convention valid for that file's type, and returns the match, or null if none apply.

      For .properties files there is no explicit start/end marker pair: any leading line starting with # is treated as the start of the config block, and the block runs until the first blank line (see getConfigEndIndex(java.lang.Iterable<java.lang.String>, com.puppycrawl.tools.checkstyle.utils.InlineConfigUtils.MatchedDelimiter)).

      Parameters:
      lines - the lines of the file.
      filePath - the file path, used to decide which non-Java conventions are valid.
      Returns:
      the matched delimiter, or null if the first line matches nothing.
    • getConfigEndIndex

      public static int getConfigEndIndex(Iterable<String> lines, InlineConfigUtils.MatchedDelimiter matched)
      Finds the index of the line where a matched config block ends. For delimiter-based matches, this is the first line starting with the end delimiter. For properties-style matches (no explicit end delimiter), this is the first blank line after the start, or the end of the file if there is no blank line.
      Parameters:
      lines - the lines of the file, including the start line at index 0.
      matched - the matched delimiter describing how to find the end.
      Returns:
      the index of the line where the config block ends (exclusive), or -1 if a delimiter-based end could not be found.
    • indexOfStartingWith

      private static int indexOfStartingWith(Iterable<String> lines, String prefix)
      Finds the index of the first line that starts with the given prefix.
      Parameters:
      lines - the lines to search.
      prefix - the prefix to search for.
      Returns:
      the index of the first matching line, or -1 if not found.
    • describeExpectedDelimiters

      public static String describeExpectedDelimiters(String filePath)
      Builds a human-readable description of every delimiter convention valid for the given file's type, for use in error messages.
      Parameters:
      filePath - the file path.
      Returns:
      a description of valid start delimiters for this file type.
    • stripPropertiesCommentPrefix

      public static List<String> stripPropertiesCommentPrefix(Collection<String> lines)
      Strips the leading # comment marker from each line in a properties-style config block. Used because config lines in .properties files must themselves be valid properties-file comments.
      Parameters:
      lines - the lines to process.
      Returns:
      the lines with a leading # removed where present.
    • stripLeadingHash

      private static String stripLeadingHash(String line)
      Strips a leading hash character from the given line if present.
      Parameters:
      line - the line to process.
      Returns:
      the line with leading hash removed, or the original line if no hash.