Class InlineConfigUtils
java.lang.Object
com.puppycrawl.tools.checkstyle.utils.InlineConfigUtils
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordDescribes which delimiter convention matched the first line of a file, so callers can locate the end of the config block and know whether the config content should be parsed as XML module config or the legacy bare key=value format. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringSeparator for delimiter descriptions.static final StringLegacy/default config end delimiter.static final StringLegacy/default config comment prefix — a loose Java block comment.static final StringLegacy/default config start delimiter for XML-style (module) config.static final StringComment prefix for .properties target files.private static final StringFile extension for properties files.private static final StringFile extension for XML files.static final StringConfig end delimiter for XML target files.static final StringConfig start delimiter for XML target files that need well-formed XML content. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringdescribeExpectedDelimiters(String filePath) Builds a human-readable description of every delimiter convention valid for the given file's type, for use in error messages.static intgetConfigEndIndex(Iterable<String> lines, InlineConfigUtils.MatchedDelimiter matched) Finds the index of the line where a matched config block ends.private static intindexOfStartingWith(Iterable<String> lines, String prefix) Finds the index of the first line that starts with the given prefix.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, ornullif none apply.private static StringstripLeadingHash(String line) Strips a leading hash character from the given line if present.Strips the leading#comment marker from each line in a properties-style config block.
-
Field Details
-
JAVA_CONFIG_PREFIX
Legacy/default config comment prefix — a loose Java block comment.- See Also:
-
JAVA_XML_CONFIG_START
Legacy/default config start delimiter for XML-style (module) config.- See Also:
-
JAVA_CONFIG_END
Legacy/default config end delimiter.- See Also:
-
XML_TARGET_CONFIG_START
Config start delimiter for XML target files that need well-formed XML content.- See Also:
-
XML_TARGET_CONFIG_END
Config end delimiter for XML target files.- See Also:
-
PROPERTIES_COMMENT_PREFIX
Comment prefix for .properties target files.- See Also:
-
XML_FILE_EXTENSION
File extension for XML files.- See Also:
-
PROPERTIES_FILE_EXTENSION
File extension for properties files.- See Also:
-
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, ornullif none apply.For
.propertiesfiles 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 (seegetConfigEndIndex(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
nullif 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
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
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
Strips the leading#comment marker from each line in a properties-style config block. Used because config lines in.propertiesfiles must themselves be valid properties-file comments.- Parameters:
lines- the lines to process.- Returns:
- the lines with a leading
#removed where present.
-
stripLeadingHash
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.
-