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