Once you have a URI object, you can call getauthority (), getfragment (), gethost (), getpath (), getport (), getquery (), getscheme (), getschemespecificpart () and getuserinfo () methods to extract multiple components. You can also call isabsolute () to determine whether the URI is absolute or relative. Call isopaque () to determine whether the URI is opaque or hierarchical. If the returned value is true, the URI is absolute or opaque. If the returned value is false, the URI is relative or hierarchical.
The program in List 1 creates a URI object using the command line parameters, calls the URI Component Extraction Method to retrieve the URI component, and calls the isabsolute () and isopaque () of the URI () the URI is classified as absolute/relative and opaque/hierarchical.
List 1: uridemo1.java
1 // URIDemo1.java
2
3 import java.net.*;
4
5 class URIDemo1
6 {
7 public static void main (String [] args) throws Exception
8 {
9 if (args.length != 1)
10 {
11 System.err.println ("usage: java URIDemo1 uri");
12 return;
13 }
14
15 URI uri = new URI (args [0]);
16
17 System.out.println ("Authority = " +uri.getAuthority ());
18 System.out.println ("Fragment = " +uri.getFragment ());
19 System.out.println ("Host = " +uri.getHost ());
20 System.out.println ("Path = " +uri.getPath ());
21 System.out.println ("Port = " +uri.getPort ());
22 System.out.println ("Query = " +uri.getQuery ());
23 System.out.println ("Scheme = " +uri.getScheme ());
24 System.out.println ("Scheme-specific part = " +
25 uri.getSchemeSpecificPart ());
26 System.out.println ("User Info = " +uri.getUserInfo ());
27 System.out.println ("URI is absolute: " +uri.isAbsolute ());
28 System.out.println ("URI is opaque: " +uri.isOpaque ());
29 }
30 }
After you enter the Java uridemo1 command, the output of List 1 is as follows:
1 query://jeff@books.com:9000/public/manuals/appliances?stove#ge:
2 Authority = jeff@books.com:9000
3 Fragment = ge
4 Host = books.com
5 Path = /public/manuals/appliances
6 Port = 9000
7 Query = stove
8 Scheme = query
9 //jeff@books.com:9000/public/manuals/appliances?stove
10 User Info = jeff
11 URI is absolute: true
12 URI is opaque: false
The output above shows that the URI is absolute because it specifies an outline (query) and the URI is layered because the query is followed by a/symbol.
Tips
You should call the Uri's compareto (Object O) and equals (Object o) to determine the Uri's order (for sorting purposes) and equality. You can refer to the SDK documentation for more information about these methods.