diff --git a/src/de/mas/wiiu/jnus/FSTUtils.java b/src/de/mas/wiiu/jnus/FSTUtils.java new file mode 100644 index 0000000..6270181 --- /dev/null +++ b/src/de/mas/wiiu/jnus/FSTUtils.java @@ -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 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 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 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 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 res = getEntryByFullPath(cur, filePath); + if (res.isPresent()) { + return res; + } + } + return Optional.empty(); + } + + public static Optional 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 getFSTEntriesByRegEx(FSTEntry root, String string) { + return getFSTEntriesByRegEx(string, root, false); + } + + public static List getFSTEntriesByRegEx(String regEx, FSTEntry entry, boolean allowNotInPackage) { + Pattern p = Pattern.compile(regEx); + return getFSTEntriesByRegExStream(p, entry, allowNotInPackage).collect(Collectors.toList()); + } + + private static Stream 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); + }); + } +} diff --git a/src/de/mas/wiiu/jnus/NUSTitle.java b/src/de/mas/wiiu/jnus/NUSTitle.java index e50ec35..27ca529 100644 --- a/src/de/mas/wiiu/jnus/NUSTitle.java +++ b/src/de/mas/wiiu/jnus/NUSTitle.java @@ -87,23 +87,6 @@ public class NUSTitle { }); } - public Optional getFSTEntryByFullPath(String givenFullPath) { - String fullPath = givenFullPath.replace("/", File.separator); - if (!fullPath.startsWith(File.separator)) { - fullPath = File.separator + fullPath; - } - - String dirPath = FilenameUtils.getFullPathNoEndSeparator(fullPath); - Optional 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 getFSTEntriesByRegEx(String regEx) { return getFSTEntriesByRegEx(regEx, FST.getRoot()); } @@ -136,30 +119,6 @@ public class NUSTitle { }); } - public Optional getFileEntryDir(String string) { - return getFileEntryDir(string.replace("/", File.separator), FST.getRoot()); - } - - public Optional 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 getFSTEntryByFullPath(String entryFullPath) { + return FSTUtils.getFSTEntryByFullPath(FST.getRoot(), entryFullPath); + } + + public Optional getFileEntryDir(String path) { + return FSTUtils.getFileEntryDir(FST.getRoot(), path); + } }