emul/src/main/java/java/net/URL.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Dec 2012 10:05:55 +0100
changeset 340 785b53689e29
parent 339 7579a0ee92fb
child 374 e5b4bd29f268
permissions -rw-r--r--
Pre-initializing handler class is no longer necessary
jtulach@120
     1
/*
jtulach@120
     2
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
jtulach@120
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@120
     4
 *
jtulach@120
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@120
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@120
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@120
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@120
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@120
    10
 *
jtulach@120
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@120
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@120
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@120
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@120
    15
 * accompanied this code).
jtulach@120
    16
 *
jtulach@120
    17
 * You should have received a copy of the GNU General Public License version
jtulach@120
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@120
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@120
    20
 *
jtulach@120
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@120
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@120
    23
 * questions.
jtulach@120
    24
 */
jtulach@120
    25
jtulach@120
    26
package java.net;
jtulach@120
    27
jtulach@120
    28
import java.io.IOException;
jtulach@120
    29
import java.io.InputStream;
jtulach@120
    30
jtulach@120
    31
/**
jtulach@120
    32
 * Class <code>URL</code> represents a Uniform Resource
jtulach@120
    33
 * Locator, a pointer to a "resource" on the World
jtulach@120
    34
 * Wide Web. A resource can be something as simple as a file or a
jtulach@120
    35
 * directory, or it can be a reference to a more complicated object,
jtulach@120
    36
 * such as a query to a database or to a search engine. More
jtulach@120
    37
 * information on the types of URLs and their formats can be found at:
jtulach@120
    38
 * <blockquote>
jtulach@120
    39
 *     <a href="http://www.socs.uts.edu.au/MosaicDocs-old/url-primer.html">
jtulach@120
    40
 *    <i>http://www.socs.uts.edu.au/MosaicDocs-old/url-primer.html</i></a>
jtulach@120
    41
 * </blockquote>
jtulach@120
    42
 * <p>
jtulach@120
    43
 * In general, a URL can be broken into several parts. The previous
jtulach@120
    44
 * example of a URL indicates that the protocol to use is
jtulach@120
    45
 * <code>http</code> (HyperText Transfer Protocol) and that the
jtulach@120
    46
 * information resides on a host machine named
jtulach@120
    47
 * <code>www.socs.uts.edu.au</code>. The information on that host
jtulach@120
    48
 * machine is named <code>/MosaicDocs-old/url-primer.html</code>. The exact
jtulach@120
    49
 * meaning of this name on the host machine is both protocol
jtulach@120
    50
 * dependent and host dependent. The information normally resides in
jtulach@120
    51
 * a file, but it could be generated on the fly. This component of
jtulach@120
    52
 * the URL is called the <i>path</i> component.
jtulach@120
    53
 * <p>
jtulach@120
    54
 * A URL can optionally specify a "port", which is the
jtulach@120
    55
 * port number to which the TCP connection is made on the remote host
jtulach@120
    56
 * machine. If the port is not specified, the default port for
jtulach@120
    57
 * the protocol is used instead. For example, the default port for
jtulach@120
    58
 * <code>http</code> is <code>80</code>. An alternative port could be
jtulach@120
    59
 * specified as:
jtulach@120
    60
 * <blockquote><pre>
jtulach@120
    61
 *     http://www.socs.uts.edu.au:80/MosaicDocs-old/url-primer.html
jtulach@120
    62
 * </pre></blockquote>
jtulach@120
    63
 * <p>
jtulach@120
    64
 * The syntax of <code>URL</code> is defined by  <a
jtulach@120
    65
 * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
jtulach@120
    66
 * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
jtulach@120
    67
 * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
jtulach@120
    68
 * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
jtulach@120
    69
 * also supports scope_ids. The syntax and usage of scope_ids is described
jtulach@120
    70
 * <a href="Inet6Address.html#scoped">here</a>.
jtulach@120
    71
 * <p>
jtulach@120
    72
 * A URL may have appended to it a "fragment", also known
jtulach@120
    73
 * as a "ref" or a "reference". The fragment is indicated by the sharp
jtulach@120
    74
 * sign character "#" followed by more characters. For example,
jtulach@120
    75
 * <blockquote><pre>
jtulach@120
    76
 *     http://java.sun.com/index.html#chapter1
jtulach@120
    77
 * </pre></blockquote>
jtulach@120
    78
 * <p>
jtulach@120
    79
 * This fragment is not technically part of the URL. Rather, it
jtulach@120
    80
 * indicates that after the specified resource is retrieved, the
jtulach@120
    81
 * application is specifically interested in that part of the
jtulach@120
    82
 * document that has the tag <code>chapter1</code> attached to it. The
jtulach@120
    83
 * meaning of a tag is resource specific.
jtulach@120
    84
 * <p>
jtulach@120
    85
 * An application can also specify a "relative URL",
jtulach@120
    86
 * which contains only enough information to reach the resource
jtulach@120
    87
 * relative to another URL. Relative URLs are frequently used within
jtulach@120
    88
 * HTML pages. For example, if the contents of the URL:
jtulach@120
    89
 * <blockquote><pre>
jtulach@120
    90
 *     http://java.sun.com/index.html
jtulach@120
    91
 * </pre></blockquote>
jtulach@120
    92
 * contained within it the relative URL:
jtulach@120
    93
 * <blockquote><pre>
jtulach@120
    94
 *     FAQ.html
jtulach@120
    95
 * </pre></blockquote>
jtulach@120
    96
 * it would be a shorthand for:
jtulach@120
    97
 * <blockquote><pre>
jtulach@120
    98
 *     http://java.sun.com/FAQ.html
jtulach@120
    99
 * </pre></blockquote>
jtulach@120
   100
 * <p>
jtulach@120
   101
 * The relative URL need not specify all the components of a URL. If
jtulach@120
   102
 * the protocol, host name, or port number is missing, the value is
jtulach@120
   103
 * inherited from the fully specified URL. The file component must be
jtulach@120
   104
 * specified. The optional fragment is not inherited.
jtulach@120
   105
 * <p>
jtulach@120
   106
 * The URL class does not itself encode or decode any URL components
jtulach@120
   107
 * according to the escaping mechanism defined in RFC2396. It is the
jtulach@120
   108
 * responsibility of the caller to encode any fields, which need to be
jtulach@120
   109
 * escaped prior to calling URL, and also to decode any escaped fields,
jtulach@120
   110
 * that are returned from URL. Furthermore, because URL has no knowledge
jtulach@120
   111
 * of URL escaping, it does not recognise equivalence between the encoded
jtulach@120
   112
 * or decoded form of the same URL. For example, the two URLs:<br>
jtulach@120
   113
 * <pre>    http://foo.com/hello world/ and http://foo.com/hello%20world</pre>
jtulach@120
   114
 * would be considered not equal to each other.
jtulach@120
   115
 * <p>
jtulach@120
   116
 * Note, the {@link java.net.URI} class does perform escaping of its
jtulach@120
   117
 * component fields in certain circumstances. The recommended way
jtulach@120
   118
 * to manage the encoding and decoding of URLs is to use {@link java.net.URI},
jtulach@120
   119
 * and to convert between these two classes using {@link #toURI()} and
jtulach@120
   120
 * {@link URI#toURL()}.
jtulach@120
   121
 * <p>
jtulach@120
   122
 * The {@link URLEncoder} and {@link URLDecoder} classes can also be
jtulach@120
   123
 * used, but only for HTML form encoding, which is not the same
jtulach@120
   124
 * as the encoding scheme defined in RFC2396.
jtulach@120
   125
 *
jtulach@120
   126
 * @author  James Gosling
jtulach@120
   127
 * @since JDK1.0
jtulach@120
   128
 */
jtulach@120
   129
public final class URL implements java.io.Serializable {
jtulach@120
   130
jtulach@120
   131
    static final long serialVersionUID = -7627629688361524110L;
jtulach@120
   132
jtulach@120
   133
    /**
jtulach@120
   134
     * The property which specifies the package prefix list to be scanned
jtulach@120
   135
     * for protocol handlers.  The value of this property (if any) should
jtulach@120
   136
     * be a vertical bar delimited list of package names to search through
jtulach@120
   137
     * for a protocol handler to load.  The policy of this class is that
jtulach@120
   138
     * all protocol handlers will be in a class called <protocolname>.Handler,
jtulach@120
   139
     * and each package in the list is examined in turn for a matching
jtulach@120
   140
     * handler.  If none are found (or the property is not specified), the
jtulach@120
   141
     * default package prefix, sun.net.www.protocol, is used.  The search
jtulach@120
   142
     * proceeds from the first package in the list to the last and stops
jtulach@120
   143
     * when a match is found.
jtulach@120
   144
     */
jtulach@120
   145
    private static final String protocolPathProp = "java.protocol.handler.pkgs";
jtulach@120
   146
jtulach@120
   147
    /**
jtulach@120
   148
     * The protocol to use (ftp, http, nntp, ... etc.) .
jtulach@120
   149
     * @serial
jtulach@120
   150
     */
jtulach@120
   151
    private String protocol;
jtulach@120
   152
jtulach@120
   153
    /**
jtulach@120
   154
     * The host name to connect to.
jtulach@120
   155
     * @serial
jtulach@120
   156
     */
jtulach@120
   157
    private String host;
jtulach@120
   158
jtulach@120
   159
    /**
jtulach@120
   160
     * The protocol port to connect to.
jtulach@120
   161
     * @serial
jtulach@120
   162
     */
jtulach@120
   163
    private int port = -1;
jtulach@120
   164
jtulach@120
   165
    /**
jtulach@120
   166
     * The specified file name on that host. <code>file</code> is
jtulach@120
   167
     * defined as <code>path[?query]</code>
jtulach@120
   168
     * @serial
jtulach@120
   169
     */
jtulach@120
   170
    private String file;
jtulach@120
   171
jtulach@120
   172
    /**
jtulach@120
   173
     * The query part of this URL.
jtulach@120
   174
     */
jtulach@120
   175
    private transient String query;
jtulach@120
   176
jtulach@120
   177
    /**
jtulach@120
   178
     * The authority part of this URL.
jtulach@120
   179
     * @serial
jtulach@120
   180
     */
jtulach@120
   181
    private String authority;
jtulach@120
   182
jtulach@120
   183
    /**
jtulach@120
   184
     * The path part of this URL.
jtulach@120
   185
     */
jtulach@120
   186
    private transient String path;
jtulach@120
   187
jtulach@120
   188
    /**
jtulach@120
   189
     * The userinfo part of this URL.
jtulach@120
   190
     */
jtulach@120
   191
    private transient String userInfo;
jtulach@120
   192
jtulach@120
   193
    /**
jtulach@120
   194
     * # reference.
jtulach@120
   195
     * @serial
jtulach@120
   196
     */
jtulach@120
   197
    private String ref;
jtulach@120
   198
jaroslav@339
   199
    /**
jaroslav@339
   200
     * The host's IP address, used in equals and hashCode.
jaroslav@339
   201
     * Computed on demand. An uninitialized or unknown hostAddress is null.
jaroslav@339
   202
     */
jaroslav@339
   203
    transient Object hostAddress;
jaroslav@339
   204
jaroslav@339
   205
    /**
jaroslav@339
   206
     * The URLStreamHandler for this URL.
jaroslav@339
   207
     */
jaroslav@339
   208
    transient URLStreamHandler handler;
jaroslav@339
   209
jtulach@120
   210
    /* Our hash code.
jtulach@120
   211
     * @serial
jtulach@120
   212
     */
jtulach@120
   213
    private int hashCode = -1;
jtulach@120
   214
jtulach@120
   215
    /**
jtulach@120
   216
     * Creates a <code>URL</code> object from the specified
jtulach@120
   217
     * <code>protocol</code>, <code>host</code>, <code>port</code>
jtulach@120
   218
     * number, and <code>file</code>.<p>
jtulach@120
   219
     *
jtulach@120
   220
     * <code>host</code> can be expressed as a host name or a literal
jtulach@120
   221
     * IP address. If IPv6 literal address is used, it should be
jtulach@120
   222
     * enclosed in square brackets (<tt>'['</tt> and <tt>']'</tt>), as
jtulach@120
   223
     * specified by <a
jtulach@120
   224
     * href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>;
jtulach@120
   225
     * However, the literal IPv6 address format defined in <a
jtulach@120
   226
     * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IP
jtulach@120
   227
     * Version 6 Addressing Architecture</i></a> is also accepted.<p>
jtulach@120
   228
     *
jtulach@120
   229
     * Specifying a <code>port</code> number of <code>-1</code>
jtulach@120
   230
     * indicates that the URL should use the default port for the
jtulach@120
   231
     * protocol.<p>
jtulach@120
   232
     *
jtulach@120
   233
     * If this is the first URL object being created with the specified
jtulach@120
   234
     * protocol, a <i>stream protocol handler</i> object, an instance of
jtulach@120
   235
     * class <code>URLStreamHandler</code>, is created for that protocol:
jtulach@120
   236
     * <ol>
jtulach@120
   237
     * <li>If the application has previously set up an instance of
jtulach@120
   238
     *     <code>URLStreamHandlerFactory</code> as the stream handler factory,
jtulach@120
   239
     *     then the <code>createURLStreamHandler</code> method of that instance
jtulach@120
   240
     *     is called with the protocol string as an argument to create the
jtulach@120
   241
     *     stream protocol handler.
jtulach@120
   242
     * <li>If no <code>URLStreamHandlerFactory</code> has yet been set up,
jtulach@120
   243
     *     or if the factory's <code>createURLStreamHandler</code> method
jtulach@120
   244
     *     returns <code>null</code>, then the constructor finds the
jtulach@120
   245
     *     value of the system property:
jtulach@120
   246
     *     <blockquote><pre>
jtulach@120
   247
     *         java.protocol.handler.pkgs
jtulach@120
   248
     *     </pre></blockquote>
jtulach@120
   249
     *     If the value of that system property is not <code>null</code>,
jtulach@120
   250
     *     it is interpreted as a list of packages separated by a vertical
jtulach@120
   251
     *     slash character '<code>|</code>'. The constructor tries to load
jtulach@120
   252
     *     the class named:
jtulach@120
   253
     *     <blockquote><pre>
jtulach@120
   254
     *         &lt;<i>package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
jtulach@120
   255
     *     </pre></blockquote>
jtulach@120
   256
     *     where &lt;<i>package</i>&gt; is replaced by the name of the package
jtulach@120
   257
     *     and &lt;<i>protocol</i>&gt; is replaced by the name of the protocol.
jtulach@120
   258
     *     If this class does not exist, or if the class exists but it is not
jtulach@120
   259
     *     a subclass of <code>URLStreamHandler</code>, then the next package
jtulach@120
   260
     *     in the list is tried.
jtulach@120
   261
     * <li>If the previous step fails to find a protocol handler, then the
jtulach@120
   262
     *     constructor tries to load from a system default package.
jtulach@120
   263
     *     <blockquote><pre>
jtulach@120
   264
     *         &lt;<i>system default package</i>&gt;.&lt;<i>protocol</i>&gt;.Handler
jtulach@120
   265
     *     </pre></blockquote>
jtulach@120
   266
     *     If this class does not exist, or if the class exists but it is not a
jtulach@120
   267
     *     subclass of <code>URLStreamHandler</code>, then a
jtulach@120
   268
     *     <code>MalformedURLException</code> is thrown.
jtulach@120
   269
     * </ol>
jtulach@120
   270
     *
jtulach@120
   271
     * <p>Protocol handlers for the following protocols are guaranteed
jtulach@120
   272
     * to exist on the search path :-
jtulach@120
   273
     * <blockquote><pre>
jtulach@120
   274
     *     http, https, ftp, file, and jar
jtulach@120
   275
     * </pre></blockquote>
jtulach@120
   276
     * Protocol handlers for additional protocols may also be
jtulach@120
   277
     * available.
jtulach@120
   278
     *
jtulach@120
   279
     * <p>No validation of the inputs is performed by this constructor.
jtulach@120
   280
     *
jtulach@120
   281
     * @param      protocol   the name of the protocol to use.
jtulach@120
   282
     * @param      host       the name of the host.
jtulach@120
   283
     * @param      port       the port number on the host.
jtulach@120
   284
     * @param      file       the file on the host
jtulach@120
   285
     * @exception  MalformedURLException  if an unknown protocol is specified.
jtulach@120
   286
     * @see        java.lang.System#getProperty(java.lang.String)
jtulach@120
   287
     * @see        java.net.URL#setURLStreamHandlerFactory(
jtulach@120
   288
     *                  java.net.URLStreamHandlerFactory)
jtulach@120
   289
     * @see        java.net.URLStreamHandler
jtulach@120
   290
     * @see        java.net.URLStreamHandlerFactory#createURLStreamHandler(
jtulach@120
   291
     *                  java.lang.String)
jtulach@120
   292
     */
jtulach@120
   293
    public URL(String protocol, String host, int port, String file)
jtulach@120
   294
        throws MalformedURLException
jtulach@120
   295
    {
jtulach@120
   296
        this(protocol, host, port, file, null);
jtulach@120
   297
    }
jtulach@120
   298
jtulach@120
   299
    /**
jtulach@120
   300
     * Creates a URL from the specified <code>protocol</code>
jtulach@120
   301
     * name, <code>host</code> name, and <code>file</code> name. The
jtulach@120
   302
     * default port for the specified protocol is used.
jtulach@120
   303
     * <p>
jtulach@120
   304
     * This method is equivalent to calling the four-argument
jtulach@120
   305
     * constructor with the arguments being <code>protocol</code>,
jtulach@120
   306
     * <code>host</code>, <code>-1</code>, and <code>file</code>.
jtulach@120
   307
     *
jtulach@120
   308
     * No validation of the inputs is performed by this constructor.
jtulach@120
   309
     *
jtulach@120
   310
     * @param      protocol   the name of the protocol to use.
jtulach@120
   311
     * @param      host       the name of the host.
jtulach@120
   312
     * @param      file       the file on the host.
jtulach@120
   313
     * @exception  MalformedURLException  if an unknown protocol is specified.
jtulach@120
   314
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
jtulach@120
   315
     *                  int, java.lang.String)
jtulach@120
   316
     */
jtulach@120
   317
    public URL(String protocol, String host, String file)
jtulach@120
   318
            throws MalformedURLException {
jtulach@120
   319
        this(protocol, host, -1, file);
jtulach@120
   320
    }
jtulach@120
   321
jaroslav@339
   322
    /**
jaroslav@339
   323
     * Creates a <code>URL</code> object from the specified
jaroslav@339
   324
     * <code>protocol</code>, <code>host</code>, <code>port</code>
jaroslav@339
   325
     * number, <code>file</code>, and <code>handler</code>. Specifying
jaroslav@339
   326
     * a <code>port</code> number of <code>-1</code> indicates that
jaroslav@339
   327
     * the URL should use the default port for the protocol. Specifying
jaroslav@339
   328
     * a <code>handler</code> of <code>null</code> indicates that the URL
jaroslav@339
   329
     * should use a default stream handler for the protocol, as outlined
jaroslav@339
   330
     * for:
jaroslav@339
   331
     *     java.net.URL#URL(java.lang.String, java.lang.String, int,
jaroslav@339
   332
     *                      java.lang.String)
jaroslav@339
   333
     *
jaroslav@339
   334
     * <p>If the handler is not null and there is a security manager,
jaroslav@339
   335
     * the security manager's <code>checkPermission</code>
jaroslav@339
   336
     * method is called with a
jaroslav@339
   337
     * <code>NetPermission("specifyStreamHandler")</code> permission.
jaroslav@339
   338
     * This may result in a SecurityException.
jaroslav@339
   339
     *
jaroslav@339
   340
     * No validation of the inputs is performed by this constructor.
jaroslav@339
   341
     *
jaroslav@339
   342
     * @param      protocol   the name of the protocol to use.
jaroslav@339
   343
     * @param      host       the name of the host.
jaroslav@339
   344
     * @param      port       the port number on the host.
jaroslav@339
   345
     * @param      file       the file on the host
jaroslav@339
   346
     * @param      handler    the stream handler for the URL.
jaroslav@339
   347
     * @exception  MalformedURLException  if an unknown protocol is specified.
jaroslav@339
   348
     * @exception  SecurityException
jaroslav@339
   349
     *        if a security manager exists and its
jaroslav@339
   350
     *        <code>checkPermission</code> method doesn't allow
jaroslav@339
   351
     *        specifying a stream handler explicitly.
jaroslav@339
   352
     * @see        java.lang.System#getProperty(java.lang.String)
jaroslav@339
   353
     * @see        java.net.URL#setURLStreamHandlerFactory(
jaroslav@339
   354
     *                  java.net.URLStreamHandlerFactory)
jaroslav@339
   355
     * @see        java.net.URLStreamHandler
jaroslav@339
   356
     * @see        java.net.URLStreamHandlerFactory#createURLStreamHandler(
jaroslav@339
   357
     *                  java.lang.String)
jaroslav@339
   358
     * @see        SecurityManager#checkPermission
jaroslav@339
   359
     * @see        java.net.NetPermission
jaroslav@339
   360
     */
jaroslav@339
   361
    public URL(String protocol, String host, int port, String file,
jaroslav@339
   362
               URLStreamHandler handler) throws MalformedURLException {
jtulach@120
   363
        if (handler != null) {
jaroslav@122
   364
            throw new SecurityException();
jtulach@120
   365
        }
jtulach@120
   366
jtulach@120
   367
        protocol = protocol.toLowerCase();
jtulach@120
   368
        this.protocol = protocol;
jtulach@120
   369
        if (host != null) {
jtulach@120
   370
jtulach@120
   371
            /**
jtulach@120
   372
             * if host is a literal IPv6 address,
jtulach@120
   373
             * we will make it conform to RFC 2732
jtulach@120
   374
             */
jtulach@120
   375
            if (host.indexOf(':') >= 0 && !host.startsWith("[")) {
jtulach@120
   376
                host = "["+host+"]";
jtulach@120
   377
            }
jtulach@120
   378
            this.host = host;
jtulach@120
   379
jtulach@120
   380
            if (port < -1) {
jtulach@120
   381
                throw new MalformedURLException("Invalid port number :" +
jtulach@120
   382
                                                    port);
jtulach@120
   383
            }
jtulach@120
   384
            this.port = port;
jtulach@120
   385
            authority = (port == -1) ? host : host + ":" + port;
jtulach@120
   386
        }
jtulach@120
   387
jtulach@120
   388
        Parts parts = new Parts(file);
jtulach@120
   389
        path = parts.getPath();
jtulach@120
   390
        query = parts.getQuery();
jtulach@120
   391
jtulach@120
   392
        if (query != null) {
jtulach@120
   393
            this.file = path + "?" + query;
jtulach@120
   394
        } else {
jtulach@120
   395
            this.file = path;
jtulach@120
   396
        }
jtulach@120
   397
        ref = parts.getRef();
jtulach@120
   398
jtulach@120
   399
        // Note: we don't do validation of the URL here. Too risky to change
jtulach@120
   400
        // right now, but worth considering for future reference. -br
jaroslav@339
   401
        if (handler == null &&
jaroslav@339
   402
            (handler = getURLStreamHandler(protocol)) == null) {
jaroslav@339
   403
            throw new MalformedURLException("unknown protocol: " + protocol);
jaroslav@339
   404
        }
jaroslav@339
   405
        this.handler = handler;
jtulach@120
   406
    }
jtulach@120
   407
jtulach@120
   408
    /**
jtulach@120
   409
     * Creates a <code>URL</code> object from the <code>String</code>
jtulach@120
   410
     * representation.
jtulach@120
   411
     * <p>
jtulach@120
   412
     * This constructor is equivalent to a call to the two-argument
jtulach@120
   413
     * constructor with a <code>null</code> first argument.
jtulach@120
   414
     *
jtulach@120
   415
     * @param      spec   the <code>String</code> to parse as a URL.
jtulach@120
   416
     * @exception  MalformedURLException  if no protocol is specified, or an
jtulach@120
   417
     *               unknown protocol is found, or <tt>spec</tt> is <tt>null</tt>.
jtulach@120
   418
     * @see        java.net.URL#URL(java.net.URL, java.lang.String)
jtulach@120
   419
     */
jtulach@120
   420
    public URL(String spec) throws MalformedURLException {
jtulach@120
   421
        this(null, spec);
jtulach@120
   422
    }
jtulach@120
   423
jtulach@120
   424
    /**
jtulach@120
   425
     * Creates a URL by parsing the given spec within a specified context.
jtulach@120
   426
     *
jtulach@120
   427
     * The new URL is created from the given context URL and the spec
jtulach@120
   428
     * argument as described in
jtulach@120
   429
     * RFC2396 &quot;Uniform Resource Identifiers : Generic * Syntax&quot; :
jtulach@120
   430
     * <blockquote><pre>
jtulach@120
   431
     *          &lt;scheme&gt;://&lt;authority&gt;&lt;path&gt;?&lt;query&gt;#&lt;fragment&gt;
jtulach@120
   432
     * </pre></blockquote>
jtulach@120
   433
     * The reference is parsed into the scheme, authority, path, query and
jtulach@120
   434
     * fragment parts. If the path component is empty and the scheme,
jtulach@120
   435
     * authority, and query components are undefined, then the new URL is a
jtulach@120
   436
     * reference to the current document. Otherwise, the fragment and query
jtulach@120
   437
     * parts present in the spec are used in the new URL.
jtulach@120
   438
     * <p>
jtulach@120
   439
     * If the scheme component is defined in the given spec and does not match
jtulach@120
   440
     * the scheme of the context, then the new URL is created as an absolute
jtulach@120
   441
     * URL based on the spec alone. Otherwise the scheme component is inherited
jtulach@120
   442
     * from the context URL.
jtulach@120
   443
     * <p>
jtulach@120
   444
     * If the authority component is present in the spec then the spec is
jtulach@120
   445
     * treated as absolute and the spec authority and path will replace the
jtulach@120
   446
     * context authority and path. If the authority component is absent in the
jtulach@120
   447
     * spec then the authority of the new URL will be inherited from the
jtulach@120
   448
     * context.
jtulach@120
   449
     * <p>
jtulach@120
   450
     * If the spec's path component begins with a slash character
jtulach@120
   451
     * &quot;/&quot; then the
jtulach@120
   452
     * path is treated as absolute and the spec path replaces the context path.
jtulach@120
   453
     * <p>
jtulach@120
   454
     * Otherwise, the path is treated as a relative path and is appended to the
jtulach@120
   455
     * context path, as described in RFC2396. Also, in this case,
jtulach@120
   456
     * the path is canonicalized through the removal of directory
jtulach@120
   457
     * changes made by occurences of &quot;..&quot; and &quot;.&quot;.
jtulach@120
   458
     * <p>
jtulach@120
   459
     * For a more detailed description of URL parsing, refer to RFC2396.
jtulach@120
   460
     *
jtulach@120
   461
     * @param      context   the context in which to parse the specification.
jtulach@120
   462
     * @param      spec      the <code>String</code> to parse as a URL.
jtulach@120
   463
     * @exception  MalformedURLException  if no protocol is specified, or an
jtulach@120
   464
     *               unknown protocol is found, or <tt>spec</tt> is <tt>null</tt>.
jtulach@120
   465
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
jtulach@120
   466
     *                  int, java.lang.String)
jtulach@120
   467
     * @see        java.net.URLStreamHandler
jtulach@120
   468
     * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
jtulach@120
   469
     *                  java.lang.String, int, int)
jtulach@120
   470
     */
jtulach@120
   471
    public URL(URL context, String spec) throws MalformedURLException {
jtulach@120
   472
        this(context, spec, null);
jtulach@120
   473
    }
jtulach@120
   474
jtulach@120
   475
    /**
jtulach@120
   476
     * Creates a URL by parsing the given spec with the specified handler
jtulach@120
   477
     * within a specified context. If the handler is null, the parsing
jtulach@120
   478
     * occurs as with the two argument constructor.
jtulach@120
   479
     *
jtulach@120
   480
     * @param      context   the context in which to parse the specification.
jtulach@120
   481
     * @param      spec      the <code>String</code> to parse as a URL.
jtulach@120
   482
     * @param      handler   the stream handler for the URL.
jtulach@120
   483
     * @exception  MalformedURLException  if no protocol is specified, or an
jtulach@120
   484
     *               unknown protocol is found, or <tt>spec</tt> is <tt>null</tt>.
jtulach@120
   485
     * @exception  SecurityException
jtulach@120
   486
     *        if a security manager exists and its
jtulach@120
   487
     *        <code>checkPermission</code> method doesn't allow
jtulach@120
   488
     *        specifying a stream handler.
jtulach@120
   489
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
jtulach@120
   490
     *                  int, java.lang.String)
jtulach@120
   491
     * @see        java.net.URLStreamHandler
jtulach@120
   492
     * @see        java.net.URLStreamHandler#parseURL(java.net.URL,
jtulach@120
   493
     *                  java.lang.String, int, int)
jtulach@120
   494
     */
jaroslav@339
   495
    public URL(URL context, String spec, URLStreamHandler handler)
jtulach@120
   496
        throws MalformedURLException
jtulach@120
   497
    {
jtulach@120
   498
        String original = spec;
jtulach@120
   499
        int i, limit, c;
jtulach@120
   500
        int start = 0;
jtulach@120
   501
        String newProtocol = null;
jtulach@120
   502
        boolean aRef=false;
jtulach@120
   503
        boolean isRelative = false;
jtulach@120
   504
jtulach@120
   505
        // Check for permission to specify a handler
jtulach@120
   506
        if (handler != null) {
jaroslav@122
   507
            throw new SecurityException();
jtulach@120
   508
        }
jtulach@120
   509
jtulach@120
   510
        try {
jtulach@120
   511
            limit = spec.length();
jtulach@120
   512
            while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
jtulach@120
   513
                limit--;        //eliminate trailing whitespace
jtulach@120
   514
            }
jtulach@120
   515
            while ((start < limit) && (spec.charAt(start) <= ' ')) {
jtulach@120
   516
                start++;        // eliminate leading whitespace
jtulach@120
   517
            }
jtulach@120
   518
jtulach@120
   519
            if (spec.regionMatches(true, start, "url:", 0, 4)) {
jtulach@120
   520
                start += 4;
jtulach@120
   521
            }
jtulach@120
   522
            if (start < spec.length() && spec.charAt(start) == '#') {
jtulach@120
   523
                /* we're assuming this is a ref relative to the context URL.
jtulach@120
   524
                 * This means protocols cannot start w/ '#', but we must parse
jtulach@120
   525
                 * ref URL's like: "hello:there" w/ a ':' in them.
jtulach@120
   526
                 */
jtulach@120
   527
                aRef=true;
jtulach@120
   528
            }
jtulach@120
   529
            for (i = start ; !aRef && (i < limit) &&
jtulach@120
   530
                     ((c = spec.charAt(i)) != '/') ; i++) {
jtulach@120
   531
                if (c == ':') {
jtulach@120
   532
jtulach@120
   533
                    String s = spec.substring(start, i).toLowerCase();
jtulach@120
   534
                    if (isValidProtocol(s)) {
jtulach@120
   535
                        newProtocol = s;
jtulach@120
   536
                        start = i + 1;
jtulach@120
   537
                    }
jtulach@120
   538
                    break;
jtulach@120
   539
                }
jtulach@120
   540
            }
jtulach@120
   541
jtulach@120
   542
            // Only use our context if the protocols match.
jtulach@120
   543
            protocol = newProtocol;
jtulach@120
   544
            if ((context != null) && ((newProtocol == null) ||
jtulach@120
   545
                            newProtocol.equalsIgnoreCase(context.protocol))) {
jtulach@120
   546
                // inherit the protocol handler from the context
jtulach@120
   547
                // if not specified to the constructor
jaroslav@339
   548
                if (handler == null) {
jaroslav@339
   549
                    handler = context.handler;
jaroslav@339
   550
                }
jtulach@120
   551
jtulach@120
   552
                // If the context is a hierarchical URL scheme and the spec
jtulach@120
   553
                // contains a matching scheme then maintain backwards
jtulach@120
   554
                // compatibility and treat it as if the spec didn't contain
jtulach@120
   555
                // the scheme; see 5.2.3 of RFC2396
jtulach@120
   556
                if (context.path != null && context.path.startsWith("/"))
jtulach@120
   557
                    newProtocol = null;
jtulach@120
   558
jtulach@120
   559
                if (newProtocol == null) {
jtulach@120
   560
                    protocol = context.protocol;
jtulach@120
   561
                    authority = context.authority;
jtulach@120
   562
                    userInfo = context.userInfo;
jtulach@120
   563
                    host = context.host;
jtulach@120
   564
                    port = context.port;
jtulach@120
   565
                    file = context.file;
jtulach@120
   566
                    path = context.path;
jtulach@120
   567
                    isRelative = true;
jtulach@120
   568
                }
jtulach@120
   569
            }
jtulach@120
   570
jtulach@120
   571
            if (protocol == null) {
jtulach@120
   572
                throw new MalformedURLException("no protocol: "+original);
jtulach@120
   573
            }
jtulach@120
   574
jtulach@120
   575
            // Get the protocol handler if not specified or the protocol
jtulach@120
   576
            // of the context could not be used
jaroslav@339
   577
            if (handler == null &&
jaroslav@339
   578
                (handler = getURLStreamHandler(protocol)) == null) {
jaroslav@339
   579
                throw new MalformedURLException("unknown protocol: "+protocol);
jaroslav@339
   580
            }
jaroslav@339
   581
            this.handler = handler;
jtulach@120
   582
jtulach@120
   583
            i = spec.indexOf('#', start);
jtulach@120
   584
            if (i >= 0) {
jaroslav@339
   585
//thrw(protocol + " hnd: " + handler.getClass().getName() + " i: " + i);
jtulach@120
   586
                ref = spec.substring(i + 1, limit);
jtulach@120
   587
                limit = i;
jtulach@120
   588
            }
jtulach@120
   589
jtulach@120
   590
            /*
jtulach@120
   591
             * Handle special case inheritance of query and fragment
jtulach@120
   592
             * implied by RFC2396 section 5.2.2.
jtulach@120
   593
             */
jtulach@120
   594
            if (isRelative && start == limit) {
jtulach@120
   595
                query = context.query;
jtulach@120
   596
                if (ref == null) {
jtulach@120
   597
                    ref = context.ref;
jtulach@120
   598
                }
jtulach@120
   599
            }
jtulach@120
   600
jaroslav@339
   601
            handler.parseURL(this, spec, start, limit);
jtulach@120
   602
jtulach@120
   603
        } catch(MalformedURLException e) {
jtulach@120
   604
            throw e;
jtulach@120
   605
        } catch(Exception e) {
jtulach@120
   606
            MalformedURLException exception = new MalformedURLException(e.getMessage());
jtulach@120
   607
            exception.initCause(e);
jtulach@120
   608
            throw exception;
jtulach@120
   609
        }
jtulach@120
   610
    }
jaroslav@339
   611
    
jtulach@120
   612
    /*
jtulach@120
   613
     * Returns true if specified string is a valid protocol name.
jtulach@120
   614
     */
jtulach@120
   615
    private boolean isValidProtocol(String protocol) {
jtulach@120
   616
        int len = protocol.length();
jtulach@120
   617
        if (len < 1)
jtulach@120
   618
            return false;
jtulach@120
   619
        char c = protocol.charAt(0);
jtulach@120
   620
        if (!Character.isLetter(c))
jtulach@120
   621
            return false;
jtulach@120
   622
        for (int i = 1; i < len; i++) {
jtulach@120
   623
            c = protocol.charAt(i);
jtulach@120
   624
            if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' &&
jtulach@120
   625
                c != '-') {
jtulach@120
   626
                return false;
jtulach@120
   627
            }
jtulach@120
   628
        }
jtulach@120
   629
        return true;
jtulach@120
   630
    }
jtulach@120
   631
jtulach@120
   632
    /**
jtulach@120
   633
     * Sets the fields of the URL. This is not a public method so that
jtulach@120
   634
     * only URLStreamHandlers can modify URL fields. URLs are
jtulach@120
   635
     * otherwise constant.
jtulach@120
   636
     *
jtulach@120
   637
     * @param protocol the name of the protocol to use
jtulach@120
   638
     * @param host the name of the host
jtulach@120
   639
       @param port the port number on the host
jtulach@120
   640
     * @param file the file on the host
jtulach@120
   641
     * @param ref the internal reference in the URL
jtulach@120
   642
     */
jtulach@120
   643
    protected void set(String protocol, String host,
jtulach@120
   644
                       int port, String file, String ref) {
jtulach@120
   645
        synchronized (this) {
jtulach@120
   646
            this.protocol = protocol;
jtulach@120
   647
            this.host = host;
jtulach@120
   648
            authority = port == -1 ? host : host + ":" + port;
jtulach@120
   649
            this.port = port;
jtulach@120
   650
            this.file = file;
jtulach@120
   651
            this.ref = ref;
jtulach@120
   652
            /* This is very important. We must recompute this after the
jtulach@120
   653
             * URL has been changed. */
jtulach@120
   654
            hashCode = -1;
jaroslav@339
   655
            hostAddress = null;
jtulach@120
   656
            int q = file.lastIndexOf('?');
jtulach@120
   657
            if (q != -1) {
jtulach@120
   658
                query = file.substring(q+1);
jtulach@120
   659
                path = file.substring(0, q);
jtulach@120
   660
            } else
jtulach@120
   661
                path = file;
jtulach@120
   662
        }
jtulach@120
   663
    }
jtulach@120
   664
jtulach@120
   665
    /**
jtulach@120
   666
     * Sets the specified 8 fields of the URL. This is not a public method so
jtulach@120
   667
     * that only URLStreamHandlers can modify URL fields. URLs are otherwise
jtulach@120
   668
     * constant.
jtulach@120
   669
     *
jtulach@120
   670
     * @param protocol the name of the protocol to use
jtulach@120
   671
     * @param host the name of the host
jtulach@120
   672
     * @param port the port number on the host
jtulach@120
   673
     * @param authority the authority part for the url
jtulach@120
   674
     * @param userInfo the username and password
jtulach@120
   675
     * @param path the file on the host
jtulach@120
   676
     * @param ref the internal reference in the URL
jtulach@120
   677
     * @param query the query part of this URL
jtulach@120
   678
     * @since 1.3
jtulach@120
   679
     */
jtulach@120
   680
    protected void set(String protocol, String host, int port,
jtulach@120
   681
                       String authority, String userInfo, String path,
jtulach@120
   682
                       String query, String ref) {
jtulach@120
   683
        synchronized (this) {
jtulach@120
   684
            this.protocol = protocol;
jtulach@120
   685
            this.host = host;
jtulach@120
   686
            this.port = port;
jtulach@120
   687
            this.file = query == null ? path : path + "?" + query;
jtulach@120
   688
            this.userInfo = userInfo;
jtulach@120
   689
            this.path = path;
jtulach@120
   690
            this.ref = ref;
jtulach@120
   691
            /* This is very important. We must recompute this after the
jtulach@120
   692
             * URL has been changed. */
jtulach@120
   693
            hashCode = -1;
jaroslav@339
   694
            hostAddress = null;
jtulach@120
   695
            this.query = query;
jtulach@120
   696
            this.authority = authority;
jtulach@120
   697
        }
jtulach@120
   698
    }
jtulach@120
   699
jtulach@120
   700
    /**
jtulach@120
   701
     * Gets the query part of this <code>URL</code>.
jtulach@120
   702
     *
jtulach@120
   703
     * @return  the query part of this <code>URL</code>,
jtulach@120
   704
     * or <CODE>null</CODE> if one does not exist
jtulach@120
   705
     * @since 1.3
jtulach@120
   706
     */
jtulach@120
   707
    public String getQuery() {
jtulach@120
   708
        return query;
jtulach@120
   709
    }
jtulach@120
   710
jtulach@120
   711
    /**
jtulach@120
   712
     * Gets the path part of this <code>URL</code>.
jtulach@120
   713
     *
jtulach@120
   714
     * @return  the path part of this <code>URL</code>, or an
jtulach@120
   715
     * empty string if one does not exist
jtulach@120
   716
     * @since 1.3
jtulach@120
   717
     */
jtulach@120
   718
    public String getPath() {
jtulach@120
   719
        return path;
jtulach@120
   720
    }
jtulach@120
   721
jtulach@120
   722
    /**
jtulach@120
   723
     * Gets the userInfo part of this <code>URL</code>.
jtulach@120
   724
     *
jtulach@120
   725
     * @return  the userInfo part of this <code>URL</code>, or
jtulach@120
   726
     * <CODE>null</CODE> if one does not exist
jtulach@120
   727
     * @since 1.3
jtulach@120
   728
     */
jtulach@120
   729
    public String getUserInfo() {
jtulach@120
   730
        return userInfo;
jtulach@120
   731
    }
jtulach@120
   732
jtulach@120
   733
    /**
jtulach@120
   734
     * Gets the authority part of this <code>URL</code>.
jtulach@120
   735
     *
jtulach@120
   736
     * @return  the authority part of this <code>URL</code>
jtulach@120
   737
     * @since 1.3
jtulach@120
   738
     */
jtulach@120
   739
    public String getAuthority() {
jtulach@120
   740
        return authority;
jtulach@120
   741
    }
jtulach@120
   742
jtulach@120
   743
    /**
jtulach@120
   744
     * Gets the port number of this <code>URL</code>.
jtulach@120
   745
     *
jtulach@120
   746
     * @return  the port number, or -1 if the port is not set
jtulach@120
   747
     */
jtulach@120
   748
    public int getPort() {
jtulach@120
   749
        return port;
jtulach@120
   750
    }
jtulach@120
   751
jtulach@120
   752
    /**
jaroslav@339
   753
     * Gets the default port number of the protocol associated
jaroslav@339
   754
     * with this <code>URL</code>. If the URL scheme or the URLStreamHandler
jaroslav@339
   755
     * for the URL do not define a default port number,
jaroslav@339
   756
     * then -1 is returned.
jaroslav@339
   757
     *
jaroslav@339
   758
     * @return  the port number
jaroslav@339
   759
     * @since 1.4
jaroslav@339
   760
     */
jaroslav@339
   761
    public int getDefaultPort() {
jaroslav@339
   762
        return handler.getDefaultPort();
jaroslav@339
   763
    }
jaroslav@339
   764
jaroslav@339
   765
    /**
jtulach@120
   766
     * Gets the protocol name of this <code>URL</code>.
jtulach@120
   767
     *
jtulach@120
   768
     * @return  the protocol of this <code>URL</code>.
jtulach@120
   769
     */
jtulach@120
   770
    public String getProtocol() {
jtulach@120
   771
        return protocol;
jtulach@120
   772
    }
jtulach@120
   773
jtulach@120
   774
    /**
jtulach@120
   775
     * Gets the host name of this <code>URL</code>, if applicable.
jtulach@120
   776
     * The format of the host conforms to RFC 2732, i.e. for a
jtulach@120
   777
     * literal IPv6 address, this method will return the IPv6 address
jtulach@120
   778
     * enclosed in square brackets (<tt>'['</tt> and <tt>']'</tt>).
jtulach@120
   779
     *
jtulach@120
   780
     * @return  the host name of this <code>URL</code>.
jtulach@120
   781
     */
jtulach@120
   782
    public String getHost() {
jtulach@120
   783
        return host;
jtulach@120
   784
    }
jtulach@120
   785
jtulach@120
   786
    /**
jtulach@120
   787
     * Gets the file name of this <code>URL</code>.
jtulach@120
   788
     * The returned file portion will be
jtulach@120
   789
     * the same as <CODE>getPath()</CODE>, plus the concatenation of
jtulach@120
   790
     * the value of <CODE>getQuery()</CODE>, if any. If there is
jtulach@120
   791
     * no query portion, this method and <CODE>getPath()</CODE> will
jtulach@120
   792
     * return identical results.
jtulach@120
   793
     *
jtulach@120
   794
     * @return  the file name of this <code>URL</code>,
jtulach@120
   795
     * or an empty string if one does not exist
jtulach@120
   796
     */
jtulach@120
   797
    public String getFile() {
jtulach@120
   798
        return file;
jtulach@120
   799
    }
jtulach@120
   800
jtulach@120
   801
    /**
jtulach@120
   802
     * Gets the anchor (also known as the "reference") of this
jtulach@120
   803
     * <code>URL</code>.
jtulach@120
   804
     *
jtulach@120
   805
     * @return  the anchor (also known as the "reference") of this
jtulach@120
   806
     *          <code>URL</code>, or <CODE>null</CODE> if one does not exist
jtulach@120
   807
     */
jtulach@120
   808
    public String getRef() {
jtulach@120
   809
        return ref;
jtulach@120
   810
    }
jtulach@120
   811
jtulach@120
   812
    /**
jtulach@120
   813
     * Compares this URL for equality with another object.<p>
jtulach@120
   814
     *
jtulach@120
   815
     * If the given object is not a URL then this method immediately returns
jtulach@120
   816
     * <code>false</code>.<p>
jtulach@120
   817
     *
jtulach@120
   818
     * Two URL objects are equal if they have the same protocol, reference
jtulach@120
   819
     * equivalent hosts, have the same port number on the host, and the same
jtulach@120
   820
     * file and fragment of the file.<p>
jtulach@120
   821
     *
jtulach@120
   822
     * Two hosts are considered equivalent if both host names can be resolved
jtulach@120
   823
     * into the same IP addresses; else if either host name can't be
jtulach@120
   824
     * resolved, the host names must be equal without regard to case; or both
jtulach@120
   825
     * host names equal to null.<p>
jtulach@120
   826
     *
jtulach@120
   827
     * Since hosts comparison requires name resolution, this operation is a
jtulach@120
   828
     * blocking operation. <p>
jtulach@120
   829
     *
jtulach@120
   830
     * Note: The defined behavior for <code>equals</code> is known to
jtulach@120
   831
     * be inconsistent with virtual hosting in HTTP.
jtulach@120
   832
     *
jtulach@120
   833
     * @param   obj   the URL to compare against.
jtulach@120
   834
     * @return  <code>true</code> if the objects are the same;
jtulach@120
   835
     *          <code>false</code> otherwise.
jtulach@120
   836
     */
jtulach@120
   837
    public boolean equals(Object obj) {
jtulach@120
   838
        if (!(obj instanceof URL))
jtulach@120
   839
            return false;
jtulach@120
   840
        URL u2 = (URL)obj;
jtulach@120
   841
jaroslav@339
   842
        return handler.equals(this, u2);
jtulach@120
   843
    }
jtulach@120
   844
jtulach@120
   845
    /**
jtulach@120
   846
     * Creates an integer suitable for hash table indexing.<p>
jtulach@120
   847
     *
jtulach@120
   848
     * The hash code is based upon all the URL components relevant for URL
jtulach@120
   849
     * comparison. As such, this operation is a blocking operation.<p>
jtulach@120
   850
     *
jtulach@120
   851
     * @return  a hash code for this <code>URL</code>.
jtulach@120
   852
     */
jtulach@120
   853
    public synchronized int hashCode() {
jtulach@120
   854
        if (hashCode != -1)
jtulach@120
   855
            return hashCode;
jtulach@120
   856
jaroslav@339
   857
        hashCode = handler.hashCode(this);
jtulach@120
   858
        return hashCode;
jtulach@120
   859
    }
jtulach@120
   860
jtulach@120
   861
    /**
jtulach@120
   862
     * Compares two URLs, excluding the fragment component.<p>
jtulach@120
   863
     *
jtulach@120
   864
     * Returns <code>true</code> if this <code>URL</code> and the
jtulach@120
   865
     * <code>other</code> argument are equal without taking the
jtulach@120
   866
     * fragment component into consideration.
jtulach@120
   867
     *
jtulach@120
   868
     * @param   other   the <code>URL</code> to compare against.
jtulach@120
   869
     * @return  <code>true</code> if they reference the same remote object;
jtulach@120
   870
     *          <code>false</code> otherwise.
jtulach@120
   871
     */
jtulach@120
   872
    public boolean sameFile(URL other) {
jaroslav@339
   873
        return handler.sameFile(this, other);
jtulach@120
   874
    }
jtulach@120
   875
jtulach@120
   876
    /**
jtulach@120
   877
     * Constructs a string representation of this <code>URL</code>. The
jtulach@120
   878
     * string is created by calling the <code>toExternalForm</code>
jtulach@120
   879
     * method of the stream protocol handler for this object.
jtulach@120
   880
     *
jtulach@120
   881
     * @return  a string representation of this object.
jtulach@120
   882
     * @see     java.net.URL#URL(java.lang.String, java.lang.String, int,
jtulach@120
   883
     *                  java.lang.String)
jtulach@120
   884
     * @see     java.net.URLStreamHandler#toExternalForm(java.net.URL)
jtulach@120
   885
     */
jtulach@120
   886
    public String toString() {
jtulach@120
   887
        return toExternalForm();
jtulach@120
   888
    }
jtulach@120
   889
jtulach@120
   890
    /**
jtulach@120
   891
     * Constructs a string representation of this <code>URL</code>. The
jtulach@120
   892
     * string is created by calling the <code>toExternalForm</code>
jtulach@120
   893
     * method of the stream protocol handler for this object.
jtulach@120
   894
     *
jtulach@120
   895
     * @return  a string representation of this object.
jtulach@120
   896
     * @see     java.net.URL#URL(java.lang.String, java.lang.String,
jtulach@120
   897
     *                  int, java.lang.String)
jtulach@120
   898
     * @see     java.net.URLStreamHandler#toExternalForm(java.net.URL)
jtulach@120
   899
     */
jtulach@120
   900
    public String toExternalForm() {
jaroslav@339
   901
        return handler.toExternalForm(this);
jtulach@120
   902
    }
jtulach@120
   903
jtulach@120
   904
    /**
jtulach@120
   905
     * Returns a {@link java.net.URLConnection URLConnection} instance that
jtulach@120
   906
     * represents a connection to the remote object referred to by the
jtulach@120
   907
     * {@code URL}.
jtulach@120
   908
     *
jtulach@120
   909
     * <P>A new instance of {@linkplain java.net.URLConnection URLConnection} is
jtulach@120
   910
     * created every time when invoking the
jtulach@120
   911
     * {@linkplain java.net.URLStreamHandler#openConnection(URL)
jtulach@120
   912
     * URLStreamHandler.openConnection(URL)} method of the protocol handler for
jtulach@120
   913
     * this URL.</P>
jtulach@120
   914
     *
jtulach@120
   915
     * <P>It should be noted that a URLConnection instance does not establish
jtulach@120
   916
     * the actual network connection on creation. This will happen only when
jtulach@120
   917
     * calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.</P>
jtulach@120
   918
     *
jtulach@120
   919
     * <P>If for the URL's protocol (such as HTTP or JAR), there
jtulach@120
   920
     * exists a public, specialized URLConnection subclass belonging
jtulach@120
   921
     * to one of the following packages or one of their subpackages:
jtulach@120
   922
     * java.lang, java.io, java.util, java.net, the connection
jtulach@120
   923
     * returned will be of that subclass. For example, for HTTP an
jtulach@120
   924
     * HttpURLConnection will be returned, and for JAR a
jtulach@120
   925
     * JarURLConnection will be returned.</P>
jtulach@120
   926
     *
jtulach@120
   927
     * @return     a {@link java.net.URLConnection URLConnection} linking
jtulach@120
   928
     *             to the URL.
jtulach@120
   929
     * @exception  IOException  if an I/O exception occurs.
jtulach@120
   930
     * @see        java.net.URL#URL(java.lang.String, java.lang.String,
jtulach@120
   931
     *             int, java.lang.String)
jtulach@120
   932
     */
jaroslav@122
   933
//    public URLConnection openConnection() throws java.io.IOException {
jaroslav@122
   934
//        return handler.openConnection(this);
jaroslav@122
   935
//    }
jtulach@120
   936
jtulach@120
   937
jtulach@120
   938
    /**
jtulach@120
   939
     * Opens a connection to this <code>URL</code> and returns an
jtulach@120
   940
     * <code>InputStream</code> for reading from that connection. This
jtulach@120
   941
     * method is a shorthand for:
jtulach@120
   942
     * <blockquote><pre>
jtulach@120
   943
     *     openConnection().getInputStream()
jtulach@120
   944
     * </pre></blockquote>
jtulach@120
   945
     *
jtulach@120
   946
     * @return     an input stream for reading from the URL connection.
jtulach@120
   947
     * @exception  IOException  if an I/O exception occurs.
jtulach@120
   948
     * @see        java.net.URL#openConnection()
jtulach@120
   949
     * @see        java.net.URLConnection#getInputStream()
jtulach@120
   950
     */
jtulach@120
   951
    public final InputStream openStream() throws java.io.IOException {
jaroslav@122
   952
        throw new IOException();
jaroslav@122
   953
//        return openConnection().getInputStream();
jtulach@120
   954
    }
jtulach@120
   955
jtulach@120
   956
    /**
jtulach@120
   957
     * Gets the contents of this URL. This method is a shorthand for:
jtulach@120
   958
     * <blockquote><pre>
jtulach@120
   959
     *     openConnection().getContent()
jtulach@120
   960
     * </pre></blockquote>
jtulach@120
   961
     *
jtulach@120
   962
     * @return     the contents of this URL.
jtulach@120
   963
     * @exception  IOException  if an I/O exception occurs.
jtulach@120
   964
     * @see        java.net.URLConnection#getContent()
jtulach@120
   965
     */
jtulach@120
   966
    public final Object getContent() throws java.io.IOException {
jaroslav@122
   967
        throw new IOException();
jaroslav@122
   968
//        return openConnection().getContent();
jtulach@120
   969
    }
jtulach@120
   970
jtulach@120
   971
    /**
jtulach@120
   972
     * Gets the contents of this URL. This method is a shorthand for:
jtulach@120
   973
     * <blockquote><pre>
jtulach@120
   974
     *     openConnection().getContent(Class[])
jtulach@120
   975
     * </pre></blockquote>
jtulach@120
   976
     *
jtulach@120
   977
     * @param classes an array of Java types
jtulach@120
   978
     * @return     the content object of this URL that is the first match of
jtulach@120
   979
     *               the types specified in the classes array.
jtulach@120
   980
     *               null if none of the requested types are supported.
jtulach@120
   981
     * @exception  IOException  if an I/O exception occurs.
jtulach@120
   982
     * @see        java.net.URLConnection#getContent(Class[])
jtulach@120
   983
     * @since 1.3
jtulach@120
   984
     */
jtulach@120
   985
    public final Object getContent(Class[] classes)
jtulach@120
   986
    throws java.io.IOException {
jaroslav@122
   987
        throw new IOException();
jaroslav@122
   988
//        return openConnection().getContent(classes);
jtulach@120
   989
    }
jtulach@120
   990
jaroslav@339
   991
    static URLStreamHandler getURLStreamHandler(String protocol) {
jaroslav@339
   992
        URLStreamHandler universal = new URLStreamHandler() {};
jaroslav@339
   993
        return universal;
jaroslav@339
   994
    }
jtulach@120
   995
jtulach@120
   996
}
jtulach@120
   997
jtulach@120
   998
class Parts {
jtulach@120
   999
    String path, query, ref;
jtulach@120
  1000
jtulach@120
  1001
    Parts(String file) {
jtulach@120
  1002
        int ind = file.indexOf('#');
jtulach@120
  1003
        ref = ind < 0 ? null: file.substring(ind + 1);
jtulach@120
  1004
        file = ind < 0 ? file: file.substring(0, ind);
jtulach@120
  1005
        int q = file.lastIndexOf('?');
jtulach@120
  1006
        if (q != -1) {
jtulach@120
  1007
            query = file.substring(q+1);
jtulach@120
  1008
            path = file.substring(0, q);
jtulach@120
  1009
        } else {
jtulach@120
  1010
            path = file;
jtulach@120
  1011
        }
jtulach@120
  1012
    }
jtulach@120
  1013
jtulach@120
  1014
    String getPath() {
jtulach@120
  1015
        return path;
jtulach@120
  1016
    }
jtulach@120
  1017
jtulach@120
  1018
    String getQuery() {
jtulach@120
  1019
        return query;
jtulach@120
  1020
    }
jtulach@120
  1021
jtulach@120
  1022
    String getRef() {
jtulach@120
  1023
        return ref;
jtulach@120
  1024
    }
jtulach@120
  1025
}