|
| 1 | +package org.infinity.util.io; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.DirectoryStream; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.NoSuchFileException; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Objects; |
| 10 | +import java.util.concurrent.ConcurrentHashMap; |
| 11 | + |
| 12 | +public class CaseAwareSingleDirectoryPathResolver { |
| 13 | + |
| 14 | + public static class ResolutionResult { |
| 15 | + private Path resolvedPart; |
| 16 | + private Path unresolvedPart; |
| 17 | + |
| 18 | + public ResolutionResult(Path resolvedPart, Path unresolvedPart) { |
| 19 | + super(); |
| 20 | + this.resolvedPart = resolvedPart; |
| 21 | + this.unresolvedPart = unresolvedPart; |
| 22 | + } |
| 23 | + |
| 24 | + public Path getResolvedPart() { |
| 25 | + return resolvedPart; |
| 26 | + } |
| 27 | + |
| 28 | + public Path getUnresolvedPart() { |
| 29 | + return unresolvedPart; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private final Map<Path, CaseAwareSingleDirectoryPathResolver> subdirectoryResolvers = new ConcurrentHashMap<>(); |
| 34 | + private final Path directory; |
| 35 | + |
| 36 | + private boolean caseSensitivityIsFiguredOut = false; |
| 37 | + private boolean caseSensitive = false; |
| 38 | + |
| 39 | + public CaseAwareSingleDirectoryPathResolver(Path resolveFrom) { |
| 40 | + this.directory = resolveFrom; |
| 41 | + } |
| 42 | + |
| 43 | + public ResolutionResult resolve(Path relativePath) { |
| 44 | + Path unresolvedPart = relativePath; |
| 45 | + Path currentLocation = directory; |
| 46 | + Path nextPathElement = unresolvedPart.subpath(0, 1); |
| 47 | + Path actualNextLocation = resolveSinglePathElementToActualLocation(currentLocation, nextPathElement); |
| 48 | + if (actualNextLocation == null) { |
| 49 | + return new ResolutionResult(currentLocation, unresolvedPart); |
| 50 | + } |
| 51 | + |
| 52 | + if (unresolvedPart.getNameCount() == 1) { // this was the last path element, nothing left to resolve |
| 53 | + return new ResolutionResult(currentLocation.resolve(actualNextLocation), null); |
| 54 | + } else { |
| 55 | + unresolvedPart = unresolvedPart.subpath(1, unresolvedPart.getNameCount()); |
| 56 | + return subdirectoryResolvers |
| 57 | + .computeIfAbsent(actualNextLocation, (key) -> new CaseAwareSingleDirectoryPathResolver(key)) |
| 58 | + .resolve(unresolvedPart); |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private Path resolveSinglePathElementToActualLocation(Path resolveFrom, Path elementToResolve) { |
| 64 | + Path directResolution = resolveFrom.resolve(elementToResolve); |
| 65 | + if (Files.exists(directResolution)) { |
| 66 | + return directResolution; |
| 67 | + } |
| 68 | + |
| 69 | + if (currentDirIsKnownToBeCaseInsensitive()) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + String searchString = elementToResolve.getFileName().toString(); |
| 74 | + try (DirectoryStream<Path> ds = Files.newDirectoryStream(resolveFrom)) { |
| 75 | + for (final Path dirPath : ds) { |
| 76 | + if (!caseSensitivityIsFiguredOut) { |
| 77 | + tryFigureOutCaseSensitivityUsingExistingPath(dirPath); |
| 78 | + if (currentDirIsKnownToBeCaseInsensitive()) { |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + String dirString = dirPath.getFileName().toString(); |
| 83 | + if (searchString.equalsIgnoreCase(dirString)) { |
| 84 | + return resolveFrom.resolve(dirPath); |
| 85 | + } |
| 86 | + } |
| 87 | + } catch (Throwable t) { |
| 88 | + throw new RuntimeException(t); |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + private boolean currentDirIsKnownToBeCaseInsensitive() { |
| 94 | + return caseSensitivityIsFiguredOut && !caseSensitive; |
| 95 | + } |
| 96 | + |
| 97 | + private boolean tryFigureOutCaseSensitivityUsingExistingPath(Path existingPath) throws IOException { |
| 98 | + Path baseDir = existingPath.getParent(); |
| 99 | + String filename = existingPath.getFileName().toString(); |
| 100 | + String lowerCaseFilename = filename.toLowerCase(); |
| 101 | + String upperCaseFilename = filename.toUpperCase(); |
| 102 | + if (Objects.equals(lowerCaseFilename, upperCaseFilename)) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + Path lowercaseFilePath = baseDir.resolve(lowerCaseFilename); |
| 108 | + Path uppercaseFilePath = baseDir.resolve(upperCaseFilename); |
| 109 | + this.caseSensitive = !Files.isSameFile(lowercaseFilePath, uppercaseFilePath); |
| 110 | + this.caseSensitivityIsFiguredOut = true; |
| 111 | + } catch (NoSuchFileException e) { |
| 112 | + this.caseSensitive = true; |
| 113 | + this.caseSensitivityIsFiguredOut = true; |
| 114 | + } |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments