URIFields.java

import java.net.*;
public class URIFields {
  public static void main(String[] args)
    throws URISyntaxException {
    if (args.length < 1) {
      System.err.println("java URIFields <absolute-uri>");
      System.exit(1);
    }
    URI uri = new URI(args[0]);
    String scheme = uri.getScheme();
    if (scheme == null) {
      System.err.println("java URIFields <absolute-uri>");
      System.exit(1);
    }
    System.out.println("Scheme: " + scheme);
    if (uri.isOpaque()) {
      System.out.println("Reste: " + uri.getSchemeSpecificPart());
      return;
    }
    try {
      uri.parseServerAuthority();
      String user =  uri.getUserInfo();
      if (user != null) System.out.println("User-info: " + user);
      System.out.println("Host: " + uri.getHost());
      int port = uri.getPort();
      if (port != -1) System.out.println("Port: " + port);
    } catch (URISyntaxException e) {
      System.out.println("Autorité: " + uri.getAuthority());
    }
    System.out.println("Path: " + uri.getPath());
    String query = uri.getQuery();
    if (query != null) System.out.println("Query: " + query);
    String fragment = uri.getFragment();
    if (fragment != null) System.out.println("Fragment: " + fragment);
  }
}