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