@ParametersAreNonnullByDefault public abstract class PathElementsFactory extends Object
PathElements
instances
This class is in charge of all the heavy PathElements
operations:
creating them from input strings, but also resolving, relativizing and
normalizing them. An instance of (an implementation of) this class is passed
to all GenericPath
instances.
Implementations have to override the necessary methods to extract the root
components and name elements from a string, but also telling whether a name
element is valid at all, or represents the current or parent directory (in
typical filesystems, those would be testing that the name is either of "."
or ".."
).
This package provides an implementation for Unix paths.
Important note: this class adheres as closely as possible
to the Path
contract with regards to resolution and relativization;
this means that for both of these operations, normalization
is not performed. You will therefore have to ensure that paths you
submit to these methods are normalized.
Modifier and Type | Field and Description |
---|---|
protected static String[] |
NO_NAMES |
protected String |
parentToken |
Modifier | Constructor and Description |
---|---|
protected |
PathElementsFactory(String rootSeparator,
String separator,
String parentToken)
Constructor
|
Modifier and Type | Method and Description |
---|---|
abstract PathElements |
getRootPathElements() |
String |
getSeparator() |
protected abstract boolean |
isAbsolute(PathElements pathElements)
Check whether a
PathElements represents an absolute path |
protected abstract boolean |
isParent(String name)
Check whether a name element represents the parent directory
|
protected abstract boolean |
isSelf(String name)
Check whether a name element represents the current directory
|
protected abstract boolean |
isValidName(String name)
Check whether a name element is valid
|
protected PathElements |
normalize(PathElements elements)
Normalize a
PathElements |
protected PathElements |
relativize(PathElements first,
PathElements second)
Relativize a path against another
|
protected PathElements |
resolve(PathElements first,
PathElements second)
Resolve a
PathElements against another |
protected PathElements |
resolveSibling(PathElements first,
PathElements second)
Resolve a
PathElements 's parent against another |
protected abstract String[] |
rootAndNames(String path)
Split an input path into the root component and all name elements
|
protected abstract String[] |
splitNames(String names)
Split a names-only input into individual name elements
|
PathElements |
toPathElements(String path)
Convert an input string into a
PathElements |
protected String |
toString(PathElements elements)
Return a string representation of a
PathElements instance |
protected String |
toUriPath(String prefix,
PathElements elements)
Make a valid, raw URI path from a path's elements and a path prefix
|
protected static final String[] NO_NAMES
protected final String parentToken
protected PathElementsFactory(String rootSeparator, String separator, String parentToken)
rootSeparator
- the separator to insert between the root component,
if any, and the first name element, if anyseparator
- the separator to insert between two name elementsparentToken
- a canonical path token to represent the parent of the
current pathpublic final String getSeparator()
protected abstract String[] rootAndNames(String path)
This method returns a two-element string array, where the first element is the root component and the second element is a string with all name elements and any trailing characters (if any) removed.
If the path has no root
, the first element of
the returned array must be null
.
path
- the pathprotected abstract String[] splitNames(String names)
The input is guaranteed to be well-formed (no root component, no root separator and no trailing characters). The name elements must be in their order of appearance in the input.
names
- the input stringprotected abstract boolean isValidName(String name)
name
- the name to checkprotected abstract boolean isSelf(String name)
name
- the name to checknormalize(PathElements)
protected abstract boolean isParent(String name)
name
- the name to checknormalize(PathElements)
protected abstract boolean isAbsolute(PathElements pathElements)
PathElements
represents an absolute pathpathElements
- the instance to checkPath.isAbsolute()
public abstract PathElements getRootPathElements()
@Nonnull public final PathElements toPathElements(String path)
PathElements
path
- the string to convertPathElements
instanceInvalidPathException
- one name element is invalidrootAndNames(String)
,
isValidName(String)
@Nonnull protected final PathElements normalize(PathElements elements)
PathElements
elements
- the instance to normalizeisSelf(String)
,
isParent(String)
,
Path.normalize()
@Nonnull protected final PathElements resolve(PathElements first, PathElements second)
PathElements
against another
This method replicates the contract of Path.resolve(Path)
. In
this method, the first
argument is the instance performing the
resolution and the second
argument is the method's argument.
absolute
, it is returned;NOTES:
UnsupportedOperationException
.first
- the path performing the operationsecond
- the path on which the operation is performedPath.resolve(Path)
@Nonnull protected final PathElements resolveSibling(PathElements first, PathElements second)
PathElements
's parent against another
This method replicates the contract of Path.resolveSibling(Path)
. In this method, the first
argument is
the instance performing the resolution and the second
argument is
the method's argument.
The rules are the same as the equivalent Path
method:
parent
is returned, or the empty path
if parent is null.first
- the path performing the operationsecond
- the path on which the operation is performed@Nonnull protected final PathElements relativize(PathElements first, PathElements second)
This mirrors the behaviour of Path.relativize(Path)
; in
this method, first
is the path performing the operation and
second
is the method's argument.
NOTE: in the event that both path elements have root components
and they are not equal, an IllegalArgumentException
is thrown
unconditionally.
first
- the path performing the operationsecond
- the path on which the operation is performedPath.relativize(Path)
@Nonnull protected final String toString(PathElements elements)
PathElements
instance
PathElements
does not define .toString()
, as the
string representation of a path is highly system dependent.
elements
- the instance@Nonnull protected final String toUriPath(@Nullable String prefix, PathElements elements)
The validity of the path prefix is NOT checked, it is supposed to be valid. Note that it must be "raw", that is not URI-encoded.
The elements must represent an absolute path according to that factory.
This method will perform normalization on the path elements before
returning the result. If any parent tokens are found at the head of the
name elements, they are removed. Name tokens are then appended and joined
with a slash (/
) character, as required by RFC 3986.
Trailing slashes, if any, are removed.
prefix
- the URI path prefix (may be null)elements
- the path elementsURI