Class URIs
- java.lang.Object
-
- com.globalmentor.net.URIs
-
public class URIs extends java.lang.Object
Various URI manipulating functions for working with URIs as defined in RFC 3986, "Uniform Resource Identifiers (URI): Generic Syntax".For file URIs Java incorrectly uses the form
file:/mnt/sdcard/...
instead offile:///mnt/sdcard/...
, but these utilities use the former for consistency.
-
-
Field Summary
Fields Modifier and Type Field Description static Characters
ALPHA_CHARACTERS
Alphabetic characters as defined by RFC 2396.static java.lang.String
AUTHORITY_PREFIX
The prefix string that introduces an authority.static java.lang.String
CURRENT_LEVEL_PATH
The collection path "./" representing the current hierarchical level of a hierarchical URI.static java.lang.String
CURRENT_LEVEL_PATH_SEGMENT
The path segment "." representing the current hierarchical level of a hierarchical URI.static java.net.URI
CURRENT_LEVEL_PATH_URI
The URI collection path of "./" representing the current hierarchical level of a hierarchical URI.static Characters
DIGIT_CHARACTERS
Digit characters as defined by RFC 2396.static java.net.URI
EMPTY_PATH_URI
A shared URI constant equal to creating a URI from an empty path.static char
ESCAPE_CHAR
The character to use for escaping URI data as defined by RFC 2396.static Characters
EXTRA_CHARACTERS
Extra characters as defined by RFC 2396.static java.lang.String
FILE_SCHEME
The file scheme identifier.static java.lang.String
FILE_URI_PATH_ROOT_PREFIX
The sequence "//" which is supposed to be present in file URIs (e.g.static char
FRAGMENT_SEPARATOR
The pound character ('#') that separates a fragment from the rest of a URI.static java.lang.String
FTP_SCHEME
The FTP scheme identifier.static Characters
GEN_DELIM_CHARACTERS
General delimiter characters defined by RFC 3986.static java.lang.String
INFO_SCHEME
The info scheme identifier.static char
INFO_SCHEME_NAMESPACE_DELIMITER
The delimiter separating the info scheme namespace from the rest of the info scheme-specific part.static java.lang.String
LOCALHOST_DOMAIN
The domain "localhost".static java.lang.String
MAILTO_SCHEME
The email address scheme identifier.static char
MAILTO_USERNAME_DOMAIN_SEPARATOR
The character separating amailto
URI username from the domain.static int
MICROSOFT_INTERNET_EXPLORER_MAXIMUM_URI_LENGTH
The maximum URL length allowed by Microsoft Internet Explorer for HTTP GET.static java.net.URI[]
NO_URIS
The shared static empty array of URIs.static Characters
NORMAL_CHARACTERS
Characters that can appear in a URI path with no escape sequences.static java.lang.String
PARENT_LEVEL_PATH
The collection path "../" representing the parent hierarchical level of a hierarchical URI.static java.lang.String
PARENT_LEVEL_PATH_SEGMENT
The path segment ".." representing the parent hierarchical level of a hierarchical URI.static java.net.URI
PARENT_LEVEL_PATH_URI
The URI collection path "../" representing the parent hierarchical level of a hierarchical URI.static Characters
PATH_CHARACTERS
Path characters defined by RFC 3986.static java.lang.String
PATH_SCHEME
The path scheme identifier for representing relative and absolute URI paths.static Characters
PATH_SEGMENT_CHARACTERS
Path segment characters defined by RFC 3986.static char
PATH_SEPARATOR
The slash character ('/'
) that separates components in a URI path.static Characters
PLAIN_ENCODE_CHARACTERS
The characters that are allowed in a URI plain encoding.protected static Characters
PLAIN_ENCODE_INITIAL_UNRESERVED_CHARACTERS
The characters that, at least initially, should not be encoded.static char
PLAIN_ENCODING_ESCAPE_CHAR
The character used for escaping characters in a URI plain encoding.static char
PLAIN_ENCODING_REPLACE_CHAR
The character used for replacing certain characters in a URI plain encoding.static char
PORT_SEPARATOR
The colon character (':'
) that separates a host from a port.static char
QUERY_NAME_VALUE_ASSIGNMENT
The character that represents assigning a value to a name in a query.static char
QUERY_NAME_VALUE_PAIR_DELIMITER
The character that separates each name-value pair in a query.static char
QUERY_SEPARATOR
The character that separates the query from the rest of a URI.static Characters
RESERVED_CHARACTERS
Reserved characters as defined by RFC 2396.static java.lang.String
RESOURCE_SCHEME
The resource scheme identifier "resource".static java.lang.String
ROOT_PATH
The path to root, consisting of a single path separator ("/").static java.lang.String
ROOT_PATH_PARENT_LEVEL
The path Java returns when it tries to resolve..
to the root path.static java.net.URI
ROOT_PATH_URI
A URI consisting only of a single path separator ("/").static Characters
SAFE_CHARACTERS
Safe characters as defined by RFC 2396.static char
SCHEME_SEPARATOR
The colon character (':') that separates a URI schema from the rest of the URI.static Characters
SUB_DELIM_CHARACTERS
Subdelimiter characters defined by RFC 3986.static java.lang.String
TEL_SCHEME
The telephone scheme identifier.static Characters
UNRESERVED_CHARACTERS
Unreserved characters defined by RFC 3986.static Characters
URI_CHARACTERS
Characters that can appear in a URI as defined by RFC 2396.static java.lang.String
URN_SCHEME
The URN scheme identifier "urn".static java.util.regex.Pattern
URN_SSP_PATTERN
The pattern to match the scheme-specific part of a URN.static int
URN_SSP_PATTERN_NID_MATCHING_GROUP
The matching group to retrieve the URN namespace identifier.static int
URN_SSP_PATTERN_NSS_MATCHING_GROUP
The matching group to retrieve the URN-namespace-specific part.static char
USER_INFO_SEPARATOR
The at sign ('@'
) that separates user information from a host in a URI.static java.lang.String
WINDOWS_UNC_PATH_URI_SSP_PREFIX
The prefix used in the scheme-specific part by Java for Windows UNC paths in file URIs.
-
Constructor Summary
Constructors Constructor Description URIs()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.net.URI
addRawNameExtension(java.net.URI uri, java.lang.String extension)
Adds the given extension to a URI name and returns the new URI with the new extension.static java.net.URI
appendQueryParameter(java.net.URI uri, java.lang.String paramName, java.lang.String paramValue)
Constructs a query string for a URI with a single name/value parameter, and appends it to the query of the given URI, if any.static java.lang.String
appendQueryParameters(java.lang.String query, URIQueryParameter... params)
Constructs a query string for a URI and appends it to the given query, if any.static java.net.URI
appendQueryParameters(java.net.URI uri, URIQueryParameter... params)
Constructs a query string for a URI and appends it to the query of the given URI, if any.static java.net.URI
appendRawQuery(java.net.URI uri, java.lang.String rawQuery)
Appends a query string to a URI.static java.net.URI
canonicalize(java.net.URI uri)
Ensures that the given URI is in canonical form.static java.net.URI
changeBase(java.net.URI uri, java.net.URI oldBaseURI, java.net.URI newBaseURI)
Changes a URI from one base to another.static java.net.URI
changeHost(java.net.URI uri, java.lang.String newHost)
Creates a new URI identical to the supplied URI with a different host.static java.lang.String
changeName(java.lang.String path, java.lang.String name)
Changes the name of the path of the given URI to the given name.static java.net.URI
changeName(java.net.URI uri, java.lang.String name)
Changes the name of the path of the given URI to the given name.static java.net.URI
changePath(java.net.URI uri, URIPath path)
Creates a new URI identical to the supplied URI with a different path.static java.net.URI
changeRawName(java.net.URI uri, java.lang.String rawName)
Changes the raw name of the path of the given URI to the given raw name.static java.net.URI
changeRawNameExtension(java.net.URI uri, java.lang.String extension)
Changes the extension of a URI name and returns a new URI with the new name extension.static java.net.URI
changeRawPath(java.net.URI uri, java.lang.String newRawPath)
Creates a new URI identical to the supplied URI with a different raw path.static java.net.URI
changeRawSchemeSpecificPart(java.net.URI uri, java.lang.String newRawSSP)
Creates a new URI identical to the supplied URI with a different raw scheme-specific part.static java.net.URI
changeScheme(java.net.URI uri, java.lang.String newScheme)
Creates a new URI identical to the supplied URI with a different scheme.static java.net.URI
checkAbsolute(java.net.URI uri)
Checks to see if a given URI is absolute.static java.lang.String
checkCollectionPath(java.lang.String path)
Checks to see if a given path represents a canonical collection, that is, it has a path that ends with a slash ('/').static java.net.URI
checkCollectionURI(java.net.URI uri)
Checks to see if a given URI represents a canonical collection, that is, it has a path that ends with a slash ('/').static java.net.URI
checkInfoNamespace(java.net.URI uri, java.lang.String infoNamespace)
Verifies that the given URI is an "info" scheme URI with the given namespace.static java.lang.String
checkNotCollectionPath(java.lang.String path)
Checks to see if a given path does not represents a canonical collection, that is, it does not have a path that ends with a slash ('/').static java.net.URI
checkNotCollectionURI(java.net.URI uri)
Checks to see if a given URI does not represents a canonical collection, that is, it does not have a path that ends with a slash ('/').static java.lang.String
checkPath(java.lang.String path)
Checks to see if a given path is only a path and not a URI with a scheme and/or authority.static java.net.URI
checkPathURI(java.net.URI uri)
Checks to see if a given URI is only a path and not a URI with a scheme, authority, query, and/or fragment.static java.net.URI
checkPlainURI(java.net.URI uri)
Checks to see if a given URI is plain, i.e.static java.lang.String
checkRelativePath(java.lang.String path)
Checks to see if a given path is only a relative path and not a URI with a scheme and/or authority.static java.net.URI
checkRoot(java.net.URI uri)
Checks to see if a given URI has the root path.static java.net.URI
checkScheme(java.net.URI uri, java.lang.String scheme)
Verifies that the given URI has the indicated scheme.static java.lang.String
compress(java.net.URI uri)
Compresses a URI into a shorter string representation.static java.lang.String
constructPath(boolean absolute, boolean collection, java.lang.String... pathElements)
Constructs an absolute path from the given elements in the form:/element1/element2
.static java.lang.String
constructQuery(URIQueryParameter... params)
Constructs a query string for a URI by URI-encoding each name-value pair, separating them with '&', and prepending the entire string (if there is at least one parameter) with '?', if there are no parameters, it doesn't do anything.static java.lang.String
constructQuery(java.lang.String params)
Deprecated.static java.lang.String
constructQueryParameters(URIQueryParameter... params)
Constructs a query string for a URI by URI-encoding each name-value pair, separating them with '&'.static java.net.URI
createInfoURI(java.lang.String namespace, java.lang.String rawIdentifier)
Creates an "info" URI with the given info namespace and identifier with no fragment.static java.net.URI
createInfoURI(java.lang.String namespace, java.lang.String rawIdentifier, java.lang.String rawFragment)
Creates an "info" URI with the given info namespace, identifier, and optional fragment.static java.net.URI
createMailtoURI(java.lang.String username, java.lang.String domain)
Creates a "mailto" URI in the formmailto:username@domain
.static java.net.URI
createPathURI(java.lang.String path)
Creates a URI from the given path, verifying that the string contains only a path.static java.net.URI
createURI(java.lang.Object contextObject, java.lang.String string)
Deprecated.because this ancient code doesn't have an obvious utility, is confusing, and jumbles various types, many of them legacy.static java.net.URI
createURI(java.lang.String scheme, java.lang.String rawSchemeSpecificPart)
Returns a URI constructed from the given parts, any of which can benull
.static java.net.URI
createURI(java.lang.String scheme, java.lang.String rawSchemeSpecificPart, java.lang.String rawFragment)
Returns a URI constructed from the given parts, any of which can benull
.static java.net.URI
createURI(java.lang.String scheme, java.lang.String rawUserInfo, java.lang.String host, int port, URIPath path, java.lang.String rawQuery, java.lang.String rawFragment)
Returns a URI constructed from the given parts, any of which can benull
.static java.net.URI
createURI(java.lang.String scheme, java.lang.String rawUserInfo, java.lang.String host, int port, java.lang.String rawPath, java.lang.String rawQuery, java.lang.String rawFragment)
Returns a URI constructed from the given parts, any of which can benull
.static java.lang.String
createURIList(java.net.URI... uris)
Creates a string of typetext/uri-list
as defined in RFC 2483, "URI Resolution Services Necessary for URN Resolution".static java.net.URI
createURN(java.lang.String nid, java.lang.String nss)
Creates a URN in the formurn:nid:nss
.static java.lang.String
decode(java.lang.CharSequence uri)
static java.lang.String
decode(java.lang.CharSequence uri, char escapeChar)
Decodes the escaped ('%') characters in the character iterator according to the URI encoding rules in RFC 2396, "Uniform Resource Identifiers (URI): Generic Syntax".static java.net.URI
decompress(java.lang.String string)
Decompresses a URI from a shorter string representation.static java.lang.String
encode(java.lang.String string)
static java.lang.String
encode(java.lang.String string, char escapeChar)
Encodes all URI reserved characters in the string according to the URI encoding rules in RFC 3986, "Uniform Resource Identifiers (URI): Generic Syntax".static java.lang.String
encode(java.net.URI uri)
static java.lang.String
encode(java.net.URI uri, char escapeChar)
Encodes all URI reserved characters in the URI according to the URI encoding rules in RFC 3986, "Uniform Resource Identifiers (URI): Generic Syntax".static java.util.Optional<java.net.URI>
findRelativeChildPath(java.net.URI parentURI, java.net.URI childURI)
Returns a URI relative to the given parent URI.static java.util.Optional<java.net.URI>
findRelativePath(java.net.URI sourceURI, java.net.URI targetURI)
Returns the path of a target URI relative to some source URI, which may be a sibling URI or even a child URI.static ContentType
getContentType(java.net.URI uri)
Returns the content type for the specified URI based on its name extension.static java.net.URI
getCurrentLevel(java.net.URI uri)
Determines the current level of a hierarchical URI.static java.net.URL
getDirectoryURL(java.net.URL url)
Returns a URL representing the directory of the given file URL.static Host
getHost(java.net.URI uri)
Returns the unencoded host and optional port of the given URI.static java.lang.String
getInfoIdentifier(java.net.URI uri)
Determines the info identifier of the given "info" scheme URI.static java.lang.String
getInfoNamespace(java.net.URI uri)
Determines the info namespace of the given "info" scheme URI.static java.lang.String
getInfoRawIdentifier(java.net.URI uri)
Determines the raw, encoded info identifier of the given "info" scheme URI.static java.lang.String
getName(java.lang.String path)
Returns the name of the resource at the given path, which will be the name of the last path component.static java.lang.String
getName(java.net.URI uri)
Returns the decoded name of the resource at the given URI's path, which will be the decoded name of the last path component.static java.lang.String
getNameExtension(java.net.URI uri)
Extracts the extension from a URI's name.static CollectionMap<java.lang.String,java.lang.String,java.util.List<java.lang.String>>
getParameterMap(java.net.URI uri)
Retrieves the parameters from the query of a URI, if present.static NameValuePair<java.lang.String,java.lang.String>[]
getParameters(java.lang.String query)
Retrieves the parameters from a URI query.static NameValuePair<java.lang.String,java.lang.String>[]
getParameters(java.net.URI uri)
Retrieves the query parameters from a URI.static java.net.URI
getParentLevel(java.net.URI uri)
Determines the parent level of a hierarchical URI.static java.net.URI
getParentURI(java.net.URI uri)
Determines the parent collection of a hierarchical URI.static URIPath
getPath(java.net.URI uri)
Returns a path object to represent the path of the URI.static java.lang.String
getPathRawPath(java.net.URI uri)
Determines the raw, encoded path of the given "path" scheme URI.static URIPath
getPathURIPath(java.net.URI uri)
static java.net.URI
getPlainURI(java.net.URI uri)
Creates a new URI identical to the supplied URI with no query or fragment.static java.lang.String
getRawName(java.net.URI uri)
Returns the raw name of the resource at the given URI's path, which will be the raw name of the last path component.static java.lang.String
getRawNameExtension(java.net.URI uri)
Extracts the raw, encoded extension from a URI's name.static java.lang.String
getRawPathQueryFragment(java.net.URI uri)
Returns the unencoded path, optional unencoded query, and optional unencoded fragment of the given URI.static java.lang.String
getRelativePath(java.lang.String absolutePath)
Determines the relative path of the given absolute path by removing the root path '/' character from the beginning of the path.static java.net.URI
getRootURI(java.net.URI uri)
Determines the canonical root URI of a URI.static java.net.URI
guessAbsoluteURI(java.lang.String string)
Creates an absolute URI from the given string, guessing what the string represents.static boolean
hasAbsolutePath(java.net.URI uri)
Determines whether the path of the URI (which may or may not be absolute) is absolute.static boolean
hasPath(java.net.URI uri)
Determines whether the URI has a path.static boolean
isChild(java.net.URI baseURI, java.net.URI uri)
Determines whether the given URI is a child relative to the given base URI.static boolean
isCollectionPath(java.lang.String path)
Determines whether the given path is a canonical collection path.static boolean
isCollectionURI(java.net.URI uri)
Determines whether the URI represents a canonical collection, that is, it has a path that ends with a slash ('/').static boolean
isHost(java.net.URI uri)
Determines whether the URI contains only a host and optional port.static boolean
isInfoNamespace(java.net.URI uri, java.lang.String infoNamespace)
Determines whether the given URI is an "info" scheme URI with the given namespace.static boolean
isPath(java.lang.String path)
Determines if a given path is only a path and not a URI with a scheme and/or authority.static boolean
isPathAbsolute(java.lang.String path)
Determines whether the given path is absolute.static boolean
isPathURI(java.net.URI uri)
Determines if a given URI contains only a path and does not have a scheme, authority, query, and/or fragment.static boolean
isPlainURI(java.net.URI uri)
Determines if a given URI is plain, i.e.static boolean
isUNCFileURI(java.net.URI uri)
Determines whether the given URI is a UNC file path URI in the formfile:////server/file.ext
.static java.net.URI
normalize(java.net.URI uri)
Normalizes a URI.static java.lang.String
normalizePath(java.lang.String path)
Normalizes the given path by resolving the '.' and '..' path segments.static java.net.URI
plainDecode(java.lang.CharSequence charSequence)
Decodes a plain-encoded URI.static java.lang.String
plainEncode(java.net.URI uri)
Encodes an absolute URI into a plain string that is safe to be used in the path of another URI, for example.static java.net.URI
relativizeChildPath(java.net.URI parentURI, java.net.URI childURI)
Returns a URI relative to the given parent URI.static java.lang.String
relativizePath(java.lang.String basePath, java.lang.String fullPath)
Deprecated.static java.net.URI
relativizePath(java.net.URI sourceURI, java.net.URI targetURI)
Returns the path of a target URI relative to some source URI, which may be a sibling URI or even a child URI.static java.net.URI
removeFragment(java.net.URI uri)
Returns a URI with its fragment, if any, removed.static java.net.URI
removeRawNameExtension(java.net.URI uri)
Removes the extension, if any, of a URI name and returns a new URI with no extension.static java.net.URI
replaceRawFragment(java.net.URI uri, java.lang.String newRawFragment)
Returns a URI with its fragment, if any, replaced.static java.net.URI
resolve(java.net.URI baseURI, URIPath path)
Resolves a URI path against a base URI.static java.net.URI
resolve(java.net.URI baseURI, java.lang.String childURI)
Resolves a string against a base URI with added functionality and bug fixes overURI.resolve(String)
.static java.net.URI
resolve(java.net.URI baseURI, java.net.URI childURI)
Resolves a relative URI against a base URI with added functionality and bug fixes overURI.resolve(URI)
.static java.net.URI
resolve(java.net.URI baseURI, java.net.URI childURI, boolean deep)
Resolves a relative URI against a base URI with added functionality and bug fixes overURI.resolve(URI)
.static java.net.URI
resolveFragment(java.net.URI uri, java.lang.String fragment)
Returns a URI constructed from a given URI and a fragment identifier.static java.net.URI
resolveRawFragment(java.net.URI uri, java.lang.String rawFragment)
Returns a URI constructed from a given URI and a raw fragment identifier.static java.net.URI
setRawNameExtension(java.net.URI uri, java.lang.String extension)
Adds the extension, if any, to a name and returns the new URI.static java.net.URI
toCollectionURI(java.net.URI uri)
Forces a URI to represent a collection by appending a trailing path separator to the URI path, if any.static java.net.URL
toValidURL(java.net.URI uri)
Creates a URL from a URI.
-
-
-
Field Detail
-
NO_URIS
public static final java.net.URI[] NO_URIS
The shared static empty array of URIs.
-
EMPTY_PATH_URI
public static final java.net.URI EMPTY_PATH_URI
A shared URI constant equal to creating a URI from an empty path.- API Note:
- This constant is useful for detecting a URI relativized against itself, for example.
-
FILE_SCHEME
public static final java.lang.String FILE_SCHEME
The file scheme identifier.- See Also:
- Constant Field Values
-
FTP_SCHEME
public static final java.lang.String FTP_SCHEME
The FTP scheme identifier.- See Also:
- Constant Field Values
-
MAILTO_SCHEME
public static final java.lang.String MAILTO_SCHEME
The email address scheme identifier.- See Also:
- Constant Field Values
-
INFO_SCHEME
public static final java.lang.String INFO_SCHEME
The info scheme identifier.- See Also:
- Constant Field Values
-
INFO_SCHEME_NAMESPACE_DELIMITER
public static final char INFO_SCHEME_NAMESPACE_DELIMITER
The delimiter separating the info scheme namespace from the rest of the info scheme-specific part.- See Also:
- Constant Field Values
-
PATH_SCHEME
public static final java.lang.String PATH_SCHEME
The path scheme identifier for representing relative and absolute URI paths.- See Also:
- Constant Field Values
-
RESOURCE_SCHEME
public static final java.lang.String RESOURCE_SCHEME
The resource scheme identifier "resource".- See Also:
- Constant Field Values
-
TEL_SCHEME
public static final java.lang.String TEL_SCHEME
The telephone scheme identifier.- See Also:
- Constant Field Values
-
URN_SCHEME
public static final java.lang.String URN_SCHEME
The URN scheme identifier "urn".- See Also:
- Constant Field Values
-
SCHEME_SEPARATOR
public static final char SCHEME_SEPARATOR
The colon character (':') that separates a URI schema from the rest of the URI.- See Also:
- Constant Field Values
-
URN_SSP_PATTERN
public static final java.util.regex.Pattern URN_SSP_PATTERN
The pattern to match the scheme-specific part of a URN.This pattern is not currently meant to be a vigorous validation of URN format, but rather a means to easily discover the components of a URN.
-
URN_SSP_PATTERN_NID_MATCHING_GROUP
public static final int URN_SSP_PATTERN_NID_MATCHING_GROUP
The matching group to retrieve the URN namespace identifier.- See Also:
- Constant Field Values
-
URN_SSP_PATTERN_NSS_MATCHING_GROUP
public static final int URN_SSP_PATTERN_NSS_MATCHING_GROUP
The matching group to retrieve the URN-namespace-specific part.- See Also:
- Constant Field Values
-
AUTHORITY_PREFIX
public static final java.lang.String AUTHORITY_PREFIX
The prefix string that introduces an authority.- See Also:
- Constant Field Values
-
USER_INFO_SEPARATOR
public static final char USER_INFO_SEPARATOR
The at sign ('@'
) that separates user information from a host in a URI.- See Also:
- Constant Field Values
-
PORT_SEPARATOR
public static final char PORT_SEPARATOR
The colon character (':'
) that separates a host from a port.- See Also:
- Constant Field Values
-
PATH_SEPARATOR
public static final char PATH_SEPARATOR
The slash character ('/'
) that separates components in a URI path.- See Also:
- Constant Field Values
-
CURRENT_LEVEL_PATH_SEGMENT
public static final java.lang.String CURRENT_LEVEL_PATH_SEGMENT
The path segment "." representing the current hierarchical level of a hierarchical URI.- See Also:
- Constant Field Values
-
CURRENT_LEVEL_PATH
public static final java.lang.String CURRENT_LEVEL_PATH
The collection path "./" representing the current hierarchical level of a hierarchical URI.- See Also:
- Constant Field Values
-
CURRENT_LEVEL_PATH_URI
public static final java.net.URI CURRENT_LEVEL_PATH_URI
The URI collection path of "./" representing the current hierarchical level of a hierarchical URI.
-
PARENT_LEVEL_PATH_SEGMENT
public static final java.lang.String PARENT_LEVEL_PATH_SEGMENT
The path segment ".." representing the parent hierarchical level of a hierarchical URI.- See Also:
- Constant Field Values
-
PARENT_LEVEL_PATH
public static final java.lang.String PARENT_LEVEL_PATH
The collection path "../" representing the parent hierarchical level of a hierarchical URI.- See Also:
- Constant Field Values
-
PARENT_LEVEL_PATH_URI
public static final java.net.URI PARENT_LEVEL_PATH_URI
The URI collection path "../" representing the parent hierarchical level of a hierarchical URI.
-
QUERY_SEPARATOR
public static final char QUERY_SEPARATOR
The character that separates the query from the rest of a URI.- See Also:
- Constant Field Values
-
QUERY_NAME_VALUE_PAIR_DELIMITER
public static final char QUERY_NAME_VALUE_PAIR_DELIMITER
The character that separates each name-value pair in a query.- See Also:
- Constant Field Values
-
QUERY_NAME_VALUE_ASSIGNMENT
public static final char QUERY_NAME_VALUE_ASSIGNMENT
The character that represents assigning a value to a name in a query.- See Also:
- Constant Field Values
-
FRAGMENT_SEPARATOR
public static final char FRAGMENT_SEPARATOR
The pound character ('#') that separates a fragment from the rest of a URI.- See Also:
- Constant Field Values
-
LOCALHOST_DOMAIN
public static final java.lang.String LOCALHOST_DOMAIN
The domain "localhost".- See Also:
- Constant Field Values
-
ROOT_PATH
public static final java.lang.String ROOT_PATH
The path to root, consisting of a single path separator ("/").
-
ROOT_PATH_URI
public static final java.net.URI ROOT_PATH_URI
A URI consisting only of a single path separator ("/").
-
ROOT_PATH_PARENT_LEVEL
public static final java.lang.String ROOT_PATH_PARENT_LEVEL
The path Java returns when it tries to resolve..
to the root path.
-
MAILTO_USERNAME_DOMAIN_SEPARATOR
public static final char MAILTO_USERNAME_DOMAIN_SEPARATOR
The character separating amailto
URI username from the domain.- See Also:
- Constant Field Values
-
ALPHA_CHARACTERS
public static final Characters ALPHA_CHARACTERS
Alphabetic characters as defined by RFC 2396.
-
DIGIT_CHARACTERS
public static final Characters DIGIT_CHARACTERS
Digit characters as defined by RFC 2396.
-
SAFE_CHARACTERS
public static final Characters SAFE_CHARACTERS
Safe characters as defined by RFC 2396.
-
EXTRA_CHARACTERS
public static final Characters EXTRA_CHARACTERS
Extra characters as defined by RFC 2396.
-
ESCAPE_CHAR
public static final char ESCAPE_CHAR
The character to use for escaping URI data as defined by RFC 2396.- See Also:
- Constant Field Values
-
RESERVED_CHARACTERS
public static final Characters RESERVED_CHARACTERS
Reserved characters as defined by RFC 2396.
-
URI_CHARACTERS
public static final Characters URI_CHARACTERS
Characters that can appear in a URI as defined by RFC 2396.
-
NORMAL_CHARACTERS
public static final Characters NORMAL_CHARACTERS
Characters that can appear in a URI path with no escape sequences.
-
UNRESERVED_CHARACTERS
public static final Characters UNRESERVED_CHARACTERS
Unreserved characters defined by RFC 3986.
-
GEN_DELIM_CHARACTERS
public static final Characters GEN_DELIM_CHARACTERS
General delimiter characters defined by RFC 3986.
-
SUB_DELIM_CHARACTERS
public static final Characters SUB_DELIM_CHARACTERS
Subdelimiter characters defined by RFC 3986.
-
PATH_SEGMENT_CHARACTERS
public static final Characters PATH_SEGMENT_CHARACTERS
Path segment characters defined by RFC 3986.
-
PATH_CHARACTERS
public static final Characters PATH_CHARACTERS
Path characters defined by RFC 3986.
-
FILE_URI_PATH_ROOT_PREFIX
public static final java.lang.String FILE_URI_PATH_ROOT_PREFIX
The sequence "//" which is supposed to be present in file URIs (e.g.file:///mnt/sdcard/...
) but which isn't present in Java file URIs.- See Also:
- File URIs in Windows.
-
MICROSOFT_INTERNET_EXPLORER_MAXIMUM_URI_LENGTH
public static final int MICROSOFT_INTERNET_EXPLORER_MAXIMUM_URI_LENGTH
The maximum URL length allowed by Microsoft Internet Explorer for HTTP GET.
-
WINDOWS_UNC_PATH_URI_SSP_PREFIX
public static final java.lang.String WINDOWS_UNC_PATH_URI_SSP_PREFIX
The prefix used in the scheme-specific part by Java for Windows UNC paths in file URIs.
-
PLAIN_ENCODING_ESCAPE_CHAR
public static final char PLAIN_ENCODING_ESCAPE_CHAR
The character used for escaping characters in a URI plain encoding.- See Also:
plainEncode(URI)
, Constant Field Values
-
PLAIN_ENCODING_REPLACE_CHAR
public static final char PLAIN_ENCODING_REPLACE_CHAR
The character used for replacing certain characters in a URI plain encoding.- See Also:
plainEncode(URI)
, Constant Field Values
-
PLAIN_ENCODE_INITIAL_UNRESERVED_CHARACTERS
protected static final Characters PLAIN_ENCODE_INITIAL_UNRESERVED_CHARACTERS
The characters that, at least initially, should not be encoded. Path separators will be replaced with 45.
-
PLAIN_ENCODE_CHARACTERS
public static final Characters PLAIN_ENCODE_CHARACTERS
The characters that are allowed in a URI plain encoding.
-
-
Method Detail
-
createURIList
public static java.lang.String createURIList(java.net.URI... uris)
Creates a string of typetext/uri-list
as defined in RFC 2483, "URI Resolution Services Necessary for URN Resolution".- Parameters:
uris
- The URIs to include in the list.- Returns:
- A URI list string.
- See Also:
- RFC 2483: URI Resolution Services Necessary for URN Resolution
-
checkScheme
public static final java.net.URI checkScheme(java.net.URI uri, java.lang.String scheme)
Verifies that the given URI has the indicated scheme.- Parameters:
uri
- The URI to check.scheme
- The scheme to match for the URI.- Returns:
- The given URI.
- Throws:
java.lang.NullPointerException
- if the given URI and/or scheme isnull
.java.lang.IllegalArgumentException
- if the scheme of the given URI does not match the given scheme.
-
changeScheme
public static java.net.URI changeScheme(java.net.URI uri, java.lang.String newScheme)
Creates a new URI identical to the supplied URI with a different scheme.- Parameters:
uri
- The URI to change.newScheme
- The new scheme information.- Returns:
- A new URI with the new scheme information.
- Throws:
java.lang.NullPointerException
- if the given URI and/or new scheme isnull
.java.lang.IllegalArgumentException
- if the given URI has no scheme or if the given scheme results in an invalid URI.
-
checkInfoNamespace
public static final java.net.URI checkInfoNamespace(java.net.URI uri, java.lang.String infoNamespace)
Verifies that the given URI is an "info" scheme URI with the given namespace.- Parameters:
uri
- The URI to check.infoNamespace
- The info namespace to match for the URI.- Returns:
- The given URI.
- Throws:
java.lang.NullPointerException
- if the given URI and/or info namespace isnull
.java.lang.IllegalArgumentException
- if the scheme of the given URI is not "info" and/or the info namespace does not match the given info namespace.
-
getInfoNamespace
public static final java.lang.String getInfoNamespace(java.net.URI uri)
Determines the info namespace of the given "info" scheme URI.- Parameters:
uri
- The URI from which the info namespace should be retrieved.- Returns:
- The info namespace of the given info URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given URI is not a valid "info" scheme URI.
-
getInfoIdentifier
public static final java.lang.String getInfoIdentifier(java.net.URI uri)
Determines the info identifier of the given "info" scheme URI.- Parameters:
uri
- The URI from which the info identifier should be retrieved.- Returns:
- The decoded info identifier of the given info URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given URI is not a valid "info" scheme URI.
-
getInfoRawIdentifier
public static final java.lang.String getInfoRawIdentifier(java.net.URI uri)
Determines the raw, encoded info identifier of the given "info" scheme URI.- Parameters:
uri
- The URI from which the info identifier should be retrieved.- Returns:
- The raw, encoded info identifier of the given info URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given URI is not a valid "info" scheme URI.
-
isInfoNamespace
public static final boolean isInfoNamespace(java.net.URI uri, java.lang.String infoNamespace)
Determines whether the given URI is an "info" scheme URI with the given namespace.- Parameters:
uri
- The URI to check.infoNamespace
- The info namespace to match for the URI.- Returns:
- The
true
if the given URI has a scheme of "info" and has the indicated info namespace. - Throws:
java.lang.NullPointerException
- if the given URI and/or info namespace isnull
.
-
getPathRawPath
public static final java.lang.String getPathRawPath(java.net.URI uri)
Determines the raw, encoded path of the given "path" scheme URI. The path will never benull
; the empty relative pathpath:
will return the empty string. Any query or fragment is ignored. This method is needed because theURI.getRawPath()
method does not recognize relative paths for the "path" scheme.- Parameters:
uri
- The path URI from which the path should be retrieved.- Returns:
- The raw, encoded path of the given path URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given URI is not a valid "path" scheme URI.
-
getPathURIPath
public static final URIPath getPathURIPath(java.net.URI uri)
- Parameters:
uri
- The path URI from which the path should be retrieved.- Returns:
- A URI path object representing the path of the given path URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given URI is not a valid "path" scheme URI.
-
changePath
public static java.net.URI changePath(java.net.URI uri, URIPath path)
Creates a new URI identical to the supplied URI with a different path.- Parameters:
uri
- The URI to change.path
- The path, ornull
if there should be no path.- Returns:
- A new URI with the new path.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given path results in an invalid URI.
-
changeRawPath
public static java.net.URI changeRawPath(java.net.URI uri, java.lang.String newRawPath)
Creates a new URI identical to the supplied URI with a different raw path.- Parameters:
uri
- The URI to change.newRawPath
- The raw, escaped path, ornull
if there should be no path.- Returns:
- A new URI with the new raw path information.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given path results in an invalid URI.
-
changeHost
public static java.net.URI changeHost(java.net.URI uri, java.lang.String newHost)
Creates a new URI identical to the supplied URI with a different host.- Parameters:
uri
- The URI to change.newHost
- The new host information.- Returns:
- A new URI with the new host information.
- Throws:
java.lang.NullPointerException
- if the given URI and/or new host isnull
.java.lang.IllegalArgumentException
- if the given URI has no host or if the given host results in an invalid URI.
-
changeRawSchemeSpecificPart
public static java.net.URI changeRawSchemeSpecificPart(java.net.URI uri, java.lang.String newRawSSP)
Creates a new URI identical to the supplied URI with a different raw scheme-specific part.- Parameters:
uri
- The URI to change.newRawSSP
- The raw, escaped scheme-specific part.- Returns:
- A new URI with the new raw scheme-specific part information.
- Throws:
java.lang.NullPointerException
- if the given URI or the scheme-specific part isnull
.java.lang.IllegalArgumentException
- if the given scheme-specific part results in an invalid URI.
-
toCollectionURI
public static java.net.URI toCollectionURI(java.net.URI uri)
Forces a URI to represent a collection by appending a trailing path separator to the URI path, if any. If the URI has no path, no change is made.This method is most useful for working with file systems that are imprecise about distinguishing between collection and non-collection nodes.
- Parameters:
uri
- The URI to represent a collection.- Returns:
- A form of the URI representing a collection.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.
-
getPath
public static URIPath getPath(java.net.URI uri)
Returns a path object to represent the path of the URI.- Parameters:
uri
- The URI for which a path object should be returned.- Returns:
- An object representing the path, or
null
if the URI has no path.
-
getRawName
public static java.lang.String getRawName(java.net.URI uri)
Returns the raw name of the resource at the given URI's path, which will be the raw name of the last path component. If the path is a collection (i.e. it ends with slash), the component before the last slash will be returned. As examples, "/path/name.ext" and "name.ext" will return "name.ext". "/path/", "path/", and "path" will all return "path". This method correctly handles "info" URIs.- Parameters:
uri
- The URI the path of which will be examined.- Returns:
- The name of the last last path component, the empty string if the path is the empty string, "/" if the path is the root path, or
null
if the URI has no path. - Throws:
java.lang.NullPointerException
- if the given URI isnull
.
-
getName
public static java.lang.String getName(java.net.URI uri)
Returns the decoded name of the resource at the given URI's path, which will be the decoded name of the last path component. If the path is a collection (i.e. it ends with slash), the component before the last slash will be returned. As examples, "/path/name.ext" and "name.ext" will return "name.ext". "/path/", "path/", and "path" will all return "path". The path name is first extracted from the URI's raw path and then decoded so that encoded 47 characters will not prevent correct parsing. This method correctly handles "info" URIs.- Parameters:
uri
- The URI the path of which will be examined.- Returns:
- The name of the last path component, the empty string if the path is the empty string, "/" if the path is the root path, or
null
if the URI has no path. - Throws:
java.lang.NullPointerException
- if the given URI isnull
.
-
changeName
public static java.lang.String changeName(java.lang.String path, java.lang.String name)
Changes the name of the path of the given URI to the given name. If the path is a collection (i.e. it ends with slash), the name is the last component before the last slash. As examples, "/path/name.ext" and "name.ext" will change "name.ext". "/path/", "path/", and "path" will all change "path". "" with return "name" and "/" will return "/name/"- Parameters:
path
- The path, which should be encoded if 47 characters are present.name
- The new name of the path.- Returns:
- A new path with the name changed to the given name.
- Throws:
java.lang.NullPointerException
- if the given path and/or name isnull
.- See Also:
getName(String)
-
changeRawName
public static java.net.URI changeRawName(java.net.URI uri, java.lang.String rawName)
Changes the raw name of the path of the given URI to the given raw name. If the path is a collection (i.e. it ends with slash), the name is the last component before the last slash. As examples, "/path/name.ext" and "name.ext" will change "name.ext". "/path/", "path/", and "path" will all change "path". "" with return "name" and "/" will return "/name/" This method correctly handles "info" URIs.- Parameters:
uri
- The URI the raw name of which to change.rawName
- The new raw name of the URI.- Returns:
- A new URI with the raw name changed to the given raw name.
- Throws:
java.lang.NullPointerException
- if the given URI and/or name isnull
.java.lang.IllegalArgumentException
- if the given URI has no path, if the name is empty, or if the name is just a "/".- See Also:
getRawName(URI)
-
changeName
public static java.net.URI changeName(java.net.URI uri, java.lang.String name)
Changes the name of the path of the given URI to the given name. If the path is a collection (i.e. it ends with slash), the name is the last component before the last slash. As examples, "/path/name.ext" and "name.ext" will change "name.ext". "/path/", "path/", and "path" will all change "path". "" with return "name" and "/" will return "/name/" This method correctly handles "info" URIs.- Parameters:
uri
- The URI the name of which to change.name
- The new unencoded name of the URI, which will be encoded.- Returns:
- A new URI with the name changed to the given name.
- Throws:
java.lang.NullPointerException
- if the given URI and/or name isnull
.java.lang.IllegalArgumentException
- if the given URI has no path.- See Also:
URIPath.encodeSegment(String)
,getName(URI)
-
addRawNameExtension
public static java.net.URI addRawNameExtension(java.net.URI uri, java.lang.String extension)
Adds the given extension to a URI name and returns the new URI with the new extension. The URI name is not checked to see if it currently has an extension.- Parameters:
uri
- The URI the name of which an extension should be added.extension
- The raw, encoded extension to add.- Returns:
- The URI with the new extension.
- Throws:
java.lang.NullPointerException
- if the given extension isnull
.java.lang.IllegalArgumentException
- if the given URI has no path.
-
getNameExtension
public static java.lang.String getNameExtension(java.net.URI uri)
Extracts the extension from a URI's name. This this the preferred method for extracting an extension from a URI, as this method correctly parses the raw form of the URI path to find the extension before decoding.- Parameters:
uri
- The URI to examine.- Returns:
- The extension of the URI's name (not including '.'), or
null
if no extension is present. - See Also:
getName(URI)
-
getRawNameExtension
public static java.lang.String getRawNameExtension(java.net.URI uri)
Extracts the raw, encoded extension from a URI's name.- Parameters:
uri
- The URI to examine.- Returns:
- The raw, encoded extension of the URI's name (not including '.'), or
null
if no extension is present. - See Also:
getRawName(URI)
-
changeRawNameExtension
public static java.net.URI changeRawNameExtension(java.net.URI uri, java.lang.String extension)
Changes the extension of a URI name and returns a new URI with the new name extension. If the URI name does not currently have an extension, one will be added.- Parameters:
uri
- The URI to examine.extension
- The raw extension to set, ornull
if the extension should be removed.- Returns:
- The name with the new extension.
- Throws:
java.lang.IllegalArgumentException
- if the given URI has no path and a non-null
extension was given.
-
setRawNameExtension
public static java.net.URI setRawNameExtension(java.net.URI uri, java.lang.String extension)
Adds the extension, if any, to a name and returns the new URI. This is a convenience method that delegates toaddRawNameExtension(URI, String)
if a non-null
extension is given.- Parameters:
uri
- The URI to examine.extension
- The raw, encoded extension to add, ornull
if no extension should be added.- Returns:
- The name with the new extension, if any.
-
removeRawNameExtension
public static java.net.URI removeRawNameExtension(java.net.URI uri)
Removes the extension, if any, of a URI name and returns a new URI with no extension. This is a convenience method that delegates tochangeRawNameExtension(URI, String)
.- Parameters:
uri
- The URI to examine.- Returns:
- The URI with no extension.
-
getPlainURI
public static java.net.URI getPlainURI(java.net.URI uri)
Creates a new URI identical to the supplied URI with no query or fragment.- Parameters:
uri
- The URI from which to remove the query and fragment, if any.- Returns:
- A new URI with no query or fragment.
-
constructQuery
public static java.lang.String constructQuery(URIQueryParameter... params)
Constructs a query string for a URI by URI-encoding each name-value pair, separating them with '&', and prepending the entire string (if there is at least one parameter) with '?', if there are no parameters, it doesn't do anything.- Parameters:
params
- The name-value pairs representing the query parameters.- Returns:
- A string representing the constructed query, or the empty string if there were no parameters.
-
constructQuery
@Deprecated public static java.lang.String constructQuery(java.lang.String params)
Deprecated.Constructs a query string for a URI by prepending the given query string, if it is not the empty string, with '?'.- Parameters:
params
- The string representing the query parameters.- Returns:
- A string representing the constructed query, or the empty string if there were no parameters.
-
appendRawQuery
public static java.net.URI appendRawQuery(java.net.URI uri, java.lang.String rawQuery)
Appends a query string to a URI.- Parameters:
uri
- The existing URI.rawQuery
- The encoded query information, without a beginning query separator.- Returns:
- A URI representing the URI with the appended query parameters.
- Throws:
java.lang.NullPointerException
- if the given URI and/or query isnull
.
-
appendQueryParameter
public static java.net.URI appendQueryParameter(java.net.URI uri, java.lang.String paramName, java.lang.String paramValue)
Constructs a query string for a URI with a single name/value parameter, and appends it to the query of the given URI, if any.- Parameters:
uri
- The existing URI.paramName
- The name of the parameter.paramValue
- The value of the parameter.- Returns:
- A URI representing the URI with the appended query parameter.
- Throws:
java.lang.NullPointerException
- if the given URI, param name, and/or param value isnull
.
-
appendQueryParameters
public static java.net.URI appendQueryParameters(java.net.URI uri, URIQueryParameter... params)
Constructs a query string for a URI and appends it to the query of the given URI, if any.- Parameters:
uri
- The existing URI.params
- The name-value pairs representing the query parameters.- Returns:
- A URI representing the URI with the appended query parameters.
- Throws:
java.lang.NullPointerException
- if the given URI and/or params isnull
.
-
appendQueryParameters
public static java.lang.String appendQueryParameters(java.lang.String query, URIQueryParameter... params)
Constructs a query string for a URI and appends it to the given query, if any.- Parameters:
query
- The existing query parameters, ornull
or the empty string if there is no query.params
- The name-value pairs representing the query parameters.- Returns:
- A string representing the query with the appended parameters, or the empty string if there was no query and there were no parameters.
-
constructQueryParameters
public static java.lang.String constructQueryParameters(URIQueryParameter... params)
Constructs a query string for a URI by URI-encoding each name-value pair, separating them with '&'. Parameters are allowed to havenull
values, in which case no '=' delimiter will be used.- Parameters:
params
- The name-value pairs representing the query parameters.- Returns:
- A string representing the constructed query, or the empty string if there were no parameters.
-
getParameterMap
public static CollectionMap<java.lang.String,java.lang.String,java.util.List<java.lang.String>> getParameterMap(java.net.URI uri)
Retrieves the parameters from the query of a URI, if present.- Parameters:
uri
- The URI from which to extract parameters.- Returns:
- An array of parameters.
-
getParameters
public static NameValuePair<java.lang.String,java.lang.String>[] getParameters(java.net.URI uri)
Retrieves the query parameters from a URI.- Parameters:
uri
- The URI which may contain a query.- Returns:
- An array of parameters represented by the URI query, or
null
if the given URI does not contain a query.
-
getParameters
public static NameValuePair<java.lang.String,java.lang.String>[] getParameters(java.lang.String query)
Retrieves the parameters from a URI query. An empty string query will return an empty array of name/value pairs.- Parameters:
query
- The string containing URI query parameters (without the '?' prefix), ornull
.- Returns:
- An array of parameters represented by the query, or
null
if the given query isnull
.
-
createPathURI
public static java.net.URI createPathURI(java.lang.String path)
Creates a URI from the given path, verifying that the string contains only a path.- Parameters:
path
- The string version of a path to convert to a URI form of that same path.- Returns:
- The URI constructed.
- Throws:
java.lang.NullPointerException
- if the given path isnull
.java.lang.IllegalArgumentException
- if the provided path specifies a URI scheme (i.e. the URI is absolute) and/or authority.- See Also:
isPathURI(URI)
-
checkRoot
public static java.net.URI checkRoot(java.net.URI uri) throws java.lang.IllegalArgumentException
Checks to see if a given URI has the root path. If the given URI does not have the root path, i.e., if it's missing or if it's not composed only by the root path, an exception is thrown.- Parameters:
uri
- The URI to check to see if it has the root path.- Returns:
- The given root URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given URI does not have the root path.- See Also:
URI.getPath()
,ROOT_PATH
-
checkAbsolute
public static java.net.URI checkAbsolute(java.net.URI uri) throws java.lang.IllegalArgumentException
Checks to see if a given URI is absolute. If the given URI is not absolute, an exception is thrown.- Parameters:
uri
- The URI to check to see if it is absolute.- Returns:
- The given absolute URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given URI is not absolute.- See Also:
URI.isAbsolute()
-
isPathURI
public static boolean isPathURI(java.net.URI uri)
Determines if a given URI contains only a path and does not have a scheme, authority, query, and/or fragment.- Parameters:
uri
- The URI to check to for path status.- Returns:
true
if the URI has a path and does not specify a scheme (i.e. the URI is not absolute), authority, query, or fragment.- Throws:
java.lang.NullPointerException
- if the given URI isnull
.
-
checkPathURI
public static java.net.URI checkPathURI(java.net.URI uri)
Checks to see if a given URI is only a path and not a URI with a scheme, authority, query, and/or fragment. If the given URI is not a path, an exception is thrown.- Parameters:
uri
- The URI to check to for path status.- Returns:
- The given path URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the provided URI specifies a URI scheme (i.e. the URI is absolute), authority, query, and/or fragment.java.lang.IllegalArgumentException
- if the given URI is not a path.- See Also:
isPath(String)
-
isPlainURI
public static boolean isPlainURI(java.net.URI uri)
Determines if a given URI is plain, i.e. it does not contain a query or a fragment.- Parameters:
uri
- The URI to check to for plainness.- Returns:
true
if the URI has no query or fragment.- Throws:
java.lang.NullPointerException
- if the given URI isnull
.
-
checkPlainURI
public static java.net.URI checkPlainURI(java.net.URI uri)
Checks to see if a given URI is plain, i.e. it does not contain a query or a fragment. If the given URI is not a plain URI, an exception is thrown.- Parameters:
uri
- The URI to check to for plainness.- Returns:
- The given plain URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the provided URI specifies a query and/or fragment.- See Also:
isPlainURI(URI)
-
getCurrentLevel
public static java.net.URI getCurrentLevel(java.net.URI uri)
Determines the current level of a hierarchical URI. This is equivalent to resolving the path "." to the URI.- Parameters:
uri
- The URI to examine.- Returns:
- A URI representing the current hierarchical level of a hierarchical URI.
-
getParentLevel
public static java.net.URI getParentLevel(java.net.URI uri)
Determines the parent level of a hierarchical URI. This is equivalent to resolving the path ".." to the URI for all non-root paths.- Parameters:
uri
- The URI to examine.- Returns:
- A URI representing the parent hierarchical level of a hierarchical URI, or
null
if the given URI's path equates to the root path.
-
getParentURI
public static java.net.URI getParentURI(java.net.URI uri)
Determines the parent collection of a hierarchical URI.Non-normalized collection paths (e.g. ending with
/.
) are not supported.- Parameters:
uri
- The URI to examine.- Returns:
- A URI representing the parent collection of a hierarchical URI; if the URI ends in '/', equivalent to resolving the path ".." to the URI; if the
URI does not end in '/', equivalent to resolving the path "." to the URI; or
null
if the given URI's path equates to the root path. - Throws:
java.lang.IllegalArgumentException
- if the URI does not have a path component.- See Also:
isCollectionURI(URI)
,getParentLevel(URI)
,getCurrentLevel(URI)
-
getRootURI
public static java.net.URI getRootURI(java.net.URI uri)
Determines the canonical root URI of a URI.- Parameters:
uri
- The URI to examine.- Returns:
- A URI representing the URI with no path and no query or fragment.
-
getContentType
public static ContentType getContentType(java.net.URI uri)
Returns the content type for the specified URI based on its name extension.- Parameters:
uri
- The URI for which to return a content type.- Returns:
- The default content type for the URI's name extension, or
null
if no known content type is associated with this URI's extension. - See Also:
Files.getExtensionContentType(String)
,getRawName(URI)
,Filenames.getExtension(String)
-
relativizeChildPath
public static java.net.URI relativizeChildPath(java.net.URI parentURI, java.net.URI childURI)
Returns a URI relative to the given parent URI. A collection URI relativized against itself will return an empty URI.This method does not support backtracking, that is, creating a path to a parent or child path. For that functionality use
findRelativePath(URI, URI)
instead.- API Note:
- This method differs from
URI.relativize(URI)
in that the parent URI must be a collection URI (i.e. end with a slash) for a relative child path to be found. - Implementation Specification:
- This method delegates to
findRelativeChildPath(URI, URI)
- Parameters:
parentURI
- The URI to which the child URI is relative.childURI
- The URI that will be relativized against the parent URI.- Returns:
- The URI representing path of the child URI to the parent URI, or the child URI if the given child URI is not a child of the given parent URI.
- See Also:
normalize(URI)
-
findRelativeChildPath
public static java.util.Optional<java.net.URI> findRelativeChildPath(@Nonnull java.net.URI parentURI, @Nonnull java.net.URI childURI)
Returns a URI relative to the given parent URI. A collection URI relativized against itself will return an empty URI.This method does not support backtracking, that is, creating a path to a parent or child path. For that functionality use
findRelativePath(URI, URI)
instead.- API Note:
- This method differs from
URI.relativize(URI)
in that the parent URI must be a collection URI (i.e. end with a slash) for a relative child path to be found. - Implementation Specification:
- This method first normalizes both URIs.
- Parameters:
parentURI
- The URI to which the child URI is relative.childURI
- The URI that will be relativized against the parent URI.- Returns:
- The URI representing path of the child URI to the parent URI, which will not be present if the given child URI is not a child of the given parent URI.
- See Also:
normalize(URI)
-
relativizePath
public static java.net.URI relativizePath(@Nonnull java.net.URI sourceURI, @Nonnull java.net.URI targetURI)
Returns the path of a target URI relative to some source URI, which may be a sibling URI or even a child URI. A collection URI relativized against itself will return an empty URI. A non-collection URI relativized against its parent will return./
. Otherwise if the source URI is not a parent of (or the same URI as) the target URI, the path will backtrack using..
path segments as appropriate.- Implementation Specification:
- This method delegates to
findRelativePath(URI, URI)
- Implementation Note:
- This implementation properly relativizes URIs that require backtracking, such as siblings, unlike Java URI relativization methods; see
JDK-6226081., This method differs from
URI.relativize(URI)
, which would return an empty URI when relativizingfoo/bar
againstfoo/
. This method instead would return./
, compliant with browser relative resolution behavior and with RFC 3986, as discussed at Is Java's URI.resolve incompatible with RFC 3986 when the relative URI contains an empty path?. - Parameters:
sourceURI
- The URI to which the other URI will be relativized.targetURI
- The URI that will be relativized against the base URI.- Returns:
- The relative path of the source URI to the target URI, or the target URI if the two URIs have no base in common.
- See Also:
normalize(URI)
-
findRelativePath
public static java.util.Optional<java.net.URI> findRelativePath(@Nonnull java.net.URI sourceURI, @Nonnull java.net.URI targetURI)
Returns the path of a target URI relative to some source URI, which may be a sibling URI or even a child URI. A collection URI relativized against itself will return an empty URI. A non-collection URI relativized against its parent will return./
. Otherwise if the source URI is not a parent of (or the same URI as) the target URI, the path will backtrack using..
path segments as appropriate.- Implementation Specification:
- This method first normalizes both URIs.
- Implementation Note:
- This implementation properly relativizes URIs that require backtracking, such as siblings, unlike Java URI relativization methods; see
JDK-6226081., This method differs from
URI.relativize(URI)
, which would return an empty URI when relativizingfoo/bar
againstfoo/
. This method instead would return./
, compliant with browser relative resolution behavior and with RFC 3986, as discussed at Is Java's URI.resolve incompatible with RFC 3986 when the relative URI contains an empty path?. - Parameters:
sourceURI
- The URI to which the other URI will be relativized.targetURI
- The URI that will be relativized against the base URI.- Returns:
- The relative path of the source URI to the target URI, which will not be present if the two URIs have no base in common.
- Throws:
java.lang.IllegalArgumentException
- if the two URIs have no base URI in common.- See Also:
normalize(URI)
-
createURN
public static java.net.URI createURN(java.lang.String nid, java.lang.String nss)
Creates a URN in the formurn:nid:nss
.- Parameters:
nid
- The namespace identifier.nss
- The namespace-specific string.- Returns:
- A URN based upon the given parameters.
- Throws:
java.lang.IllegalArgumentException
- if the resulting string violates RFC 2396.- See Also:
- RFC 2141
-
createInfoURI
public static java.net.URI createInfoURI(java.lang.String namespace, java.lang.String rawIdentifier)
Creates an "info" URI with the given info namespace and identifier with no fragment.- Parameters:
namespace
- The info namespace.rawIdentifier
- The raw, encoded info identifier.- Returns:
- An info URI based upon the given parameters.
- Throws:
java.lang.NullPointerException
- if the given namespace and/or identifier isnull
.java.lang.IllegalArgumentException
- if the given namespace, and/or identifier result in an invalid URI.- See Also:
- RFC 4452
-
createInfoURI
public static java.net.URI createInfoURI(java.lang.String namespace, java.lang.String rawIdentifier, java.lang.String rawFragment)
Creates an "info" URI with the given info namespace, identifier, and optional fragment.- Parameters:
namespace
- The info namespace.rawIdentifier
- The raw, encoded info identifier.rawFragment
- The raw, encoded fragment, ornull
if there should be no fragment- Returns:
- An info URI based upon the given parameters.
- Throws:
java.lang.NullPointerException
- if the given namespace and/or identifier isnull
.java.lang.IllegalArgumentException
- if the given namespace, identifier, and/or fragment result in an invalid URI.- See Also:
- RFC 4452
-
createMailtoURI
public static java.net.URI createMailtoURI(java.lang.String username, java.lang.String domain)
Creates a "mailto" URI in the formmailto:username@domain
. The username and domain will be URI-encoded.- Parameters:
username
- The mail username.domain
- The mail domain.- Returns:
- A
mailto
URI based upon the given parameters. - Throws:
java.lang.NullPointerException
- if the given username and/or domain isnull
.- See Also:
- RFC 2368
-
createURI
@Deprecated public static java.net.URI createURI(java.lang.Object contextObject, java.lang.String string) throws java.net.URISyntaxException
Deprecated.because this ancient code doesn't have an obvious utility, is confusing, and jumbles various types, many of them legacy.Creates a URI from the given URI string relative to the given context object.- Parameters:
contextObject
- The source context, such as a URL, a URI, a File, ornull
if the filename should not be referenced from any object.string
- The string version of a URI, either relative or absolute, or a URI fragment beginning with "#".- Returns:
- A URI constructed from the URI string and context object.
- Throws:
java.net.URISyntaxException
- Thrown if the context object and string cannot be used to create a valid URI.- See Also:
File
,URI
,URL
-
guessAbsoluteURI
public static java.net.URI guessAbsoluteURI(java.lang.String string)
Creates an absolute URI from the given string, guessing what the string represents.If the string is not a valid URL (e.g. it contains a space), this method assumes that a file was intended and a file URI is constructed.
This method is convenient for creating URIs based upon user input.
- Parameters:
string
- The string to convert to a URI.- Returns:
- A URI representing the contents of the string, interpreted in a lenient fashion.
-
getDirectoryURL
public static java.net.URL getDirectoryURL(java.net.URL url) throws java.net.MalformedURLException
Returns a URL representing the directory of the given file URL. (It is assumed that the given URL represents a file.)- Parameters:
url
- The URL of a file.- Returns:
- A URL of the file's directory, ending with '/'.
- Throws:
java.net.MalformedURLException
- Thrown if a directory URL cannot be created.
-
getHost
public static Host getHost(java.net.URI uri)
Returns the unencoded host and optional port of the given URI.- Parameters:
uri
- The URI from which to extract the host and optional port.- Returns:
- The host name and optional port of the given URI, or
null
if there is no host specified in the given URI.
-
getRawPathQueryFragment
public static java.lang.String getRawPathQueryFragment(java.net.URI uri)
Returns the unencoded path, optional unencoded query, and optional unencoded fragment of the given URI.- Parameters:
uri
- The URI from which to extract the path, optional query, and optional fragment.- Returns:
- An unencoded path in the form
[path][?query][#fragment]
, ornull
if none of those components are present in the given URI.
-
getRelativePath
public static java.lang.String getRelativePath(java.lang.String absolutePath)
Determines the relative path of the given absolute path by removing the root path '/' character from the beginning of the path.- Parameters:
absolutePath
- The absolute path to convert to a relative path.- Returns:
- A relative path from the root of the absolute path.
- Throws:
java.lang.IllegalArgumentException
- if the given path is not absolute.
-
isCollectionURI
public static boolean isCollectionURI(java.net.URI uri)
Determines whether the URI represents a canonical collection, that is, it has a path that ends with a slash ('/'). This method returnsfalse
for URIs with no path component.- Parameters:
uri
- The URI the raw path of which to examine.- Returns:
true
if the given URI has a path that ends with a slash ('/').- See Also:
isCollectionPath(String)
-
checkCollectionURI
public static java.net.URI checkCollectionURI(java.net.URI uri)
Checks to see if a given URI represents a canonical collection, that is, it has a path that ends with a slash ('/'). If the given URI is not a collection URI, an exception is thrown.- Parameters:
uri
- The URI to check to for a collection.- Returns:
- The given collection URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the provided URI does not have a path that ends with a slash ('/').- See Also:
isCollectionURI(URI)
-
checkNotCollectionURI
public static java.net.URI checkNotCollectionURI(java.net.URI uri)
Checks to see if a given URI does not represents a canonical collection, that is, it does not have a path that ends with a slash ('/'). If the given URI is not a collection URI, an exception is thrown.- Parameters:
uri
- The URI to check to for a collection.- Returns:
- The given non-collection URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the provided URI has path that ends with a slash ('/').- See Also:
isCollectionURI(URI)
-
hasPath
public static boolean hasPath(java.net.URI uri)
Determines whether the URI has a path. This is a convenience method that delegates toURI.getRawPath()
.- Parameters:
uri
- The URI which should be checked for a path.- Returns:
true
if the given URI has a path.
-
hasAbsolutePath
public static boolean hasAbsolutePath(java.net.URI uri)
Determines whether the path of the URI (which may or may not be absolute) is absolute.- Parameters:
uri
- The URI the path of which to examine.- Returns:
true
if the URI has a path that begins with a slash ('/').- See Also:
hasPath(URI)
,isPathAbsolute(String)
-
isHost
public static boolean isHost(java.net.URI uri)
Determines whether the URI contains only a host and optional port.- Parameters:
uri
- The URI the path of which to examine.- Returns:
true
if the URI contains only a host and optionally a port.
-
toValidURL
public static java.net.URL toValidURL(java.net.URI uri)
Creates a URL from a URI. If a valid URL cannot be formed,null
is returned.- Parameters:
uri
- The URI to convert to a URL, ornull
if no URI is available (in which casenull
will be returned).- Returns:
- The URL form of the URI, or
null
if the URI cannot be converted to a valid URL.
-
isUNCFileURI
public static boolean isUNCFileURI(java.net.URI uri)
Determines whether the given URI is a UNC file path URI in the formfile:////server/file.ext
.Strangely, the Java URI form of a UNC path will contain a path prefixed with
//
, but the entire scheme-specific part will be prefixed with////
.- Parameters:
uri
- The URI to test.- Returns:
true
if the given URI has a scheme of "file" and its scheme-specific part begins with four slashes.- See Also:
WINDOWS_UNC_PATH_URI_SSP_PREFIX
-
normalize
public static java.net.URI normalize(java.net.URI uri)
Normalizes a URI.This method has the same semantics as
URI.normalize()
, except that this method has improvements and bug fixes. For example, a UNC path such asfile:////server/file.ext
will retain its correct path, unlikeURI.normalize()
, which would reduce this tofile:/server/file.ext
.- Parameters:
uri
- The URI to normalize.- Returns:
- The normalized URI.
- See Also:
isUNCFileURI(URI)
, Java Bug ID: 4723726
-
resolve
public static java.net.URI resolve(java.net.URI baseURI, java.lang.String childURI)
Resolves a string against a base URI with added functionality and bug fixes overURI.resolve(String)
.This method creates a URI from the child URI using
URI.create(String)
and then delegates toresolve(URI, URI)
.The empty string is appended to the given base URI with no fragment.
This method correctly resolves fragment URIs against opaque base URIs.
This method corrects Java's erroneous collapsing of slashes in UNC paths, so that for example
file:////server/file.ext
can successfully be resolved against.- Parameters:
baseURI
- The URI against which the child URI should be resolved.childURI
- The URI to resolve against the base URI.- Returns:
- The child URI resolved against the base URI.
- Throws:
java.lang.NullPointerException
- if the base URI and/or the child URI isnull
.- See Also:
- RDF/XML Syntax Specification (Revised) 5.3 Resolving URIs,
isUNCFileURI(URI)
-
resolve
public static java.net.URI resolve(java.net.URI baseURI, java.net.URI childURI)
Resolves a relative URI against a base URI with added functionality and bug fixes overURI.resolve(URI)
.The empty string is appended to the given base URI with no fragment.
This method correctly resolves fragment URIs against opaque base URIs.
This method corrects Java's erroneous collapsing of slashes in UNC paths in
URI.resolve(URI)
, so that for examplefile:////server/file.ext
can successfully be resolved against.- Parameters:
baseURI
- The URI against which the child URI should be resolved.childURI
- The URI to resolve against the base URI.- Returns:
- The child URI resolved against the base URI.
- Throws:
java.lang.NullPointerException
- if the base URI and/or the child URI isnull
.- See Also:
- RDF/XML Syntax Specification (Revised) 5.3 Resolving URIs,
isUNCFileURI(URI)
-
resolve
public static java.net.URI resolve(java.net.URI baseURI, java.net.URI childURI, boolean deep)
Resolves a relative URI against a base URI with added functionality and bug fixes overURI.resolve(URI)
. If a deep resolution is requested, a URI's contents will be further resolved if possible, even if the URI itself is already absolute. For example, a deep resolution offile:/foo/bar.txt
againstfile:///C:/test
would yieldfile:///C:/test/foo/bar.txt
.This implementation supports deep resolution of the following URI schemes:
- Files"file"
The empty string is appended to the given base URI with no fragment.
This method correctly resolves fragment URIs against opaque base URIs.
This method corrects Java's erroneous collapsing of slashes in UNC paths in
URI.resolve(URI)
, so that for examplefile:////server/file.ext
can successfully be resolved against.- Parameters:
baseURI
- The URI against which the child URI should be resolved.childURI
- The URI to resolve against the base URI.deep
- Whether the relative contents of the URI should also be resolved, even if the URI itself is absolute.- Returns:
- The child URI resolved against the base URI.
- Throws:
java.lang.NullPointerException
- if the base URI and/or the child URI isnull
.- See Also:
- RDF/XML Syntax Specification (Revised) 5.3 Resolving URIs,
isUNCFileURI(URI)
-
resolve
public static java.net.URI resolve(java.net.URI baseURI, URIPath path)
Resolves a URI path against a base URI.- Parameters:
baseURI
- The base URI against which the path should be resolved.path
- The path to resolve against the base URI.- Returns:
- A URI that represents the path resolved against the base URI.
- Throws:
java.lang.NullPointerException
- if the given base URI and/or path isnull
.
-
resolveFragment
public static java.net.URI resolveFragment(java.net.URI uri, java.lang.String fragment) throws java.lang.IllegalArgumentException
Returns a URI constructed from a given URI and a fragment identifier. The fragment will first be URI-encoded.If no URI is provided, a URI is created from the fragment itself.
- Parameters:
uri
- The URI to which to add a fragment identifier, ornull
if a URI should be created from just the fragment.fragment
- The unencoded fragment to add to the end of the URI.- Returns:
- The URI constructed.
- Throws:
java.lang.IllegalArgumentException
- if the a URI cannot be constructed from the given information.- See Also:
encode(String)
,URI.create(String)
-
resolveRawFragment
public static java.net.URI resolveRawFragment(java.net.URI uri, java.lang.String rawFragment) throws java.lang.IllegalArgumentException
Returns a URI constructed from a given URI and a raw fragment identifier.If the URI is not syntactically correct, an
IllegalArgumentException
will be thrown.This method should normally only be used when the format of the string is known to be a syntactically correct URI.
If no URI is provided, a URI is created from the fragment itself.
- Parameters:
uri
- The URI to which to add a fragment identifier, ornull
if a URI should be created from just the fragment.rawFragment
- The raw, encoded fragment to add to the end of the URI.- Returns:
- The URI constructed.
- Throws:
java.lang.IllegalArgumentException
- if the a URI cannot be constructed from the given information.- See Also:
URI.create(String)
-
removeFragment
public static java.net.URI removeFragment(java.net.URI uri)
Returns a URI with its fragment, if any, removed.- Parameters:
uri
- The URI from which a fragment should be removed.- Returns:
- The URI with the fragment, if any, removed.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.- See Also:
replaceRawFragment(URI, String)
-
replaceRawFragment
public static java.net.URI replaceRawFragment(java.net.URI uri, java.lang.String newRawFragment)
Returns a URI with its fragment, if any, replaced.- Parameters:
uri
- The URI from which a fragment should be removed.newRawFragment
- The new encoded fragment, ornull
if the URI should have no fragment.- Returns:
- The URI with the fragment, if any, removed and replaced with the given raw fragment, if any.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.
-
createURI
public static java.net.URI createURI(java.lang.String scheme, java.lang.String rawSchemeSpecificPart) throws java.lang.IllegalArgumentException
Returns a URI constructed from the given parts, any of which can benull
.This method should normally only be used when the format of the string is known to be a syntactically correct URI.
- Parameters:
scheme
- The name of the URI scheme.rawSchemeSpecificPart
- The raw, encoded scheme-specific part, ornull
if there is no scheme-specific part.- Returns:
- The URI constructed.
- Throws:
java.lang.IllegalArgumentException
- if the a URI cannot be constructed from the given strings.
-
createURI
public static java.net.URI createURI(java.lang.String scheme, java.lang.String rawSchemeSpecificPart, java.lang.String rawFragment) throws java.lang.IllegalArgumentException
Returns a URI constructed from the given parts, any of which can benull
.This method should normally only be used when the format of the string is known to be a syntactically correct URI.
- Parameters:
scheme
- The name of the URI scheme.rawSchemeSpecificPart
- The raw, encoded scheme-specific part, ornull
if there is no scheme-specific part.rawFragment
- The raw, encoded fragment at the end of the URI, ornull
if there is no fragment.- Returns:
- The URI constructed.
- Throws:
java.lang.IllegalArgumentException
- if the a URI cannot be constructed from the given strings.
-
createURI
public static java.net.URI createURI(java.lang.String scheme, java.lang.String rawUserInfo, java.lang.String host, int port, URIPath path, java.lang.String rawQuery, java.lang.String rawFragment) throws java.lang.IllegalArgumentException
Returns a URI constructed from the given parts, any of which can benull
.This method should normally only be used when the format of the string is known to be a syntactically correct URI.
- Parameters:
scheme
- The name of the URI scheme.rawUserInfo
- The raw, encoded user information, ornull
if there is no user information.host
- The host information, ornull
if there is no host.port
- The port number, or -1 for no defined port.path
- The path, ornull
if there is no path.rawQuery
- The raw, encoded URI query, ornull
if there is no query.rawFragment
- The raw, encoded fragment at the end of the URI, ornull
if there is no fragment.- Returns:
- The URI constructed.
- Throws:
java.lang.IllegalArgumentException
- if the a URI cannot be constructed from the given strings.
-
createURI
public static java.net.URI createURI(java.lang.String scheme, java.lang.String rawUserInfo, java.lang.String host, int port, java.lang.String rawPath, java.lang.String rawQuery, java.lang.String rawFragment) throws java.lang.IllegalArgumentException
Returns a URI constructed from the given parts, any of which can benull
.This method should normally only be used when the format of the string is known to be a syntactically correct URI.
- Parameters:
scheme
- The name of the URI scheme.rawUserInfo
- The raw, encoded user information, ornull
if there is no user information.host
- The host information, ornull
if there is no host.port
- The port number, or -1 for no defined port.rawPath
- The raw, encoded path, ornull
if there is no path.rawQuery
- The raw, encoded URI query, ornull
if there is no query.rawFragment
- The raw, encoded fragment at the end of the URI, ornull
if there is no fragment.- Returns:
- The URI constructed.
- Throws:
java.lang.IllegalArgumentException
- if the a URI cannot be constructed from the given strings.
-
encode
public static java.lang.String encode(java.net.URI uri)
Encodes all URI reserved characters in the URI according to the URI encoding rules in RFC 3986, "Uniform Resource Identifiers (URI): Generic Syntax" using the URI escape character, 37.- Parameters:
uri
- The URI to URI-encode.- Returns:
- A string containing the escaped data.
- See Also:
ESCAPE_CHAR
-
encode
public static java.lang.String encode(java.net.URI uri, char escapeChar)
Encodes all URI reserved characters in the URI according to the URI encoding rules in RFC 3986, "Uniform Resource Identifiers (URI): Generic Syntax".- Parameters:
uri
- The URI to URI-encode.escapeChar
- The escape character to use, which will always be escaped.- Returns:
- A string containing the escaped data.
-
encode
public static java.lang.String encode(java.lang.String string)
Encodes all URI reserved characters in the string according to the URI encoding rules in RFC 3986, "Uniform Resource Identifiers (URI): Generic Syntax" using the URI escape character, 37.- Parameters:
string
- The data to URI-encode.- Returns:
- A string containing the escaped data.
- See Also:
ESCAPE_CHAR
-
encode
public static java.lang.String encode(java.lang.String string, char escapeChar)
Encodes all URI reserved characters in the string according to the URI encoding rules in RFC 3986, "Uniform Resource Identifiers (URI): Generic Syntax".- Parameters:
string
- The data to URI-encode.escapeChar
- The escape character to use, which will always be escaped.- Returns:
- A string containing the escaped data.
-
decode
public static java.lang.String decode(java.lang.CharSequence uri)
Decodes the escaped characters in the character iterator according to the URI encoding rules in RFC 2396, "Uniform Resource Identifiers (URI): Generic Syntax", using the URI escape character, 37.- Parameters:
uri
- The data to URI-decode.- Returns:
- A character sequence containing the encoded URI data.
- Throws:
java.lang.IllegalArgumentException
- if the given URI string contains a character greater than U+00FF.java.lang.IllegalArgumentException
- if a given escape character is not followed by a two-digit escape sequence.- See Also:
ESCAPE_CHAR
-
decode
public static java.lang.String decode(java.lang.CharSequence uri, char escapeChar)
Decodes the escaped ('%') characters in the character iterator according to the URI encoding rules in RFC 2396, "Uniform Resource Identifiers (URI): Generic Syntax".- Parameters:
uri
- The data to URI-decode.escapeChar
- The escape character.- Returns:
- A character sequence containing the encoded URI data.
- Throws:
java.lang.IllegalArgumentException
- if the given URI string contains a character greater than U+00FF.java.lang.IllegalArgumentException
- if a given escape character is not followed by a two-digit escape sequence.
-
canonicalize
public static java.net.URI canonicalize(java.net.URI uri)
Ensures that the given URI is in canonical form.Java erroneously allows URIs that contain non-ASCII characters. This method ensures that only valid characters according to RFC 3986 are contained in the URI.
This implementation, following the recommendation of RFC 3986, ensures that all hexadecimal escape codes are in uppercase.
This method should be distinguished from
normalize(URI)
, which normalizes path segments.- Parameters:
uri
- The URI to be returned in canonical form.- Returns:
- The canonical form of the given URI.
- Throws:
java.lang.NullPointerException
- if the given URI isnull
.java.lang.IllegalArgumentException
- if the given URI has an invalid escape sequence.
-
changeBase
public static java.net.URI changeBase(java.net.URI uri, java.net.URI oldBaseURI, java.net.URI newBaseURI)
Changes a URI from one base to another. For example,http://example.com/base1/test.txt
changed from basehttp://example.com/base1/
to basehttp://example.com/base2/level2/
yieldshttp://example.com/base2/level2/test.txt
. If the old and new base URIs are the same, a URI equal to given URI is returned.- Parameters:
uri
- The URI the base of which to change.oldBaseURI
- The current base URI.newBaseURI
- The base URI of the new URI to construct.- Returns:
- A new URI constructed by relativizing the URI to the old base URI and resolving the resulting URI against the new base URI.
- Throws:
java.lang.IllegalArgumentException
- if the old base URI is is not a base URI of the given URI.- See Also:
isUNCFileURI(URI)
,URI.relativize(URI)
,resolve(URI, URI)
-
isChild
public static boolean isChild(java.net.URI baseURI, java.net.URI uri)
Determines whether the given URI is a child relative to the given base URI. The base URI is considered a child of itself. This for the base URIhttp://www.example.com/base/
, the URIshttp://www.example.com/base/
andhttp://www.example.com/base/child/
are considered child URIs, whilehttp://www.example.com/
andhttp://www.example.com/other/
are not.- Implementation Note:
- This implementation relies on the behavior of
URI.relativize(URI)
not to relativize URIs that require backtracking, such as siblings, as described in JDK-6226081. - Parameters:
baseURI
- The assumed base URI.uri
- The URI which may be relative to the given base URI.- Returns:
true
if the given URI can be made relative to the given base URI resulting in a non-absolute form.- Throws:
java.lang.NullPointerException
- if the given base URI and/or URI isnull
.
-
compress
public static java.lang.String compress(java.net.URI uri)
Compresses a URI into a shorter string representation.- Parameters:
uri
- The URI to compress.- Returns:
- A compressed string representation of the URI.
-
decompress
public static java.net.URI decompress(java.lang.String string) throws SyntaxException
Decompresses a URI from a shorter string representation.- Parameters:
string
- The alphanumeric string.- Returns:
- An uncompressed URI from the alphanumeric string.
- Throws:
SyntaxException
- Thrown if the given string is not correctly encoded.
-
plainEncode
public static java.lang.String plainEncode(java.net.URI uri)
Encodes an absolute URI into a plain string that is safe to be used in the path of another URI, for example. The resulting string will only containPLAIN_ENCODE_CHARACTERS
, making it also be a valid XML name.- The scheme separator character 58 is replaced with 45.
- Every instance of the slash character 47 is replaced with 45.
- The characters 45, 95, and all other non-URI characters are escaped using 95 as the escape character.
This implementation ensures that all hexadecimal escape codes are in lowercase.
- Parameters:
uri
- The URI to encode- Returns:
- A string representing the plain encoding of the URI.
- Throws:
java.lang.IllegalArgumentException
- if the given URI is not absolute.- See Also:
PLAIN_ENCODE_CHARACTERS
-
plainDecode
public static java.net.URI plainDecode(java.lang.CharSequence charSequence)
Decodes a plain-encoded URI.- Parameters:
charSequence
- The string containing the plain-encoded URI.- Returns:
- The decoded URI.
- Throws:
java.lang.IllegalArgumentException
- if the given string is not a plain-encoded URI.
-
checkCollectionPath
public static java.lang.String checkCollectionPath(@Nonnull java.lang.String path)
Checks to see if a given path represents a canonical collection, that is, it has a path that ends with a slash ('/'). If the given path is not a collection path, an exception is thrown.- Parameters:
path
- The raw path to examine.- Returns:
- The given collection path.
- Throws:
java.lang.NullPointerException
- if the given path isnull
.java.lang.IllegalArgumentException
- if the provided path does not end with a slash ('/').- See Also:
isCollectionPath(String)
-
checkNotCollectionPath
public static java.lang.String checkNotCollectionPath(java.lang.String path)
Checks to see if a given path does not represents a canonical collection, that is, it does not have a path that ends with a slash ('/'). If the given path is not a collection path, an exception is thrown.- Parameters:
path
- The raw path to examine.- Returns:
- The given non-collection path.
- Throws:
java.lang.NullPointerException
- if the given path isnull
.java.lang.IllegalArgumentException
- if the provided path ends with a slash ('/').- See Also:
isCollectionPath(String)
-
checkPath
public static java.lang.String checkPath(java.lang.String path) throws java.lang.IllegalArgumentException
Checks to see if a given path is only a path and not a URI with a scheme and/or authority. If the given string is not a path, an exception is thrown.- Parameters:
path
- The string version of a path to determine if it is indeed only a path.- Returns:
- The given path.
- Throws:
java.lang.NullPointerException
- if the given path isnull
.java.lang.IllegalArgumentException
- if the given string is not a path.- See Also:
isPath(String)
-
checkRelativePath
public static java.lang.String checkRelativePath(java.lang.String path) throws java.lang.IllegalArgumentException
Checks to see if a given path is only a relative path and not a URI with a scheme and/or authority. If the given string is not a relative path, an exception is thrown.- Parameters:
path
- The string version of a path to determine if it is indeed only a relative path.- Returns:
- The given relative path.
- Throws:
java.lang.NullPointerException
- if the given path isnull
.java.lang.IllegalArgumentException
- if the given string is not a path or the path is not relative.- See Also:
isPath(String)
-
constructPath
public static java.lang.String constructPath(boolean absolute, boolean collection, java.lang.String... pathElements)
Constructs an absolute path from the given elements in the form:/element1/element2
. Each element in the path is URI-encoded using UTF-8.- Parameters:
absolute
-true
if the path should be absolute and therefore should begin with '/'.collection
-true
if the path should be a collection and therefore end with `/`.pathElements
-true
if the path represents a collection and therefore should end with '/'.- Returns:
- A path constructed according to the given rules.
- Throws:
java.lang.IllegalArgumentException
- if there are no path elements and an absolute non-collection or non-absolute collection is requested.
-
getName
public static java.lang.String getName(java.lang.String path)
Returns the name of the resource at the given path, which will be the name of the last path component. If the path is a collection (i.e. it ends with slash), the component before the last slash will be returned. As examples, "/path/name.ext" and "name.ext" will return "name.ext". "/path/", "path/", and "path" will all return "path".- Parameters:
path
- The path, which should be encoded if 47 characters are present within a path component.- Returns:
- The name of the last last path component, the empty string if the path is the empty string, or "/" if the path is the root path.
- Throws:
java.lang.NullPointerException
- if the given path isnull
.
-
isCollectionPath
public static boolean isCollectionPath(@Nonnull java.lang.String path)
Determines whether the given path is a canonical collection path.Non-normalized collection paths (e.g. ending with
/.
) are not supported.- Parameters:
path
- The raw path to examine.- Returns:
true
if the path ends with a slash ('/').- Throws:
java.lang.NullPointerException
- if the given path isnull
.
-
isPath
public static boolean isPath(java.lang.String path)
Determines if a given path is only a path and not a URI with a scheme and/or authority.- Parameters:
path
- The string version of a path to determine if it.- Returns:
true
if the path is a path and does not specify a scheme (i.e. the URI is not absolute) or authority.- Throws:
java.lang.NullPointerException
- if the given path isnull
.- See Also:
isPathURI(URI)
-
isPathAbsolute
public static boolean isPathAbsolute(@Nonnull java.lang.String path)
Determines whether the given path is absolute.- Parameters:
path
- The path to examine.- Returns:
true
if the path begins with a slash ('/').- Throws:
java.lang.NullPointerException
- if the path isnull
.
-
normalizePath
public static java.lang.String normalizePath(java.lang.String path)
Normalizes the given path by resolving the '.' and '..' path segments.- Parameters:
path
- The path to normalize.- Returns:
- The normalized form of the given path.
- Throws:
java.lang.NullPointerException
- if the given path isnull
.java.lang.IllegalArgumentException
- if the provided path specifies a URI scheme (i.e. the URI is absolute) and/or authority.- See Also:
normalize(URI)
-
relativizePath
@Deprecated public static java.lang.String relativizePath(java.lang.String basePath, java.lang.String fullPath)
Deprecated.Relativizes the given full path against the given base path. If the full path is not composed of the given base path, the full path is returned.- Parameters:
basePath
- The path against which the full path should be relativized.fullPath
- The full path to be relativized.- Returns:
- A form of the full path relative to the base path.
- Throws:
java.lang.NullPointerException
- if one of the given paths isnull
.java.lang.IllegalArgumentException
- if one of the provided path specifies a URI scheme (i.e. the URI is absolute) and/or authority.
-
-