com.xlrit.gears.runner.utils.MiscUtils Maven / Gradle / Ivy
The newest version!
package com.xlrit.gears.runner.utils;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.entity.ContentType;
public class MiscUtils {
private static final FileFilter ENTER_DIR = file -> !file.getName().endsWith("ignore");
public static String getNameNoExtension(File file) {
return file.getName().split("\\.")[0];
}
/**
* Get the file extension of the given file, or null if it doesn't have an extension.
*/
public static String getExtension(File file) {
String[] nameExtension = file.getName().split("\\.");
return nameExtension.length == 2
? nameExtension[1]
: "";
}
public static String getTableNamePrefix(File gearsDir) {
File gearsJson = new File(gearsDir, "gears.json");
if (!gearsJson.isFile()) {
throw new IllegalArgumentException("No gears.json found in " + gearsDir.getAbsolutePath());
}
try {
ObjectMapper om = new ObjectMapper();
JsonNode gearsRoot = om.readTree(gearsJson);
JsonNode prefix = gearsRoot.get("generatorOptions").get("tableNamePrefix");
if (!prefix.isTextual()) {
throw new IllegalArgumentException("generatorOptions.tableNamePrefix not found as text in gears.json");
}
return prefix.asText();
}
catch (IOException e) {
throw new IllegalArgumentException(String.format("gears.json could not be read: %s", e.getMessage()), e);
}
}
public static String unquoteIfQuoted(String string) {
if (string.length() >= 2
&& string.startsWith("'")
&& string.endsWith("'")) {
return string.substring(1, string.length() - 1);
}
return string;
}
public static List listFilteredFiles(File dir, String pattern, String ... extensions) {
if (!dir.isDirectory()) {
throw new IllegalArgumentException(String.format("Specified file %s is not a directory", dir));
}
FileFilter includeFile = globPatternToFilter(dir, pattern, extensions);
return walk(dir, includeFile)
.sorted(Comparator.comparing(MiscUtils::getNameNoExtension))
.toList();
}
private static Stream walk(File dir, FileFilter includeFile) {
return Arrays.stream(Objects.requireNonNull(dir.listFiles())).flatMap(entry -> {
if (entry.isDirectory() && ENTER_DIR.accept(entry)) return walk(entry, includeFile);
if (entry.isFile() && includeFile.accept(entry)) return Stream.of(entry);
else return Stream.empty();
});
}
private static FileFilter globPatternToFilter(File dir, String pattern, String ... extensions) {
Path basePath = Paths.get(dir.getPath());
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
return file -> {
Path relPath = basePath.relativize(Paths.get(file.getPath()));
return pathMatcher.matches(relPath)
&& Arrays.stream(extensions).anyMatch(extension -> relPath.toString().endsWith("." + extension));
};
}
public static final ContentType TEXT_PLAIN_UTF8 = ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8);
public static final ContentType TEXT_CSV_UTF8 = ContentType.create("text/csv");
public static ContentType getContentType(String fileName) {
return switch (FilenameUtils.getExtension(fileName)) {
case "jpeg", "jpg" -> ContentType.IMAGE_JPEG;
case "png" -> ContentType.IMAGE_PNG;
case "webp" -> ContentType.IMAGE_WEBP;
case "svg" -> ContentType.IMAGE_SVG;
case "tiff", "tif" -> ContentType.IMAGE_TIFF;
case "gif" -> ContentType.IMAGE_GIF;
case "bpm" -> ContentType.IMAGE_BMP;
case "xml" -> ContentType.APPLICATION_XML;
case "json" -> ContentType.APPLICATION_JSON;
case "html", "htm" -> ContentType.TEXT_HTML;
case "xhtml" -> ContentType.APPLICATION_XHTML_XML;
case "bin" -> ContentType.APPLICATION_OCTET_STREAM;
case "csv" -> TEXT_CSV_UTF8;
case "txt" -> TEXT_PLAIN_UTF8;
case "gz" -> ContentType.create("application/gzip");
case "rar" -> ContentType.create("application/vnd.rar");
case "tar" -> ContentType.create("application/x-tar");
case "zip" -> ContentType.create("application/zip");
case "7z" -> ContentType.create("application/x-7z-compressed");
case "pdf" -> ContentType.create("application/pdf");
case "doc" -> ContentType.create("application/msword");
case "docx" -> ContentType.create("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
case "xls" -> ContentType.create("application/vnd.ms-excel");
case "xlsx" -> ContentType.create("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
case "ppt" -> ContentType.create("application/vnd.ms-powerpoint");
case "pptx" -> ContentType.create("application/vnd.openxmlformats-officedocument.presentationml.presentation");
default -> TEXT_PLAIN_UTF8;
};
}
}