U2.java

import java.net.*;
import java.io.*;

/**
Demonstrate the URI class's parsing abilities.  Parses one or more
URIs given on the command line.  If no command line arguments,
parses a number of sample URIs.
*/
public class U2
{
    public static void main(String argv[])
    {
        String[] samples = {
            "http://www.ace-net.com/",
            "mailto:leipold@acm.org",
            "http://bozoid/cgi-bin/cMonth.pl?comic=aj&year=2001",
            "ftp://me:secret@ftp.xyz.com/bozo.txt"
            };
        if (argv.length <= 0)
            for (int i=0; i<samples.length; i++)
                demoURI(System.out,samples[i]);
        else
            for (int i=0; i<argv.length; i++)
                demoURI(System.out,argv[i]);
    }

    private static void demoURI(PrintStream p,String ustring)
    {
        try {
            p.println("URI = " + ustring);
            URI uri = new URI(ustring);
            p.println("  scheme: " + uri.getScheme());
            p.println("     ssp: " + uri.getSchemeSpecificPart());
            p.println("    auth: " + uri.getAuthority());
            p.println("    user: " + uri.getUserInfo());
            p.println("    host: " + uri.getHost());
            p.println("    port: " + uri.getPort());
            p.println("    path: " + uri.getPath());
            p.println("   query: " + uri.getQuery());
            p.println("    frag: " + uri.getFragment());
            p.println();
            }
        catch (URISyntaxException e) {
            e.printStackTrace();
            }
    }
}