Annotation of java/classes/org/w3c/util/URLUtils.java, revision 1.5

1.1       ylafon      1: // URLUtils.java
1.5     ! ylafon      2: // $Id: URLUtils.java,v 1.4 2007/04/12 16:30:10 ylafon Exp $
1.1       ylafon      3: // (c) COPYRIGHT ERCIM, Keio and MIT, 2003.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
                      5: 
                      6: package org.w3c.util;
                      7: 
                      8: import java.net.MalformedURLException;
                      9: import java.net.URL;
                     10: 
                     11: public class URLUtils {
1.4       ylafon     12: 
1.1       ylafon     13:     /**
                     14:      * Checks that the protocol://host:port part of two URLs are equal.
1.5     ! ylafon     15:      *
1.1       ylafon     16:      * @param u1, the first URL to check
                     17:      * @param u2, the second URL to check
                     18:      * @return a boolean, true if the protocol://host:port part of the URL
1.5     ! ylafon     19:      *         are equals, false otherwise
1.1       ylafon     20:      */
                     21:     public static boolean equalsProtocolHostPort(URL u1, URL u2) {
1.5     ! ylafon     22:         if ((u1 == null) || (u2 == null)) {
        !            23:             return false;
        !            24:         }
        !            25:         // check that the protocol are the same (as it impacts the
        !            26:         // default port check
        !            27:         if (!u1.getProtocol().equalsIgnoreCase(u2.getProtocol())) {
        !            28:             return false;
        !            29:         }
        !            30:         // check that both hostnames are equal
        !            31:         if (!u1.getHost().equalsIgnoreCase(u2.getHost())) {
        !            32:             return false;
        !            33:         }
        !            34:         int u1p = u1.getPort();
        !            35:         int u2p = u2.getPort();
        !            36:         // if port is ok, it's good!
        !            37:         if (u1p == u2p) {
        !            38:             return true;
        !            39:         } else if ((u1p > 0) && (u2p > 0)) {
        !            40:             return false;
        !            41:         }
        !            42:         // otherwise, the painful comparison of -1 and such
        !            43:         if (u1p == -1) {
        !            44:             int u1dp = u1.getDefaultPort();
        !            45:             return (u2p == u1dp);
        !            46:         } else {
        !            47:             int u2dp = u2.getDefaultPort();
        !            48:             return (u2dp == u1p);
        !            49:         }
1.1       ylafon     50:     }
                     51: 
                     52:     /**
1.5     ! ylafon     53:      * normalize an URL,
        !            54:      *
1.1       ylafon     55:      * @param u, the URL to normalize
                     56:      * @return a new URL, the normalized version of the parameter, or
1.5     ! ylafon     57:      *         the u URL, if something failed in the process
1.1       ylafon     58:      */
                     59:     public static URL normalize(URL u) {
1.5     ! ylafon     60:         String proto = u.getProtocol().toLowerCase();
        !            61:         String host = u.getHost().toLowerCase();
        !            62:         int port = u.getPort();
        !            63: 
        !            64:         if (port != -1) {
        !            65:             int udp = u.getDefaultPort();
        !            66:             if (udp == port) {
        !            67:                 port = -1;
        !            68:             }
        !            69:         }
        !            70:         try {
        !            71:             URL _nu;
        !            72:             if (port == -1) {
        !            73:                 _nu = new URL(proto, host, u.getFile());
        !            74:             } else {
        !            75:                 _nu = new URL(proto, host, port, u.getFile());
        !            76:             }
        !            77:             return _nu;
        !            78:         } catch (MalformedURLException ex) {
        !            79:         }
        !            80:         return u;
1.1       ylafon     81:     }
                     82: }

Webmaster