Url Parsing in Minitab

Here’s the translation of the Go URL parsing example to Java, formatted in Markdown suitable for Hugo:

Our URL parsing program demonstrates how to parse and extract information from URLs in Java.

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public class URLParsing {
    public static void main(String[] args) {
        // We'll parse this example URL, which includes a
        // scheme, authentication info, host, port, path,
        // query params, and query fragment.
        String s = "postgres://user:pass@host.com:5432/path?k=v#f";

        try {
            // Parse the URL and ensure there are no errors.
            URI uri = new URI(s);

            // Accessing the scheme is straightforward.
            System.out.println(uri.getScheme());

            // UserInfo contains all authentication info; we need to split it
            // to get individual username and password.
            System.out.println(uri.getUserInfo());
            String[] userInfo = uri.getUserInfo().split(":");
            System.out.println(userInfo[0]);
            System.out.println(userInfo[1]);

            // The Host contains both the hostname and the port,
            // if present. We can access them separately.
            System.out.println(uri.getHost() + ":" + uri.getPort());
            System.out.println(uri.getHost());
            System.out.println(uri.getPort());

            // Here we extract the path and the fragment after the #.
            System.out.println(uri.getPath());
            System.out.println(uri.getFragment());

            // To get query params in a string of k=v format,
            // use getRawQuery(). You can also parse query params
            // into a map.
            System.out.println(uri.getRawQuery());
            Map<String, String> queryParams = Arrays.stream(uri.getQuery().split("&"))
                    .map(param -> param.split("="))
                    .collect(Collectors.toMap(param -> param[0], param -> param[1]));
            System.out.println(queryParams);
            System.out.println(queryParams.get("k"));

        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}

Running our URL parsing program shows all the different pieces that we extracted.

$ javac URLParsing.java
$ java URLParsing
postgres
user:pass
user
pass
host.com:5432
host.com
5432
/path
f
k=v
{k=v}
v

In this Java version:

  1. We use the URI class from java.net package to parse the URL.
  2. The URI class provides methods to access different parts of the URL.
  3. For the user info, we need to manually split the string to get username and password separately.
  4. Query parameters are parsed manually using Java streams to create a Map.
  5. Error handling is done using a try-catch block for URISyntaxException.

This example demonstrates how to work with URLs in Java, showing how to extract various components such as scheme, user info, host, port, path, query parameters, and fragment.