mirror of
https://github.com/Maschell/JNUSLib.git
synced 2024-11-22 07:59:19 +01:00
Add FSTUtils class for common FST operations
This commit is contained in:
parent
dd8f7bcb5f
commit
c2579f1cd8
105
src/de/mas/wiiu/jnus/FSTUtils.java
Normal file
105
src/de/mas/wiiu/jnus/FSTUtils.java
Normal file
@ -0,0 +1,105 @@
|
||||
package de.mas.wiiu.jnus;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.io.filefilter.RegexFileFilter;
|
||||
|
||||
import de.mas.wiiu.jnus.entities.fst.FSTEntry;
|
||||
import lombok.val;
|
||||
|
||||
public class FSTUtils {
|
||||
public static Optional<FSTEntry> getFSTEntryByFullPath(FSTEntry root, String givenFullPath) {
|
||||
String fullPath = givenFullPath.replace("/", File.separator);
|
||||
if (!fullPath.startsWith(File.separator)) {
|
||||
fullPath = File.separator + fullPath;
|
||||
}
|
||||
|
||||
String dirPath = FilenameUtils.getFullPathNoEndSeparator(fullPath);
|
||||
Optional<FSTEntry> pathOpt = Optional.of(root);
|
||||
if (!dirPath.equals(File.separator)) {
|
||||
pathOpt = getFileEntryDir(root, dirPath);
|
||||
}
|
||||
|
||||
String path = fullPath;
|
||||
|
||||
return pathOpt.flatMap(e -> e.getChildren().stream().filter(c -> c.getFullPath().equals(path)).findAny());
|
||||
}
|
||||
|
||||
public static Optional<FSTEntry> getFileEntryDir(FSTEntry curEntry, String string) {
|
||||
string = string.replace("/", File.separator);
|
||||
|
||||
if (!string.endsWith(File.separator)) {
|
||||
string += File.separator;
|
||||
}
|
||||
for (val curChild : curEntry.getDirChildren()) {
|
||||
String compareTo = curChild.getFullPath();
|
||||
if (!compareTo.endsWith(File.separator)) {
|
||||
compareTo += File.separator;
|
||||
}
|
||||
if (string.startsWith(compareTo)) {
|
||||
if (string.equals(compareTo)) {
|
||||
return Optional.of(curChild);
|
||||
}
|
||||
return getFileEntryDir(curChild, string);
|
||||
}
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public static Optional<FSTEntry> getEntryByFullPath(FSTEntry root, String filePath) {
|
||||
for (FSTEntry cur : root.getFileChildren()) {
|
||||
if (cur.getFullPath().equals(filePath)) {
|
||||
return Optional.of(cur);
|
||||
}
|
||||
}
|
||||
|
||||
for (FSTEntry cur : root.getDirChildren()) {
|
||||
Optional<FSTEntry> res = getEntryByFullPath(cur, filePath);
|
||||
if (res.isPresent()) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public static Optional<FSTEntry> getChildOfDirectory(FSTEntry root, String filename) {
|
||||
for (FSTEntry cur : root.getChildren()) {
|
||||
if (cur.getFilename().equalsIgnoreCase(filename)) {
|
||||
return Optional.of(cur);
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public static List<FSTEntry> getFSTEntriesByRegEx(FSTEntry root, String string) {
|
||||
return getFSTEntriesByRegEx(string, root, false);
|
||||
}
|
||||
|
||||
public static List<FSTEntry> getFSTEntriesByRegEx(String regEx, FSTEntry entry, boolean allowNotInPackage) {
|
||||
Pattern p = Pattern.compile(regEx);
|
||||
return getFSTEntriesByRegExStream(p, entry, allowNotInPackage).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static Stream<FSTEntry> getFSTEntriesByRegExStream(Pattern p, FSTEntry entry, boolean allowNotInPackage) {
|
||||
return entry.getChildren().stream()//
|
||||
.filter(e -> allowNotInPackage || !e.isNotInPackage()) //
|
||||
.flatMap(e -> {
|
||||
if (!e.isDir()) {
|
||||
if (p.matcher(e.getFullPath().replace("/", File.separator)).matches()) {
|
||||
return Stream.of(e);
|
||||
} else {
|
||||
return Stream.empty();
|
||||
}
|
||||
}
|
||||
return getFSTEntriesByRegExStream(p, e, allowNotInPackage);
|
||||
});
|
||||
}
|
||||
}
|
@ -87,23 +87,6 @@ public class NUSTitle {
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<FSTEntry> getFSTEntryByFullPath(String givenFullPath) {
|
||||
String fullPath = givenFullPath.replace("/", File.separator);
|
||||
if (!fullPath.startsWith(File.separator)) {
|
||||
fullPath = File.separator + fullPath;
|
||||
}
|
||||
|
||||
String dirPath = FilenameUtils.getFullPathNoEndSeparator(fullPath);
|
||||
Optional<FSTEntry> pathOpt = Optional.of(FST.getRoot());
|
||||
if (!dirPath.equals(File.separator)) {
|
||||
pathOpt = getFileEntryDir(dirPath);
|
||||
}
|
||||
|
||||
String path = fullPath;
|
||||
|
||||
return pathOpt.flatMap(e -> e.getChildren().stream().filter(c -> c.getFullPath().equals(path)).findAny());
|
||||
}
|
||||
|
||||
public List<FSTEntry> getFSTEntriesByRegEx(String regEx) {
|
||||
return getFSTEntriesByRegEx(regEx, FST.getRoot());
|
||||
}
|
||||
@ -136,30 +119,6 @@ public class NUSTitle {
|
||||
});
|
||||
}
|
||||
|
||||
public Optional<FSTEntry> getFileEntryDir(String string) {
|
||||
return getFileEntryDir(string.replace("/", File.separator), FST.getRoot());
|
||||
}
|
||||
|
||||
public Optional<FSTEntry> getFileEntryDir(String string, FSTEntry curEntry) {
|
||||
if (!string.endsWith(File.separator)) {
|
||||
string += File.separator;
|
||||
}
|
||||
for (val curChild : curEntry.getDirChildren()) {
|
||||
String compareTo = curChild.getFullPath();
|
||||
if (!compareTo.endsWith(File.separator)) {
|
||||
compareTo += File.separator;
|
||||
}
|
||||
if (string.startsWith(compareTo)) {
|
||||
if (string.equals(compareTo)) {
|
||||
return Optional.of(curChild);
|
||||
}
|
||||
return getFileEntryDir(string, curChild);
|
||||
}
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public void printFiles() {
|
||||
getFST().getRoot().printRecursive(0);
|
||||
}
|
||||
@ -201,4 +160,12 @@ public class NUSTitle {
|
||||
public String toString() {
|
||||
return "NUSTitle [dataProvider=" + dataProvider + "]";
|
||||
}
|
||||
|
||||
public Optional<FSTEntry> getFSTEntryByFullPath(String entryFullPath) {
|
||||
return FSTUtils.getFSTEntryByFullPath(FST.getRoot(), entryFullPath);
|
||||
}
|
||||
|
||||
public Optional<FSTEntry> getFileEntryDir(String path) {
|
||||
return FSTUtils.getFileEntryDir(FST.getRoot(), path);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user