Module watamebot

Class ResourceUtils

java.lang.Object
net.foxgenesis.util.resource.ResourceUtils

public final class ResourceUtils extends Object
Utility class containing methods for handling resources.
Author:
Ashley
  • Constructor Details

    • ResourceUtils

      public ResourceUtils()
  • Method Details

    • linesFromResource

      public static List<String> linesFromResource(URL path) throws IOException
      Read all lines from a resource
      Parameters:
      path - - URL path to the resource
      Returns:
      Returns all lines as a List
      Throws:
      IOException - Thrown if an error occurs while reading the InputStream of the resource
    • getProperties

      public static Properties getProperties(Path path, ModuleResource defaults) throws IOException
      Parse a Properties file at the specified Path. If the specified file was not found, the defaults will be written to its location.
      Parameters:
      path - - path to the properties file
      defaults - - (optional) resource containing the default properties file
      Returns:
      Returns a Properties file that was parsed from the specified path
      Throws:
      IOException - If an I/O error occurs
      FileNotFoundException - If the specified file was not found and no defaults were given
    • loadConfiguration

      public static org.apache.commons.configuration2.Configuration loadConfiguration(ConfigType type, ModuleResource defaults, Path directory, String output) throws IOException, org.apache.commons.configuration2.ex.ConfigurationException
      Load a configuration file of the provided type from the specified directory and file. If the file is not found, the provided ModuleResource pointing to the configuration defaults will be used instead.

      This method is effectively equivalent to:

       Configuration configuration = switch (type) {
              case PROPERTIES -> loadProperties(defaults, directory, output);
              case INI -> loadINI(defaults, directory, output);
              case JSON -> loadJSON(defaults, directory, output);
              case XML -> loadXML(defaults, directory, output);
              default -> throw new IllegalArgumentException("Unknown type: " + type);
       };
       
      Parameters:
      type - - configuration type
      defaults - - resource containing the configuration defaults
      directory - - the directory containing the configuration file
      output - - the name of the configuration file
      Returns:
      Returns the parsed Configuration
      Throws:
      IOException - If an I/O error occurs
      org.apache.commons.configuration2.ex.ConfigurationException - If an error occurs
      SecurityException - Thrown if the specified file is not readable
      See Also:
    • loadProperties

      public static org.apache.commons.configuration2.PropertiesConfiguration loadProperties(ModuleResource defaults, Path dir, String output) throws IOException, org.apache.commons.configuration2.ex.ConfigurationException
      Load a .properties configuration file from the specified directory and file. If the file is not found, the provided ModuleResource pointing to the configuration defaults will be used instead.
      Parameters:
      defaults - - resource containing the configuration defaults
      dir - - the directory containing the configuration file
      output - - the name of the configuration file
      Returns:
      Returns the parsed PropertiesConfiguration
      Throws:
      IOException - If an I/O error occurs
      org.apache.commons.configuration2.ex.ConfigurationException - If an error occurs
      SecurityException - Thrown if the specified file is not readable
    • loadINI

      public static org.apache.commons.configuration2.INIConfiguration loadINI(ModuleResource defaults, Path dir, String output) throws IOException, org.apache.commons.configuration2.ex.ConfigurationException
      Load an .ini configuration file from the specified directory and file. If the file is not found, the provided ModuleResource pointing to the configuration defaults will be used instead.
      Parameters:
      defaults - - resource containing the configuration defaults
      dir - - the directory containing the configuration file
      output - - the name of the configuration file
      Returns:
      Returns the parsed INIConfiguration
      Throws:
      IOException - If an I/O error occurs
      org.apache.commons.configuration2.ex.ConfigurationException - If an error occurs
      SecurityException - Thrown if the specified file is not readable
    • loadJSON

      public static org.apache.commons.configuration2.JSONConfiguration loadJSON(ModuleResource defaults, Path dir, String output) throws IOException, org.apache.commons.configuration2.ex.ConfigurationException
      Load a .json configuration file from the specified directory and file. If the file is not found, the provided ModuleResource pointing to the configuration defaults will be used instead.
      Parameters:
      defaults - - resource containing the configuration defaults
      dir - - the directory containing the configuration file
      output - - the name of the configuration file
      Returns:
      Returns the parsed JSONConfiguration
      Throws:
      IOException - If an I/O error occurs
      org.apache.commons.configuration2.ex.ConfigurationException - If an error occurs
      SecurityException - Thrown if the specified file is not readable
    • loadXML

      public static org.apache.commons.configuration2.XMLConfiguration loadXML(ModuleResource defaults, Path dir, String output) throws IOException, org.apache.commons.configuration2.ex.ConfigurationException
      Load a .xml configuration file from the specified directory and file. If the file is not found, the provided ModuleResource pointing to the configuration defaults will be used instead.
      Parameters:
      defaults - - resource containing the configuration defaults
      dir - - the directory containing the configuration file
      output - - the name of the configuration file
      Returns:
      Returns the parsed XMLConfiguration
      Throws:
      IOException - If an I/O error occurs
      org.apache.commons.configuration2.ex.ConfigurationException - If an error occurs
      SecurityException - Thrown if the specified file is not readable
    • toString

      public static String toString(@NotNull @NotNull InputStream input) throws IOException
      Read all data from the specified InputStream and parse it as a string.

      The input stream will be closed after completion

      Parameters:
      input - - the input stream to read
      Returns:
      Returns a string containing the data from the specified input stream
      Throws:
      IOException - If an I/O error occurs
    • toString

      public static String toString(@NotNull @NotNull URL url) throws IOException
      Read all data from the specified URL and parse it as a string.

      The input stream will be closed after completion

      Parameters:
      url - - the URL to read from
      Returns:
      Returns a string containing the data from the specified URL
      Throws:
      IOException - If an I/O error occurs
    • toSplitString

      public static String[] toSplitString(@NotNull @NotNull InputStream input) throws IOException
      Read all data from the specified InputStream and parse all lines as a string array.

      The input stream will be closed after completion

      This method is effectively equivalent to:

       toString(input).split("(\\r\\n|\\r|\\n)")
       
      Parameters:
      input - - the input stream to read
      Returns:
      Returns a string array containing the data from the specified input stream
      Throws:
      IOException - If an I/O error occurs