Java Reference
In-Depth Information
For various technical reasons that don't have a lot of practical impact, Java can't always
initially detect syntax errors in the authority component. The immediate symptom of
this failing is normally an inability to return the individual parts of the authority, port,
host, and user info. In this event, you can call parseServerAuthority() to force the
authority to be reparsed:
public URI parseServerAuthority () throws URISyntaxException
The original URI does not change ( URI objects are immutable), but the URI returned will
have separate authority parts for user info, host, and port. If the authority cannot be
parsed, a URISyntaxException is thrown.
Example 5-6 uses these methods to split URIs entered on the command line into their
component parts. It's similar to Example 5-4 but works with any syntactically correct
URI, not just the ones Java has a protocol handler for.
Example 5-6. The parts of a URI
import java.net.* ;
public class URISplitter {
public static void main ( String args []) {
for ( int i = 0 ; i < args . length ; i ++) {
try {
URI u = new URI ( args [ i ]);
System . out . println ( "The URI is " + u );
if ( u . isOpaque ()) {
System . out . println ( "This is an opaque URI." );
System . out . println ( "The scheme is " + u . getScheme ());
System . out . println ( "The scheme specific part is "
+ u . getSchemeSpecificPart ());
System . out . println ( "The fragment ID is " + u . getFragment ());
} else {
System . out . println ( "This is a hierarchical URI." );
System . out . println ( "The scheme is " + u . getScheme ());
try {
u = u . parseServerAuthority ();
System . out . println ( "The host is " + u . getHost ());
System . out . println ( "The user info is " + u . getUserInfo ());
System . out . println ( "The port is " + u . getPort ());
} catch ( URISyntaxException ex ) {
// Must be a registry based authority
System . out . println ( "The authority is " + u . getAuthority ());
}
System . out . println ( "The path is " + u . getPath ());
System . out . println ( "The query string is " + u . getQuery ());
System . out . println ( "The fragment ID is " + u . getFragment ());
}
} catch ( URISyntaxException ex ) {
Search WWH ::




Custom Search