Class AbstractArchiveFileAccess
- All Implemented Interfaces:
FileAccess,AutoCloseable
- Direct Known Subclasses:
InMemoryFileAccess,ZipFileAccess
FileAccess implementations that work with archive-like storage
structures (ZIP files, in-memory archives, cloud storage buckets) where root path detection and
path normalization are needed.
This class provides common functionality for:
- Automatic detection of root directories within archive structures
- Path conversion between storage paths and relative paths
- Consistent handling of single-root vs multi-root archives
Usage Pattern: Subclasses should:
- Call the constructor after loading file metadata
- Implement
getStorageFilePaths()to provide file paths from storage - Use
stripRootPath(String)when returning paths to callers - Use
FileAccess.fullPath(String)when accessing storage with caller-provided paths
Root Path Detection: This class automatically detects if all files in the archive share a common root directory. For example, if a ZIP contains:
module-v1.0/imsmanifest.xml module-v1.0/content/page1.html module-v1.0/resources/style.cssThe root path will be detected as "module-v1.0", and
stripRootPath(String) will
convert storage paths to relative paths like "imsmanifest.xml", "content/page1.html", etc.- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs an AbstractArchiveFileAccess. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckForRootPath(Set<String> topLevelDirs, String entryName) Checks if a file entry represents a root-level directory or if multiple top-level directories are present in the provided set.voidclose()Default implementation of close for archive-based implementations.protected StringDetects the common root directory by analyzing file paths from storage.Gets the detected root path within the archive.Template method that subclasses must implement to provide all file paths from storage.protected final voidInitializes the root path by detecting it from storage file paths.protected StringnormalizeDirectory(String dir) Normalizes a directory path by ensuring it ends with a slash.protected StringstripRootPath(String storagePath) Strips the root path prefix from an internal storage path to create a relative path.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.jcputney.elearning.parser.api.FileAccess
clearCaches, fileExists, fileExistsBatch, fileExistsInternal, fullPath, getAllFiles, getFileContents, getFileContentsInternal, getTotalSize, listFiles, listFilesInternal, prefetchCommonFiles
-
Field Details
-
rootPath
The detected root path within the archive. Empty string if files are at the root level or if multiple top-level directories exist.
-
-
Constructor Details
-
AbstractArchiveFileAccess
protected AbstractArchiveFileAccess()Constructs an AbstractArchiveFileAccess. Subclasses must callinitializeRootPath()after loading file metadata.
-
-
Method Details
-
getRootPath
Gets the detected root path within the archive.- Specified by:
getRootPathin interfaceFileAccess- Returns:
- The root path, or an empty string if files are at the root level.
-
close
Default implementation of close for archive-based implementations. Subclasses that manage external resources (like file handles) should override this.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- if an error occurs while closing resources
-
initializeRootPath
protected final void initializeRootPath()Initializes the root path by detecting it from storage file paths. Subclasses must call this method after loading their file metadata. -
getStorageFilePaths
Template method that subclasses must implement to provide all file paths from storage. These paths should be in their original storage format (e.g., with root prefix if present).This method is called during construction to detect the root path.
- Returns:
- An iterable of all file paths in their storage format
-
detectRootPath
Detects the common root directory by analyzing file paths from storage.This method determines if all files in the archive are contained within a single top-level directory. If so, it returns that directory name as the root path. Otherwise, it returns an empty string, indicating files are at the root level or span multiple top-level directories.
Detection Algorithm:
- Extract the first path component from each file path
- If all files share the same first component, that becomes the root path
- If files have different first components, or are at the root level, return empty string
- Returns:
- The detected root directory name, or empty string if no common root exists
-
normalizeDirectory
Normalizes a directory path by ensuring it ends with a slash.- Parameters:
dir- The directory path to normalize.- Returns:
- The normalized directory path ending with "/".
-
checkForRootPath
Checks if a file entry represents a root-level directory or if multiple top-level directories are present in the provided set.- Parameters:
topLevelDirs- A set of strings representing the top-level directories encountered so far. This set is modified by the method to add new entries if applicable.entryName- The name of the file entry to evaluate, potentially containing a directory path.- Returns:
- True if the file entry is at the root level or if more than one top-level directory is present; false otherwise.
-
stripRootPath
Strips the root path prefix from an internal storage path to create a relative path.This is the inverse operation of
FileAccess.fullPath(String).Example: If rootPath="module-root" and storagePath="module-root/page.html", this method returns "page.html".
- Parameters:
storagePath- The absolute storage path to convert to a relative path.- Returns:
- The path relative to rootPath, or the original path if no root prefix exists.
- Throws:
IllegalArgumentException- if storagePath is null
-