Class XmlUri
java.lang.Object
org.libxmq.imp.xml.XmlUri
URI references (RFC 3986): parsing, saving and resolving against a base.
This is a port of xml_uri.c, which in turn is a port of libxml2 uri.c.
It is used to resolve xml:base, xsl:import, xsl:include and document()
the way libxml2 xmlBuildURI does, and gives byte identical results to
the C version.
The C code works on UTF-8 bytes, so internally everything is done on
UTF-8 byte arrays with indexes that mirror the pointer arithmetic of
the C code. Strings are converted with
toUtf8(String) and
fromUtf8(byte[]) at the public API. Unescaping %XX can produce
byte sequences that are not valid UTF-8 (eg "%FF"). Such bytes are kept
losslessly in the Java strings: every byte that is not part of a well
formed UTF-8 sequence is mapped to the lone surrogate U+DC00+byte (the
"surrogateescape" convention) and mapped back to the same byte when
the string is converted to UTF-8 again. Thus converting a result with
toUtf8(String) always gives exactly the bytes the C version
produces, and for valid UTF-8 results the string is the normal decoding.
Other lone surrogates in input strings become '?'. A C string ends at
the first NUL byte, therefore input strings are cut at the first U+0000
and results are cut at the first NUL (eg from unescaping "%00").-
Field Summary
FieldsModifier and TypeFieldDescriptionThe authority part, or null.intThe parsing flags, 2 disables unescaping of the parts.The fragment identifier, or null.The opaque part, or null.The path string, or null.intThe port number, 0 if empty and -1 for an empty server.The query string (unescaped), or null.The query string (as it appears in the URI), or null.The scheme, eg "http", or null.The server part, or null.The user part, or null. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringComputes the final URI of the reference by resolving it against the base, likebuildSafe(String, String, int[])(libxml2 xmlBuildURI).static StringComputes the final URI of the reference done by checking that the given URI is valid, and building the final URI using the base URI.static XmlUricreate()Simply creates an empty XmlUri.static StringPort of xmlURIEscapeStr: escape a string to hex, except unreserved characters, "@" and the characters in list.static StringfromUtf8(byte[] b) Convert UTF-8 bytes to a string.static StringnormalizeUriPath(String path) Applies the 5 normalization steps to a path string--that is, RFC 2396 Section 5.2, steps 6.c through 6.g.static XmlUriParse an URI based on RFC 3986.static XmlUriParse an URI but allows to keep intact the original fragments.intparseReference(String str) Parse an URI reference string based on RFC 3986 and fills in the appropriate fields of this structure.static intParse an URI based on RFC 3986.save()Save the URI as an escaped string.static byte[]Convert a string to the UTF-8 bytes the C code works on.static StringunescapeString(String str, int len) Unescaping routine, but does not check that the string is an URI.
-
Field Details
-
scheme
The scheme, eg "http", or null. -
opaque
The opaque part, or null. -
authority
The authority part, or null. -
server
The server part, or null. -
user
The user part, or null. -
port
public int portThe port number, 0 if empty and -1 for an empty server. -
path
The path string, or null. -
query
The query string (unescaped), or null. -
fragment
The fragment identifier, or null. -
cleanup
public int cleanupThe parsing flags, 2 disables unescaping of the parts. -
query_raw
The query string (as it appears in the URI), or null.
-
-
Method Details
-
toUtf8
Convert a string to the UTF-8 bytes the C code works on. Lone surrogates U+DC80..U+DCFF become the bytes 0x80..0xFF (see the class documentation), other lone surrogates become '?'. The string is cut at the first U+0000.- Parameters:
s- the string, may be null- Returns:
- the UTF-8 bytes or null if s is null
-
fromUtf8
Convert UTF-8 bytes to a string. Bytes that are not part of a well formed UTF-8 sequence become the lone surrogates U+DC80..U+DCFF so thattoUtf8(String)gives back exactly the same bytes.- Parameters:
b- the bytes, may be null- Returns:
- the string or null if b is null
-
parseSafe
Parse an URI based on RFC 3986. URI-reference = [ absoluteURI | relativeURI ] [ "\#" fragment ]- Parameters:
str- the URI string to analyzeuri_out- uri_out[0] is set to the parsed URI or null on error- Returns:
- 0 on success, an error code (typically 1) if the URI is invalid or -1 if a memory allocation failed.
-
parse
-
parseReference
Parse an URI reference string based on RFC 3986 and fills in the appropriate fields of this structure. The fields are cleared first, except for port and cleanup (like in the C version). URI-reference = URI / relative-ref- Parameters:
str- the string to analyze- Returns:
- 0 or the error code
-
parseRaw
-
create
-
save
Save the URI as an escaped string.- Returns:
- the escaped string, or null if the result would be longer than the maximum URI length (1MiB)
-
normalizeUriPath
Applies the 5 normalization steps to a path string--that is, RFC 2396 Section 5.2, steps 6.c through 6.g. The C version normalizes the string in place, here the normalized path is returned.- Parameters:
path- the path string- Returns:
- the normalized path, or null if path is null
-
unescapeString
Unescaping routine, but does not check that the string is an URI. The output is a direct byte translation of %XX values (no encoding), see the class documentation for bytes that are not valid UTF-8. The result ends at the first NUL (eg from "%00").- Parameters:
str- the string to unescapelen- the length in UTF-8 bytes to unescape (or <= 0 to indicate full string)- Returns:
- a copy of the string, but unescaped, null if str is null
-
buildSafe
Computes the final URI of the reference done by checking that the given URI is valid, and building the final URI using the base URI. This is processed according to section 5.2 of the RFC 2396 (libxml2 xmlBuildURISafe).- Parameters:
uri- the URI instance found in the documentbase- the base value, may be nullerror_out- if not null, error_out[0] is set to 0 on success, -1 if the result is too long or an error code if URI or base are invalid- Returns:
- the resulting URI, or null on error
-
build
Computes the final URI of the reference by resolving it against the base, likebuildSafe(String, String, int[])(libxml2 xmlBuildURI).- Parameters:
uri- the URI instance found in the documentbase- the base value, may be null- Returns:
- a new URI string or null in case of error
-
escapeStr
Port of xmlURIEscapeStr: escape a string to hex, except unreserved characters, "@" and the characters in list. Every byte of the UTF-8 encoding of a non-ASCII character is escaped.- Parameters:
str- the string to escapelist- extra characters that are not escaped, may be null- Returns:
- the escaped string or null if str is null
-