PandaLib/pandalib-config/src/main/java/fr/pandacube/lib/config/AbstractConfig.java

125 lines
3.6 KiB
Java
Raw Normal View History

2024-12-27 23:15:37 +01:00
package fr.pandacube.lib.config;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Class that loads a specific config file or directory.
*/
public abstract class AbstractConfig {
/**
* The {@link File} corresponding to this config file or directory.
*/
protected final File configFile;
/**
* Creates a new {@link AbstractConfig}.
* @param configDir the parent directory
* @param fileOrDirName The name of the config file or folder
* @param type if the provided name is a file or a directory
* @throws IOException if we cannot create the file or directory.
*/
protected AbstractConfig(File configDir, String fileOrDirName, FileType type) throws IOException {
configFile = new File(configDir, fileOrDirName);
if (type == FileType.DIR)
configFile.mkdir();
else
configFile.createNewFile();
}
/**
* Gets the lines from the provided file.
* @param ignoreEmpty {@code true} if we ignore the empty lines.
* @param ignoreHashtagComment {@code true} if we ignore the comment lines (starting with {@code #}).
* @param trimOutput {@code true} if we want to trim all lines using {@link String#trim()}.
* @param f the file to read.
* @return the list of lines, filtered according to the parameters, or null if its not a regular file.
* @throws IOException if an IO error occurs.
*/
protected static List<String> getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput, File f) throws IOException {
if (!f.isFile())
return null;
BufferedReader reader = new BufferedReader(new FileReader(f));
List<String> lines = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
String trimmedLine = line.trim();
2024-12-27 23:15:37 +01:00
if (ignoreEmpty && trimmedLine.isEmpty())
continue;
if (ignoreHashtagComment && trimmedLine.startsWith("#"))
continue;
if (trimOutput)
lines.add(trimmedLine);
else
lines.add(line);
}
reader.close();
return lines;
}
/**
* Gets the lines from the config file.
* @param ignoreEmpty {@code true} if we ignore the empty lines.
* @param ignoreHashtagComment {@code true} if we ignore the comment lines (starting with {@code #}).
* @param trimOutput {@code true} if we want to trim all lines using {@link String#trim()}.
* @return the list of lines, filtered according to the parameters, or null if its not a regular file.
* @throws IOException if an IO error occurs.
*/
protected List<String> getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput) throws IOException {
return getFileLines(ignoreEmpty, ignoreHashtagComment, trimOutput, configFile);
}
/**
* Gets the list of files in the config directory.
* @return the list of files in the config directory, or null if this config is not a directory.
*/
protected List<File> getFileList() {
File[] arr = configFile.listFiles();
return arr != null ? List.of(arr) : null;
}
/**
* Splits the provided string into a list of permission nodes.
* The permission nodes must be separated by {@code ";"}.
* @param perms one or more permissions nodes, separated by {@code ";"}.
* @return {@code null} if the parameter is null or is equal to {@code "*"}, or the string split using {@code ";"}.
*/
public static List<String> splitPermissionsString(String perms) {
if (perms == null || perms.equals("*"))
return null;
return List.of(perms.split(";"));
}
/**
* The type of config.
*/
protected enum FileType {
/** A config file. */
FILE,
/** A config directory. */
DIR
}
}