1.1 --- a/emul/src/main/java/java/net/URL.java Mon Dec 17 09:48:42 2012 +0100
1.2 +++ b/emul/src/main/java/java/net/URL.java Mon Dec 17 09:55:07 2012 +0100
1.3 @@ -196,6 +196,17 @@
1.4 */
1.5 private String ref;
1.6
1.7 + /**
1.8 + * The host's IP address, used in equals and hashCode.
1.9 + * Computed on demand. An uninitialized or unknown hostAddress is null.
1.10 + */
1.11 + transient Object hostAddress;
1.12 +
1.13 + /**
1.14 + * The URLStreamHandler for this URL.
1.15 + */
1.16 + transient URLStreamHandler handler;
1.17 +
1.18 /* Our hash code.
1.19 * @serial
1.20 */
1.21 @@ -308,8 +319,47 @@
1.22 this(protocol, host, -1, file);
1.23 }
1.24
1.25 - private URL(String protocol, String host, int port, String file,
1.26 - Object handler) throws MalformedURLException {
1.27 + /**
1.28 + * Creates a <code>URL</code> object from the specified
1.29 + * <code>protocol</code>, <code>host</code>, <code>port</code>
1.30 + * number, <code>file</code>, and <code>handler</code>. Specifying
1.31 + * a <code>port</code> number of <code>-1</code> indicates that
1.32 + * the URL should use the default port for the protocol. Specifying
1.33 + * a <code>handler</code> of <code>null</code> indicates that the URL
1.34 + * should use a default stream handler for the protocol, as outlined
1.35 + * for:
1.36 + * java.net.URL#URL(java.lang.String, java.lang.String, int,
1.37 + * java.lang.String)
1.38 + *
1.39 + * <p>If the handler is not null and there is a security manager,
1.40 + * the security manager's <code>checkPermission</code>
1.41 + * method is called with a
1.42 + * <code>NetPermission("specifyStreamHandler")</code> permission.
1.43 + * This may result in a SecurityException.
1.44 + *
1.45 + * No validation of the inputs is performed by this constructor.
1.46 + *
1.47 + * @param protocol the name of the protocol to use.
1.48 + * @param host the name of the host.
1.49 + * @param port the port number on the host.
1.50 + * @param file the file on the host
1.51 + * @param handler the stream handler for the URL.
1.52 + * @exception MalformedURLException if an unknown protocol is specified.
1.53 + * @exception SecurityException
1.54 + * if a security manager exists and its
1.55 + * <code>checkPermission</code> method doesn't allow
1.56 + * specifying a stream handler explicitly.
1.57 + * @see java.lang.System#getProperty(java.lang.String)
1.58 + * @see java.net.URL#setURLStreamHandlerFactory(
1.59 + * java.net.URLStreamHandlerFactory)
1.60 + * @see java.net.URLStreamHandler
1.61 + * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
1.62 + * java.lang.String)
1.63 + * @see SecurityManager#checkPermission
1.64 + * @see java.net.NetPermission
1.65 + */
1.66 + public URL(String protocol, String host, int port, String file,
1.67 + URLStreamHandler handler) throws MalformedURLException {
1.68 if (handler != null) {
1.69 throw new SecurityException();
1.70 }
1.71 @@ -348,10 +398,11 @@
1.72
1.73 // Note: we don't do validation of the URL here. Too risky to change
1.74 // right now, but worth considering for future reference. -br
1.75 -// if (handler == null &&
1.76 -// (handler = getURLStreamHandler(protocol)) == null) {
1.77 -// throw new MalformedURLException("unknown protocol: " + protocol);
1.78 -// }
1.79 + if (handler == null &&
1.80 + (handler = getURLStreamHandler(protocol)) == null) {
1.81 + throw new MalformedURLException("unknown protocol: " + protocol);
1.82 + }
1.83 + this.handler = handler;
1.84 }
1.85
1.86 /**
1.87 @@ -441,7 +492,7 @@
1.88 * @see java.net.URLStreamHandler#parseURL(java.net.URL,
1.89 * java.lang.String, int, int)
1.90 */
1.91 - private URL(URL context, String spec, Object handler)
1.92 + public URL(URL context, String spec, URLStreamHandler handler)
1.93 throws MalformedURLException
1.94 {
1.95 String original = spec;
1.96 @@ -494,9 +545,9 @@
1.97 newProtocol.equalsIgnoreCase(context.protocol))) {
1.98 // inherit the protocol handler from the context
1.99 // if not specified to the constructor
1.100 -// if (handler == null) {
1.101 -// handler = context.handler;
1.102 -// }
1.103 + if (handler == null) {
1.104 + handler = context.handler;
1.105 + }
1.106
1.107 // If the context is a hierarchical URL scheme and the spec
1.108 // contains a matching scheme then maintain backwards
1.109 @@ -523,15 +574,15 @@
1.110
1.111 // Get the protocol handler if not specified or the protocol
1.112 // of the context could not be used
1.113 -// if (handler == null &&
1.114 -// (handler = getURLStreamHandler(protocol)) == null) {
1.115 -// throw new MalformedURLException("unknown protocol: "+protocol);
1.116 -// }
1.117 -
1.118 -// this.handler = handler;
1.119 + if (handler == null &&
1.120 + (handler = getURLStreamHandler(protocol)) == null) {
1.121 + throw new MalformedURLException("unknown protocol: "+protocol);
1.122 + }
1.123 + this.handler = handler;
1.124
1.125 i = spec.indexOf('#', start);
1.126 if (i >= 0) {
1.127 +//thrw(protocol + " hnd: " + handler.getClass().getName() + " i: " + i);
1.128 ref = spec.substring(i + 1, limit);
1.129 limit = i;
1.130 }
1.131 @@ -547,7 +598,7 @@
1.132 }
1.133 }
1.134
1.135 -// handler.parseURL(this, spec, start, limit);
1.136 + handler.parseURL(this, spec, start, limit);
1.137
1.138 } catch(MalformedURLException e) {
1.139 throw e;
1.140 @@ -557,7 +608,7 @@
1.141 throw exception;
1.142 }
1.143 }
1.144 -
1.145 +
1.146 /*
1.147 * Returns true if specified string is a valid protocol name.
1.148 */
1.149 @@ -601,6 +652,7 @@
1.150 /* This is very important. We must recompute this after the
1.151 * URL has been changed. */
1.152 hashCode = -1;
1.153 + hostAddress = null;
1.154 int q = file.lastIndexOf('?');
1.155 if (q != -1) {
1.156 query = file.substring(q+1);
1.157 @@ -639,6 +691,7 @@
1.158 /* This is very important. We must recompute this after the
1.159 * URL has been changed. */
1.160 hashCode = -1;
1.161 + hostAddress = null;
1.162 this.query = query;
1.163 this.authority = authority;
1.164 }
1.165 @@ -697,6 +750,19 @@
1.166 }
1.167
1.168 /**
1.169 + * Gets the default port number of the protocol associated
1.170 + * with this <code>URL</code>. If the URL scheme or the URLStreamHandler
1.171 + * for the URL do not define a default port number,
1.172 + * then -1 is returned.
1.173 + *
1.174 + * @return the port number
1.175 + * @since 1.4
1.176 + */
1.177 + public int getDefaultPort() {
1.178 + return handler.getDefaultPort();
1.179 + }
1.180 +
1.181 + /**
1.182 * Gets the protocol name of this <code>URL</code>.
1.183 *
1.184 * @return the protocol of this <code>URL</code>.
1.185 @@ -773,8 +839,7 @@
1.186 return false;
1.187 URL u2 = (URL)obj;
1.188
1.189 - // return handler.equals(this, u2);
1.190 - return u2 == this;
1.191 + return handler.equals(this, u2);
1.192 }
1.193
1.194 /**
1.195 @@ -789,7 +854,7 @@
1.196 if (hashCode != -1)
1.197 return hashCode;
1.198
1.199 - // hashCode = handler.hashCode(this);
1.200 + hashCode = handler.hashCode(this);
1.201 return hashCode;
1.202 }
1.203
1.204 @@ -805,8 +870,7 @@
1.205 * <code>false</code> otherwise.
1.206 */
1.207 public boolean sameFile(URL other) {
1.208 -// return handler.sameFile(this, other);
1.209 - throw new UnsupportedOperationException();
1.210 + return handler.sameFile(this, other);
1.211 }
1.212
1.213 /**
1.214 @@ -834,8 +898,7 @@
1.215 * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
1.216 */
1.217 public String toExternalForm() {
1.218 - throw new UnsupportedOperationException();
1.219 -// return handler.toExternalForm(this);
1.220 + return handler.toExternalForm(this);
1.221 }
1.222
1.223 /**
1.224 @@ -925,6 +988,11 @@
1.225 // return openConnection().getContent(classes);
1.226 }
1.227
1.228 + static URLStreamHandler getURLStreamHandler(String protocol) {
1.229 + Class<URLStreamHandler> c = URLStreamHandler.class; // XXX only here to pre-initialize URLStreamHandler
1.230 + URLStreamHandler universal = new URLStreamHandler() {};
1.231 + return universal;
1.232 + }
1.233
1.234 }
1.235
2.1 --- a/emul/src/main/java/java/net/URLStreamHandler.java Mon Dec 17 09:48:42 2012 +0100
2.2 +++ b/emul/src/main/java/java/net/URLStreamHandler.java Mon Dec 17 09:55:07 2012 +0100
2.3 @@ -25,13 +25,6 @@
2.4
2.5 package java.net;
2.6
2.7 -import java.io.IOException;
2.8 -import java.io.InputStream;
2.9 -import java.io.File;
2.10 -import java.io.OutputStream;
2.11 -import java.util.Hashtable;
2.12 -import sun.net.util.IPAddressUtil;
2.13 -import sun.net.www.ParseUtil;
2.14
2.15 /**
2.16 * The abstract class <code>URLStreamHandler</code> is the common
2.17 @@ -69,7 +62,7 @@
2.18 * @exception IOException if an I/O error occurs while opening the
2.19 * connection.
2.20 */
2.21 - abstract protected URLConnection openConnection(URL u) throws IOException;
2.22 +// abstract protected URLConnection openConnection(URL u) throws IOException;
2.23
2.24 /**
2.25 * Same as openConnection(URL), except that the connection will be
2.26 @@ -93,9 +86,9 @@
2.27 * implements the protocol doesn't support this method.
2.28 * @since 1.5
2.29 */
2.30 - protected URLConnection openConnection(URL u, Proxy p) throws IOException {
2.31 - throw new UnsupportedOperationException("Method not implemented.");
2.32 - }
2.33 +// protected URLConnection openConnection(URL u, Proxy p) throws IOException {
2.34 +// throw new UnsupportedOperationException("Method not implemented.");
2.35 +// }
2.36
2.37 /**
2.38 * Parses the string representation of a <code>URL</code> into a
2.39 @@ -185,11 +178,11 @@
2.40
2.41 String nhost = host ;
2.42 host = nhost.substring(0,ind+1);
2.43 - if (!IPAddressUtil.
2.44 - isIPv6LiteralAddress(host.substring(1, ind))) {
2.45 - throw new IllegalArgumentException(
2.46 - "Invalid host: "+ host);
2.47 - }
2.48 +// if (!IPAddressUtil.
2.49 +// isIPv6LiteralAddress(host.substring(1, ind))) {
2.50 +// throw new IllegalArgumentException(
2.51 +// "Invalid host: "+ host);
2.52 +// }
2.53
2.54 port = -1 ;
2.55 if (nhost.length() > ind+1) {
2.56 @@ -351,7 +344,7 @@
2.57 h += protocol.hashCode();
2.58
2.59 // Generate the host part.
2.60 - InetAddress addr = getHostAddress(u);
2.61 + Object addr = getHostAddress(u);
2.62 if (addr != null) {
2.63 h += addr.hashCode();
2.64 } else {
2.65 @@ -425,22 +418,7 @@
2.66 * IP address.
2.67 * @since 1.3
2.68 */
2.69 - protected synchronized InetAddress getHostAddress(URL u) {
2.70 - if (u.hostAddress != null)
2.71 - return u.hostAddress;
2.72 -
2.73 - String host = u.getHost();
2.74 - if (host == null || host.equals("")) {
2.75 - return null;
2.76 - } else {
2.77 - try {
2.78 - u.hostAddress = InetAddress.getByName(host);
2.79 - } catch (UnknownHostException ex) {
2.80 - return null;
2.81 - } catch (SecurityException se) {
2.82 - return null;
2.83 - }
2.84 - }
2.85 + private synchronized Object getHostAddress(URL u) {
2.86 return u.hostAddress;
2.87 }
2.88
2.89 @@ -453,8 +431,8 @@
2.90 * @since 1.3
2.91 */
2.92 protected boolean hostsEqual(URL u1, URL u2) {
2.93 - InetAddress a1 = getHostAddress(u1);
2.94 - InetAddress a2 = getHostAddress(u2);
2.95 + Object a1 = getHostAddress(u1);
2.96 + Object a2 = getHostAddress(u2);
2.97 // if we have internet address for both, compare them
2.98 if (a1 != null && a2 != null) {
2.99 return a1.equals(a2);
3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareStringsTest.java Mon Dec 17 09:48:42 2012 +0100
3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareStringsTest.java Mon Dec 17 09:55:07 2012 +0100
3.3 @@ -17,6 +17,8 @@
3.4 */
3.5 package org.apidesign.vm4brwsr.tck;
3.6
3.7 +import java.net.MalformedURLException;
3.8 +import java.net.URL;
3.9 import org.apidesign.vm4brwsr.Compare;
3.10 import org.apidesign.vm4brwsr.CompareVMs;
3.11 import org.testng.annotations.Factory;
3.12 @@ -26,6 +28,10 @@
3.13 * @author Jaroslav Tulach <jtulach@netbeans.org>
3.14 */
3.15 public class CompareStringsTest {
3.16 + @Compare public static Object compareURLs() throws MalformedURLException {
3.17 + return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
3.18 + }
3.19 +
3.20 @Compare public String deleteLastTwoCharacters() {
3.21 StringBuilder sb = new StringBuilder();
3.22 sb.append("453.0");