System.err.println(args[i] + " does not seem to be a URI.");
}
System.out.println( );
} // end for
} // end main
} // end URISplitter
下面就是运行这三个URI实例后的结果:
% java URISplitter tel:+1-800-9988-9938
\http://www.xml.com/pub/a/2003/09/17/stax.html#id=_hbc \urn:isbn:1-565-92870-9
The URI is tel:+1-800-9988-9938
This is an opaque URI.
The scheme is tel
The scheme specific part is +1-800-9988-9938
The fragment ID is null
The URI is http://www.xml.com/pub/a/2003/09/17/stax.html#id=_hbc
This is a hierarchical URI.
The scheme is http
The host is null
The user info is null
The port is -1
The path is /pub/a/2003/09/17/stax.html
The query string is null
The fragment ID is id=_hbc
The URI is urn:isbn:1-565-92870-9
This is an opaque URI.
The scheme is urn
The scheme specific part is isbn:1-565-92870-9
The fragment ID is null
处理相对的 URI
类URI 提供了三种方法进行相对的和绝对的URI之间的转换。
public URI resolve(URI uri)
它比较参数uri 和这个URI ,然后用它构成一个新的URI 对象,它会有一个绝对的URI。例如,思考这三行代码:
URI absolute = new URI("http://www.example.com/");
URI relative = new URI("images/logo.png");
URI resolved = absolute.resolve(relative);
在它们执行后,resolved 会有一个绝对的URI
http://www.example.com/images/logo.png.
如果这个被调用的URI 它本身就没有绝对的URI,那么方法resolve( ) 会尽量的处理这个URI,返回一个新的相对的URI对象。比如,下面这三个语句:
URI top = new URI("javafaq/books/");
URI relative = new URI("jnp3/examples/07/index.html");
URI resolved = top.resolve(relative);
在执行后,resolved 现在有一个相对的URI
javafaq/books/jnp3/examples/07/index.html
就没有scheme(协议)和authority(认证)
public URI resolve(String uri)
这是个简便的方法,它能方便地把参数string 转化成一个URI,然后与被调用的这个URI组合后,返回一个新的URI对象。也就是,它与resolve(newURI(str)) 是等价的。使用这个方法,前面的两个例子就可以重写如下:
URI absolute = new URI("http://www.example.com/");
URI resolved = absolute.resolve("images/logo.png");
URI top = new URI("javafaq/books/");
resolved = top.resolve("jnp3/examples/07/index.html");
public URI relativize(URI uri)
反向操作这个过程也可以;也就是,从一个绝对的URI到一个相对的URI。方法relativize( ) 用参数uri 创建一个新的URI 对象,这个uri 对被调用的URI 是相对的。参数没有改变。例如:
URI absolute = new URI("http://www.example.com/images/logo.png");
URI top = new URI("http://www.example.com/");
URI relative = top.relativize(absolute);
现在的 URI 对象包含了一个相对的 URI images/logo.png.
一些有用的方法
类URI 有不少普通的但很实用的方法:equals(), hashCode( ), toString( ), 和 compareTo( )
public boolean equals(Object o)
URI之间的比较与你期待中的一样好。它不是string之间的直接做比较。相同的URI必须要么是有层次关系的要么就是层次关系模糊的。Scheme和authority部分的比较是不区分大小写的。也就是http 和 HTTP 都表示的是相同的协议,authority 的比较www.example.com 和 www.EXAMPLE.com 也是一样的。URI中剩下的部分就得区分大小写了,除了用来转义非法字符的16进制的数字以外。在做比较前,%xx还没有被解码。
http://www.example.com/A and http://www.example.com/%41 are unequal URIs.
public int hashCode( )
方法hashCode( ) 是个普通的hashCode( ) 方法,没有什么特别的。相同的URI就会有相同的哈希码,不同的URI就很难拥有相同的哈希码了。
public int compareTo(Object o)
URI是可以排序的。排序是基于对各个部分字符比较的结果上的,以下面的顺序:
· 如果scheme是不同的,就比较scheme,不考虑大小写。
· 否则,如果scheme是相同的,我们 就认为层次关系模糊的URI比有层次关系的URI优先级高。
· 如果比较的两个URI都是层次关系模糊的,这时就以它们的scheme-specific进行排序。
· 如果scheme和层次关系模糊的scheme-specific都相同了,就比较URI的fragment
· 如果做比较的URI都是有层次关系的,就看它们的authority,比较其中的user info, host, port。
· 如果scheme和authority都相同了,就看它们的path。
· 如果path也相同了,接着比较query string。