jdk/src/share/classes/java/net/URI.java
author juh
Tue, 30 Jul 2013 11:04:19 -0700
changeset 19069 1d9cb0d080e3
parent 19049 543b417ea7b2
child 20455 f6f9a0c2796b
permissions -rw-r--r--
8021833: javadoc cleanup in java.net Summary: <code> and <tt> converted to {@code }; package.html to package-info.java Reviewed-by: darcy, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.charset.CodingErrorAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.nio.charset.CharacterCodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.text.Normalizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.nio.cs.ThreadLocalCoders;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.Character;             // for javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.lang.NullPointerException;  // for javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Represents a Uniform Resource Identifier (URI) reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p> Aside from some minor deviations noted below, an instance of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * class represents a URI reference as defined by
708
a780486c413c 6630348: Invalid html tags (extra double quote)
chegar
parents: 2
diff changeset
    52
 * <a href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * also supports scope_ids. The syntax and usage of scope_ids is described
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <a href="Inet6Address.html#scoped">here</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * This class provides constructors for creating URI instances from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * their components or by parsing their string forms, methods for accessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * various components of an instance, and methods for normalizing, resolving,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * and relativizing URI instances.  Instances of this class are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
    64
 * <h3> URI syntax and components </h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * At the highest level a URI reference (hereinafter simply "URI") in string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * form has the syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    70
 * [<i>scheme</i><b>{@code :}</b>]<i>scheme-specific-part</i>[<b>{@code #}</b><i>fragment</i>]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * where square brackets [...] delineate optional components and the characters
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    74
 * <b>{@code :}</b> and <b>{@code #}</b> stand for themselves.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p> An <i>absolute</i> URI specifies a scheme; a URI that is not absolute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * said to be <i>relative</i>.  URIs are also classified according to whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * they are <i>opaque</i> or <i>hierarchical</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p> An <i>opaque</i> URI is an absolute URI whose scheme-specific part does
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    81
 * not begin with a slash character ({@code '/'}).  Opaque URIs are not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * subject to further parsing.  Some examples of opaque URIs are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    85
 * <tr><td>{@code mailto:java-net@java.sun.com}<td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    86
 * <tr><td>{@code news:comp.lang.java}<td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    87
 * <tr><td>{@code urn:isbn:096139210x}</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p> A <i>hierarchical</i> URI is either an absolute URI whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * scheme-specific part begins with a slash character, or a relative URI, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * is, a URI that does not specify a scheme.  Some examples of hierarchical
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * URIs are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    96
 * {@code http://java.sun.com/j2se/1.3/}<br>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    97
 * {@code docs/guide/collections/designfaq.html#28}<br>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    98
 * {@code ../../../demo/jfc/SwingSet2/src/SwingSet2.java}<br>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
    99
 * {@code file:///~/calendar}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p> A hierarchical URI is subject to further parsing according to the syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   105
 * [<i>scheme</i><b>{@code :}</b>][<b>{@code //}</b><i>authority</i>][<i>path</i>][<b>{@code ?}</b><i>query</i>][<b>{@code #}</b><i>fragment</i>]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   108
 * where the characters <b>{@code :}</b>, <b>{@code /}</b>,
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   109
 * <b>{@code ?}</b>, and <b>{@code #}</b> stand for themselves.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * scheme-specific part of a hierarchical URI consists of the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * between the scheme and fragment components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <p> The authority component of a hierarchical URI is, if specified, either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <i>server-based</i> or <i>registry-based</i>.  A server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * parses according to the familiar syntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   118
 * [<i>user-info</i><b>{@code @}</b>]<i>host</i>[<b>{@code :}</b><i>port</i>]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   121
 * where the characters <b>{@code @}</b> and <b>{@code :}</b> stand for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * themselves.  Nearly all URI schemes currently in use are server-based.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * authority component that does not parse in this way is considered to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * registry-based.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <p> The path component of a hierarchical URI is itself said to be absolute
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   127
 * if it begins with a slash character ({@code '/'}); otherwise it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * relative.  The path of a hierarchical URI that is either absolute or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * specifies an authority is always absolute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <p> All told, then, a URI instance has the following nine components:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <blockquote><table summary="Describes the components of a URI:scheme,scheme-specific-part,authority,user-info,host,port,path,query,fragment">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <tr><th><i>Component</i></th><th><i>Type</i></th></tr>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   135
 * <tr><td>scheme</td><td>{@code String}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   136
 * <tr><td>scheme-specific-part&nbsp;&nbsp;&nbsp;&nbsp;</td><td>{@code String}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   137
 * <tr><td>authority</td><td>{@code String}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   138
 * <tr><td>user-info</td><td>{@code String}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   139
 * <tr><td>host</td><td>{@code String}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   140
 * <tr><td>port</td><td>{@code int}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   141
 * <tr><td>path</td><td>{@code String}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   142
 * <tr><td>query</td><td>{@code String}</td></tr>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   143
 * <tr><td>fragment</td><td>{@code String}</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * In a given instance any particular component is either <i>undefined</i> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <i>defined</i> with a distinct value.  Undefined string components are
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   148
 * represented by {@code null}, while undefined integer components are
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   149
 * represented by {@code -1}.  A string component may be defined to have the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * empty string as its value; this is not equivalent to that component being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <p> Whether a particular component is or is not defined in an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * depends upon the type of the URI being represented.  An absolute URI has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * scheme component.  An opaque URI has a scheme, a scheme-specific part, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * possibly a fragment, but has no other components.  A hierarchical URI always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * has a path (though it may be empty) and a scheme-specific-part (which at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * least contains the path), and may have any of the other components.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * authority component is present and is server-based then the host component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * will be defined and the user-information and port components may be defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <h4> Operations on URI instances </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * The key operations supported by this class are those of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <i>normalization</i>, <i>resolution</i>, and <i>relativization</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   168
 * <p> <i>Normalization</i> is the process of removing unnecessary {@code "."}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   169
 * and {@code ".."} segments from the path component of a hierarchical URI.
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   170
 * Each {@code "."} segment is simply removed.  A {@code ".."} segment is
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   171
 * removed only if it is preceded by a non-{@code ".."} segment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * Normalization has no effect upon opaque URIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * <p> <i>Resolution</i> is the process of resolving one URI against another,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * <i>base</i> URI.  The resulting URI is constructed from components of both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * URIs in the manner specified by RFC&nbsp;2396, taking components from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * base URI for those not specified in the original.  For hierarchical URIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * the path of the original is resolved against the path of the base and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * normalized.  The result, for example, of resolving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   182
 * {@code docs/guide/collections/designfaq.html#28}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   183
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   184
 * &nbsp;&nbsp;&nbsp;&nbsp;(1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   187
 * against the base URI {@code http://java.sun.com/j2se/1.3/} is the result
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   191
 * {@code http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * Resolving the relative URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   197
 * {@code ../../../demo/jfc/SwingSet2/src/SwingSet2.java}&nbsp;&nbsp;&nbsp;&nbsp;(2)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * against this result yields, in turn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   203
 * {@code http://java.sun.com/j2se/1.3/demo/jfc/SwingSet2/src/SwingSet2.java}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * Resolution of both absolute and relative URIs, and of both absolute and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * relative paths in the case of hierarchical URIs, is supported.  Resolving
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   208
 * the URI {@code file:///~calendar} against any other URI simply yields the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * original URI, since it is absolute.  Resolving the relative URI (2) above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * against the relative base URI (1) yields the normalized, but still relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   214
 * {@code demo/jfc/SwingSet2/src/SwingSet2.java}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * <p> <i>Relativization</i>, finally, is the inverse of resolution: For any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * two normalized URIs <i>u</i> and&nbsp;<i>v</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   221
 *   <i>u</i>{@code .relativize(}<i>u</i>{@code .resolve(}<i>v</i>{@code )).equals(}<i>v</i>{@code )}&nbsp;&nbsp;and<br>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   222
 *   <i>u</i>{@code .resolve(}<i>u</i>{@code .relativize(}<i>v</i>{@code )).equals(}<i>v</i>{@code )}&nbsp;&nbsp;.<br>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * This operation is often useful when constructing a document containing URIs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * that must be made relative to the base URI of the document wherever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * possible.  For example, relativizing the URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   230
 * {@code http://java.sun.com/j2se/1.3/docs/guide/index.html}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * against the base URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   236
 * {@code http://java.sun.com/j2se/1.3}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   239
 * yields the relative URI {@code docs/guide/index.html}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * <h4> Character categories </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * RFC&nbsp;2396 specifies precisely which characters are permitted in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * various components of a URI reference.  The following categories, most of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 * which are taken from that specification, are used below to describe these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * constraints:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * <blockquote><table cellspacing=2 summary="Describes categories alpha,digit,alphanum,unreserved,punct,reserved,escaped,and other">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *   <tr><th valign=top><i>alpha</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *       <td>The US-ASCII alphabetic characters,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   252
 *        {@code 'A'}&nbsp;through&nbsp;{@code 'Z'}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   253
 *        and {@code 'a'}&nbsp;through&nbsp;{@code 'z'}</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *   <tr><th valign=top><i>digit</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *       <td>The US-ASCII decimal digit characters,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   256
 *       {@code '0'}&nbsp;through&nbsp;{@code '9'}</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *   <tr><th valign=top><i>alphanum</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *       <td>All <i>alpha</i> and <i>digit</i> characters</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *   <tr><th valign=top><i>unreserved</i>&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *       <td>All <i>alphanum</i> characters together with those in the string
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   261
 *        {@code "_-!.~'()*"}</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 *   <tr><th valign=top><i>punct</i></th>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   263
 *       <td>The characters in the string {@code ",;:$&+="}</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *   <tr><th valign=top><i>reserved</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 *       <td>All <i>punct</i> characters together with those in the string
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   266
 *        {@code "?/[]@"}</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *   <tr><th valign=top><i>escaped</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *       <td>Escaped octets, that is, triplets consisting of the percent
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   269
 *           character ({@code '%'}) followed by two hexadecimal digits
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   270
 *           ({@code '0'}-{@code '9'}, {@code 'A'}-{@code 'F'}, and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   271
 *           {@code 'a'}-{@code 'f'})</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 *   <tr><th valign=top><i>other</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 *       <td>The Unicode characters that are not in the US-ASCII character set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 *           are not control characters (according to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 *           java.lang.Character#isISOControl(char) Character.isISOControl}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 *           method), and are not space characters (according to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 *           java.lang.Character#isSpaceChar(char) Character.isSpaceChar}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 *           method)&nbsp;&nbsp;<i>(<b>Deviation from RFC 2396</b>, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 *           limited to US-ASCII)</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * <p><a name="legal-chars"></a> The set of all legal URI characters consists of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * the <i>unreserved</i>, <i>reserved</i>, <i>escaped</i>, and <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 * <h4> Escaped octets, quotation, encoding, and decoding </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * RFC 2396 allows escaped octets to appear in the user-info, path, query, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 * fragment components.  Escaping serves two purposes in URIs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 *   <li><p> To <i>encode</i> non-US-ASCII characters when a URI is required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 *   conform strictly to RFC&nbsp;2396 by not containing any <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *   characters.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 *   <li><p> To <i>quote</i> characters that are otherwise illegal in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 *   component.  The user-info, path, query, and fragment components differ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 *   slightly in terms of which characters are considered legal and illegal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * These purposes are served in this class by three related operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 *   <li><p><a name="encode"></a> A character is <i>encoded</i> by replacing it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 *   with the sequence of escaped octets that represent that character in the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   311
 *   UTF-8 character set.  The Euro currency symbol ({@code '\u005Cu20AC'}),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   312
 *   for example, is encoded as {@code "%E2%82%AC"}.  <i>(<b>Deviation from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 *   RFC&nbsp;2396</b>, which does not specify any particular character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 *   set.)</i> </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 *   <li><p><a name="quote"></a> An illegal character is <i>quoted</i> simply by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 *   encoding it.  The space character, for example, is quoted by replacing it
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   318
 *   with {@code "%20"}.  UTF-8 contains US-ASCII, hence for US-ASCII
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 *   characters this transformation has exactly the effect required by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 *   RFC&nbsp;2396. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 *   <li><p><a name="decode"></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 *   A sequence of escaped octets is <i>decoded</i> by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 *   replacing it with the sequence of characters that it represents in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 *   UTF-8 character set.  UTF-8 contains US-ASCII, hence decoding has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 *   effect of de-quoting any quoted US-ASCII characters as well as that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 *   decoding any encoded non-US-ASCII characters.  If a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 *   href="../nio/charset/CharsetDecoder.html#ce">decoding error</a> occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 *   when decoding the escaped octets then the erroneous octets are replaced by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   330
 *   {@code '\u005CuFFFD'}, the Unicode replacement character.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 * These operations are exposed in the constructors and methods of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 * as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 *
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
   339
 *   <li><p> The {@linkplain #URI(java.lang.String) single-argument
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
   340
 *   constructor} requires any illegal characters in its argument to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 *   quoted and preserves any escaped octets and <i>other</i> characters that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 *   are present.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
 *
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
   344
 *   <li><p> The {@linkplain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
 *   #URI(java.lang.String,java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String)
18800
e7fa560afcfb 8020318: Fix doclint issues in java.net
juh
parents: 18156
diff changeset
   346
 *   multi-argument constructors} quote illegal characters as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 *   required by the components in which they appear.  The percent character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   348
 *   ({@code '%'}) is always quoted by these constructors.  Any <i>other</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 *   characters are preserved.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 *   <li><p> The {@link #getRawUserInfo() getRawUserInfo}, {@link #getRawPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
 *   getRawPath}, {@link #getRawQuery() getRawQuery}, {@link #getRawFragment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
 *   getRawFragment}, {@link #getRawAuthority() getRawAuthority}, and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
 *   #getRawSchemeSpecificPart() getRawSchemeSpecificPart} methods return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 *   values of their corresponding components in raw form, without interpreting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 *   any escaped octets.  The strings returned by these methods may contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
 *   both escaped octets and <i>other</i> characters, and will not contain any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
 *   illegal characters.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
 *   <li><p> The {@link #getUserInfo() getUserInfo}, {@link #getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
 *   getPath}, {@link #getQuery() getQuery}, {@link #getFragment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
 *   getFragment}, {@link #getAuthority() getAuthority}, and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
 *   #getSchemeSpecificPart() getSchemeSpecificPart} methods decode any escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 *   octets in their corresponding components.  The strings returned by these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 *   methods may contain both <i>other</i> characters and illegal characters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
 *   and will not contain any escaped octets.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 *   <li><p> The {@link #toString() toString} method returns a URI string with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 *   all necessary quotation but which may contain <i>other</i> characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 *   <li><p> The {@link #toASCIIString() toASCIIString} method returns a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 *   quoted and encoded URI string that does not contain any <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
 *   characters.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
 * <h4> Identities </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
 * For any URI <i>u</i>, it is always the case that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
 * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   384
 * {@code new URI(}<i>u</i>{@code .toString()).equals(}<i>u</i>{@code )}&nbsp;.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
 * For any URI <i>u</i> that does not contain redundant syntax such as two
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   388
 * slashes before an empty authority (as in {@code file:///tmp/}&nbsp;) or a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
 * colon following a host name but no port (as in
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   390
 * {@code http://java.sun.com:}&nbsp;), and that does not encode characters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 * except those that must be quoted, the following identities also hold:
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   392
 * <p><pre>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   393
 *     new URI(<i>u</i>.getScheme(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   394
 *             <i>u</i>.getSchemeSpecificPart(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   395
 *             <i>u</i>.getFragment())
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   396
 *     .equals(<i>u</i>)</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
 * in all cases,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   398
 * <p><pre>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   399
 *     new URI(<i>u</i>.getScheme(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   400
 *             <i>u</i>.getUserInfo(), <i>u</i>.getAuthority(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   401
 *             <i>u</i>.getPath(), <i>u</i>.getQuery(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   402
 *             <i>u</i>.getFragment())
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   403
 *     .equals(<i>u</i>)</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
 * if <i>u</i> is hierarchical, and
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   405
 * <p><pre>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   406
 *     new URI(<i>u</i>.getScheme(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   407
 *             <i>u</i>.getUserInfo(), <i>u</i>.getHost(), <i>u</i>.getPort(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   408
 *             <i>u</i>.getPath(), <i>u</i>.getQuery(),
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   409
 *             <i>u</i>.getFragment())
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   410
 *     .equals(<i>u</i>)</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 * if <i>u</i> is hierarchical and has either no authority or a server-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
 * authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
 * <h4> URIs, URLs, and URNs </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
 * A URI is a uniform resource <i>identifier</i> while a URL is a uniform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
 * resource <i>locator</i>.  Hence every URL is a URI, abstractly speaking, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
 * not every URI is a URL.  This is because there is another subcategory of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
 * URIs, uniform resource <i>names</i> (URNs), which name resources but do not
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   421
 * specify how to locate them.  The {@code mailto}, {@code news}, and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   422
 * {@code isbn} URIs shown above are examples of URNs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
 * <p> The conceptual distinction between URIs and URLs is reflected in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
 * differences between this class and the {@link URL} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
 * <p> An instance of this class represents a URI reference in the syntactic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
 * sense defined by RFC&nbsp;2396.  A URI may be either absolute or relative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 * A URI string is parsed according to the generic syntax without regard to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 * scheme, if any, that it specifies.  No lookup of the host, if any, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 * performed, and no scheme-dependent stream handler is constructed.  Equality,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * hashing, and comparison are defined strictly in terms of the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 * content of the instance.  In other words, a URI instance is little more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 * a structured string that supports the syntactic, scheme-independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
 * operations of comparison, normalization, resolution, and relativization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
 * <p> An instance of the {@link URL} class, by contrast, represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
 * syntactic components of a URL together with some of the information required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
 * to access the resource that it describes.  A URL must be absolute, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
 * it must always specify a scheme.  A URL string is parsed according to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 * scheme.  A stream handler is always established for a URL, and in fact it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 * impossible to create a URL instance for a scheme for which no handler is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 * available.  Equality and hashing depend upon both the scheme and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 * Internet address of the host, if any; comparison is not defined.  In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 * words, a URL is a structured string that supports the syntactic operation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 * resolution as well as the network I/O operations of looking up the host and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 * opening a connection to the specified resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 *
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5627
diff changeset
   453
 * @see <a href="http://www.ietf.org/rfc/rfc2279.txt"><i>RFC&nbsp;2279: UTF-8, a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 * transformation format of ISO 10646</i></a>, <br><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IPv6 Addressing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 * Architecture</i></a>, <br><a
708
a780486c413c 6630348: Invalid html tags (extra double quote)
chegar
parents: 2
diff changeset
   457
 * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
 * Resource Identifiers (URI): Generic Syntax</i></a>, <br><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
 * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
 * Literal IPv6 Addresses in URLs</i></a>, <br><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
 * href="URISyntaxException.html">URISyntaxException</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
public final class URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    implements Comparable<URI>, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    // Note: Comments containing the word "ASSERT" indicate places where a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    // throw of an InternalError should be replaced by an appropriate assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    // statement once asserts are enabled in the build.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    static final long serialVersionUID = -6052424284110960213L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    // -- Properties and components of this instance --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    // Components of all URIs: [<scheme>:]<scheme-specific-part>[#<fragment>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    private transient String scheme;            // null ==> relative URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    private transient String fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    // Hierarchical URI components: [//<authority>]<path>[?<query>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private transient String authority;         // Registry or server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    // Server-based authority: [<userInfo>@]<host>[:<port>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    private transient String userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    private transient String host;              // null ==> registry-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private transient int port = -1;            // -1 ==> undefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    // Remaining components of hierarchical URIs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    private transient String path;              // null ==> opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    private transient String query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    // The remaining fields may be computed on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    private volatile transient String schemeSpecificPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private volatile transient int hash;        // Zero ==> undefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private volatile transient String decodedUserInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    private volatile transient String decodedAuthority = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    private volatile transient String decodedPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    private volatile transient String decodedQuery = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    private volatile transient String decodedFragment = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    private volatile transient String decodedSchemeSpecificPart = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * The string form of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    private volatile String string;             // The only serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    // -- Constructors and factories --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    private URI() { }                           // Used internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Constructs a URI by parsing the given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <p> This constructor parses the given string exactly as specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * grammar in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Appendix&nbsp;A, <b><i>except for the following deviations:</i></b> </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
   526
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *   <li><p> An empty authority component is permitted as long as it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *   followed by a non-empty path, a query component, or a fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *   component.  This allows the parsing of URIs such as
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   531
     *   {@code "file:///foo/bar"}, which seems to be the intent of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *   RFC&nbsp;2396 although the grammar does not permit it.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *   authority component is empty then the user-information, host, and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *   components are undefined. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *   <li><p> Empty relative paths are permitted; this seems to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *   intent of RFC&nbsp;2396 although the grammar does not permit it.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *   primary consequence of this deviation is that a standalone fragment
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   539
     *   such as {@code "#foo"} parses as a relative URI with an empty path
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *   and the given fragment, and can be usefully <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *   href="#resolve-frag">resolved</a> against a base URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *   <li><p> IPv4 addresses in host components are parsed rigorously, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *   specified by <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *   href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>: Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *   element of a dotted-quad address must contain no more than three
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *   decimal digits.  Each element is further constrained to have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *   no greater than 255. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *   <li> <p> Hostnames in host components that comprise only a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *   domain label are permitted to start with an <i>alphanum</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *   character. This seems to be the intent of <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *   href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *   section&nbsp;3.2.2 although the grammar does not permit it. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *   consequence of this deviation is that the authority component of a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   556
     *   hierarchical URI such as {@code s://123}, will parse as a server-based
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *   authority. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *   <li><p> IPv6 addresses are permitted for the host component.  An IPv6
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   560
     *   address must be enclosed in square brackets ({@code '['} and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   561
     *   {@code ']'}) as specified by <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *   href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *   IPv6 address itself must parse according to <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *   href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>.  IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *   addresses are further constrained to describe no more than sixteen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *   bytes of address information, a constraint implicit in RFC&nbsp;2373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *   but not expressible in the grammar. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *   <li><p> Characters in the <i>other</i> category are permitted wherever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *   RFC&nbsp;2396 permits <i>escaped</i> octets, that is, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *   user-information, path, query, and fragment components, as well as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *   the authority component if the authority is registry-based.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *   allows URIs to contain Unicode characters beyond those in the US-ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *   character set. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param  str   The string to be parsed into a URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   581
     *          If {@code str} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @throws  URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *          If the given string violates RFC&nbsp;2396, as augmented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *          by the above deviations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public URI(String str) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        new Parser(str).parse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Constructs a hierarchical URI from the given components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <p> If a scheme is given then the path, if also given, must either be
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   595
     * empty or begin with a slash character ({@code '/'}).  Otherwise a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   596
     * component of the new URI may be left undefined by passing {@code null}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   597
     * for the corresponding parameter or, in the case of the {@code port}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   598
     * parameter, by passing {@code -1}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * <p> This constructor first builds a URI string from the given components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * according to the rules specified in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * section&nbsp;5.2, step&nbsp;7: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *   <li><p> Initially, the result string is empty. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *   <li><p> If a scheme is given then it is appended to the result,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   610
     *   followed by a colon character ({@code ':'}).  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *   <li><p> If user information, a host, or a port are given then the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   613
     *   string {@code "//"} is appended.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *   <li><p> If user information is given then it is appended, followed by
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   616
     *   a commercial-at character ({@code '@'}).  Any character not in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *   <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *   categories is <a href="#quote">quoted</a>.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *   <li><p> If a host is given then it is appended.  If the host is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *   literal IPv6 address but is not enclosed in square brackets
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   622
     *   ({@code '['} and {@code ']'}) then the square brackets are added.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *   <li><p> If a port number is given then a colon character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   626
     *   ({@code ':'}) is appended, followed by the port number in decimal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *   <li><p> If a path is given then it is appended.  Any character not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *   the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   631
     *   categories, and not equal to the slash character ({@code '/'}) or the
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   632
     *   commercial-at character ({@code '@'}), is quoted.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *   <li><p> If a query is given then a question-mark character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   635
     *   ({@code '?'}) is appended, followed by the query.  Any character that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *   is not a <a href="#legal-chars">legal URI character</a> is quoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *   <li><p> Finally, if a fragment is given then a hash character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   640
     *   ({@code '#'}) is appended, followed by the fragment.  Any character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *   that is not a legal URI character is quoted.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * <p> The resulting URI string is then parsed as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * #URI(String)} constructor and then invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * #parseServerAuthority()} method upon the result; this may cause a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * URISyntaxException} to be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @param   scheme    Scheme name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @param   userInfo  User name and authorization information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param   host      Host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @param   port      Port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @param   path      Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @param   query     Query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @param   fragment  Fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *         If both a scheme and a path are given but the path is relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *         if the URI string constructed from the given components violates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *         RFC&nbsp;2396, or if the authority component of the string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *         present but cannot be parsed as a server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    public URI(String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
               String userInfo, String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
               String path, String query, String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        String s = toString(scheme, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                            null, userInfo, host, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                            path, query, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        checkPath(s, scheme, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        new Parser(s).parse(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Constructs a hierarchical URI from the given components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <p> If a scheme is given then the path, if also given, must either be
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   680
     * empty or begin with a slash character ({@code '/'}).  Otherwise a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   681
     * component of the new URI may be left undefined by passing {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * for the corresponding parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <p> This constructor first builds a URI string from the given components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * according to the rules specified in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * section&nbsp;5.2, step&nbsp;7: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *   <li><p> Initially, the result string is empty.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *   <li><p> If a scheme is given then it is appended to the result,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   694
     *   followed by a colon character ({@code ':'}).  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   696
     *   <li><p> If an authority is given then the string {@code "//"} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *   appended, followed by the authority.  If the authority contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *   literal IPv6 address then the address must be enclosed in square
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   699
     *   brackets ({@code '['} and {@code ']'}).  Any character not in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *   <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *   categories, and not equal to the commercial-at character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   702
     *   ({@code '@'}), is <a href="#quote">quoted</a>.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *   <li><p> If a path is given then it is appended.  Any character not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *   the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   706
     *   categories, and not equal to the slash character ({@code '/'}) or the
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   707
     *   commercial-at character ({@code '@'}), is quoted.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *   <li><p> If a query is given then a question-mark character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   710
     *   ({@code '?'}) is appended, followed by the query.  Any character that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *   is not a <a href="#legal-chars">legal URI character</a> is quoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *   <li><p> Finally, if a fragment is given then a hash character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   715
     *   ({@code '#'}) is appended, followed by the fragment.  Any character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *   that is not a legal URI character is quoted.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <p> The resulting URI string is then parsed as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * #URI(String)} constructor and then invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * #parseServerAuthority()} method upon the result; this may cause a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * URISyntaxException} to be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @param   scheme     Scheme name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @param   authority  Authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @param   path       Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @param   query      Query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @param   fragment   Fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *         If both a scheme and a path are given but the path is relative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *         if the URI string constructed from the given components violates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *         RFC&nbsp;2396, or if the authority component of the string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *         present but cannot be parsed as a server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    public URI(String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
               String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
               String path, String query, String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        String s = toString(scheme, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                            authority, null, null, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                            path, query, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        checkPath(s, scheme, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        new Parser(s).parse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * Constructs a hierarchical URI from the given components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   752
     * <p> A component may be left undefined by passing {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * <p> This convenience constructor works as if by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * seven-argument constructor as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   757
     * <blockquote>
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   758
     * {@code new} {@link #URI(String, String, String, int, String, String, String)
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   759
     * URI}{@code (scheme, null, host, -1, path, null, fragment);}
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   760
     * </blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @param   scheme    Scheme name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @param   host      Host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param   path      Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @param   fragment  Fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @throws  URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *          If the URI string constructed from the given components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *          violates RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public URI(String scheme, String host, String path, String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        this(scheme, null, host, -1, path, null, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Constructs a URI from the given components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   780
     * <p> A component may be left undefined by passing {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <p> This constructor first builds a URI in string form using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * components as follows:  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *   <li><p> Initially, the result string is empty.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *   <li><p> If a scheme is given then it is appended to the result,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   790
     *   followed by a colon character ({@code ':'}).  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *   <li><p> If a scheme-specific part is given then it is appended.  Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *   character that is not a <a href="#legal-chars">legal URI character</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *   is <a href="#quote">quoted</a>.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *   <li><p> Finally, if a fragment is given then a hash character
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   797
     *   ({@code '#'}) is appended to the string, followed by the fragment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *   Any character that is not a legal URI character is quoted.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * <p> The resulting URI string is then parsed in order to create the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * URI instance as if by invoking the {@link #URI(String)} constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * this may cause a {@link URISyntaxException} to be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @param   scheme    Scheme name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @param   ssp       Scheme-specific part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @param   fragment  Fragment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @throws  URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *          If the URI string constructed from the given components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *          violates RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    public URI(String scheme, String ssp, String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        new Parser(toString(scheme, ssp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                            null, null, null, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                            null, null, fragment))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            .parse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * Creates a URI by parsing the given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * <p> This convenience factory method works as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * #URI(String)} constructor; any {@link URISyntaxException} thrown by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * constructor is caught and wrapped in a new {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * IllegalArgumentException} object, which is then thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * <p> This method is provided for use in situations where it is known that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * the given string is a legal URI, for example for URI constants declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * within in a program, and so it would be considered a programming error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * for the string not to parse as such.  The constructors, which throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * {@link URISyntaxException} directly, should be used situations where a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * URI is being constructed from user input or from some other source that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * may be prone to errors.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @param  str   The string to be parsed into a URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @return The new URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   843
     *          If {@code str} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *          If the given string violates RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    public static URI create(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            return new URI(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        } catch (URISyntaxException x) {
6307
613f5033f5f2 6339649: URI.create should include a detail message when throwing IllegalArgumentException
michaelm
parents: 5820
diff changeset
   852
            throw new IllegalArgumentException(x.getMessage(), x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    // -- Operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * Attempts to parse this URI's authority component, if defined, into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * user-information, host, and port components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * <p> If this URI's authority component has already been recognized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * being server-based then it will already have been parsed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * user-information, host, and port components.  In this case, or if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * URI has no authority component, this method simply returns this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * <p> Otherwise this method attempts once more to parse the authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * component into user-information, host, and port components, and throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * an exception describing why the authority component could not be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * in that way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * <p> This method is provided because the generic URI syntax specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * cannot always distinguish a malformed server-based authority from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * legitimate registry-based authority.  It must therefore treat some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * instances of the former as instances of the latter.  The authority
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   878
     * component in the URI string {@code "//foo:bar"}, for example, is not a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * legal server-based authority but it is legal as a registry-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * <p> In many common situations, for example when working URIs that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * known to be either URNs or URLs, the hierarchical URIs being used will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * always be server-based.  They therefore must either be parsed as such or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * treated as an error.  In these cases a statement such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * <blockquote>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   888
     * {@code URI }<i>u</i>{@code  = new URI(str).parseServerAuthority();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * <p> can be used to ensure that <i>u</i> always refers to a URI that, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * it has an authority component, has a server-based authority with proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * user-information, host, and port components.  Invoking this method also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * ensures that if the authority could not be parsed in that way then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * appropriate diagnostic message can be issued based upon the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * that is thrown. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @return  A URI whose authority field has been parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *          as a server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @throws  URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *          If the authority component of this URI is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *          but cannot be parsed as a server-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *          according to RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    public URI parseServerAuthority()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        // We could be clever and cache the error message and index from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        // exception thrown during the original parse, but that would require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        // either more fields or a more-obscure representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        if ((host != null) || (authority == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        defineString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        new Parser(string).parse(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Normalizes this URI's path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * <p> If this URI is opaque, or if its path is already in normal form,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * then this URI is returned.  Otherwise a new URI is constructed that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * identical to this URI except that its path is computed by normalizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * this URI's path in a manner consistent with <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * section&nbsp;5.2, step&nbsp;6, sub-steps&nbsp;c through&nbsp;f; that is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   932
     *   <li><p> All {@code "."} segments are removed. </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   934
     *   <li><p> If a {@code ".."} segment is preceded by a non-{@code ".."}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *   segment then both of these segments are removed.  This step is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *   repeated until it is no longer applicable. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *   <li><p> If the path is relative, and if its first segment contains a
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   939
     *   colon character ({@code ':'}), then a {@code "."} segment is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *   prepended.  This prevents a relative URI with a path such as
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   941
     *   {@code "a:b/c/d"} from later being re-parsed as an opaque URI with a
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   942
     *   scheme of {@code "a"} and a scheme-specific part of {@code "b/c/d"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *   <b><i>(Deviation from RFC&nbsp;2396)</i></b> </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   947
     * <p> A normalized path will begin with one or more {@code ".."} segments
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   948
     * if there were insufficient non-{@code ".."} segments preceding them to
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   949
     * allow their removal.  A normalized path will begin with a {@code "."}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * segment if one was inserted by step 3 above.  Otherwise, a normalized
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   951
     * path will not contain any {@code "."} or {@code ".."} segments. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @return  A URI equivalent to this URI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *          but whose path is in normal form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    public URI normalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        return normalize(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Resolves the given URI against this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * <p> If the given URI is already absolute, or if this URI is opaque, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * the given URI is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * <p><a name="resolve-frag"></a> If the given URI's fragment component is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * defined, its path component is empty, and its scheme, authority, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * query components are undefined, then a URI with the given fragment but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * with all other components equal to those of this URI is returned.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * allows a URI representing a standalone fragment reference, such as
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
   971
     * {@code "#foo"}, to be usefully resolved against a base URI.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * <p> Otherwise this method constructs a new hierarchical URI in a manner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * consistent with <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * section&nbsp;5.2; that is: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *   <li><p> A new URI is constructed with this URI's scheme and the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *   URI's query and fragment components. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *   <li><p> If the given URI has an authority component then the new URI's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *   authority and path are taken from the given URI. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *   <li><p> Otherwise the new URI's authority component is copied from
8562
72c0c44969c1 7018137: HTML4 compliance issues
chegar
parents: 7668
diff changeset
   987
     *   this URI, and its path is computed as follows: </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
   989
     *   <ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *     <li><p> If the given URI's path is absolute then the new URI's path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *     is taken from the given URI. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *     <li><p> Otherwise the given URI's path is relative, and so the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *     URI's path is computed by resolving the path of the given URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     *     against the path of this URI.  This is done by concatenating all but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     *     the last segment of this URI's path, if any, with the given URI's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *     path and then normalizing the result as if by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *     #normalize() normalize} method. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *
8562
72c0c44969c1 7018137: HTML4 compliance issues
chegar
parents: 7668
diff changeset
  1001
     *   </ol></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * <p> The result of this method is absolute if, and only if, either this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * URI is absolute or the given URI is absolute.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @param  uri  The URI to be resolved against this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @return The resulting URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1012
     *          If {@code uri} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    public URI resolve(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        return resolve(this, uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * Constructs a new URI by parsing the given string and then resolving it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * against this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * <p> This convenience method works as if invoking it were equivalent to
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1023
     * evaluating the expression {@link #resolve(java.net.URI)
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1024
     * resolve}{@code (URI.}{@link #create(String) create}{@code (str))}. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * @param  str   The string to be parsed into a URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @return The resulting URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1030
     *          If {@code str} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *          If the given string violates RFC&nbsp;2396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    public URI resolve(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        return resolve(URI.create(str));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * Relativizes the given URI against this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * <p> The relativization of the given URI against this URI is computed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * follows: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *   <li><p> If either this URI or the given URI are opaque, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *   scheme and authority components of the two URIs are not identical, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *   if the path of this URI is not a prefix of the path of the given URI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *   then the given URI is returned. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *   <li><p> Otherwise a new relative hierarchical URI is constructed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *   query and fragment components taken from the given URI and with a path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *   component computed by removing this URI's path from the beginning of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *   the given URI's path. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @param  uri  The URI to be relativized against this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @return The resulting URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @throws  NullPointerException
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1063
     *          If {@code uri} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    public URI relativize(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        return relativize(this, uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * Constructs a URL from this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * <p> This convenience method works as if invoking it were equivalent to
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1073
     * evaluating the expression {@code new URL(this.toString())} after
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * first checking that this URI is absolute. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @return  A URL constructed from this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *          If this URL is not absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @throws  MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *          If a protocol handler for the URL could not be found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *          or if some other error occurred while constructing the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    public URL toURL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (!isAbsolute())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            throw new IllegalArgumentException("URI is not absolute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        return new URL(toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    // -- Component access methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * Returns the scheme component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * <p> The scheme component of a URI, if defined, only contains characters
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1098
     * in the <i>alphanum</i> category and in the string {@code "-.+"}.  A
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * scheme always starts with an <i>alpha</i> character. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * The scheme component of a URI cannot contain escaped octets, hence this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * method does not perform any decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * @return  The scheme component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1105
     *          or {@code null} if the scheme is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    public String getScheme() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        return scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * Tells whether or not this URI is absolute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * <p> A URI is absolute if, and only if, it has a scheme component. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1116
     * @return  {@code true} if, and only if, this URI is absolute
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    public boolean isAbsolute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        return scheme != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * Tells whether or not this URI is opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * <p> A URI is opaque if, and only if, it is absolute and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * scheme-specific part does not begin with a slash character ('/').
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * An opaque URI has a scheme, a scheme-specific part, and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * a fragment; all other components are undefined. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1130
     * @return  {@code true} if, and only if, this URI is opaque
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    public boolean isOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        return path == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * Returns the raw scheme-specific part of this URI.  The scheme-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * part is never undefined, though it may be empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * <p> The scheme-specific part of a URI only contains legal URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * characters. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @return  The raw scheme-specific part of this URI
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1144
     *          (never {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    public String getRawSchemeSpecificPart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        defineSchemeSpecificPart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        return schemeSpecificPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * Returns the decoded scheme-specific part of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * {@link #getRawSchemeSpecificPart() getRawSchemeSpecificPart} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * except that all sequences of escaped octets are <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * @return  The decoded scheme-specific part of this URI
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1160
     *          (never {@code null})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    public String getSchemeSpecificPart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        if (decodedSchemeSpecificPart == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            decodedSchemeSpecificPart = decode(getRawSchemeSpecificPart());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        return decodedSchemeSpecificPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Returns the raw authority component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * <p> The authority component of a URI, if defined, only contains the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1172
     * commercial-at character ({@code '@'}) and characters in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, and <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * categories.  If the authority is server-based then it is further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * constrained to have valid user-information, host, and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * components. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * @return  The raw authority component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1179
     *          or {@code null} if the authority is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    public String getRawAuthority() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        return authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * Returns the decoded authority component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * {@link #getRawAuthority() getRawAuthority} method except that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * sequences of escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @return  The decoded authority component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1193
     *          or {@code null} if the authority is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    public String getAuthority() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        if (decodedAuthority == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            decodedAuthority = decode(authority);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        return decodedAuthority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * Returns the raw user-information component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * <p> The user-information component of a URI, if defined, only contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * characters in the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * <i>other</i> categories. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @return  The raw user-information component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1209
     *          or {@code null} if the user information is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    public String getRawUserInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        return userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * Returns the decoded user-information component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * {@link #getRawUserInfo() getRawUserInfo} method except that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * sequences of escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @return  The decoded user-information component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1223
     *          or {@code null} if the user information is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    public String getUserInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        if ((decodedUserInfo == null) && (userInfo != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            decodedUserInfo = decode(userInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        return decodedUserInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * Returns the host component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * <p> The host component of a URI, if defined, will have one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * following forms: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
  1237
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     *   <li><p> A domain name consisting of one or more <i>labels</i>
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1240
     *   separated by period characters ({@code '.'}), optionally followed by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *   a period character.  Each label consists of <i>alphanum</i> characters
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1242
     *   as well as hyphen characters ({@code '-'}), though hyphens never
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     *   occur as the first or last characters in a label. The rightmost
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *   label of a domain name consisting of two or more labels, begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     *   with an <i>alpha</i> character. </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     *   <li><p> A dotted-quad IPv4 address of the form
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1248
     *   <i>digit</i>{@code +.}<i>digit</i>{@code +.}<i>digit</i>{@code +.}<i>digit</i>{@code +},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     *   where no <i>digit</i> sequence is longer than three characters and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     *   sequence has a value larger than 255. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1252
     *   <li><p> An IPv6 address enclosed in square brackets ({@code '['} and
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1253
     *   {@code ']'}) and consisting of hexadecimal digits, colon characters
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1254
     *   ({@code ':'}), and possibly an embedded IPv4 address.  The full
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *   syntax of IPv6 addresses is specified in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     *   href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     *   Addressing Architecture</i></a>.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * The host component of a URI cannot contain escaped octets, hence this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * method does not perform any decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * @return  The host component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1265
     *          or {@code null} if the host is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * Returns the port number of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * <p> The port component of a URI, if defined, is a non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * integer. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * @return  The port component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1278
     *          or {@code -1} if the port is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * Returns the raw path component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * <p> The path component of a URI, if defined, only contains the slash
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1288
     * character ({@code '/'}), the commercial-at character ({@code '@'}),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * and characters in the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * and <i>other</i> categories. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @return  The path component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1293
     *          or {@code null} if the path is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    public String getRawPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * Returns the decoded path component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * {@link #getRawPath() getRawPath} method except that all sequences of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * @return  The decoded path component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1307
     *          or {@code null} if the path is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    public String getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        if ((decodedPath == null) && (path != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            decodedPath = decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        return decodedPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * Returns the raw query component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * <p> The query component of a URI, if defined, only contains legal URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * characters. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * @return  The raw query component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1322
     *          or {@code null} if the query is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    public String getRawQuery() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        return query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * Returns the decoded query component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * {@link #getRawQuery() getRawQuery} method except that all sequences of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * @return  The decoded query component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1336
     *          or {@code null} if the query is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    public String getQuery() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        if ((decodedQuery == null) && (query != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            decodedQuery = decode(query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        return decodedQuery;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * Returns the raw fragment component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * <p> The fragment component of a URI, if defined, only contains legal URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * characters. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * @return  The raw fragment component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1351
     *          or {@code null} if the fragment is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    public String getRawFragment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        return fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * Returns the decoded fragment component of this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * <p> The string returned by this method is equal to that returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * {@link #getRawFragment() getRawFragment} method except that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * sequences of escaped octets are <a href="#decode">decoded</a>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @return  The decoded fragment component of this URI,
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1365
     *          or {@code null} if the fragment is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    public String getFragment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        if ((decodedFragment == null) && (fragment != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            decodedFragment = decode(fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        return decodedFragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    // -- Equality, comparison, hash code, toString, and serialization --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * Tests this URI for equality with another object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * <p> If the given object is not a URI then this method immediately
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1380
     * returns {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * <p> For two URIs to be considered equal requires that either both are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * opaque or both are hierarchical.  Their schemes must either both be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * undefined or else be equal without regard to case. Their fragments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * must either both be undefined or else be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * <p> For two opaque URIs to be considered equal, their scheme-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * parts must be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * <p> For two hierarchical URIs to be considered equal, their paths must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * be equal and their queries must either both be undefined or else be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * equal.  Their authorities must either both be undefined, or both be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * registry-based, or both be server-based.  If their authorities are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * defined and are registry-based, then they must be equal.  If their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * authorities are defined and are server-based, then their hosts must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * equal without regard to case, their port numbers must be equal, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * their user-information components must be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * <p> When testing the user-information, path, query, fragment, authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * or scheme-specific parts of two URIs for equality, the raw forms rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * than the encoded forms of these components are compared and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * hexadecimal digits of escaped octets are compared without regard to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * <p> This method satisfies the general contract of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * java.lang.Object#equals(Object) Object.equals} method. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * @param   ob   The object to which this object is to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1410
     * @return  {@code true} if, and only if, the given object is a URI that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     *          is identical to this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    public boolean equals(Object ob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        if (ob == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        if (!(ob instanceof URI))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        URI that = (URI)ob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        if (this.isOpaque() != that.isOpaque()) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        if (!equalIgnoringCase(this.scheme, that.scheme)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        if (!equal(this.fragment, that.fragment)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        // Opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        if (this.isOpaque())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            return equal(this.schemeSpecificPart, that.schemeSpecificPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        // Hierarchical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        if (!equal(this.path, that.path)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        if (!equal(this.query, that.query)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        // Authorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        if (this.authority == that.authority) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        if (this.host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            // Server-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            if (!equal(this.userInfo, that.userInfo)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            if (!equalIgnoringCase(this.host, that.host)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            if (this.port != that.port) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        } else if (this.authority != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            // Registry-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            if (!equal(this.authority, that.authority)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        } else if (this.authority != that.authority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * Returns a hash-code value for this URI.  The hash code is based upon all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * of the URI's components, and satisfies the general contract of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * {@link java.lang.Object#hashCode() Object.hashCode} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @return  A hash-code value for this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        if (hash != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        int h = hashIgnoringCase(0, scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        h = hash(h, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        if (isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            h = hash(h, schemeSpecificPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            h = hash(h, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            h = hash(h, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                h = hash(h, userInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                h = hashIgnoringCase(h, host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                h += 1949 * port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                h = hash(h, authority);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        hash = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * Compares this URI to another object, which must be a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * <p> When comparing corresponding components of two URIs, if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * component is undefined but the other is defined then the first is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * considered to be less than the second.  Unless otherwise noted, string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * components are ordered according to their natural, case-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * ordering as defined by the {@link java.lang.String#compareTo(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * String.compareTo} method.  String components that are subject to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * encoding are compared by comparing their raw forms rather than their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * encoded forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * <p> The ordering of URIs is defined as follows: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
  1491
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     *   <li><p> Two URIs with different schemes are ordered according the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     *   ordering of their schemes, without regard to case. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     *   <li><p> A hierarchical URI is considered to be less than an opaque URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     *   with an identical scheme. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     *   <li><p> Two opaque URIs with identical schemes are ordered according
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     *   to the ordering of their scheme-specific parts. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     *   <li><p> Two opaque URIs with identical schemes and scheme-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     *   parts are ordered according to the ordering of their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     *   fragments. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     *   <li><p> Two hierarchical URIs with identical schemes are ordered
8562
72c0c44969c1 7018137: HTML4 compliance issues
chegar
parents: 7668
diff changeset
  1507
     *   according to the ordering of their authority components: </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     *
19049
543b417ea7b2 8021421: More doclint fixes in java.net
chegar
parents: 18800
diff changeset
  1509
     *   <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     *     <li><p> If both authority components are server-based then the URIs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     *     are ordered according to their user-information components; if these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     *     components are identical then the URIs are ordered according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     *     ordering of their hosts, without regard to case; if the hosts are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     *     identical then the URIs are ordered according to the ordering of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     *     their ports. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     *     <li><p> If one or both authority components are registry-based then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     *     the URIs are ordered according to the ordering of their authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     *     components. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     *
8562
72c0c44969c1 7018137: HTML4 compliance issues
chegar
parents: 7668
diff changeset
  1522
     *   </ul></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     *   <li><p> Finally, two hierarchical URIs with identical schemes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     *   authority components are ordered according to the ordering of their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     *   paths; if their paths are identical then they are ordered according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     *   the ordering of their queries; if the queries are identical then they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     *   are ordered according to the order of their fragments. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * <p> This method satisfies the general contract of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * java.lang.Comparable#compareTo(Object) Comparable.compareTo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * method. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * @param   that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     *          The object to which this URI is to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * @return  A negative integer, zero, or a positive integer as this URI is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     *          less than, equal to, or greater than the given URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * @throws  ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     *          If the given object is not a URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    public int compareTo(URI that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        if ((c = compareIgnoringCase(this.scheme, that.scheme)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        if (this.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            if (that.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                // Both opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                if ((c = compare(this.schemeSpecificPart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                                 that.schemeSpecificPart)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                    return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                return compare(this.fragment, that.fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            return +1;                  // Opaque > hierarchical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        } else if (that.isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            return -1;                  // Hierarchical < opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        // Hierarchical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        if ((this.host != null) && (that.host != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            // Both server-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            if ((c = compare(this.userInfo, that.userInfo)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            if ((c = compareIgnoringCase(this.host, that.host)) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            if ((c = this.port - that.port) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            // If one or both authorities are registry-based then we simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            // compare them in the usual, case-sensitive way.  If one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            // registry-based and one is server-based then the strings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            // guaranteed to be unequal, hence the comparison will never return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            // zero and the compareTo and equals methods will remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            // consistent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            if ((c = compare(this.authority, that.authority)) != 0) return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        if ((c = compare(this.path, that.path)) != 0) return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        if ((c = compare(this.query, that.query)) != 0) return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        return compare(this.fragment, that.fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * Returns the content of this URI as a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * <p> If this URI was created by invoking one of the constructors in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * class then a string equivalent to the original input string, or to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * string computed from the originally-given components, as appropriate, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * returned.  Otherwise this URI was created by normalization, resolution,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * or relativization, and so a string is constructed from this URI's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * components according to the rules specified in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * section&nbsp;5.2, step&nbsp;7. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * @return  The string form of this URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        defineString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * Returns the content of this URI as a US-ASCII string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * <p> If this URI does not contain any characters in the <i>other</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * category then an invocation of this method will return the same value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * an invocation of the {@link #toString() toString} method.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * this method works as if by invoking that method and then <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * href="#encode">encoding</a> the result.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * @return  The string form of this URI, encoded as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     *          so that it only contains characters in the US-ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     *          charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    public String toASCIIString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        defineString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        return encode(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    // -- Serialization support --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * Saves the content of this URI to the given serial stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1631
     * <p> The only serializable field of a URI instance is its {@code string}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * field.  That field is given a value, if it does not have one already,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * and then the {@link java.io.ObjectOutputStream#defaultWriteObject()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * method of the given object-output stream is invoked. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * @param  os  The object-output stream to which this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     *             is to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    private void writeObject(ObjectOutputStream os)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        defineString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        os.defaultWriteObject();        // Writes the string field only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * Reconstitutes a URI from the given serial stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * <p> The {@link java.io.ObjectInputStream#defaultReadObject()} method is
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 19049
diff changeset
  1650
     * invoked to read the value of the {@code string} field.  The result is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * then parsed in the usual way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * @param  is  The object-input stream from which this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     *             is being read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    private void readObject(ObjectInputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        port = -1;                      // Argh
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        is.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            new Parser(string).parse(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        } catch (URISyntaxException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            IOException y = new InvalidObjectException("Invalid URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            y.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            throw y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    // -- End of public methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    // -- Utility methods for string-field comparison and hashing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    // These methods return appropriate values for null string arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    // thereby simplifying the equals, hashCode, and compareTo methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
    // The case-ignoring methods should only be applied to strings whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    // characters are all known to be US-ASCII.  Because of this restriction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    // these methods are faster than the similar methods in the String class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    // US-ASCII only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    private static int toLower(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        if ((c >= 'A') && (c <= 'Z'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            return c + ('a' - 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
15272
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1690
    // US-ASCII only
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1691
    private static int toUpper(char c) {
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1692
        if ((c >= 'a') && (c <= 'z'))
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1693
            return c - ('a' - 'A');
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1694
        return c;
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1695
    }
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1696
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    private static boolean equal(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        if (s == t) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        if ((s != null) && (t != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            if (s.length() != t.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            if (s.indexOf('%') < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                return s.equals(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            for (int i = 0; i < n;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                char d = t.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                if (c != '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                    if (c != d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                }
10422
83581a2cf49d 7041800: URI.equals may incorrectly return true with escaped octets
chegar
parents: 9035
diff changeset
  1714
                if (d != '%')
83581a2cf49d 7041800: URI.equals may incorrectly return true with escaped octets
chegar
parents: 9035
diff changeset
  1715
                    return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                if (toLower(s.charAt(i)) != toLower(t.charAt(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                if (toLower(s.charAt(i)) != toLower(t.charAt(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    // US-ASCII only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    private static boolean equalIgnoringCase(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        if (s == t) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        if ((s != null) && (t != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            if (t.length() != n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                if (toLower(s.charAt(i)) != toLower(t.charAt(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    private static int hash(int hash, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        if (s == null) return hash;
15272
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1747
        return s.indexOf('%') < 0 ? hash * 127 + s.hashCode()
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1748
                                  : normalizedHash(hash, s);
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1749
    }
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1750
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1751
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1752
    private static int normalizedHash(int hash, String s) {
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1753
        int h = 0;
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1754
        for (int index = 0; index < s.length(); index++) {
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1755
            char ch = s.charAt(index);
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1756
            h = 31 * h + ch;
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1757
            if (ch == '%') {
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1758
                /*
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1759
                 * Process the next two encoded characters
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1760
                 */
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1761
                for (int i = index + 1; i < index + 3; i++)
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1762
                    h = 31 * h + toUpper(s.charAt(i));
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1763
                index += 2;
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1764
            }
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1765
        }
b0055428d835 7171415: java.net.URI.equals/hashCode not consistent for some URIs
khazra
parents: 10422
diff changeset
  1766
        return hash * 127 + h;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    // US-ASCII only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    private static int hashIgnoringCase(int hash, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        if (s == null) return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        int h = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            h = 31 * h + toLower(s.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    private static int compare(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        if (s == t) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            if (t != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                return s.compareTo(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                return +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    // US-ASCII only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    private static int compareIgnoringCase(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        if (s == t) return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            if (t != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                int sn = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                int tn = t.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                int n = sn < tn ? sn : tn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                    int c = toLower(s.charAt(i)) - toLower(t.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                    if (c != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                return sn - tn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            return +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    // -- String construction --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
    // If a scheme is given then the path, if given, must be absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    private static void checkPath(String s, String scheme, String path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        if (scheme != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            if ((path != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                && ((path.length() > 0) && (path.charAt(0) != '/')))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                throw new URISyntaxException(s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                                             "Relative path in absolute URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    private void appendAuthority(StringBuffer sb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                                 String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                                 String userInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                                 String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                                 int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            sb.append("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            if (userInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                sb.append(quote(userInfo, L_USERINFO, H_USERINFO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                sb.append('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            boolean needBrackets = ((host.indexOf(':') >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                                    && !host.startsWith("[")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                                    && !host.endsWith("]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            if (needBrackets) sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            sb.append(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            if (needBrackets) sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            if (port != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                sb.append(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        } else if (authority != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
            sb.append("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            if (authority.startsWith("[")) {
8778
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1853
                // authority should (but may not) contain an embedded IPv6 address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                int end = authority.indexOf("]");
8778
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1855
                String doquote = authority, dontquote = "";
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1856
                if (end != -1 && authority.indexOf(":") != -1) {
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1857
                    // the authority contains an IPv6 address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                    if (end == authority.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                        dontquote = authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                        doquote = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                    } else {
8778
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1862
                        dontquote = authority.substring(0 , end + 1);
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1863
                        doquote = authority.substring(end + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                    }
8778
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1865
                }
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1866
                sb.append(dontquote);
f2ef7d12277f 7023363: URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag") should throw URISyntaxException
chegar
parents: 8562
diff changeset
  1867
                sb.append(quote(doquote,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                            L_REG_NAME | L_SERVER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                            H_REG_NAME | H_SERVER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                sb.append(quote(authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                            L_REG_NAME | L_SERVER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                            H_REG_NAME | H_SERVER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    private void appendSchemeSpecificPart(StringBuffer sb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                                          String opaquePart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                                          String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                                          String userInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                                          String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                                          int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                                          String path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                                          String query)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        if (opaquePart != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
            /* check if SSP begins with an IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
             * because we must not quote a literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            if (opaquePart.startsWith("//[")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                int end =  opaquePart.indexOf("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                if (end != -1 && opaquePart.indexOf(":")!=-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                    String doquote, dontquote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                    if (end == opaquePart.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                        dontquote = opaquePart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                        doquote = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                        dontquote = opaquePart.substring(0,end+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                        doquote = opaquePart.substring(end+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                    sb.append (dontquote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                    sb.append(quote(doquote, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                sb.append(quote(opaquePart, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            appendAuthority(sb, authority, userInfo, host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            if (path != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                sb.append(quote(path, L_PATH, H_PATH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            if (query != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                sb.append('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                sb.append(quote(query, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    private void appendFragment(StringBuffer sb, String fragment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        if (fragment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            sb.append('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            sb.append(quote(fragment, L_URIC, H_URIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    private String toString(String scheme,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                            String opaquePart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                            String authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                            String userInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                            String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                            int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                            String path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                            String query,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                            String fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        if (scheme != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            sb.append(scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        appendSchemeSpecificPart(sb, opaquePart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                                 authority, userInfo, host, port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                                 path, query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        appendFragment(sb, fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    private void defineSchemeSpecificPart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        if (schemeSpecificPart != null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        appendSchemeSpecificPart(sb, null, getAuthority(), getUserInfo(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                                 host, port, getPath(), getQuery());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        if (sb.length() == 0) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        schemeSpecificPart = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    private void defineString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        if (string != null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        if (scheme != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            sb.append(scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        if (isOpaque()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            sb.append(schemeSpecificPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                sb.append("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                if (userInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                    sb.append(userInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                    sb.append('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                boolean needBrackets = ((host.indexOf(':') >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                                    && !host.startsWith("[")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                                    && !host.endsWith("]"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                if (needBrackets) sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                sb.append(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                if (needBrackets) sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                if (port != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                    sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                    sb.append(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            } else if (authority != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                sb.append("//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                sb.append(authority);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            if (path != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                sb.append(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            if (query != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                sb.append('?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                sb.append(query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        if (fragment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            sb.append('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            sb.append(fragment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        string = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    // -- Normalization, resolution, and relativization --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    // RFC2396 5.2 (6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
    private static String resolvePath(String base, String child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                                      boolean absolute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        int i = base.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        int cn = child.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        String path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        if (cn == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            // 5.2 (6a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            if (i >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                path = base.substring(0, i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            StringBuffer sb = new StringBuffer(base.length() + cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            // 5.2 (6a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            if (i >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                sb.append(base.substring(0, i + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            // 5.2 (6b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            sb.append(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            path = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        // 5.2 (6c-f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        String np = normalize(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        // 5.2 (6g): If the result is absolute but the path begins with "../",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        // then we simply leave the path as-is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        return np;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    // RFC2396 5.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    private static URI resolve(URI base, URI child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        // check if child if opaque first so that NPE is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        // if child is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        if (child.isOpaque() || base.isOpaque())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        // 5.2 (2): Reference to current document (lone fragment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        if ((child.scheme == null) && (child.authority == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            && child.path.equals("") && (child.fragment != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            && (child.query == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
            if ((base.fragment != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                && child.fragment.equals(base.fragment)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
            URI ru = new URI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            ru.scheme = base.scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            ru.authority = base.authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            ru.userInfo = base.userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
            ru.host = base.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
            ru.port = base.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            ru.path = base.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
            ru.fragment = child.fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            ru.query = base.query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            return ru;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        // 5.2 (3): Child is absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        if (child.scheme != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        URI ru = new URI();             // Resolved URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        ru.scheme = base.scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        ru.query = child.query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        ru.fragment = child.fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        // 5.2 (4): Authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        if (child.authority == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
            ru.authority = base.authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            ru.host = base.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            ru.userInfo = base.userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            ru.port = base.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            String cp = (child.path == null) ? "" : child.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            if ((cp.length() > 0) && (cp.charAt(0) == '/')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                // 5.2 (5): Child path is absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                ru.path = child.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                // 5.2 (6): Resolve relative path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                ru.path = resolvePath(base.path, cp, base.isAbsolute());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            ru.authority = child.authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            ru.host = child.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            ru.userInfo = child.userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            ru.host = child.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
            ru.port = child.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            ru.path = child.path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        // 5.2 (7): Recombine (nothing to do here)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        return ru;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
    // If the given URI's path is normal then return the URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    // o.w., return a new URI containing the normalized path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
    private static URI normalize(URI u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        if (u.isOpaque() || (u.path == null) || (u.path.length() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
            return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        String np = normalize(u.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        if (np == u.path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        URI v = new URI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        v.scheme = u.scheme;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        v.fragment = u.fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        v.authority = u.authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        v.userInfo = u.userInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        v.host = u.host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        v.port = u.port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        v.path = np;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        v.query = u.query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
    // If both URIs are hierarchical, their scheme and authority components are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    // identical, and the base path is a prefix of the child's path, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
    // return a relative URI that, when resolved against the base, yields the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
    // child; otherwise, return the child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
    private static URI relativize(URI base, URI child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        // check if child if opaque first so that NPE is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        // if child is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        if (child.isOpaque() || base.isOpaque())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        if (!equalIgnoringCase(base.scheme, child.scheme)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            || !equal(base.authority, child.authority))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        String bp = normalize(base.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        String cp = normalize(child.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        if (!bp.equals(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            if (!bp.endsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                bp = bp + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
            if (!cp.startsWith(bp))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        URI v = new URI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        v.path = cp.substring(bp.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        v.query = child.query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        v.fragment = child.fragment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    // -- Path normalization --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
    // The following algorithm for path normalization avoids the creation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
    // string object for each segment, as well as the use of a string buffer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
    // compute the final result, by using a single char array and editing it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
    // place.  The array is first split into segments, replacing each slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
    // with '\0' and creating a segment-index array, each element of which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
    // the index of the first char in the corresponding segment.  We then walk
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
    // through both arrays, removing ".", "..", and other segments as necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
    // by setting their entries in the index array to -1.  Finally, the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    // arrays are used to rejoin the segments and compute the final result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    // This code is based upon src/solaris/native/java/io/canonicalize_md.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
    // Check the given path to see if it might need normalization.  A path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    // might need normalization if it contains duplicate slashes, a "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
    // segment, or a ".." segment.  Return -1 if no further normalization is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
    // possible, otherwise return the number of segments found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
    // This method takes a string argument rather than a char array so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
    // this test can be performed without invoking path.toCharArray().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
    static private int needsNormalization(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        boolean normal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        int ns = 0;                     // Number of segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        int end = path.length() - 1;    // Index of last char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        int p = 0;                      // Index of next char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        // Skip initial slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
            if (path.charAt(p) != '/') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        if (p > 1) normal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        // Scan segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            // Looking at "." or ".." ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            if ((path.charAt(p) == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                && ((p == end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                    || ((path.charAt(p + 1) == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                        || ((path.charAt(p + 1) == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                            && ((p + 1 == end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                                || (path.charAt(p + 2) == '/')))))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                normal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            ns++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            // Find beginning of next segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                if (path.charAt(p++) != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                // Skip redundant slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                    if (path.charAt(p) != '/') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                    normal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        return normal ? -1 : ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    // Split the given path into segments, replacing slashes with nulls and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
    // filling in the given segment-index array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
    // Preconditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    //   segs.length == Number of segments in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    // Postconditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
    //   All slashes in path replaced by '\0'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    //   segs[i] == Index of first char in segment i (0 <= i < segs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    static private void split(char[] path, int[] segs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        int end = path.length - 1;      // Index of last char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        int p = 0;                      // Index of next char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
        int i = 0;                      // Index of current segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        // Skip initial slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            if (path[p] != '/') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            path[p] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            // Note start of segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            segs[i++] = p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            // Find beginning of next segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                if (path[p++] != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                path[p - 1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                // Skip redundant slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                while (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                    if (path[p] != '/') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                    path[p++] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        if (i != segs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            throw new InternalError();  // ASSERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
    // Join the segments in the given path according to the given segment-index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
    // array, ignoring those segments whose index entries have been set to -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
    // and inserting slashes as needed.  Return the length of the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
    // path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
    // Preconditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
    //   segs[i] == -1 implies segment i is to be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
    //   path computed by split, as above, with '\0' having replaced '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
    // Postconditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
    //   path[0] .. path[return value] == Resulting path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
    static private int join(char[] path, int[] segs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        int ns = segs.length;           // Number of segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        int end = path.length - 1;      // Index of last char in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        int p = 0;                      // Index of next path char to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        if (path[p] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            // Restore initial slash for absolute paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            path[p++] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
        for (int i = 0; i < ns; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            int q = segs[i];            // Current segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            if (q == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                // Ignore this segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            if (p == q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                // We're already at this segment, so just skip to its end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                while ((p <= end) && (path[p] != '\0'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                if (p <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                    // Preserve trailing slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                    path[p++] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
            } else if (p < q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                // Copy q down to p
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                while ((q <= end) && (path[q] != '\0'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                    path[p++] = path[q++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                if (q <= end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                    // Preserve trailing slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                    path[p++] = '/';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                throw new InternalError(); // ASSERT false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
    // Remove "." segments from the given path, and remove segment pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
    // consisting of a non-".." segment followed by a ".." segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
    private static void removeDots(char[] path, int[] segs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        int ns = segs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        int end = path.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        for (int i = 0; i < ns; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            int dots = 0;               // Number of dots found (0, 1, or 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
            // Find next occurrence of "." or ".."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                int p = segs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                if (path[p] == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                    if (p == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                        dots = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                    } else if (path[p + 1] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                        dots = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                    } else if ((path[p + 1] == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                               && ((p + 1 == end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                                   || (path[p + 2] == '\0'))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                        dots = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            } while (i < ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            if ((i > ns) || (dots == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            if (dots == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                // Remove this occurrence of "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                segs[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                // If there is a preceding non-".." segment, remove both that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                // segment and this occurrence of ".."; otherwise, leave this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                // ".." segment as-is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                for (j = i - 1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                    if (segs[j] != -1) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                if (j >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                    int q = segs[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                    if (!((path[q] == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                          && (path[q + 1] == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                          && (path[q + 2] == '\0'))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                        segs[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                        segs[j] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
    // DEVIATION: If the normalized path is relative, and if the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
    // segment could be parsed as a scheme name, then prepend a "." segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    private static void maybeAddLeadingDot(char[] path, int[] segs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        if (path[0] == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            // The path is absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
        int ns = segs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        int f = 0;                      // Index of first segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        while (f < ns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
            if (segs[f] >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            f++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        if ((f >= ns) || (f == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            // The path is empty, or else the original first segment survived,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            // in which case we already know that no leading "." is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        int p = segs[f];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        while ((p < path.length) && (path[p] != ':') && (path[p] != '\0')) p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        if (p >= path.length || path[p] == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            // No colon in first segment, so no "." needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        // At this point we know that the first segment is unused,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        // hence we can insert a "." segment at that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        path[0] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
        path[1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        segs[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
    // Normalize the given path string.  A normal path string has no empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
    // segments (i.e., occurrences of "//"), no segments equal to ".", and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
    // segments equal to ".." that are preceded by a segment not equal to "..".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
    // In contrast to Unix-style pathname normalization, for URI paths we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
    // always retain trailing slashes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
    private static String normalize(String ps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        // Does this path need normalization?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
        int ns = needsNormalization(ps);        // Number of segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        if (ns < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            // Nope -- just return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
            return ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        char[] path = ps.toCharArray();         // Path in char-array form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        // Split path into segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        int[] segs = new int[ns];               // Segment-index array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        split(path, segs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        // Remove dots
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        removeDots(path, segs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        // Prevent scheme-name confusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        maybeAddLeadingDot(path, segs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        // Join the remaining segments and return the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        String s = new String(path, 0, join(path, segs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        if (s.equals(ps)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            // string was already normalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
            return ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    // -- Character classes for parsing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
    // RFC2396 precisely specifies which characters in the US-ASCII charset are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
    // permissible in the various components of a URI reference.  We here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
    // define a set of mask pairs to aid in enforcing these restrictions.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
    // mask pair consists of two longs, a low mask and a high mask.  Taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
    // together they represent a 128-bit mask, where bit i is set iff the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
    // character with value i is permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
    // This approach is more efficient than sequentially searching arrays of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
    // permitted characters.  It could be made still more efficient by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
    // precompiling the mask information so that a character's presence in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
    // given mask could be determined by a single table lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
    // Compute the low-order mask for the characters in the given string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
    private static long lowMask(String chars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        int n = chars.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        long m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
            char c = chars.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            if (c < 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                m |= (1L << c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
    // Compute the high-order mask for the characters in the given string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
    private static long highMask(String chars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
        int n = chars.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
        long m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            char c = chars.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
            if ((c >= 64) && (c < 128))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                m |= (1L << (c - 64));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
    // Compute a low-order mask for the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
    // between first and last, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
    private static long lowMask(char first, char last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        long m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        int f = Math.max(Math.min(first, 63), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
        int l = Math.max(Math.min(last, 63), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        for (int i = f; i <= l; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            m |= 1L << i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
    // Compute a high-order mask for the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
    // between first and last, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
    private static long highMask(char first, char last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
        long m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
        int f = Math.max(Math.min(first, 127), 64) - 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        int l = Math.max(Math.min(last, 127), 64) - 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        for (int i = f; i <= l; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
            m |= 1L << i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
    // Tell whether the given character is permitted by the given mask pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
    private static boolean match(char c, long lowMask, long highMask) {
5612
c0d1673e1ca6 6773270: java.net.URI fails to escape \u0000
michaelm
parents: 715
diff changeset
  2515
        if (c == 0) // 0 doesn't have a slot in the mask. So, it never matches.
c0d1673e1ca6 6773270: java.net.URI fails to escape \u0000
michaelm
parents: 715
diff changeset
  2516
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        if (c < 64)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
            return ((1L << c) & lowMask) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
        if (c < 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
            return ((1L << (c - 64)) & highMask) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
    // Character-class masks, in reverse order from RFC2396 because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
    // initializers for static fields cannot make forward references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
    // digit    = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
    //            "8" | "9"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
    private static final long L_DIGIT = lowMask('0', '9');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
    private static final long H_DIGIT = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
    // upalpha  = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
    //            "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
    //            "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
    private static final long L_UPALPHA = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
    private static final long H_UPALPHA = highMask('A', 'Z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
    // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
    //            "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
    //            "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
    private static final long L_LOWALPHA = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
    private static final long H_LOWALPHA = highMask('a', 'z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    // alpha         = lowalpha | upalpha
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
    private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
    private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
    // alphanum      = alpha | digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
    private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
    private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
    // hex           = digit | "A" | "B" | "C" | "D" | "E" | "F" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
    //                         "a" | "b" | "c" | "d" | "e" | "f"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
    private static final long L_HEX = L_DIGIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
    private static final long H_HEX = highMask('A', 'F') | highMask('a', 'f');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
    // mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
    //                 "(" | ")"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
    private static final long L_MARK = lowMask("-_.!~*'()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
    private static final long H_MARK = highMask("-_.!~*'()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
    // unreserved    = alphanum | mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
    private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
    private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
    // reserved      = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
    //                 "$" | "," | "[" | "]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
    // Added per RFC2732: "[", "]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
    private static final long L_RESERVED = lowMask(";/?:@&=+$,[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
    private static final long H_RESERVED = highMask(";/?:@&=+$,[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
    // The zero'th bit is used to indicate that escape pairs and non-US-ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
    // characters are allowed; this is handled by the scanEscape method below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
    private static final long L_ESCAPED = 1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
    private static final long H_ESCAPED = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
    // uric          = reserved | unreserved | escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
    private static final long L_URIC = L_RESERVED | L_UNRESERVED | L_ESCAPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
    private static final long H_URIC = H_RESERVED | H_UNRESERVED | H_ESCAPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
    // pchar         = unreserved | escaped |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
    //                 ":" | "@" | "&" | "=" | "+" | "$" | ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
    private static final long L_PCHAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
        = L_UNRESERVED | L_ESCAPED | lowMask(":@&=+$,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
    private static final long H_PCHAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
        = H_UNRESERVED | H_ESCAPED | highMask(":@&=+$,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
    // All valid path characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
    private static final long L_PATH = L_PCHAR | lowMask(";/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
    private static final long H_PATH = H_PCHAR | highMask(";/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
    // Dash, for use in domainlabel and toplabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
    private static final long L_DASH = lowMask("-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
    private static final long H_DASH = highMask("-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
    // Dot, for use in hostnames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
    private static final long L_DOT = lowMask(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
    private static final long H_DOT = highMask(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
    // userinfo      = *( unreserved | escaped |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
    //                    ";" | ":" | "&" | "=" | "+" | "$" | "," )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
    private static final long L_USERINFO
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
        = L_UNRESERVED | L_ESCAPED | lowMask(";:&=+$,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
    private static final long H_USERINFO
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
        = H_UNRESERVED | H_ESCAPED | highMask(";:&=+$,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    // reg_name      = 1*( unreserved | escaped | "$" | "," |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
    //                     ";" | ":" | "@" | "&" | "=" | "+" )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
    private static final long L_REG_NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
        = L_UNRESERVED | L_ESCAPED | lowMask("$,;:@&=+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
    private static final long H_REG_NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
        = H_UNRESERVED | H_ESCAPED | highMask("$,;:@&=+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    // All valid characters for server-based authorities
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
    private static final long L_SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        = L_USERINFO | L_ALPHANUM | L_DASH | lowMask(".:@[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
    private static final long H_SERVER
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
        = H_USERINFO | H_ALPHANUM | H_DASH | highMask(".:@[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
    // Special case of server authority that represents an IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
    // In this case, a % does not signify an escape sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
    private static final long L_SERVER_PERCENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        = L_SERVER | lowMask("%");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
    private static final long H_SERVER_PERCENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
        = H_SERVER | highMask("%");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
    private static final long L_LEFT_BRACKET = lowMask("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
    private static final long H_LEFT_BRACKET = highMask("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
    // scheme        = alpha *( alpha | digit | "+" | "-" | "." )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
    private static final long L_SCHEME = L_ALPHA | L_DIGIT | lowMask("+-.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
    private static final long H_SCHEME = H_ALPHA | H_DIGIT | highMask("+-.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
    // uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
    //                 "&" | "=" | "+" | "$" | ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
    private static final long L_URIC_NO_SLASH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
        = L_UNRESERVED | L_ESCAPED | lowMask(";?:@&=+$,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
    private static final long H_URIC_NO_SLASH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
        = H_UNRESERVED | H_ESCAPED | highMask(";?:@&=+$,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
    // -- Escaping and encoding --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
    private final static char[] hexDigits = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
        '0', '1', '2', '3', '4', '5', '6', '7',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
    private static void appendEscape(StringBuffer sb, byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        sb.append('%');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        sb.append(hexDigits[(b >> 4) & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
        sb.append(hexDigits[(b >> 0) & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
    private static void appendEncoded(StringBuffer sb, char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
            bb = ThreadLocalCoders.encoderFor("UTF-8")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                .encode(CharBuffer.wrap("" + c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
        } catch (CharacterCodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
        while (bb.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
            int b = bb.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
            if (b >= 0x80)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                appendEscape(sb, (byte)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                sb.append((char)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
    // Quote any characters in s that are not permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
    // by the given mask pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
    private static String quote(String s, long lowMask, long highMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
        StringBuffer sb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
        boolean allowNonASCII = ((lowMask & L_ESCAPED) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
            if (c < '\u0080') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
                if (!match(c, lowMask, highMask)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                    if (sb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                        sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
                        sb.append(s.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
                    appendEscape(sb, (byte)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                    if (sb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                        sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
            } else if (allowNonASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                       && (Character.isSpaceChar(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                           || Character.isISOControl(c))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                if (sb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                    sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                    sb.append(s.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                appendEncoded(sb, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                if (sb != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                    sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
        return (sb == null) ? s : sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
    // Encodes all characters >= \u0080 into escaped, normalized UTF-8 octets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
    // assuming that s is otherwise legal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
    private static String encode(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
        // First check whether we actually need to encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
        for (int i = 0;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
            if (s.charAt(i) >= '\u0080')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            if (++i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
        String ns = Normalizer.normalize(s, Normalizer.Form.NFC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
        ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
            bb = ThreadLocalCoders.encoderFor("UTF-8")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                .encode(CharBuffer.wrap(ns));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
        } catch (CharacterCodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
        while (bb.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
            int b = bb.get() & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
            if (b >= 0x80)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                appendEscape(sb, (byte)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                sb.append((char)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
    private static int decode(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
        if ((c >= '0') && (c <= '9'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
            return c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
        if ((c >= 'a') && (c <= 'f'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
            return c - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        if ((c >= 'A') && (c <= 'F'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
            return c - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
        assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
    private static byte decode(char c1, char c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        return (byte)(  ((decode(c1) & 0xf) << 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
                      | ((decode(c2) & 0xf) << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
    // Evaluates all escapes in s, applying UTF-8 decoding if needed.  Assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
    // that escapes are well-formed syntactically, i.e., of the form %XX.  If a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
    // sequence of escaped octets is not valid UTF-8 then the erroneous octets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
    // are replaced with '\uFFFD'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
    // Exception: any "%" found between "[]" is left alone. It is an IPv6 literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
    //            with a scope_id
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
    private static String decode(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
        if (s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
        if (s.indexOf('%') < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
        StringBuffer sb = new StringBuffer(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
        ByteBuffer bb = ByteBuffer.allocate(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
        CharBuffer cb = CharBuffer.allocate(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
        CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
            .onMalformedInput(CodingErrorAction.REPLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
        // This is not horribly efficient, but it will do for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
        char c = s.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
        boolean betweenBrackets = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
        for (int i = 0; i < n;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
            assert c == s.charAt(i);    // Loop invariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
            if (c == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                betweenBrackets = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
            } else if (betweenBrackets && c == ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                betweenBrackets = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
            if (c != '%' || betweenBrackets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                if (++i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            int ui = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
                assert (n - i >= 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                bb.put(decode(s.charAt(++i), s.charAt(++i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                if (++i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
                if (c != '%')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
            cb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
            dec.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
            CoderResult cr = dec.decode(bb, cb, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
            assert cr.isUnderflow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
            cr = dec.flush(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
            assert cr.isUnderflow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
            sb.append(cb.flip().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
    // -- Parsing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
    // For convenience we wrap the input URI string in a new instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
    // following internal class.  This saves always having to pass the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
    // string as an argument to each internal scan/parse method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
    private class Parser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
        private String input;           // URI input string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
        private boolean requireServerAuthority = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
        Parser(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
            input = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
            string = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
        // -- Methods for throwing URISyntaxException in various ways --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
        private void fail(String reason) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
            throw new URISyntaxException(input, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
        private void fail(String reason, int p) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
            throw new URISyntaxException(input, reason, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
        private void failExpecting(String expected, int p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
            fail("Expected " + expected, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
        private void failExpecting(String expected, String prior, int p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
            fail("Expected " + expected + " following " + prior, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
        // -- Simple access to the input string --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        // Return a substring of the input string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        private String substring(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
            return input.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        // Return the char at position p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
        // assuming that p < input.length()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
        private char charAt(int p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
            return input.charAt(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
        // Tells whether start < end and, if so, whether charAt(start) == c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
        private boolean at(int start, int end, char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
            return (start < end) && (charAt(start) == c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
        // Tells whether start + s.length() < end and, if so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
        // whether the chars at the start position match s exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
        private boolean at(int start, int end, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
            int sn = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
            if (sn > end - p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
            while (i < sn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                if (charAt(p++) != s.charAt(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
            return (i == sn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        // -- Scanning --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
        // The various scan and parse methods that follow use a uniform
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
        // convention of taking the current start position and end index as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
        // their first two arguments.  The start is inclusive while the end is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
        // exclusive, just as in the String class, i.e., a start/end pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
        // denotes the left-open interval [start, end) of the input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
        // These methods never proceed past the end position.  They may return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
        // -1 to indicate outright failure, but more often they simply return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
        // the position of the first char after the last char scanned.  Thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
        // a typical idiom is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
        //     int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
        //     int q = scan(p, end, ...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
        //     if (q > p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
        //         // We scanned something
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
        //         ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
        //     else if (q == p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
        //         // We scanned nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
        //         ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
        //     else if (q == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
        //         // Something went wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
        //         ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
        // Scan a specific char: If the char at the given start position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
        // equal to c, return the index of the next char; otherwise, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
        // start position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
        private int scan(int start, int end, char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
            if ((start < end) && (charAt(start) == c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                return start + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
            return start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
        // Scan forward from the given start position.  Stop at the first char
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
        // in the err string (in which case -1 is returned), or the first char
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
        // in the stop string (in which case the index of the preceding char is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        // returned), or the end of the input string (in which case the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
        // of the input string is returned).  May return the start position if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
        // nothing matches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
        private int scan(int start, int end, String err, String stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
            while (p < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                char c = charAt(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
                if (err.indexOf(c) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                if (stop.indexOf(c) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
        // Scan a potential escape sequence, starting at the given position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        // with the given first char (i.e., charAt(start) == c).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
        // This method assumes that if escapes are allowed then visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        // non-US-ASCII chars are also allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
        private int scanEscape(int start, int n, char first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
            char c = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
            if (c == '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                // Process escape pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                if ((p + 3 <= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                    && match(charAt(p + 1), L_HEX, H_HEX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                    && match(charAt(p + 2), L_HEX, H_HEX)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                    return p + 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                fail("Malformed escape pair", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            } else if ((c > 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
                       && !Character.isSpaceChar(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                       && !Character.isISOControl(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
                // Allow unescaped but visible non-US-ASCII chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                return p + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        // Scan chars that match the given mask pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
        private int scan(int start, int n, long lowMask, long highMask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
            while (p < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
                char c = charAt(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
                if (match(c, lowMask, highMask)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                if ((lowMask & L_ESCAPED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
                    int q = scanEscape(p, n, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
                    if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
                        p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        // Check that each of the chars in [start, end) matches the given mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
        private void checkChars(int start, int end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                                long lowMask, long highMask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                                String what)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
            int p = scan(start, end, lowMask, highMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
            if (p < end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                fail("Illegal character in " + what, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
        // Check that the char at position p matches the given mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
        private void checkChar(int p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
                               long lowMask, long highMask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
                               String what)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            checkChars(p, p + 1, lowMask, highMask, what);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
        // -- Parsing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
        // [<scheme>:]<scheme-specific-part>[#<fragment>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
        void parse(boolean rsa) throws URISyntaxException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
            requireServerAuthority = rsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
            int ssp;                    // Start of scheme-specific part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
            int n = input.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
            int p = scan(0, n, "/?#", ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
            if ((p >= 0) && at(p, n, ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                if (p == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
                    failExpecting("scheme name", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
                checkChar(0, L_ALPHA, H_ALPHA, "scheme name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
                checkChars(1, p, L_SCHEME, H_SCHEME, "scheme name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
                scheme = substring(0, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
                p++;                    // Skip ':'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
                ssp = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
                if (at(p, n, '/')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
                    p = parseHierarchical(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
                    int q = scan(p, n, "", "#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
                    if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
                        failExpecting("scheme-specific part", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                    checkChars(p, q, L_URIC, H_URIC, "opaque part");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
                    p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
                ssp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
                p = parseHierarchical(0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
            schemeSpecificPart = substring(ssp, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
            if (at(p, n, '#')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
                checkChars(p + 1, n, L_URIC, H_URIC, "fragment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                fragment = substring(p + 1, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
                p = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
            if (p < n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
                fail("end of URI", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
        // [//authority]<path>[?<query>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
        // DEVIATION from RFC2396: We allow an empty authority component as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        // long as it's followed by a non-empty path, query component, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
        // fragment component.  This is so that URIs such as "file:///foo/bar"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
        // will parse.  This seems to be the intent of RFC2396, though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
        // grammar does not permit it.  If the authority is empty then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
        // userInfo, host, and port components are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
        // DEVIATION from RFC2396: We allow empty relative paths.  This seems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
        // to be the intent of RFC2396, but the grammar does not permit it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        // The primary consequence of this deviation is that "#f" parses as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
        // relative URI with an empty path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
        private int parseHierarchical(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
            if (at(p, n, '/') && at(p + 1, n, '/')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                p += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
                int q = scan(p, n, "", "/?#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
                if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
                    p = parseAuthority(p, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
                } else if (q < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
                    // DEVIATION: Allow empty authority prior to non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                    // path, query component or fragment identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                    failExpecting("authority", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
            int q = scan(p, n, "", "?#"); // DEVIATION: May be empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
            checkChars(p, q, L_PATH, H_PATH, "path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
            path = substring(p, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
            p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
            if (at(p, n, '?')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
                q = scan(p, n, "", "#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
                checkChars(p, q, L_URIC, H_URIC, "query");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
                query = substring(p, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
        // authority     = server | reg_name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
        // Ambiguity: An authority that is a registry name rather than a server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
        // might have a prefix that parses as a server.  We use the fact that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
        // the authority component is always followed by '/' or the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
        // input string to resolve this: If the complete authority did not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
        // parse as a server then we try to parse it as a registry name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
        private int parseAuthority(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
            int q = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
            URISyntaxException ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
            boolean serverChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
            boolean regChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
            if (scan(p, n, "", "]") > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
                // contains a literal IPv6 address, therefore % is allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
                serverChars = (scan(p, n, L_SERVER_PERCENT, H_SERVER_PERCENT) == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
                serverChars = (scan(p, n, L_SERVER, H_SERVER) == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
            regChars = (scan(p, n, L_REG_NAME, H_REG_NAME) == n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
            if (regChars && !serverChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                // Must be a registry-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
                authority = substring(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
                return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
            if (serverChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
                // Might be (probably is) a server-based authority, so attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
                // to parse it as such.  If the attempt fails, try to treat it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
                // as a registry-based authority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
                    q = parseServer(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
                    if (q < n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
                        failExpecting("end of authority", q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
                    authority = substring(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
                } catch (URISyntaxException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
                    // Undo results of failed parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
                    userInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
                    host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
                    port = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
                    if (requireServerAuthority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
                        // If we're insisting upon a server-based authority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
                        // then just re-throw the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
                        throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
                        // Save the exception in case it doesn't parse as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
                        // registry either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
                        ex = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
                        q = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
            if (q < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
                if (regChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
                    // Registry-based authority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
                    authority = substring(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
                } else if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
                    // Re-throw exception; it was probably due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
                    // a malformed IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
                    throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
                    fail("Illegal character in authority", q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
        // [<userinfo>@]<host>[:<port>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
        private int parseServer(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
            // userinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
            q = scan(p, n, "/?#", "@");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
            if ((q >= p) && at(q, n, '@')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
                checkChars(p, q, L_USERINFO, H_USERINFO, "user info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
                userInfo = substring(p, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
                p = q + 1;              // Skip '@'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
            // hostname, IPv4 address, or IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
            if (at(p, n, '[')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
                // DEVIATION from RFC2396: Support IPv6 addresses, per RFC2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
                q = scan(p, n, "/?#", "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
                if ((q > p) && at(q, n, ']')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
                    // look for a "%" scope id
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
                    int r = scan (p, q, "", "%");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
                    if (r > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
                        parseIPv6Reference(p, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                        if (r+1 == q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
                            fail ("scope id expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
                        checkChars (r+1, q, L_ALPHANUM, H_ALPHANUM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
                                                "scope id");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
                        parseIPv6Reference(p, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
                    host = substring(p-1, q+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
                    p = q + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
                    failExpecting("closing bracket for IPv6 address", q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
                q = parseIPv4Address(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
                if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
                    q = parseHostname(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
            // port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
            if (at(p, n, ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
                q = scan(p, n, "", "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
                if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
                    checkChars(p, q, L_DIGIT, H_DIGIT, "port number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
                        port = Integer.parseInt(substring(p, q));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
                    } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
                        fail("Malformed port number", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
                    p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
            if (p < n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
                failExpecting("port number", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
        // Scan a string of decimal digits whose value fits in a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
        private int scanByte(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
            int q = scan(p, n, L_DIGIT, H_DIGIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
            if (q <= p) return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
            if (Integer.parseInt(substring(p, q)) > 255) return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
            return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
        // Scan an IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
        // If the strict argument is true then we require that the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
        // interval contain nothing besides an IPv4 address; if it is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
        // then we only require that it start with an IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
        // If the interval does not contain or start with (depending upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
        // strict argument) a legal IPv4 address characters then we return -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
        // immediately; otherwise we insist that these characters parse as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
        // legal IPv4 address and throw an exception on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
        // We assume that any string of decimal digits and dots must be an IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
        // address.  It won't parse as a hostname anyway, so making that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
        // assumption here allows more meaningful exceptions to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
        private int scanIPv4Address(int start, int n, boolean strict)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
            int m = scan(p, n, L_DIGIT | L_DOT, H_DIGIT | H_DOT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
            if ((m <= p) || (strict && (m != n)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
                // Per RFC2732: At most three digits per byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
                // Further constraint: Each element fits in a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
                if ((q = scanByte(p, m)) <= p) break;   p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
                if ((q = scan(p, m, '.')) <= p) break;  p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
                if ((q = scanByte(p, m)) <= p) break;   p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
                if ((q = scan(p, m, '.')) <= p) break;  p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
                if ((q = scanByte(p, m)) <= p) break;   p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
                if ((q = scan(p, m, '.')) <= p) break;  p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
                if ((q = scanByte(p, m)) <= p) break;   p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
                if (q < m) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
                return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
            fail("Malformed IPv4 address", q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
        // Take an IPv4 address: Throw an exception if the given interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
        // contains anything except an IPv4 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
        private int takeIPv4Address(int start, int n, String expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
            int p = scanIPv4Address(start, n, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
            if (p <= start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                failExpecting(expected, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
        // Attempt to parse an IPv4 address, returning -1 on failure but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
        // allowing the given interval to contain [:<characters>] after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
        // the IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
        private int parseIPv4Address(int start, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
            int p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                p = scanIPv4Address(start, n, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
            } catch (URISyntaxException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
            if (p > start && p < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
                // IPv4 address is followed by something - check that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
                // it's a ":" as this is the only valid character to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
                // follow an address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                if (charAt(p) != ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                    p = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
            if (p > start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
                host = substring(start, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
        // hostname      = domainlabel [ "." ] | 1*( domainlabel "." ) toplabel [ "." ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
        // domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
        // toplabel      = alpha | alpha *( alphanum | "-" ) alphanum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
        private int parseHostname(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
            int l = -1;                 // Start of last parsed label
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
                // domainlabel = alphanum [ *( alphanum | "-" ) alphanum ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
                q = scan(p, n, L_ALPHANUM, H_ALPHANUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
                if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
                l = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
                if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
                    p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
                    q = scan(p, n, L_ALPHANUM | L_DASH, H_ALPHANUM | H_DASH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
                    if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
                        if (charAt(q - 1) == '-')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
                            fail("Illegal character in hostname", q - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
                        p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                q = scan(p, n, '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
            } while (p < n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
            if ((p < n) && !at(p, n, ':'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
                fail("Illegal character in hostname", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
            if (l < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
                failExpecting("hostname", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
            // for a fully qualified hostname check that the rightmost
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
            // label starts with an alpha character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
            if (l > start && !match(charAt(l), L_ALPHA, H_ALPHA)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
                fail("Illegal character in hostname", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
            host = substring(start, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
        // IPv6 address parsing, from RFC2373: IPv6 Addressing Architecture
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
        // Bug: The grammar in RFC2373 Appendix B does not allow addresses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
        // the form ::12.34.56.78, which are clearly shown in the examples
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
        // earlier in the document.  Here is the original grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
        //   IPv6address = hexpart [ ":" IPv4address ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
        //   hexpart     = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
        //   hexseq      = hex4 *( ":" hex4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        //   hex4        = 1*4HEXDIG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
        // We therefore use the following revised grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
        //   IPv6address = hexseq [ ":" IPv4address ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
        //                 | hexseq [ "::" [ hexpost ] ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
        //                 | "::" [ hexpost ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
        //   hexpost     = hexseq | hexseq ":" IPv4address | IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
        //   hexseq      = hex4 *( ":" hex4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
        //   hex4        = 1*4HEXDIG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
        // This covers all and only the following cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
        //   hexseq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
        //   hexseq : IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
        //   hexseq ::
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
        //   hexseq :: hexseq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
        //   hexseq :: hexseq : IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
        //   hexseq :: IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
        //   :: hexseq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
        //   :: hexseq : IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
        //   :: IPv4address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
        //   ::
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
        // Additionally we constrain the IPv6 address as follows :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
        //  i.  IPv6 addresses without compressed zeros should contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
        //      exactly 16 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
        //  ii. IPv6 addresses with compressed zeros should contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
        //      less than 16 bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
        private int ipv6byteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
        private int parseIPv6Reference(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
            boolean compressedZeros = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
            q = scanHexSeq(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
            if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                if (at(p, n, "::")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                    compressedZeros = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
                    p = scanHexPost(p + 2, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                } else if (at(p, n, ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
                    p = takeIPv4Address(p + 1,  n, "IPv4 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
                    ipv6byteCount += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
            } else if (at(p, n, "::")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                compressedZeros = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                p = scanHexPost(p + 2, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
            if (p < n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
                fail("Malformed IPv6 address", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
            if (ipv6byteCount > 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                fail("IPv6 address too long", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
            if (!compressedZeros && ipv6byteCount < 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
                fail("IPv6 address too short", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
            if (compressedZeros && ipv6byteCount == 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
                fail("Malformed IPv6 address", start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
        private int scanHexPost(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
            if (p == n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
            q = scanHexSeq(p, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
            if (q > p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
                if (at(p, n, ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
                    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
                    p = takeIPv4Address(p, n, "hex digits or IPv4 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
                    ipv6byteCount += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
                p = takeIPv4Address(p, n, "hex digits or IPv4 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
                ipv6byteCount += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
        // Scan a hex sequence; return -1 if one could not be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
        private int scanHexSeq(int start, int n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
            throws URISyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
            int p = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
            int q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
            q = scan(p, n, L_HEX, H_HEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
            if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
            if (at(q, n, '.'))          // Beginning of IPv4 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
            if (q > p + 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
                fail("IPv6 hexadecimal digit sequence too long", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
            ipv6byteCount += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
            p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
            while (p < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
                if (!at(p, n, ':'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
                if (at(p + 1, n, ':'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
                    break;              // "::"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
                p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
                q = scan(p, n, L_HEX, H_HEX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
                if (q <= p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
                    failExpecting("digits for an IPv6 address", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
                if (at(q, n, '.')) {    // Beginning of IPv4 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                    p--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
                if (q > p + 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
                    fail("IPv6 hexadecimal digit sequence too long", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                ipv6byteCount += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                p = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
}