Class YAMLProcessor

java.lang.Object
com.sk89q.util.yaml.YAMLNode
com.sk89q.util.yaml.YAMLProcessor

public class YAMLProcessor extends YAMLNode
YAML configuration loader. To use this class, construct it with path to a file and call its load() method. For specifying node paths in the various get*() methods, they support SK's path notation, allowing you to select child nodes by delimiting node names with periods.

For example, given the following configuration file:

members:
     - Hollie
     - Jason
     - Bobo
     - Aya
     - Tetsu
 worldguard:
     fire:
         spread: false
         blocks: [cloth, rock, glass]
 sturmeh:
     cool: false
     eats:
         babies: true

Calling code could access sturmeh's baby eating state by using getBoolean("sturmeh.eats.babies", false). For lists, there are methods such as getStringList that will return a type safe list.

  • Field Details

    • LINE_BREAK

      public static final String LINE_BREAK
    • COMMENT_CHAR

      public static final char COMMENT_CHAR
      See Also:
    • yaml

      protected final org.yaml.snakeyaml.Yaml yaml
    • file

      protected final File file
    • format

      protected YAMLFormat format
  • Constructor Details

    • YAMLProcessor

      public YAMLProcessor(File file, boolean writeDefaults, YAMLFormat format)
    • YAMLProcessor

      public YAMLProcessor(File file, boolean writeDefaults)
  • Method Details

    • load

      public void load() throws IOException
      Loads the configuration file.
      Throws:
      IOException - on load error
    • setHeader

      public void setHeader(String... headerLines)
      Set the header for the file as a series of lines that are terminated by a new line sequence.
      Parameters:
      headerLines - header lines to prepend
    • setHeader

      public void setHeader(String header)
      Set the header for the file. A header can be provided to prepend the YAML data output on configuration save. The header is printed raw and so must be manually commented if used. A new line will be appended after the header, however, if a header is provided.
      Parameters:
      header - header to prepend
    • getHeader

      public String getHeader()
      Return the set header.
      Returns:
      the header text
    • save

      public boolean save()
      Saves the configuration to disk. All errors are clobbered.
      Returns:
      true if it was successful
    • getInputStream

      public InputStream getInputStream() throws IOException
      Throws:
      IOException
    • getOutputStream

      public OutputStream getOutputStream() throws IOException
      Throws:
      IOException
    • getComment

      public String getComment(String key)
      Returns a root-level comment.
      Parameters:
      key - the property key
      Returns:
      the comment or null
    • setComment

      public void setComment(String key, String comment)
    • setComment

      public void setComment(String key, String... comment)
      Set a root-level comment.
      Parameters:
      key - the property key
      comment - the comment. May be null, in which case the comment is removed.
    • getComments

      public Map<String,String> getComments()
      Returns root-level comments.
      Returns:
      map of root-level comments
    • setComments

      public void setComments(Map<String,String> comments)
      Set root-level comments from a map.
      Parameters:
      comments - comment map
    • getEmptyNode

      public static YAMLNode getEmptyNode(boolean writeDefaults)
      Get an empty ConfigurationNode for using as a default in methods that select a node from a node list.
      Parameters:
      writeDefaults - true to write default values when a property is requested that doesn't exist
      Returns:
      a node