Class ExampleMacro

java.lang.Object
org.apache.maven.doxia.macro.AbstractMacro
com.puppycrawl.tools.checkstyle.site.ExampleMacro
All Implemented Interfaces:
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 Details

    • XML_CONFIG_START

      private static final String XML_CONFIG_START
      Starting delimiter for config snippets.
      See Also:
    • XML_CONFIG_END

      private static final String XML_CONFIG_END
      Ending delimiter for config snippets.
      See Also:
    • CODE_SNIPPET_START

      private static final String CODE_SNIPPET_START
      Starting delimiter for code snippets.
      See Also:
    • CODE_SNIPPET_END

      private static final String CODE_SNIPPET_END
      Ending delimiter for code snippets.
      See Also:
    • XML_PATTERN

      private static final Pattern XML_PATTERN
      The pattern of xml code blocks.
    • lastPath

      private String lastPath
      The path of the last file.
    • lastLines

      private List<String> lastLines
      The line contents of the last file.
  • Constructor Details

    • ExampleMacro

      public ExampleMacro()
  • 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
    • 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.
    • getConfigSnippet

      private static String getConfigSnippet(Collection<String> lines)
      Extract a configuration snippet from the given lines. Config delimiters use the whole line for themselves and have no indentation. We use equals() instead of contains() to be more strict because some examples contain those delimiters. If the delimiters are not found, returns the entire file content.
      Parameters:
      lines - the lines to extract the snippet from.
      Returns:
      the configuration snippet.
    • getCodeSnippet

      private static String getCodeSnippet(Collection<String> lines)
      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.
    • writeSnippet

      private static void writeSnippet(org.apache.maven.doxia.sink.Sink sink, String snippet)
      Writes the given snippet inside a formatted source block.
      Parameters:
      sink - the sink to write to.
      snippet - the snippet to write.
    • escapeHtml

      private static String escapeHtml(String snippet)
      Escapes HTML special characters in the snippet.
      Parameters:
      snippet - the snippet to escape.
      Returns:
      the escaped snippet.
    • isXml

      private static boolean isXml(String snippet)
      Determines if the given snippet is likely an XML fragment.
      Parameters:
      snippet - the code snippet to analyze.
      Returns:
      true if the snippet appears to be XML, otherwise false.