Class XmlUri

java.lang.Object
org.libxmq.imp.xml.XmlUri

public final class XmlUri extends Object
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 Details

    • scheme

      public String scheme
      The scheme, eg "http", or null.
    • opaque

      public String opaque
      The opaque part, or null.
    • authority

      public String authority
      The authority part, or null.
    • server

      public String server
      The server part, or null.
    • user

      public String user
      The user part, or null.
    • port

      public int port
      The port number, 0 if empty and -1 for an empty server.
    • path

      public String path
      The path string, or null.
    • query

      public String query
      The query string (unescaped), or null.
    • fragment

      public String fragment
      The fragment identifier, or null.
    • cleanup

      public int cleanup
      The parsing flags, 2 disables unescaping of the parts.
    • query_raw

      public String query_raw
      The query string (as it appears in the URI), or null.
  • Method Details

    • toUtf8

      public static byte[] toUtf8(String s)
      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

      public static String fromUtf8(byte[] b)
      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 that toUtf8(String) gives back exactly the same bytes.
      Parameters:
      b - the bytes, may be null
      Returns:
      the string or null if b is null
    • parseSafe

      public static int parseSafe(String str, XmlUri[] uri_out)
      Parse an URI based on RFC 3986. URI-reference = [ absoluteURI | relativeURI ] [ "\#" fragment ]
      Parameters:
      str - the URI string to analyze
      uri_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

      public static XmlUri parse(String str)
      Parse an URI based on RFC 3986. URI-reference = [ absoluteURI | relativeURI ] [ "\#" fragment ]
      Parameters:
      str - the URI string to analyze
      Returns:
      a newly built XmlUri or null in case of error
    • parseReference

      public int parseReference(String str)
      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

      public static XmlUri parseRaw(String str, int raw)
      Parse an URI but allows to keep intact the original fragments. URI-reference = URI / relative-ref
      Parameters:
      str - the URI string to analyze
      raw - if 1 unescaping of URI pieces are disabled
      Returns:
      a newly built XmlUri or null in case of error
    • create

      public static XmlUri create()
      Simply creates an empty XmlUri.
      Returns:
      the new structure
    • save

      public String 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

      public static String normalizeUriPath(String path)
      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

      public static String unescapeString(String str, int len)
      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 unescape
      len - 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

      public static String buildSafe(String uri, String base, int[] error_out)
      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 document
      base - the base value, may be null
      error_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

      public static String build(String uri, String base)
      Computes the final URI of the reference by resolving it against the base, like buildSafe(String, String, int[]) (libxml2 xmlBuildURI).
      Parameters:
      uri - the URI instance found in the document
      base - the base value, may be null
      Returns:
      a new URI string or null in case of error
    • escapeStr

      public static String escapeStr(String str, String list)
      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 escape
      list - extra characters that are not escaped, may be null
      Returns:
      the escaped string or null if str is null