2024-12-27 23:15:37 +01:00
|
|
|
|
package fr.pandacube.lib.config;
|
2023-06-20 00:15:46 +02:00
|
|
|
|
|
2016-07-26 02:06:21 +02:00
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-03 01:45:18 +02:00
|
|
|
|
* Class that loads a specific config file or directory.
|
2016-07-26 02:06:21 +02:00
|
|
|
|
*/
|
|
|
|
|
public abstract class AbstractConfig {
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-20 00:15:46 +02:00
|
|
|
|
* The {@link File} corresponding to this config file or directory.
|
2016-07-26 02:06:21 +02:00
|
|
|
|
*/
|
2022-07-10 00:55:56 +02:00
|
|
|
|
protected final File configFile;
|
2016-07-26 02:06:21 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2022-08-03 01:45:18 +02:00
|
|
|
|
* Creates a new {@link AbstractConfig}.
|
2022-07-10 00:55:56 +02:00
|
|
|
|
* @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
|
2022-08-03 01:45:18 +02:00
|
|
|
|
* @throws IOException if we cannot create the file or directory.
|
2016-07-26 02:06:21 +02:00
|
|
|
|
*/
|
2022-08-03 01:45:18 +02:00
|
|
|
|
protected AbstractConfig(File configDir, String fileOrDirName, FileType type) throws IOException {
|
2016-07-26 02:06:21 +02:00
|
|
|
|
configFile = new File(configDir, fileOrDirName);
|
|
|
|
|
if (type == FileType.DIR)
|
|
|
|
|
configFile.mkdir();
|
|
|
|
|
else
|
|
|
|
|
configFile.createNewFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-03 01:45:18 +02:00
|
|
|
|
* 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 it’s not a regular file.
|
|
|
|
|
* @throws IOException if an IO error occurs.
|
2016-07-26 02:06:21 +02:00
|
|
|
|
*/
|
2022-08-03 01:45:18 +02:00
|
|
|
|
protected static List<String> getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput, File f) throws IOException {
|
2016-07-26 02:06:21 +02:00
|
|
|
|
if (!f.isFile())
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(f));
|
|
|
|
|
|
2016-10-17 00:03:04 +02:00
|
|
|
|
List<String> lines = new ArrayList<>();
|
2016-07-26 02:06:21 +02:00
|
|
|
|
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
String trimmedLine = line.trim();
|
|
|
|
|
|
2024-12-27 23:15:37 +01:00
|
|
|
|
if (ignoreEmpty && trimmedLine.isEmpty())
|
2016-07-26 02:06:21 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (ignoreHashtagComment && trimmedLine.startsWith("#"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (trimOutput)
|
|
|
|
|
lines.add(trimmedLine);
|
|
|
|
|
else
|
|
|
|
|
lines.add(line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reader.close();
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
2022-08-03 01:45:18 +02:00
|
|
|
|
|
|
|
|
|
|
2016-07-26 02:06:21 +02:00
|
|
|
|
/**
|
2022-08-03 01:45:18 +02:00
|
|
|
|
* 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 it’s not a regular file.
|
|
|
|
|
* @throws IOException if an IO error occurs.
|
2016-07-26 02:06:21 +02:00
|
|
|
|
*/
|
|
|
|
|
protected List<String> getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput) throws IOException {
|
|
|
|
|
return getFileLines(ignoreEmpty, ignoreHashtagComment, trimOutput, configFile);
|
|
|
|
|
}
|
2022-08-03 01:45:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2016-07-26 02:06:21 +02:00
|
|
|
|
protected List<File> getFileList() {
|
2023-06-20 00:15:46 +02:00
|
|
|
|
File[] arr = configFile.listFiles();
|
|
|
|
|
return arr != null ? List.of(arr) : null;
|
2016-07-26 02:06:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-03 01:45:18 +02:00
|
|
|
|
* 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 ";"}.
|
2023-06-20 00:15:46 +02:00
|
|
|
|
* @return {@code null} if the parameter is null or is equal to {@code "*"}, or the string split using {@code ";"}.
|
2016-07-26 02:06:21 +02:00
|
|
|
|
*/
|
|
|
|
|
public static List<String> splitPermissionsString(String perms) {
|
|
|
|
|
if (perms == null || perms.equals("*"))
|
|
|
|
|
return null;
|
2022-08-03 01:45:18 +02:00
|
|
|
|
return List.of(perms.split(";"));
|
2016-08-18 14:42:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-08-03 01:45:18 +02:00
|
|
|
|
/**
|
|
|
|
|
* The type of config.
|
|
|
|
|
*/
|
2016-07-26 02:06:21 +02:00
|
|
|
|
protected enum FileType {
|
2022-08-03 01:45:18 +02:00
|
|
|
|
/** A config file. */
|
|
|
|
|
FILE,
|
|
|
|
|
/** A config directory. */
|
|
|
|
|
DIR
|
2016-07-26 02:06:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|