Annotation of java/classes/org/w3c/util/URLUtils.java, revision 1.2
1.1 ylafon 1: // URLUtils.java
1.2 ! ylafon 2: // $Id: URLUtils.java,v 1.1 2003/04/02 18:38:53 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.lang.reflect.Method;
9: import java.lang.reflect.InvocationTargetException;
10: import java.net.MalformedURLException;
11: import java.net.URL;
12:
13: public class URLUtils {
14:
15: static Method url_defport;
16:
17: static {
18: try {
19: Class c = java.net.URL.class;
1.2 ! ylafon 20: url_defport = c.getMethod("getDefaultPort", (Object [])null);
1.1 ylafon 21: } catch (NoSuchMethodException ex) {
22: // not using a recent jdk...
23: url_defport = null;
24: }
25: }
26:
27: /**
28: * Checks that the protocol://host:port part of two URLs are equal.
29: * @param u1, the first URL to check
30: * @param u2, the second URL to check
31: * @return a boolean, true if the protocol://host:port part of the URL
32: * are equals, false otherwise
33: */
34: public static boolean equalsProtocolHostPort(URL u1, URL u2) {
35: if ((u1 == null) || (u2 == null)) {
36: return false;
37: }
38: // check that the protocol are the same (as it impacts the
39: // default port check
40: if (!u1.getProtocol().equalsIgnoreCase(u2.getProtocol())) {
41: return false;
42: }
43: // check that both hostnames are equal
44: if (!u1.getHost().equalsIgnoreCase(u2.getHost())) {
45: return false;
46: }
47: int u1p = u1.getPort();
48: int u2p = u2.getPort();
49: // if port is ok, it's good!
50: if (u1p == u2p) {
51: return true;
52: } else if ((u1p>0) && (u2p>0)) {
53: return false;
54: }
55: // otherwise, the painful comparison of -1 and such
56: if (url_defport != null) {
57: if (u1p == -1) {
58: try {
59: int u1dp;
1.2 ! ylafon 60: u1dp = ((Integer) url_defport.invoke(u1,
! 61: (Object [])null)).intValue();
1.1 ylafon 62: return (u2p == u1dp);
63: } catch (InvocationTargetException ex) {
64: } catch (IllegalAccessException iex) {
65: }
66: } else {
67: try {
68: int u2dp;
1.2 ! ylafon 69: u2dp = ((Integer) url_defport.invoke(u2,
! 70: (Object [])null)).intValue();
1.1 ylafon 71: return (u1p == u2dp);
72: } catch (InvocationTargetException ex) {
73: } catch (IllegalAccessException iex) {
74: }
75: }
76: }
77: // no JDK 1.4 this is becoming painful...
78: if (u1p == -1) {
79: String s = u1.getProtocol();
80: int u1dp = 0;
81: if (s.equalsIgnoreCase("http")) {
82: u1dp = 80;
83: } else if (s.equalsIgnoreCase("https")) {
84: u1dp = 443;
85: } // FIXME do others?
86: return (u2p == u1dp);
87: } else {
88: String s = u2.getProtocol();
89: int u2dp = 0;
90: if (s.equalsIgnoreCase("http")) {
91: u2dp = 80;
92: } else if (s.equalsIgnoreCase("https")) {
93: u2dp = 443;
94: } // FIXME do others?
95: return (u1p == u2dp);
96: }
97: }
98:
99: /**
100: * normalize an URL,
101: * @param u, the URL to normalize
102: * @return a new URL, the normalized version of the parameter, or
103: * the u URL, if something failed in the process
104: */
105: public static URL normalize(URL u) {
106: String proto = u.getProtocol().toLowerCase();
107: String host = u.getHost().toLowerCase();
108: int port = u.getPort();
109:
110: if (port != -1) {
111: if (url_defport != null) {
112: try {
113: int udp;
1.2 ! ylafon 114: udp = ((Integer) url_defport.invoke(u,
! 115: (Object [])null)).intValue();
1.1 ylafon 116: // we have the default, skip the port part
117: if (udp == port) {
118: port = -1;
119: }
120: } catch (InvocationTargetException ex) {
121: } catch (IllegalAccessException iex) {
122: }
123: } else {
124: switch(port) {
125: case 21:
126: if (proto.equals("ftp")) {
127: port = -1;
128: }
129: break;
130: case 80:
131: if (proto.equals("http")) {
132: port = -1;
133: }
134: break;
135: case 443:
136: if (proto.equals("https")) {
137: port = -1;
138: }
139: break;
140: }
141: }
142: }
143: try {
144: URL _nu;
145: if (port == -1) {
146: _nu = new URL(proto, host, u.getFile());
147: } else {
148: _nu = new URL(proto, host, port, u.getFile());
149: }
150: return _nu;
151: } catch (MalformedURLException ex) {
152: }
153: return u;
154: }
155: }
Webmaster