Class YamlHeaderFormatter

java.lang.Object
org.simpleyaml.configuration.comments.format.YamlHeaderFormatter
All Implemented Interfaces:
CommentFormatter

public class YamlHeaderFormatter extends Object implements CommentFormatter
The header is the first block comment at the top of the configuration file.

It ends with a blank line or with the end of the file.

  • Field Details

  • Constructor Details

    • YamlHeaderFormatter

      protected YamlHeaderFormatter(YamlCommentFormatterConfiguration configuration)
    • YamlHeaderFormatter

      public YamlHeaderFormatter(String commentPrefix, boolean strip)
    • YamlHeaderFormatter

      public YamlHeaderFormatter()
  • Method Details

    • parse

      public String parse(String raw, CommentType type, KeyTree.Node node) throws IOException
      Parse the header of a contents string, like a file contents.

      The header is the first block comment at the top of the configuration file. It ends with a blank line or with the end of the file.

      If no blank line is found after the first block comment and it is not the end of the file then null is returned (the comment will be attached to the first key, so that comment is not considered a header).

      The blank line suffix will be stripped away, i.e. the result string will not have a blank line at the end.

      If stripPrefix() is true then String.trim() will be applied to every line of the header and the commentPrefix() will be stripped away.

      Specified by:
      parse in interface CommentFormatter
      Parameters:
      raw - the string to parse
      type - the comment type
      node - the comment node
      Returns:
      the header
      Throws:
      IOException
    • parse

      public String parse(Reader raw, CommentType type, KeyTree.Node node) throws IOException
      Description copied from interface: CommentFormatter
      Parse the comment from a reader that may contain a raw-formatted comment (for instance from a configuration file) to a human-friendly contentful representation of that comment.
      Specified by:
      parse in interface CommentFormatter
      Parameters:
      raw - the comment to parse that may contain special format characters with leading and trailing space
      type - the comment type
      node - the comment node
      Returns:
      the comment in the expected format to be read by humans
      Throws:
      IOException - if comment cannot be parsed
    • dump

      public String dump(String header, CommentType type, KeyTree.Node node)
      Given the header (for instance the one returned by parse(java.lang.String, org.simpleyaml.configuration.comments.CommentType, org.simpleyaml.configuration.comments.KeyTree.Node)), returns the final string formatted to be dumped somewhere like a file.

      The header ends with a blank line and every line is prefixed with the commentPrefix().

      If all header lines are already prefixed with a # then the commentPrefix() is not applied.

      Specified by:
      dump in interface CommentFormatter
      Parameters:
      header - the header to dump
      type - the comment type
      node - the comment node
      Returns:
      the final string to be dumped
    • stripPrefix

      public boolean stripPrefix()
      If strip is true then String.trim() will be applied to every line of the header and the commentPrefix() will be stripped away.

      Default is false.

      Returns:
      if stripping the prefix is desired
    • stripPrefix

      public YamlHeaderFormatter stripPrefix(boolean stripPrefix)
      Set if stripping the prefix is desired.

      If strip is true then String.trim() will be applied to every line of the header and the commentPrefix() will be stripped away.

      Default is false.

      Parameters:
      stripPrefix - if stripping the prefix is desired
      Returns:
      this object, for chaining
    • commentPrefix

      public String commentPrefix()
      Gets the comment prefix to apply to every line of the header.

      By default is "# ", i.e. a # followed by a space.

      Returns:
      the comment prefix
    • commentPrefix

      public YamlHeaderFormatter commentPrefix(String commentPrefix)
      Sets the comment prefix to apply to every line of the header.

      By default is "# ", i.e. a # followed by a space.

      Parameters:
      commentPrefix - the comment prefix
      Returns:
      this object, for chaining
    • prefixFirst

      public YamlHeaderFormatter prefixFirst(String commentPrefixFirst)
      Sets the comment prefix to apply to the beginning of the header.
      Parameters:
      commentPrefixFirst - the comment prefix to apply to the beginning of the header
      Returns:
      this object, for chaining
    • commentSuffix

      public YamlHeaderFormatter commentSuffix(String suffixMultiline)
      Set the comment suffix to append to every line of the header.
      Parameters:
      suffixMultiline - the suffix to append to every line of the header
      Returns:
      this object, for chaining
    • suffixLast

      public YamlHeaderFormatter suffixLast(String suffixLast)
      Set the suffix to append to the end of the header. If not included, a blank line \n\n will be added to the suffix.

      Default is a blank line.

      Parameters:
      suffixLast - the suffix to append to the end of the header
      Returns:
      this object, for chaining
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object