PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jdk/src/share/classes/java/net/URI.java

#
Java | 3526 lines | 1523 code | 282 blank | 1721 comment | 564 complexity | e917807aea7e39135d35cd0637573bf1 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package java.net;
  26. import java.io.IOException;
  27. import java.io.InvalidObjectException;
  28. import java.io.ObjectInputStream;
  29. import java.io.ObjectOutputStream;
  30. import java.io.Serializable;
  31. import java.nio.ByteBuffer;
  32. import java.nio.CharBuffer;
  33. import java.nio.charset.CharsetDecoder;
  34. import java.nio.charset.CharsetEncoder;
  35. import java.nio.charset.CoderResult;
  36. import java.nio.charset.CodingErrorAction;
  37. import java.nio.charset.CharacterCodingException;
  38. import java.text.Normalizer;
  39. import sun.nio.cs.ThreadLocalCoders;
  40. import java.lang.Character; // for javadoc
  41. import java.lang.NullPointerException; // for javadoc
  42. /**
  43. * Represents a Uniform Resource Identifier (URI) reference.
  44. *
  45. * <p> Aside from some minor deviations noted below, an instance of this
  46. * class represents a URI reference as defined by
  47. * <a href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
  48. * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
  49. * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
  50. * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
  51. * also supports scope_ids. The syntax and usage of scope_ids is described
  52. * <a href="Inet6Address.html#scoped">here</a>.
  53. * This class provides constructors for creating URI instances from
  54. * their components or by parsing their string forms, methods for accessing the
  55. * various components of an instance, and methods for normalizing, resolving,
  56. * and relativizing URI instances. Instances of this class are immutable.
  57. *
  58. *
  59. * <h4> URI syntax and components </h4>
  60. *
  61. * At the highest level a URI reference (hereinafter simply "URI") in string
  62. * form has the syntax
  63. *
  64. * <blockquote>
  65. * [<i>scheme</i><tt><b>:</b></tt><i></i>]<i>scheme-specific-part</i>[<tt><b>#</b></tt><i>fragment</i>]
  66. * </blockquote>
  67. *
  68. * where square brackets [...] delineate optional components and the characters
  69. * <tt><b>:</b></tt> and <tt><b>#</b></tt> stand for themselves.
  70. *
  71. * <p> An <i>absolute</i> URI specifies a scheme; a URI that is not absolute is
  72. * said to be <i>relative</i>. URIs are also classified according to whether
  73. * they are <i>opaque</i> or <i>hierarchical</i>.
  74. *
  75. * <p> An <i>opaque</i> URI is an absolute URI whose scheme-specific part does
  76. * not begin with a slash character (<tt>'/'</tt>). Opaque URIs are not
  77. * subject to further parsing. Some examples of opaque URIs are:
  78. *
  79. * <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
  80. * <tr><td><tt>mailto:java-net@java.sun.com</tt><td></tr>
  81. * <tr><td><tt>news:comp.lang.java</tt><td></tr>
  82. * <tr><td><tt>urn:isbn:096139210x</tt></td></tr>
  83. * </table></blockquote>
  84. *
  85. * <p> A <i>hierarchical</i> URI is either an absolute URI whose
  86. * scheme-specific part begins with a slash character, or a relative URI, that
  87. * is, a URI that does not specify a scheme. Some examples of hierarchical
  88. * URIs are:
  89. *
  90. * <blockquote>
  91. * <tt>http://java.sun.com/j2se/1.3/</tt><br>
  92. * <tt>docs/guide/collections/designfaq.html#28</tt><br>
  93. * <tt>../../../demo/jfc/SwingSet2/src/SwingSet2.java</tt><br>
  94. * <tt>file:///~/calendar</tt>
  95. * </blockquote>
  96. *
  97. * <p> A hierarchical URI is subject to further parsing according to the syntax
  98. *
  99. * <blockquote>
  100. * [<i>scheme</i><tt><b>:</b></tt>][<tt><b>//</b></tt><i>authority</i>][<i>path</i>][<tt><b>?</b></tt><i>query</i>][<tt><b>#</b></tt><i>fragment</i>]
  101. * </blockquote>
  102. *
  103. * where the characters <tt><b>:</b></tt>, <tt><b>/</b></tt>,
  104. * <tt><b>?</b></tt>, and <tt><b>#</b></tt> stand for themselves. The
  105. * scheme-specific part of a hierarchical URI consists of the characters
  106. * between the scheme and fragment components.
  107. *
  108. * <p> The authority component of a hierarchical URI is, if specified, either
  109. * <i>server-based</i> or <i>registry-based</i>. A server-based authority
  110. * parses according to the familiar syntax
  111. *
  112. * <blockquote>
  113. * [<i>user-info</i><tt><b>@</b></tt>]<i>host</i>[<tt><b>:</b></tt><i>port</i>]
  114. * </blockquote>
  115. *
  116. * where the characters <tt><b>@</b></tt> and <tt><b>:</b></tt> stand for
  117. * themselves. Nearly all URI schemes currently in use are server-based. An
  118. * authority component that does not parse in this way is considered to be
  119. * registry-based.
  120. *
  121. * <p> The path component of a hierarchical URI is itself said to be absolute
  122. * if it begins with a slash character (<tt>'/'</tt>); otherwise it is
  123. * relative. The path of a hierarchical URI that is either absolute or
  124. * specifies an authority is always absolute.
  125. *
  126. * <p> All told, then, a URI instance has the following nine components:
  127. *
  128. * <blockquote><table summary="Describes the components of a URI:scheme,scheme-specific-part,authority,user-info,host,port,path,query,fragment">
  129. * <tr><th><i>Component</i></th><th><i>Type</i></th></tr>
  130. * <tr><td>scheme</td><td><tt>String</tt></td></tr>
  131. * <tr><td>scheme-specific-part&nbsp;&nbsp;&nbsp;&nbsp;</td><td><tt>String</tt></td></tr>
  132. * <tr><td>authority</td><td><tt>String</tt></td></tr>
  133. * <tr><td>user-info</td><td><tt>String</tt></td></tr>
  134. * <tr><td>host</td><td><tt>String</tt></td></tr>
  135. * <tr><td>port</td><td><tt>int</tt></td></tr>
  136. * <tr><td>path</td><td><tt>String</tt></td></tr>
  137. * <tr><td>query</td><td><tt>String</tt></td></tr>
  138. * <tr><td>fragment</td><td><tt>String</tt></td></tr>
  139. * </table></blockquote>
  140. *
  141. * In a given instance any particular component is either <i>undefined</i> or
  142. * <i>defined</i> with a distinct value. Undefined string components are
  143. * represented by <tt>null</tt>, while undefined integer components are
  144. * represented by <tt>-1</tt>. A string component may be defined to have the
  145. * empty string as its value; this is not equivalent to that component being
  146. * undefined.
  147. *
  148. * <p> Whether a particular component is or is not defined in an instance
  149. * depends upon the type of the URI being represented. An absolute URI has a
  150. * scheme component. An opaque URI has a scheme, a scheme-specific part, and
  151. * possibly a fragment, but has no other components. A hierarchical URI always
  152. * has a path (though it may be empty) and a scheme-specific-part (which at
  153. * least contains the path), and may have any of the other components. If the
  154. * authority component is present and is server-based then the host component
  155. * will be defined and the user-information and port components may be defined.
  156. *
  157. *
  158. * <h4> Operations on URI instances </h4>
  159. *
  160. * The key operations supported by this class are those of
  161. * <i>normalization</i>, <i>resolution</i>, and <i>relativization</i>.
  162. *
  163. * <p> <i>Normalization</i> is the process of removing unnecessary <tt>"."</tt>
  164. * and <tt>".."</tt> segments from the path component of a hierarchical URI.
  165. * Each <tt>"."</tt> segment is simply removed. A <tt>".."</tt> segment is
  166. * removed only if it is preceded by a non-<tt>".."</tt> segment.
  167. * Normalization has no effect upon opaque URIs.
  168. *
  169. * <p> <i>Resolution</i> is the process of resolving one URI against another,
  170. * <i>base</i> URI. The resulting URI is constructed from components of both
  171. * URIs in the manner specified by RFC&nbsp;2396, taking components from the
  172. * base URI for those not specified in the original. For hierarchical URIs,
  173. * the path of the original is resolved against the path of the base and then
  174. * normalized. The result, for example, of resolving
  175. *
  176. * <blockquote>
  177. * <tt>docs/guide/collections/designfaq.html#28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt>(1)
  178. * </blockquote>
  179. *
  180. * against the base URI <tt>http://java.sun.com/j2se/1.3/</tt> is the result
  181. * URI
  182. *
  183. * <blockquote>
  184. * <tt>http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28</tt>
  185. * </blockquote>
  186. *
  187. * Resolving the relative URI
  188. *
  189. * <blockquote>
  190. * <tt>../../../demo/jfc/SwingSet2/src/SwingSet2.java&nbsp;&nbsp;&nbsp;&nbsp;</tt>(2)
  191. * </blockquote>
  192. *
  193. * against this result yields, in turn,
  194. *
  195. * <blockquote>
  196. * <tt>http://java.sun.com/j2se/1.3/demo/jfc/SwingSet2/src/SwingSet2.java</tt>
  197. * </blockquote>
  198. *
  199. * Resolution of both absolute and relative URIs, and of both absolute and
  200. * relative paths in the case of hierarchical URIs, is supported. Resolving
  201. * the URI <tt>file:///~calendar</tt> against any other URI simply yields the
  202. * original URI, since it is absolute. Resolving the relative URI (2) above
  203. * against the relative base URI (1) yields the normalized, but still relative,
  204. * URI
  205. *
  206. * <blockquote>
  207. * <tt>demo/jfc/SwingSet2/src/SwingSet2.java</tt>
  208. * </blockquote>
  209. *
  210. * <p> <i>Relativization</i>, finally, is the inverse of resolution: For any
  211. * two normalized URIs <i>u</i> and&nbsp;<i>v</i>,
  212. *
  213. * <blockquote>
  214. * <i>u</i><tt>.relativize(</tt><i>u</i><tt>.resolve(</tt><i>v</i><tt>)).equals(</tt><i>v</i><tt>)</tt>&nbsp;&nbsp;and<br>
  215. * <i>u</i><tt>.resolve(</tt><i>u</i><tt>.relativize(</tt><i>v</i><tt>)).equals(</tt><i>v</i><tt>)</tt>&nbsp;&nbsp;.<br>
  216. * </blockquote>
  217. *
  218. * This operation is often useful when constructing a document containing URIs
  219. * that must be made relative to the base URI of the document wherever
  220. * possible. For example, relativizing the URI
  221. *
  222. * <blockquote>
  223. * <tt>http://java.sun.com/j2se/1.3/docs/guide/index.html</tt>
  224. * </blockquote>
  225. *
  226. * against the base URI
  227. *
  228. * <blockquote>
  229. * <tt>http://java.sun.com/j2se/1.3</tt>
  230. * </blockquote>
  231. *
  232. * yields the relative URI <tt>docs/guide/index.html</tt>.
  233. *
  234. *
  235. * <h4> Character categories </h4>
  236. *
  237. * RFC&nbsp;2396 specifies precisely which characters are permitted in the
  238. * various components of a URI reference. The following categories, most of
  239. * which are taken from that specification, are used below to describe these
  240. * constraints:
  241. *
  242. * <blockquote><table cellspacing=2 summary="Describes categories alpha,digit,alphanum,unreserved,punct,reserved,escaped,and other">
  243. * <tr><th valign=top><i>alpha</i></th>
  244. * <td>The US-ASCII alphabetic characters,
  245. * <tt>'A'</tt>&nbsp;through&nbsp;<tt>'Z'</tt>
  246. * and <tt>'a'</tt>&nbsp;through&nbsp;<tt>'z'</tt></td></tr>
  247. * <tr><th valign=top><i>digit</i></th>
  248. * <td>The US-ASCII decimal digit characters,
  249. * <tt>'0'</tt>&nbsp;through&nbsp;<tt>'9'</tt></td></tr>
  250. * <tr><th valign=top><i>alphanum</i></th>
  251. * <td>All <i>alpha</i> and <i>digit</i> characters</td></tr>
  252. * <tr><th valign=top><i>unreserved</i>&nbsp;&nbsp;&nbsp;&nbsp;</th>
  253. * <td>All <i>alphanum</i> characters together with those in the string
  254. * <tt>"_-!.~'()*"</tt></td></tr>
  255. * <tr><th valign=top><i>punct</i></th>
  256. * <td>The characters in the string <tt>",;:$&+="</tt></td></tr>
  257. * <tr><th valign=top><i>reserved</i></th>
  258. * <td>All <i>punct</i> characters together with those in the string
  259. * <tt>"?/[]@"</tt></td></tr>
  260. * <tr><th valign=top><i>escaped</i></th>
  261. * <td>Escaped octets, that is, triplets consisting of the percent
  262. * character (<tt>'%'</tt>) followed by two hexadecimal digits
  263. * (<tt>'0'</tt>-<tt>'9'</tt>, <tt>'A'</tt>-<tt>'F'</tt>, and
  264. * <tt>'a'</tt>-<tt>'f'</tt>)</td></tr>
  265. * <tr><th valign=top><i>other</i></th>
  266. * <td>The Unicode characters that are not in the US-ASCII character set,
  267. * are not control characters (according to the {@link
  268. * java.lang.Character#isISOControl(char) Character.isISOControl}
  269. * method), and are not space characters (according to the {@link
  270. * java.lang.Character#isSpaceChar(char) Character.isSpaceChar}
  271. * method)&nbsp;&nbsp;<i>(<b>Deviation from RFC 2396</b>, which is
  272. * limited to US-ASCII)</i></td></tr>
  273. * </table></blockquote>
  274. *
  275. * <p><a name="legal-chars"></a> The set of all legal URI characters consists of
  276. * the <i>unreserved</i>, <i>reserved</i>, <i>escaped</i>, and <i>other</i>
  277. * characters.
  278. *
  279. *
  280. * <h4> Escaped octets, quotation, encoding, and decoding </h4>
  281. *
  282. * RFC 2396 allows escaped octets to appear in the user-info, path, query, and
  283. * fragment components. Escaping serves two purposes in URIs:
  284. *
  285. * <ul>
  286. *
  287. * <li><p> To <i>encode</i> non-US-ASCII characters when a URI is required to
  288. * conform strictly to RFC&nbsp;2396 by not containing any <i>other</i>
  289. * characters. </p></li>
  290. *
  291. * <li><p> To <i>quote</i> characters that are otherwise illegal in a
  292. * component. The user-info, path, query, and fragment components differ
  293. * slightly in terms of which characters are considered legal and illegal.
  294. * </p></li>
  295. *
  296. * </ul>
  297. *
  298. * These purposes are served in this class by three related operations:
  299. *
  300. * <ul>
  301. *
  302. * <li><p><a name="encode"></a> A character is <i>encoded</i> by replacing it
  303. * with the sequence of escaped octets that represent that character in the
  304. * UTF-8 character set. The Euro currency symbol (<tt>'&#92;u20AC'</tt>),
  305. * for example, is encoded as <tt>"%E2%82%AC"</tt>. <i>(<b>Deviation from
  306. * RFC&nbsp;2396</b>, which does not specify any particular character
  307. * set.)</i> </p></li>
  308. *
  309. * <li><p><a name="quote"></a> An illegal character is <i>quoted</i> simply by
  310. * encoding it. The space character, for example, is quoted by replacing it
  311. * with <tt>"%20"</tt>. UTF-8 contains US-ASCII, hence for US-ASCII
  312. * characters this transformation has exactly the effect required by
  313. * RFC&nbsp;2396. </p></li>
  314. *
  315. * <li><p><a name="decode"></a>
  316. * A sequence of escaped octets is <i>decoded</i> by
  317. * replacing it with the sequence of characters that it represents in the
  318. * UTF-8 character set. UTF-8 contains US-ASCII, hence decoding has the
  319. * effect of de-quoting any quoted US-ASCII characters as well as that of
  320. * decoding any encoded non-US-ASCII characters. If a <a
  321. * href="../nio/charset/CharsetDecoder.html#ce">decoding error</a> occurs
  322. * when decoding the escaped octets then the erroneous octets are replaced by
  323. * <tt>'&#92;uFFFD'</tt>, the Unicode replacement character. </p></li>
  324. *
  325. * </ul>
  326. *
  327. * These operations are exposed in the constructors and methods of this class
  328. * as follows:
  329. *
  330. * <ul>
  331. *
  332. * <li><p> The {@link #URI(java.lang.String) <code>single-argument
  333. * constructor</code>} requires any illegal characters in its argument to be
  334. * quoted and preserves any escaped octets and <i>other</i> characters that
  335. * are present. </p></li>
  336. *
  337. * <li><p> The {@link
  338. * #URI(java.lang.String,java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String)
  339. * <code>multi-argument constructors</code>} quote illegal characters as
  340. * required by the components in which they appear. The percent character
  341. * (<tt>'%'</tt>) is always quoted by these constructors. Any <i>other</i>
  342. * characters are preserved. </p></li>
  343. *
  344. * <li><p> The {@link #getRawUserInfo() getRawUserInfo}, {@link #getRawPath()
  345. * getRawPath}, {@link #getRawQuery() getRawQuery}, {@link #getRawFragment()
  346. * getRawFragment}, {@link #getRawAuthority() getRawAuthority}, and {@link
  347. * #getRawSchemeSpecificPart() getRawSchemeSpecificPart} methods return the
  348. * values of their corresponding components in raw form, without interpreting
  349. * any escaped octets. The strings returned by these methods may contain
  350. * both escaped octets and <i>other</i> characters, and will not contain any
  351. * illegal characters. </p></li>
  352. *
  353. * <li><p> The {@link #getUserInfo() getUserInfo}, {@link #getPath()
  354. * getPath}, {@link #getQuery() getQuery}, {@link #getFragment()
  355. * getFragment}, {@link #getAuthority() getAuthority}, and {@link
  356. * #getSchemeSpecificPart() getSchemeSpecificPart} methods decode any escaped
  357. * octets in their corresponding components. The strings returned by these
  358. * methods may contain both <i>other</i> characters and illegal characters,
  359. * and will not contain any escaped octets. </p></li>
  360. *
  361. * <li><p> The {@link #toString() toString} method returns a URI string with
  362. * all necessary quotation but which may contain <i>other</i> characters.
  363. * </p></li>
  364. *
  365. * <li><p> The {@link #toASCIIString() toASCIIString} method returns a fully
  366. * quoted and encoded URI string that does not contain any <i>other</i>
  367. * characters. </p></li>
  368. *
  369. * </ul>
  370. *
  371. *
  372. * <h4> Identities </h4>
  373. *
  374. * For any URI <i>u</i>, it is always the case that
  375. *
  376. * <blockquote>
  377. * <tt>new URI(</tt><i>u</i><tt>.toString()).equals(</tt><i>u</i><tt>)</tt>&nbsp;.
  378. * </blockquote>
  379. *
  380. * For any URI <i>u</i> that does not contain redundant syntax such as two
  381. * slashes before an empty authority (as in <tt>file:///tmp/</tt>&nbsp;) or a
  382. * colon following a host name but no port (as in
  383. * <tt>http://java.sun.com:</tt>&nbsp;), and that does not encode characters
  384. * except those that must be quoted, the following identities also hold:
  385. *
  386. * <blockquote>
  387. * <tt>new URI(</tt><i>u</i><tt>.getScheme(),<br>
  388. * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getSchemeSpecificPart(),<br>
  389. * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getFragment())<br>
  390. * .equals(</tt><i>u</i><tt>)</tt>
  391. * </blockquote>
  392. *
  393. * in all cases,
  394. *
  395. * <blockquote>
  396. * <tt>new URI(</tt><i>u</i><tt>.getScheme(),<br>
  397. * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getUserInfo(),&nbsp;</tt><i>u</i><tt>.getAuthority(),<br>
  398. * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getPath(),&nbsp;</tt><i>u</i><tt>.getQuery(),<br>
  399. * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getFragment())<br>
  400. * .equals(</tt><i>u</i><tt>)</tt>
  401. * </blockquote>
  402. *
  403. * if <i>u</i> is hierarchical, and
  404. *
  405. * <blockquote>
  406. * <tt>new URI(</tt><i>u</i><tt>.getScheme(),<br>
  407. * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getUserInfo(),&nbsp;</tt><i>u</i><tt>.getHost(),&nbsp;</tt><i>u</i><tt>.getPort(),<br>
  408. * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getPath(),&nbsp;</tt><i>u</i><tt>.getQuery(),<br>
  409. * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getFragment())<br>
  410. * .equals(</tt><i>u</i><tt>)</tt>
  411. * </blockquote>
  412. *
  413. * if <i>u</i> is hierarchical and has either no authority or a server-based
  414. * authority.
  415. *
  416. *
  417. * <h4> URIs, URLs, and URNs </h4>
  418. *
  419. * A URI is a uniform resource <i>identifier</i> while a URL is a uniform
  420. * resource <i>locator</i>. Hence every URL is a URI, abstractly speaking, but
  421. * not every URI is a URL. This is because there is another subcategory of
  422. * URIs, uniform resource <i>names</i> (URNs), which name resources but do not
  423. * specify how to locate them. The <tt>mailto</tt>, <tt>news</tt>, and
  424. * <tt>isbn</tt> URIs shown above are examples of URNs.
  425. *
  426. * <p> The conceptual distinction between URIs and URLs is reflected in the
  427. * differences between this class and the {@link URL} class.
  428. *
  429. * <p> An instance of this class represents a URI reference in the syntactic
  430. * sense defined by RFC&nbsp;2396. A URI may be either absolute or relative.
  431. * A URI string is parsed according to the generic syntax without regard to the
  432. * scheme, if any, that it specifies. No lookup of the host, if any, is
  433. * performed, and no scheme-dependent stream handler is constructed. Equality,
  434. * hashing, and comparison are defined strictly in terms of the character
  435. * content of the instance. In other words, a URI instance is little more than
  436. * a structured string that supports the syntactic, scheme-independent
  437. * operations of comparison, normalization, resolution, and relativization.
  438. *
  439. * <p> An instance of the {@link URL} class, by contrast, represents the
  440. * syntactic components of a URL together with some of the information required
  441. * to access the resource that it describes. A URL must be absolute, that is,
  442. * it must always specify a scheme. A URL string is parsed according to its
  443. * scheme. A stream handler is always established for a URL, and in fact it is
  444. * impossible to create a URL instance for a scheme for which no handler is
  445. * available. Equality and hashing depend upon both the scheme and the
  446. * Internet address of the host, if any; comparison is not defined. In other
  447. * words, a URL is a structured string that supports the syntactic operation of
  448. * resolution as well as the network I/O operations of looking up the host and
  449. * opening a connection to the specified resource.
  450. *
  451. *
  452. * @author Mark Reinhold
  453. * @since 1.4
  454. *
  455. * @see <a href="http://www.ietf.org/rfc/rfc2279.txt"><i>RFC&nbsp;2279: UTF-8, a
  456. * transformation format of ISO 10646</i></a>, <br><a
  457. * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IPv6 Addressing
  458. * Architecture</i></a>, <br><a
  459. * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
  460. * Resource Identifiers (URI): Generic Syntax</i></a>, <br><a
  461. * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
  462. * Literal IPv6 Addresses in URLs</i></a>, <br><a
  463. * href="URISyntaxException.html">URISyntaxException</a>
  464. */
  465. public final class URI
  466. implements Comparable<URI>, Serializable
  467. {
  468. // Note: Comments containing the word "ASSERT" indicate places where a
  469. // throw of an InternalError should be replaced by an appropriate assertion
  470. // statement once asserts are enabled in the build.
  471. static final long serialVersionUID = -6052424284110960213L;
  472. // -- Properties and components of this instance --
  473. // Components of all URIs: [<scheme>:]<scheme-specific-part>[#<fragment>]
  474. private transient String scheme; // null ==> relative URI
  475. private transient String fragment;
  476. // Hierarchical URI components: [//<authority>]<path>[?<query>]
  477. private transient String authority; // Registry or server
  478. // Server-based authority: [<userInfo>@]<host>[:<port>]
  479. private transient String userInfo;
  480. private transient String host; // null ==> registry-based
  481. private transient int port = -1; // -1 ==> undefined
  482. // Remaining components of hierarchical URIs
  483. private transient String path; // null ==> opaque
  484. private transient String query;
  485. // The remaining fields may be computed on demand
  486. private volatile transient String schemeSpecificPart;
  487. private volatile transient int hash; // Zero ==> undefined
  488. private volatile transient String decodedUserInfo = null;
  489. private volatile transient String decodedAuthority = null;
  490. private volatile transient String decodedPath = null;
  491. private volatile transient String decodedQuery = null;
  492. private volatile transient String decodedFragment = null;
  493. private volatile transient String decodedSchemeSpecificPart = null;
  494. /**
  495. * The string form of this URI.
  496. *
  497. * @serial
  498. */
  499. private volatile String string; // The only serializable field
  500. // -- Constructors and factories --
  501. private URI() { } // Used internally
  502. /**
  503. * Constructs a URI by parsing the given string.
  504. *
  505. * <p> This constructor parses the given string exactly as specified by the
  506. * grammar in <a
  507. * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  508. * Appendix&nbsp;A, <b><i>except for the following deviations:</i></b> </p>
  509. *
  510. * <ul type=disc>
  511. *
  512. * <li><p> An empty authority component is permitted as long as it is
  513. * followed by a non-empty path, a query component, or a fragment
  514. * component. This allows the parsing of URIs such as
  515. * <tt>"file:///foo/bar"</tt>, which seems to be the intent of
  516. * RFC&nbsp;2396 although the grammar does not permit it. If the
  517. * authority component is empty then the user-information, host, and port
  518. * components are undefined. </p></li>
  519. *
  520. * <li><p> Empty relative paths are permitted; this seems to be the
  521. * intent of RFC&nbsp;2396 although the grammar does not permit it. The
  522. * primary consequence of this deviation is that a standalone fragment
  523. * such as <tt>"#foo"</tt> parses as a relative URI with an empty path
  524. * and the given fragment, and can be usefully <a
  525. * href="#resolve-frag">resolved</a> against a base URI.
  526. *
  527. * <li><p> IPv4 addresses in host components are parsed rigorously, as
  528. * specified by <a
  529. * href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>: Each
  530. * element of a dotted-quad address must contain no more than three
  531. * decimal digits. Each element is further constrained to have a value
  532. * no greater than 255. </p></li>
  533. *
  534. * <li> <p> Hostnames in host components that comprise only a single
  535. * domain label are permitted to start with an <i>alphanum</i>
  536. * character. This seems to be the intent of <a
  537. * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>
  538. * section&nbsp;3.2.2 although the grammar does not permit it. The
  539. * consequence of this deviation is that the authority component of a
  540. * hierarchical URI such as <tt>s://123</tt>, will parse as a server-based
  541. * authority. </p></li>
  542. *
  543. * <li><p> IPv6 addresses are permitted for the host component. An IPv6
  544. * address must be enclosed in square brackets (<tt>'['</tt> and
  545. * <tt>']'</tt>) as specified by <a
  546. * href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>. The
  547. * IPv6 address itself must parse according to <a
  548. * href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>. IPv6
  549. * addresses are further constrained to describe no more than sixteen
  550. * bytes of address information, a constraint implicit in RFC&nbsp;2373
  551. * but not expressible in the grammar. </p></li>
  552. *
  553. * <li><p> Characters in the <i>other</i> category are permitted wherever
  554. * RFC&nbsp;2396 permits <i>escaped</i> octets, that is, in the
  555. * user-information, path, query, and fragment components, as well as in
  556. * the authority component if the authority is registry-based. This
  557. * allows URIs to contain Unicode characters beyond those in the US-ASCII
  558. * character set. </p></li>
  559. *
  560. * </ul>
  561. *
  562. * @param str The string to be parsed into a URI
  563. *
  564. * @throws NullPointerException
  565. * If <tt>str</tt> is <tt>null</tt>
  566. *
  567. * @throws URISyntaxException
  568. * If the given string violates RFC&nbsp;2396, as augmented
  569. * by the above deviations
  570. */
  571. public URI(String str) throws URISyntaxException {
  572. new Parser(str).parse(false);
  573. }
  574. /**
  575. * Constructs a hierarchical URI from the given components.
  576. *
  577. * <p> If a scheme is given then the path, if also given, must either be
  578. * empty or begin with a slash character (<tt>'/'</tt>). Otherwise a
  579. * component of the new URI may be left undefined by passing <tt>null</tt>
  580. * for the corresponding parameter or, in the case of the <tt>port</tt>
  581. * parameter, by passing <tt>-1</tt>.
  582. *
  583. * <p> This constructor first builds a URI string from the given components
  584. * according to the rules specified in <a
  585. * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  586. * section&nbsp;5.2, step&nbsp;7: </p>
  587. *
  588. * <ol>
  589. *
  590. * <li><p> Initially, the result string is empty. </p></li>
  591. *
  592. * <li><p> If a scheme is given then it is appended to the result,
  593. * followed by a colon character (<tt>':'</tt>). </p></li>
  594. *
  595. * <li><p> If user information, a host, or a port are given then the
  596. * string <tt>"//"</tt> is appended. </p></li>
  597. *
  598. * <li><p> If user information is given then it is appended, followed by
  599. * a commercial-at character (<tt>'@'</tt>). Any character not in the
  600. * <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
  601. * categories is <a href="#quote">quoted</a>. </p></li>
  602. *
  603. * <li><p> If a host is given then it is appended. If the host is a
  604. * literal IPv6 address but is not enclosed in square brackets
  605. * (<tt>'['</tt> and <tt>']'</tt>) then the square brackets are added.
  606. * </p></li>
  607. *
  608. * <li><p> If a port number is given then a colon character
  609. * (<tt>':'</tt>) is appended, followed by the port number in decimal.
  610. * </p></li>
  611. *
  612. * <li><p> If a path is given then it is appended. Any character not in
  613. * the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
  614. * categories, and not equal to the slash character (<tt>'/'</tt>) or the
  615. * commercial-at character (<tt>'@'</tt>), is quoted. </p></li>
  616. *
  617. * <li><p> If a query is given then a question-mark character
  618. * (<tt>'?'</tt>) is appended, followed by the query. Any character that
  619. * is not a <a href="#legal-chars">legal URI character</a> is quoted.
  620. * </p></li>
  621. *
  622. * <li><p> Finally, if a fragment is given then a hash character
  623. * (<tt>'#'</tt>) is appended, followed by the fragment. Any character
  624. * that is not a legal URI character is quoted. </p></li>
  625. *
  626. * </ol>
  627. *
  628. * <p> The resulting URI string is then parsed as if by invoking the {@link
  629. * #URI(String)} constructor and then invoking the {@link
  630. * #parseServerAuthority()} method upon the result; this may cause a {@link
  631. * URISyntaxException} to be thrown. </p>
  632. *
  633. * @param scheme Scheme name
  634. * @param userInfo User name and authorization information
  635. * @param host Host name
  636. * @param port Port number
  637. * @param path Path
  638. * @param query Query
  639. * @param fragment Fragment
  640. *
  641. * @throws URISyntaxException
  642. * If both a scheme and a path are given but the path is relative,
  643. * if the URI string constructed from the given components violates
  644. * RFC&nbsp;2396, or if the authority component of the string is
  645. * present but cannot be parsed as a server-based authority
  646. */
  647. public URI(String scheme,
  648. String userInfo, String host, int port,
  649. String path, String query, String fragment)
  650. throws URISyntaxException
  651. {
  652. String s = toString(scheme, null,
  653. null, userInfo, host, port,
  654. path, query, fragment);
  655. checkPath(s, scheme, path);
  656. new Parser(s).parse(true);
  657. }
  658. /**
  659. * Constructs a hierarchical URI from the given components.
  660. *
  661. * <p> If a scheme is given then the path, if also given, must either be
  662. * empty or begin with a slash character (<tt>'/'</tt>). Otherwise a
  663. * component of the new URI may be left undefined by passing <tt>null</tt>
  664. * for the corresponding parameter.
  665. *
  666. * <p> This constructor first builds a URI string from the given components
  667. * according to the rules specified in <a
  668. * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  669. * section&nbsp;5.2, step&nbsp;7: </p>
  670. *
  671. * <ol>
  672. *
  673. * <li><p> Initially, the result string is empty. </p></li>
  674. *
  675. * <li><p> If a scheme is given then it is appended to the result,
  676. * followed by a colon character (<tt>':'</tt>). </p></li>
  677. *
  678. * <li><p> If an authority is given then the string <tt>"//"</tt> is
  679. * appended, followed by the authority. If the authority contains a
  680. * literal IPv6 address then the address must be enclosed in square
  681. * brackets (<tt>'['</tt> and <tt>']'</tt>). Any character not in the
  682. * <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
  683. * categories, and not equal to the commercial-at character
  684. * (<tt>'@'</tt>), is <a href="#quote">quoted</a>. </p></li>
  685. *
  686. * <li><p> If a path is given then it is appended. Any character not in
  687. * the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
  688. * categories, and not equal to the slash character (<tt>'/'</tt>) or the
  689. * commercial-at character (<tt>'@'</tt>), is quoted. </p></li>
  690. *
  691. * <li><p> If a query is given then a question-mark character
  692. * (<tt>'?'</tt>) is appended, followed by the query. Any character that
  693. * is not a <a href="#legal-chars">legal URI character</a> is quoted.
  694. * </p></li>
  695. *
  696. * <li><p> Finally, if a fragment is given then a hash character
  697. * (<tt>'#'</tt>) is appended, followed by the fragment. Any character
  698. * that is not a legal URI character is quoted. </p></li>
  699. *
  700. * </ol>
  701. *
  702. * <p> The resulting URI string is then parsed as if by invoking the {@link
  703. * #URI(String)} constructor and then invoking the {@link
  704. * #parseServerAuthority()} method upon the result; this may cause a {@link
  705. * URISyntaxException} to be thrown. </p>
  706. *
  707. * @param scheme Scheme name
  708. * @param authority Authority
  709. * @param path Path
  710. * @param query Query
  711. * @param fragment Fragment
  712. *
  713. * @throws URISyntaxException
  714. * If both a scheme and a path are given but the path is relative,
  715. * if the URI string constructed from the given components violates
  716. * RFC&nbsp;2396, or if the authority component of the string is
  717. * present but cannot be parsed as a server-based authority
  718. */
  719. public URI(String scheme,
  720. String authority,
  721. String path, String query, String fragment)
  722. throws URISyntaxException
  723. {
  724. String s = toString(scheme, null,
  725. authority, null, null, -1,
  726. path, query, fragment);
  727. checkPath(s, scheme, path);
  728. new Parser(s).parse(false);
  729. }
  730. /**
  731. * Constructs a hierarchical URI from the given components.
  732. *
  733. * <p> A component may be left undefined by passing <tt>null</tt>.
  734. *
  735. * <p> This convenience constructor works as if by invoking the
  736. * seven-argument constructor as follows:
  737. *
  738. * <blockquote><tt>
  739. * new&nbsp;{@link #URI(String, String, String, int, String, String, String)
  740. * URI}(scheme,&nbsp;null,&nbsp;host,&nbsp;-1,&nbsp;path,&nbsp;null,&nbsp;fragment);
  741. * </tt></blockquote>
  742. *
  743. * @param scheme Scheme name
  744. * @param host Host name
  745. * @param path Path
  746. * @param fragment Fragment
  747. *
  748. * @throws URISyntaxException
  749. * If the URI string constructed from the given components
  750. * violates RFC&nbsp;2396
  751. */
  752. public URI(String scheme, String host, String path, String fragment)
  753. throws URISyntaxException
  754. {
  755. this(scheme, null, host, -1, path, null, fragment);
  756. }
  757. /**
  758. * Constructs a URI from the given components.
  759. *
  760. * <p> A component may be left undefined by passing <tt>null</tt>.
  761. *
  762. * <p> This constructor first builds a URI in string form using the given
  763. * components as follows: </p>
  764. *
  765. * <ol>
  766. *
  767. * <li><p> Initially, the result string is empty. </p></li>
  768. *
  769. * <li><p> If a scheme is given then it is appended to the result,
  770. * followed by a colon character (<tt>':'</tt>). </p></li>
  771. *
  772. * <li><p> If a scheme-specific part is given then it is appended. Any
  773. * character that is not a <a href="#legal-chars">legal URI character</a>
  774. * is <a href="#quote">quoted</a>. </p></li>
  775. *
  776. * <li><p> Finally, if a fragment is given then a hash character
  777. * (<tt>'#'</tt>) is appended to the string, followed by the fragment.
  778. * Any character that is not a legal URI character is quoted. </p></li>
  779. *
  780. * </ol>
  781. *
  782. * <p> The resulting URI string is then parsed in order to create the new
  783. * URI instance as if by invoking the {@link #URI(String)} constructor;
  784. * this may cause a {@link URISyntaxException} to be thrown. </p>
  785. *
  786. * @param scheme Scheme name
  787. * @param ssp Scheme-specific part
  788. * @param fragment Fragment
  789. *
  790. * @throws URISyntaxException
  791. * If the URI string constructed from the given components
  792. * violates RFC&nbsp;2396
  793. */
  794. public URI(String scheme, String ssp, String fragment)
  795. throws URISyntaxException
  796. {
  797. new Parser(toString(scheme, ssp,
  798. null, null, null, -1,
  799. null, null, fragment))
  800. .parse(false);
  801. }
  802. /**
  803. * Creates a URI by parsing the given string.
  804. *
  805. * <p> This convenience factory method works as if by invoking the {@link
  806. * #URI(String)} constructor; any {@link URISyntaxException} thrown by the
  807. * constructor is caught and wrapped in a new {@link
  808. * IllegalArgumentException} object, which is then thrown.
  809. *
  810. * <p> This method is provided for use in situations where it is known that
  811. * the given string is a legal URI, for example for URI constants declared
  812. * within in a program, and so it would be considered a programming error
  813. * for the string not to parse as such. The constructors, which throw
  814. * {@link URISyntaxException} directly, should be used situations where a
  815. * URI is being constructed from user input or from some other source that
  816. * may be prone to errors. </p>
  817. *
  818. * @param str The string to be parsed into a URI
  819. * @return The new URI
  820. *
  821. * @throws NullPointerException
  822. * If <tt>str</tt> is <tt>null</tt>
  823. *
  824. * @throws IllegalArgumentException
  825. * If the given string violates RFC&nbsp;2396
  826. */
  827. public static URI create(String str) {
  828. try {
  829. return new URI(str);
  830. } catch (URISyntaxException x) {
  831. throw new IllegalArgumentException(x.getMessage(), x);
  832. }
  833. }
  834. // -- Operations --
  835. /**
  836. * Attempts to parse this URI's authority component, if defined, into
  837. * user-information, host, and port components.
  838. *
  839. * <p> If this URI's authority component has already been recognized as
  840. * being server-based then it will already have been parsed into
  841. * user-information, host, and port components. In this case, or if this
  842. * URI has no authority component, this method simply returns this URI.
  843. *
  844. * <p> Otherwise this method attempts once more to parse the authority
  845. * component into user-information, host, and port components, and throws
  846. * an exception describing why the authority component could not be parsed
  847. * in that way.
  848. *
  849. * <p> This method is provided because the generic URI syntax specified in
  850. * <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>
  851. * cannot always distinguish a malformed server-based authority from a
  852. * legitimate registry-based authority. It must therefore treat some
  853. * instances of the former as instances of the latter. The authority
  854. * component in the URI string <tt>"//foo:bar"</tt>, for example, is not a
  855. * legal server-based authority but it is legal as a registry-based
  856. * authority.
  857. *
  858. * <p> In many common situations, for example when working URIs that are
  859. * known to be either URNs or URLs, the hierarchical URIs being used will
  860. * always be server-based. They therefore must either be parsed as such or
  861. * treated as an error. In these cases a statement such as
  862. *
  863. * <blockquote>
  864. * <tt>URI </tt><i>u</i><tt> = new URI(str).parseServerAuthority();</tt>
  865. * </blockquote>
  866. *
  867. * <p> can be used to ensure that <i>u</i> always refers to a URI that, if
  868. * it has an authority component, has a server-based authority with proper
  869. * user-information, host, and port components. Invoking this method also
  870. * ensures that if the authority could not be parsed in that way then an
  871. * appropriate diagnostic message can be issued based upon the exception
  872. * that is thrown. </p>
  873. *
  874. * @return A URI whose authority field has been parsed
  875. * as a server-based authority
  876. *
  877. * @throws URISyntaxException
  878. * If the authority component of this URI is defined
  879. * but cannot be parsed as a server-based authority
  880. * according to RFC&nbsp;2396
  881. */
  882. public URI parseServerAuthority()
  883. throws URISyntaxException
  884. {
  885. // We could be clever and cache the error message and index from the
  886. // exception thrown during the original parse, but that would require
  887. // either more fields or a more-obscure representation.
  888. if ((host != null) || (authority == null))
  889. return this;
  890. defineString();
  891. new Parser(string).parse(true);
  892. return this;
  893. }
  894. /**
  895. * Normalizes this URI's path.
  896. *
  897. * <p> If this URI is opaque, or if its path is already in normal form,
  898. * then this URI is returned. Otherwise a new URI is constructed that is
  899. * identical to this URI except that its path is computed by normalizing
  900. * this URI's path in a manner consistent with <a
  901. * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  902. * section&nbsp;5.2, step&nbsp;6, sub-steps&nbsp;c through&nbsp;f; that is:
  903. * </p>
  904. *
  905. * <ol>
  906. *
  907. * <li><p> All <tt>"."</tt> segments are removed. </p></li>
  908. *
  909. * <li><p> If a <tt>".."</tt> segment is preceded by a non-<tt>".."</tt>
  910. * segment then both of these segments are removed. This step is
  911. * repeated until it is no longer applicable. </p></li>
  912. *
  913. * <li><p> If the path is relative, and if its first segment contains a
  914. * colon character (<tt>':'</tt>), then a <tt>"."</tt> segment is
  915. * prepended. This prevents a relative URI with a path such as
  916. * <tt>"a:b/c/d"</tt> from later being re-parsed as an opaque URI with a
  917. * scheme of <tt>"a"</tt> and a scheme-specific part of <tt>"b/c/d"</tt>.
  918. * <b><i>(Deviation from RFC&nbsp;2396)</i></b> </p></li>
  919. *
  920. * </ol>
  921. *
  922. * <p> A normalized path will begin with one or more <tt>".."</tt> segments
  923. * if there were insufficient non-<tt>".."</tt> segments preceding them to
  924. * allow their removal. A normalized path will begin with a <tt>"."</tt>
  925. * segment if one was inserted by step 3 above. Otherwise, a normalized
  926. * path will not contain any <tt>"."</tt> or <tt>".."</tt> segments. </p>
  927. *
  928. * @return A URI equivalent to this URI,
  929. * but whose path is in normal form
  930. */
  931. public URI normalize() {
  932. return normalize(this);
  933. }
  934. /**
  935. * Resolves the given URI against this URI.
  936. *
  937. * <p> If the given URI is already absolute, or if this URI is opaque, then
  938. * the given URI is returned.
  939. *
  940. * <p><a name="resolve-frag"></a> If the given URI's fragment component is
  941. * defined, its path component is empty, and its scheme, authority, and
  942. * query components are undefined, then a URI with the given fragment but
  943. * with all other components equal to those of this URI is returned. This
  944. * allows a URI representing a standalone fragment reference, such as
  945. * <tt>"#foo"</tt>, to be usefully resolved against a base URI.
  946. *
  947. * <p> Otherwise this method constructs a new hierarchical URI in a manner
  948. * consistent with <a
  949. * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  950. * section&nbsp;5.2; that is: </p>
  951. *
  952. * <ol>
  953. *
  954. * <li><p> A new URI is constructed with this URI's scheme and the given
  955. * URI's query and fragment components. </p></li>
  956. *
  957. * <li><p> If the given URI has an authority component then the new URI's
  958. * authority and path are taken from the given URI. </p></li>
  959. *
  960. * <li><p> Otherwise the new URI's authority component is copied from
  961. * this URI, and its path is computed as follows: </p>
  962. *
  963. * <ol type=a>
  964. *
  965. * <li><p> If the given URI's path is absolute then the new URI's path
  966. * is taken from the given URI. </p></li>
  967. *
  968. * <li><p> Otherwise the given URI's path is relative, and so the new
  969. * URI's path is computed by resolving the path of the given URI
  970. * against the path of this URI. This is done by concatenating all but
  971. * the last segment of this URI's path, if any, with the given URI's
  972. * path and then normalizing the result as if by invoking the {@link
  973. * #normalize() normalize} method. </p></li>
  974. *
  975. * </ol></li>
  976. *
  977. * </ol>
  978. *
  979. * <p> The result of this method is absolute if, and only if, either this
  980. * URI is absolute or the given URI is absolute. </p>
  981. *
  982. * @param uri The URI to be resolved against this URI
  983. * @return The resulting URI
  984. *
  985. * @throws NullPointerException
  986. * If <tt>uri</tt> is <tt>null</tt>
  987. */
  988. public URI resolve(URI uri) {
  989. return resolve(this, uri);
  990. }
  991. /**
  992. * Constructs a new URI by parsing the given string and then resolving it
  993. * against this URI.
  994. *
  995. * <p> This convenience method works as if invoking it were equivalent to
  996. * evaluating the expression <tt>{@link #resolve(java.net.URI)
  997. * resolve}(URI.{@link #create(String) create}(str))</tt>. </p>
  998. *
  999. * @param str The string to be parsed into a URI
  1000. * @return The resulting URI
  1001. *
  1002. * @throws NullPointerException
  1003. * If <tt>str</tt> is <tt>null</tt>
  1004. *
  1005. * @throws IllegalArgumentException
  1006. * If the given string violates RFC&nbsp;2396
  1007. */
  1008. public URI resolve(String str) {
  1009. return resolve(URI.create(str));
  1010. }
  1011. /**
  1012. * Relativizes the given URI against this URI.
  1013. *
  1014. * <p> The relativization of the given URI against this URI is computed as
  1015. * follows: </p>
  1016. *
  1017. * <ol>
  1018. *
  1019. * <li><p> If either this URI or the given URI are opaque, or if the
  1020. * scheme and authority components of the two URIs are not identical, or
  1021. * if the path of this URI is not a prefix of the path of the given URI,
  1022. * then the given URI is returned. </p></li>
  1023. *
  1024. * <li><p> Otherwise a new relative hierarchical URI is constructed with
  1025. * query and fragment components taken from the given URI and with a path
  1026. * component computed by removing this URI's path from the beginning of
  1027. * the given URI's path. </p></li>
  1028. *
  1029. * </ol>
  1030. *
  1031. * @param uri The URI to be relativized against this URI
  1032. * @return The resulting URI
  1033. *
  1034. * @throws NullPointerException
  1035. * If <tt>uri</tt> is <tt>null</tt>
  1036. */
  1037. public URI relativize(URI uri) {
  1038. return relativize(this, uri);
  1039. }
  1040. /**
  1041. * Constructs a URL from this URI.
  1042. *
  1043. * <p> This convenience method works as if invoking it were equivalent to
  1044. * evaluating the expression <tt>new&nbsp;URL(this.toString())</tt> after
  1045. * first checking that this URI is absolute. </p>
  1046. *
  1047. * @return A URL constructed from this URI
  1048. *
  1049. * @throws IllegalArgumentException
  1050. * If this URL is not absolute
  1051. *
  1052. * @throws MalformedURLException
  1053. * If a protocol handler for the URL could not be found,
  1054. * or if some other error occurred while constructing the URL
  1055. */
  1056. public URL toURL()
  1057. throws MalformedURLException {
  1058. if (!isAbsolute())
  1059. throw new IllegalArgumentException("URI is not absolute");
  1060. return new URL(toString());
  1061. }
  1062. // -- Component access methods --
  1063. /**
  1064. * Returns the scheme component of this URI.
  1065. *
  1066. * <p> The scheme component of a URI, if defined, only contains characters
  1067. * in the <i>alphanum</i> category and in the string <tt>"-.+"</tt>. A
  1068. * scheme always starts with an <i>alpha</i> character. <p>
  1069. *
  1070. * The scheme component of a URI cannot contain escaped octets, hence this
  1071. * method does not perform any decoding.
  1072. *
  1073. * @return The scheme component of this URI,
  1074. * or <tt>null</tt> if the scheme is undefined
  1075. */
  1076. public String getScheme() {
  1077. return scheme;
  1078. }
  1079. /**
  1080. * Tells whether or not this URI is absolute.
  1081. *
  1082. * <p> A URI is absolute if, and only if, it has a scheme component. </p>
  1083. *
  1084. * @return <tt>true</tt> if, and only if, this URI is absolute
  1085. */
  1086. public boolean isAbsolute() {
  1087. return scheme != null;
  1088. }
  1089. /**
  1090. * Tells whether or not this URI is opaque.
  1091. *
  1092. * <p> A URI is opaque if, and only if, it is absolute and its
  1093. * scheme-specific part does not begin with a slash character ('/').
  1094. * An opaque URI has a scheme, a scheme-specif…

Large files files are truncated, but you can click here to view the full file