# HG changeset patch # User Jaroslav Tulach # Date 1355734507 -3600 # Node ID 7579a0ee92fb5bef2dad5816ef78b51569818ed7 # Parent d377291de3bcb2b970408fe85dd6357a299b275e Patching URL and URLStreamHandler to compile diff -r d377291de3bc -r 7579a0ee92fb emul/src/main/java/java/net/URL.java --- a/emul/src/main/java/java/net/URL.java Mon Dec 17 09:48:42 2012 +0100 +++ b/emul/src/main/java/java/net/URL.java Mon Dec 17 09:55:07 2012 +0100 @@ -196,6 +196,17 @@ */ private String ref; + /** + * The host's IP address, used in equals and hashCode. + * Computed on demand. An uninitialized or unknown hostAddress is null. + */ + transient Object hostAddress; + + /** + * The URLStreamHandler for this URL. + */ + transient URLStreamHandler handler; + /* Our hash code. * @serial */ @@ -308,8 +319,47 @@ this(protocol, host, -1, file); } - private URL(String protocol, String host, int port, String file, - Object handler) throws MalformedURLException { + /** + * Creates a URL object from the specified + * protocol, host, port + * number, file, and handler. Specifying + * a port number of -1 indicates that + * the URL should use the default port for the protocol. Specifying + * a handler of null indicates that the URL + * should use a default stream handler for the protocol, as outlined + * for: + * java.net.URL#URL(java.lang.String, java.lang.String, int, + * java.lang.String) + * + *

If the handler is not null and there is a security manager, + * the security manager's checkPermission + * method is called with a + * NetPermission("specifyStreamHandler") permission. + * This may result in a SecurityException. + * + * No validation of the inputs is performed by this constructor. + * + * @param protocol the name of the protocol to use. + * @param host the name of the host. + * @param port the port number on the host. + * @param file the file on the host + * @param handler the stream handler for the URL. + * @exception MalformedURLException if an unknown protocol is specified. + * @exception SecurityException + * if a security manager exists and its + * checkPermission method doesn't allow + * specifying a stream handler explicitly. + * @see java.lang.System#getProperty(java.lang.String) + * @see java.net.URL#setURLStreamHandlerFactory( + * java.net.URLStreamHandlerFactory) + * @see java.net.URLStreamHandler + * @see java.net.URLStreamHandlerFactory#createURLStreamHandler( + * java.lang.String) + * @see SecurityManager#checkPermission + * @see java.net.NetPermission + */ + public URL(String protocol, String host, int port, String file, + URLStreamHandler handler) throws MalformedURLException { if (handler != null) { throw new SecurityException(); } @@ -348,10 +398,11 @@ // Note: we don't do validation of the URL here. Too risky to change // right now, but worth considering for future reference. -br -// if (handler == null && -// (handler = getURLStreamHandler(protocol)) == null) { -// throw new MalformedURLException("unknown protocol: " + protocol); -// } + if (handler == null && + (handler = getURLStreamHandler(protocol)) == null) { + throw new MalformedURLException("unknown protocol: " + protocol); + } + this.handler = handler; } /** @@ -441,7 +492,7 @@ * @see java.net.URLStreamHandler#parseURL(java.net.URL, * java.lang.String, int, int) */ - private URL(URL context, String spec, Object handler) + public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException { String original = spec; @@ -494,9 +545,9 @@ newProtocol.equalsIgnoreCase(context.protocol))) { // inherit the protocol handler from the context // if not specified to the constructor -// if (handler == null) { -// handler = context.handler; -// } + if (handler == null) { + handler = context.handler; + } // If the context is a hierarchical URL scheme and the spec // contains a matching scheme then maintain backwards @@ -523,15 +574,15 @@ // Get the protocol handler if not specified or the protocol // of the context could not be used -// if (handler == null && -// (handler = getURLStreamHandler(protocol)) == null) { -// throw new MalformedURLException("unknown protocol: "+protocol); -// } - -// this.handler = handler; + if (handler == null && + (handler = getURLStreamHandler(protocol)) == null) { + throw new MalformedURLException("unknown protocol: "+protocol); + } + this.handler = handler; i = spec.indexOf('#', start); if (i >= 0) { +//thrw(protocol + " hnd: " + handler.getClass().getName() + " i: " + i); ref = spec.substring(i + 1, limit); limit = i; } @@ -547,7 +598,7 @@ } } -// handler.parseURL(this, spec, start, limit); + handler.parseURL(this, spec, start, limit); } catch(MalformedURLException e) { throw e; @@ -557,7 +608,7 @@ throw exception; } } - + /* * Returns true if specified string is a valid protocol name. */ @@ -601,6 +652,7 @@ /* This is very important. We must recompute this after the * URL has been changed. */ hashCode = -1; + hostAddress = null; int q = file.lastIndexOf('?'); if (q != -1) { query = file.substring(q+1); @@ -639,6 +691,7 @@ /* This is very important. We must recompute this after the * URL has been changed. */ hashCode = -1; + hostAddress = null; this.query = query; this.authority = authority; } @@ -697,6 +750,19 @@ } /** + * Gets the default port number of the protocol associated + * with this URL. If the URL scheme or the URLStreamHandler + * for the URL do not define a default port number, + * then -1 is returned. + * + * @return the port number + * @since 1.4 + */ + public int getDefaultPort() { + return handler.getDefaultPort(); + } + + /** * Gets the protocol name of this URL. * * @return the protocol of this URL. @@ -773,8 +839,7 @@ return false; URL u2 = (URL)obj; - // return handler.equals(this, u2); - return u2 == this; + return handler.equals(this, u2); } /** @@ -789,7 +854,7 @@ if (hashCode != -1) return hashCode; - // hashCode = handler.hashCode(this); + hashCode = handler.hashCode(this); return hashCode; } @@ -805,8 +870,7 @@ * false otherwise. */ public boolean sameFile(URL other) { -// return handler.sameFile(this, other); - throw new UnsupportedOperationException(); + return handler.sameFile(this, other); } /** @@ -834,8 +898,7 @@ * @see java.net.URLStreamHandler#toExternalForm(java.net.URL) */ public String toExternalForm() { - throw new UnsupportedOperationException(); -// return handler.toExternalForm(this); + return handler.toExternalForm(this); } /** @@ -925,6 +988,11 @@ // return openConnection().getContent(classes); } + static URLStreamHandler getURLStreamHandler(String protocol) { + Class c = URLStreamHandler.class; // XXX only here to pre-initialize URLStreamHandler + URLStreamHandler universal = new URLStreamHandler() {}; + return universal; + } } diff -r d377291de3bc -r 7579a0ee92fb emul/src/main/java/java/net/URLStreamHandler.java --- a/emul/src/main/java/java/net/URLStreamHandler.java Mon Dec 17 09:48:42 2012 +0100 +++ b/emul/src/main/java/java/net/URLStreamHandler.java Mon Dec 17 09:55:07 2012 +0100 @@ -25,13 +25,6 @@ package java.net; -import java.io.IOException; -import java.io.InputStream; -import java.io.File; -import java.io.OutputStream; -import java.util.Hashtable; -import sun.net.util.IPAddressUtil; -import sun.net.www.ParseUtil; /** * The abstract class URLStreamHandler is the common @@ -69,7 +62,7 @@ * @exception IOException if an I/O error occurs while opening the * connection. */ - abstract protected URLConnection openConnection(URL u) throws IOException; +// abstract protected URLConnection openConnection(URL u) throws IOException; /** * Same as openConnection(URL), except that the connection will be @@ -93,9 +86,9 @@ * implements the protocol doesn't support this method. * @since 1.5 */ - protected URLConnection openConnection(URL u, Proxy p) throws IOException { - throw new UnsupportedOperationException("Method not implemented."); - } +// protected URLConnection openConnection(URL u, Proxy p) throws IOException { +// throw new UnsupportedOperationException("Method not implemented."); +// } /** * Parses the string representation of a URL into a @@ -185,11 +178,11 @@ String nhost = host ; host = nhost.substring(0,ind+1); - if (!IPAddressUtil. - isIPv6LiteralAddress(host.substring(1, ind))) { - throw new IllegalArgumentException( - "Invalid host: "+ host); - } +// if (!IPAddressUtil. +// isIPv6LiteralAddress(host.substring(1, ind))) { +// throw new IllegalArgumentException( +// "Invalid host: "+ host); +// } port = -1 ; if (nhost.length() > ind+1) { @@ -351,7 +344,7 @@ h += protocol.hashCode(); // Generate the host part. - InetAddress addr = getHostAddress(u); + Object addr = getHostAddress(u); if (addr != null) { h += addr.hashCode(); } else { @@ -425,22 +418,7 @@ * IP address. * @since 1.3 */ - protected synchronized InetAddress getHostAddress(URL u) { - if (u.hostAddress != null) - return u.hostAddress; - - String host = u.getHost(); - if (host == null || host.equals("")) { - return null; - } else { - try { - u.hostAddress = InetAddress.getByName(host); - } catch (UnknownHostException ex) { - return null; - } catch (SecurityException se) { - return null; - } - } + private synchronized Object getHostAddress(URL u) { return u.hostAddress; } @@ -453,8 +431,8 @@ * @since 1.3 */ protected boolean hostsEqual(URL u1, URL u2) { - InetAddress a1 = getHostAddress(u1); - InetAddress a2 = getHostAddress(u2); + Object a1 = getHostAddress(u1); + Object a2 = getHostAddress(u2); // if we have internet address for both, compare them if (a1 != null && a2 != null) { return a1.equals(a2); diff -r d377291de3bc -r 7579a0ee92fb vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareStringsTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareStringsTest.java Mon Dec 17 09:48:42 2012 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/tck/CompareStringsTest.java Mon Dec 17 09:55:07 2012 +0100 @@ -17,6 +17,8 @@ */ package org.apidesign.vm4brwsr.tck; +import java.net.MalformedURLException; +import java.net.URL; import org.apidesign.vm4brwsr.Compare; import org.apidesign.vm4brwsr.CompareVMs; import org.testng.annotations.Factory; @@ -26,6 +28,10 @@ * @author Jaroslav Tulach */ public class CompareStringsTest { + @Compare public static Object compareURLs() throws MalformedURLException { + return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString(); + } + @Compare public String deleteLastTwoCharacters() { StringBuilder sb = new StringBuilder(); sb.append("453.0");