Class 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

      Fields 
      Modifier and Type Field Description
      private static java.lang.String CODE_SNIPPET_END
      Ending delimiter for code snippets.
      private static java.lang.String CODE_SNIPPET_START
      Starting delimiter for code snippets.
      private static java.lang.String INDENTATION
      Eight whitespace characters.
      private java.util.List<java.lang.String> lastLines
      The line contents of the last file.
      private java.lang.String lastPath
      The path of the last file.
      private static java.lang.String NEWLINE
      Newline character.
      private static java.lang.String XML_CONFIG_END
      Ending delimiter for config snippets.
      private static java.lang.String XML_CONFIG_START
      Starting delimiter for config snippets.
      • Fields inherited from interface org.apache.maven.doxia.macro.Macro

        EOL, ROLE
    • Constructor Summary

      Constructors 
      Constructor Description
      ExampleMacro()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void execute​(org.apache.maven.doxia.sink.Sink sink, org.apache.maven.doxia.macro.MacroRequest request)  
      private static java.lang.String getCodeSnippet​(java.util.Collection<java.lang.String> lines)
      Extract a code snippet from the given lines.
      private static java.lang.String getConfigSnippet​(java.util.Collection<java.lang.String> lines)
      Extract a configuration snippet from the given lines.
      private static java.util.List<java.lang.String> readFile​(java.lang.String path)
      Read the file at the given path and returns its contents as a list of lines.
      private static void writeSnippet​(org.apache.maven.doxia.sink.Sink sink, java.lang.String snippet)
      Write the given snippet to the file inside a source block.
      • Methods inherited from class org.apache.maven.doxia.macro.AbstractMacro

        enableLogging, getAttributesFromMap, getLog, required
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • 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 java.util.List<java.lang.String> readFile​(java.lang.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 java.lang.String getConfigSnippet​(java.util.Collection<java.lang.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.
        Parameters:
        lines - the lines to extract the snippet from.
        Returns:
        the configuration snippet.
      • getCodeSnippet

        private static java.lang.String getCodeSnippet​(java.util.Collection<java.lang.String> lines)
        Extract a code snippet from the given lines. Code delimiters can be indented, so we use contains() instead of equals().
        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,
                                         java.lang.String snippet)
        Write the given snippet to the file inside a source block.
        Parameters:
        sink - the sink to write to.
        snippet - the snippet to write.