Class ExampleMacro
java.lang.Object
org.apache.maven.doxia.macro.AbstractMacro
com.puppycrawl.tools.checkstyle.site.ExampleMacro
- All Implemented Interfaces:
org.apache.maven.doxia.logging.LogEnabled,org.apache.maven.doxia.macro.Macro
@Component(role=org.apache.maven.doxia.macro.Macro.class,
hint="example")
public class ExampleMacro
extends org.apache.maven.doxia.macro.AbstractMacro
A macro that inserts a snippet of code or configuration from a file.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringEnding delimiter for code snippets.private static final StringStarting delimiter for code snippets.private static final StringTheprettyprintlanguage class for XML content.The line contents of the last file.private StringThe path of the last file.private static final StringFile extension for .properties files.Fields inherited from interface org.apache.maven.doxia.macro.Macro
EOL, ROLE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static StringdetermineLanguageClass(String extension, boolean isConfig) Determines theprettyprintlanguage class to use for a snippet, based on the source file's extension.private static StringescapeHtml(String snippet) Escapes HTML special characters in the snippet.voidexecute(org.apache.maven.doxia.sink.Sink sink, org.apache.maven.doxia.macro.MacroRequest request) private static StringgetCodeSnippet(Collection<String> lines) Extract a code snippet from the given lines.private static StringgetConfigSnippet(Collection<String> lines, String extension) Extracts the configuration snippet from the given lines.private static StringgetExtension(String path) Extracts the file extension (including the leading dot) from a file path, or an empty string if the path has no extension.private static booleanhasCodeSnippetEnd(String line) Checks if the line contains a code snippet end delimiter.private static booleanhasCodeSnippetStart(String line) Checks if the line contains a code snippet start delimiter.Read the file at the given path and returns its contents as a list of lines.private static voidwriteCodeSnippet(org.apache.maven.doxia.sink.Sink sink, List<String> lines, String path) Extracts the code snippet from the given lines and writes it to the sink.private static voidwriteConfigSnippet(org.apache.maven.doxia.sink.Sink sink, List<String> lines, String path) Extracts the config snippet from the given lines and writes it to the sink.private static voidwriteSnippet(org.apache.maven.doxia.sink.Sink sink, String snippet, String extension, boolean isConfig) Writes the given snippet inside a formatted source block, choosing syntax highlighting based on the source file's extension.Methods inherited from class org.apache.maven.doxia.macro.AbstractMacro
enableLogging, getAttributesFromMap, getLog, required
-
Field Details
-
CODE_SNIPPET_START
Starting delimiter for code snippets.- See Also:
-
CODE_SNIPPET_END
Ending delimiter for code snippets.- See Also:
-
PROPERTIES_EXTENSION
File extension for .properties files.- See Also:
-
LANGUAGE_XML
Theprettyprintlanguage class for XML content.- See Also:
-
lastPath
The path of the last file. -
lastLines
The line contents of the last file.
-
-
Constructor Details
-
ExampleMacro
public ExampleMacro()Creates a newExampleMacroinstance.
-
-
Method Details
-
execute
public void execute(org.apache.maven.doxia.sink.Sink sink, org.apache.maven.doxia.macro.MacroRequest request) throws org.apache.maven.doxia.macro.MacroExecutionException - Throws:
org.apache.maven.doxia.macro.MacroExecutionException
-
writeConfigSnippet
private static void writeConfigSnippet(org.apache.maven.doxia.sink.Sink sink, List<String> lines, String path) throws org.apache.maven.doxia.macro.MacroExecutionException Extracts the config snippet from the given lines and writes it to the sink.- Parameters:
sink- the sink to write to.lines- the lines of the source file.path- the file path, used for extension detection and error messages.- Throws:
org.apache.maven.doxia.macro.MacroExecutionException- if the config block is invalid or empty.
-
writeCodeSnippet
private static void writeCodeSnippet(org.apache.maven.doxia.sink.Sink sink, List<String> lines, String path) throws org.apache.maven.doxia.macro.MacroExecutionException Extracts the code snippet from the given lines and writes it to the sink.- Parameters:
sink- the sink to write to.lines- the lines of the source file.path- the file path, used for tab-replacement detection and error messages.- Throws:
org.apache.maven.doxia.macro.MacroExecutionException- if the code snippet is empty.
-
readFile
private static List<String> readFile(String path) throws org.apache.maven.doxia.macro.MacroExecutionException Read the file at the given path and returns its contents as a list of lines.- Parameters:
path- the path to the file to read.- Returns:
- the contents of the file as a list of lines.
- Throws:
org.apache.maven.doxia.macro.MacroExecutionException- if the file could not be read.
-
getExtension
Extracts the file extension (including the leading dot) from a file path, or an empty string if the path has no extension.- Parameters:
path- the file path.- Returns:
- the file extension, e.g.
".xml", or""if none.
-
getConfigSnippet
Extracts the configuration snippet from the given lines.- Parameters:
lines- the lines to extract the config from.extension- the file extension (e.g.".xml"), used to decide which delimiter conventions are valid.- Returns:
- the configuration snippet.
- Throws:
IllegalArgumentException- if the config block is invalid.
-
getCodeSnippet
Extract a code snippet from the given lines. Code delimiters can be indented, so we use contains() instead of equals(). If the delimiters are not found, returns the file content excluding the XML config block (if present).- Parameters:
lines- the lines to extract the snippet from.- Returns:
- the code snippet.
-
hasCodeSnippetStart
Checks if the line contains a code snippet start delimiter.- Parameters:
line- the line to check.- Returns:
- true if the line contains a start delimiter.
-
hasCodeSnippetEnd
Checks if the line contains a code snippet end delimiter.- Parameters:
line- the line to check.- Returns:
- true if the line contains an end delimiter.
-
writeSnippet
private static void writeSnippet(org.apache.maven.doxia.sink.Sink sink, String snippet, String extension, boolean isConfig) Writes the given snippet inside a formatted source block, choosing syntax highlighting based on the source file's extension. Config snippets (the content inside a Java-comment-style config block) are always XML regardless of the target file's extension, except for.propertiesfiles, whose bare#-comment config block is not highlighted at all.- Parameters:
sink- the sink to write to.snippet- the snippet to write.extension- the source file extension, e.g.".xml",".sql".isConfig- true if the snippet is a config block rather than a code block.
-
determineLanguageClass
Determines theprettyprintlanguage class to use for a snippet, based on the source file's extension.- Parameters:
extension- the source file extension, e.g.".xml",".sql".isConfig- true if the snippet is a config block (always XML syntax, except for.propertiesfiles.- Returns:
- the language class to apply, or
nullif the snippet should not be syntax-highlighted.
-
escapeHtml
Escapes HTML special characters in the snippet.- Parameters:
snippet- the snippet to escape.- Returns:
- the escaped snippet.
-