java.lang.Object
net.foxgenesis.util.resource.ResourceUtils
Utility class containing methods for handling resources.
- Author:
- Ashley
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic PropertiesgetProperties(Path path, ModuleResource defaults) Parse aPropertiesfile at the specifiedPath.linesFromResource(URL path) Read all lines from a resourcestatic org.apache.commons.configuration2.ConfigurationloadConfiguration(ConfigType type, ModuleResource defaults, Path directory, String output) Load a configuration file of the providedtypefrom the specified directory and file.static org.apache.commons.configuration2.INIConfigurationloadINI(ModuleResource defaults, Path dir, String output) Load an.iniconfiguration file from the specified directory and file.static org.apache.commons.configuration2.JSONConfigurationloadJSON(ModuleResource defaults, Path dir, String output) Load a.jsonconfiguration file from the specified directory and file.static org.apache.commons.configuration2.PropertiesConfigurationloadProperties(ModuleResource defaults, Path dir, String output) Load a.propertiesconfiguration file from the specified directory and file.static org.apache.commons.configuration2.XMLConfigurationloadXML(ModuleResource defaults, Path dir, String output) Load a.xmlconfiguration file from the specified directory and file.static String[]toSplitString(@NotNull InputStream input) Read all data from the specifiedInputStreamand parse all lines as a string array.static StringtoString(@NotNull InputStream input) Read all data from the specifiedInputStreamand parse it as a string.static StringRead all data from the specifiedURLand parse it as a string.
-
Constructor Details
-
ResourceUtils
public ResourceUtils()
-
-
Method Details
-
linesFromResource
Read all lines from a resource- Parameters:
path- -URLpath to the resource- Returns:
- Returns all lines as a
List - Throws:
IOException- Thrown if an error occurs while reading theInputStreamof the resource
-
getProperties
Parse aPropertiesfile at the specifiedPath. If the specified file was not found, the defaults will be written to its location.- Parameters:
path- - path to the properties filedefaults- - (optional) resource containing the default properties file- Returns:
- Returns a
Propertiesfile that was parsed from the specified path - Throws:
IOException- If an I/O error occursFileNotFoundException- 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 providedtypefrom the specified directory and file. If the file is not found, the providedModuleResourcepointing 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 typedefaults- - resource containing the configuration defaultsdirectory- - the directory containing the configuration fileoutput- - the name of the configuration file- Returns:
- Returns the parsed
Configuration - Throws:
IOException- If an I/O error occursorg.apache.commons.configuration2.ex.ConfigurationException- If an error occursSecurityException- 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.propertiesconfiguration file from the specified directory and file. If the file is not found, the providedModuleResourcepointing to the configuration defaults will be used instead.- Parameters:
defaults- - resource containing the configuration defaultsdir- - the directory containing the configuration fileoutput- - the name of the configuration file- Returns:
- Returns the parsed
PropertiesConfiguration - Throws:
IOException- If an I/O error occursorg.apache.commons.configuration2.ex.ConfigurationException- If an error occursSecurityException- 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.iniconfiguration file from the specified directory and file. If the file is not found, the providedModuleResourcepointing to the configuration defaults will be used instead.- Parameters:
defaults- - resource containing the configuration defaultsdir- - the directory containing the configuration fileoutput- - the name of the configuration file- Returns:
- Returns the parsed
INIConfiguration - Throws:
IOException- If an I/O error occursorg.apache.commons.configuration2.ex.ConfigurationException- If an error occursSecurityException- 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.jsonconfiguration file from the specified directory and file. If the file is not found, the providedModuleResourcepointing to the configuration defaults will be used instead.- Parameters:
defaults- - resource containing the configuration defaultsdir- - the directory containing the configuration fileoutput- - the name of the configuration file- Returns:
- Returns the parsed
JSONConfiguration - Throws:
IOException- If an I/O error occursorg.apache.commons.configuration2.ex.ConfigurationException- If an error occursSecurityException- 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.xmlconfiguration file from the specified directory and file. If the file is not found, the providedModuleResourcepointing to the configuration defaults will be used instead.- Parameters:
defaults- - resource containing the configuration defaultsdir- - the directory containing the configuration fileoutput- - the name of the configuration file- Returns:
- Returns the parsed
XMLConfiguration - Throws:
IOException- If an I/O error occursorg.apache.commons.configuration2.ex.ConfigurationException- If an error occursSecurityException- Thrown if the specified file is not readable
-
toString
Read all data from the specifiedInputStreamand 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
Read all data from the specifiedURLand parse it as a string.The input stream will be closed after completion
- Parameters:
url- - theURLto read from- Returns:
- Returns a string containing the data from the specified URL
- Throws:
IOException- If an I/O error occurs
-
toSplitString
Read all data from the specifiedInputStreamand 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
-