| 1 | // URLStreamHandler.java - Superclass of all stream protocol handlers.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1999, 2002 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | package java.net;
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * @author Warren Levy <[email protected]>
|
|---|
| 15 | * @date March 4, 1999.
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Written using on-line Java Platform 1.2 API Specification, as well
|
|---|
| 20 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
|---|
| 21 | * Status: Believed complete and correct.
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | public abstract class URLStreamHandler
|
|---|
| 25 | {
|
|---|
| 26 | protected abstract URLConnection openConnection(URL u)
|
|---|
| 27 | throws java.io.IOException;
|
|---|
| 28 |
|
|---|
| 29 | protected void parseURL(URL u, String spec, int start, int limit)
|
|---|
| 30 | {
|
|---|
| 31 | String host = u.getHost();
|
|---|
| 32 | int port = u.getPort();
|
|---|
| 33 | String file = u.getFile();
|
|---|
| 34 |
|
|---|
| 35 | /* TBD: The JDK 1.2 doc specifically says that limit is the position
|
|---|
| 36 | * to stop parsing at and that it will be either the end of the string
|
|---|
| 37 | * or the position of '#'; thus the doc infers that this method does
|
|---|
| 38 | * not set the ref.
|
|---|
| 39 | */
|
|---|
| 40 | if (spec.regionMatches (start, "//", 0, 2))
|
|---|
| 41 | {
|
|---|
| 42 | int hostEnd;
|
|---|
| 43 | int colon;
|
|---|
| 44 |
|
|---|
| 45 | start += 2;
|
|---|
| 46 | int slash = spec.indexOf('/', start);
|
|---|
| 47 | if (slash >= 0)
|
|---|
| 48 | hostEnd = slash;
|
|---|
| 49 | else
|
|---|
| 50 | hostEnd = limit;
|
|---|
| 51 |
|
|---|
| 52 | host = spec.substring (start, hostEnd);
|
|---|
| 53 |
|
|---|
| 54 | // Look for optional port number. It is valid for the non-port
|
|---|
| 55 | // part of the host name to be null (e.g. a URL "http://:80").
|
|---|
| 56 | // TBD: JDK 1.2 in this case sets host to null rather than "";
|
|---|
| 57 | // this is undocumented and likely an unintended side effect in 1.2
|
|---|
| 58 | // so we'll be simple here and stick with "". Note that
|
|---|
| 59 | // "http://" or "http:///" produce a "" host in JDK 1.2.
|
|---|
| 60 | if ((colon = host.indexOf(':')) >= 0)
|
|---|
| 61 | {
|
|---|
| 62 | try
|
|---|
| 63 | {
|
|---|
| 64 | port = Integer.parseInt(host.substring(colon + 1));
|
|---|
| 65 | }
|
|---|
| 66 | catch (NumberFormatException e)
|
|---|
| 67 | {
|
|---|
| 68 | ; // Ignore invalid port values; port is already set to u's
|
|---|
| 69 | // port.
|
|---|
| 70 | }
|
|---|
| 71 | host = host.substring(0, colon);
|
|---|
| 72 | }
|
|---|
| 73 | file = null;
|
|---|
| 74 | start = hostEnd;
|
|---|
| 75 | }
|
|---|
| 76 | else if (host == null)
|
|---|
| 77 | host = "";
|
|---|
| 78 |
|
|---|
| 79 | if (start < limit && spec.charAt(start) == '/')
|
|---|
| 80 | {
|
|---|
| 81 | // This is an absolute path name; ignore any file context.
|
|---|
| 82 | file = spec.substring(start, limit);
|
|---|
| 83 | }
|
|---|
| 84 | else if (file == null || file.length() <= 0)
|
|---|
| 85 | {
|
|---|
| 86 | // No file context available; just spec for file.
|
|---|
| 87 | file = spec.substring(start, limit);
|
|---|
| 88 | }
|
|---|
| 89 | else if (start < limit)
|
|---|
| 90 | {
|
|---|
| 91 | // Context is available, but only override it if there is a new file.
|
|---|
| 92 | file = file.substring(0, file.lastIndexOf('/'))
|
|---|
| 93 | + '/' + spec.substring(start, limit);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | u.set(u.getProtocol(), host, port, file, u.getRef());
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | private static String canonicalizeFilename(String file)
|
|---|
| 100 | {
|
|---|
| 101 | int index;
|
|---|
| 102 |
|
|---|
| 103 | // Replace "/./" with "/". This probably isn't very efficient in
|
|---|
| 104 | // the general case, but it's probably not bad most of the time.
|
|---|
| 105 | while ((index = file.indexOf("/./")) >= 0)
|
|---|
| 106 | file = file.substring(0, index) + file.substring(index + 2);
|
|---|
| 107 |
|
|---|
| 108 | // Process "/../" correctly. This probably isn't very efficient in
|
|---|
| 109 | // the general case, but it's probably not bad most of the time.
|
|---|
| 110 | while ((index = file.indexOf("/../")) >= 0)
|
|---|
| 111 | {
|
|---|
| 112 | // Strip of the previous directory - if it exists.
|
|---|
| 113 | int previous = file.lastIndexOf('/', index - 1);
|
|---|
| 114 | if (previous >= 0)
|
|---|
| 115 | file = file.substring(0, previous) + file.substring(index + 3);
|
|---|
| 116 | else
|
|---|
| 117 | break;
|
|---|
| 118 | }
|
|---|
| 119 | return file;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | public boolean sameFile(URL url1, URL url2)
|
|---|
| 123 | {
|
|---|
| 124 | if (url1 == url2)
|
|---|
| 125 | return true;
|
|---|
| 126 | // This comparison is very conservative. It assumes that any
|
|---|
| 127 | // field can be null.
|
|---|
| 128 | if (url1 == null || url2 == null || url1.getPort() != url2.getPort())
|
|---|
| 129 | return false;
|
|---|
| 130 | String s1, s2;
|
|---|
| 131 | s1 = url1.getProtocol();
|
|---|
| 132 | s2 = url2.getProtocol();
|
|---|
| 133 | if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
|
|---|
| 134 | return false;
|
|---|
| 135 | s1 = url1.getHost();
|
|---|
| 136 | s2 = url2.getHost();
|
|---|
| 137 | if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
|
|---|
| 138 | return false;
|
|---|
| 139 | s1 = canonicalizeFilename(url1.getFile());
|
|---|
| 140 | s2 = canonicalizeFilename(url2.getFile());
|
|---|
| 141 | if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
|
|---|
| 142 | return false;
|
|---|
| 143 | return true;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | protected void setURL(URL u, String protocol, String host, int port,
|
|---|
| 147 | String file, String ref)
|
|---|
| 148 | {
|
|---|
| 149 | u.set(protocol, host, port, file, ref);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | protected String toExternalForm(URL u)
|
|---|
| 153 | {
|
|---|
| 154 | String resStr, host, file, ref;
|
|---|
| 155 | int port;
|
|---|
| 156 |
|
|---|
| 157 | resStr = u.getProtocol() + ":";
|
|---|
| 158 | host = u.getHost();
|
|---|
| 159 | port = u.getPort();
|
|---|
| 160 | file = u.getFile();
|
|---|
| 161 | ref = u.getRef();
|
|---|
| 162 |
|
|---|
| 163 | // JDK 1.2 online doc infers that host could be null because it
|
|---|
| 164 | // explicitly states that file cannot be null, but is silent on host.
|
|---|
| 165 | //
|
|---|
| 166 | // Note that this produces different results from JDK 1.2 as JDK 1.2
|
|---|
| 167 | // ignores a non-default port if host is null or "". That is inconsistent
|
|---|
| 168 | // with the spec since the result of this method is spec'ed so it can be
|
|---|
| 169 | // used to construct a new URL that is equivalent to the original.
|
|---|
| 170 | if (host == null)
|
|---|
| 171 | host = "";
|
|---|
| 172 | if (port >= 0 || ! (host.length() == 0))
|
|---|
| 173 | resStr = resStr + "//" + host + (port < 0 ? "" : ":" + port);
|
|---|
| 174 |
|
|---|
| 175 | resStr = resStr + file;
|
|---|
| 176 |
|
|---|
| 177 | if (ref != null)
|
|---|
| 178 | resStr = resStr + "#" + ref;
|
|---|
| 179 |
|
|---|
| 180 | return resStr;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|