Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / util / URLUtils.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/util/URLUtils.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/util/URLUtils.java
new file mode 100644 (file)
index 0000000..43ac820
--- /dev/null
@@ -0,0 +1,155 @@
+// URLUtils.java\r
+// $Id: URLUtils.java,v 1.1 2010/06/15 12:25:37 smhuang Exp $\r
+// (c) COPYRIGHT ERCIM, Keio and MIT, 2003.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.util;\r
+\r
+import java.lang.reflect.Method;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+\r
+public class URLUtils {\r
+    \r
+    static Method url_defport;\r
+    \r
+    static {\r
+       try {\r
+           Class c = java.net.URL.class;\r
+           url_defport = c.getMethod("getDefaultPort", (Class [])null);\r
+       } catch (NoSuchMethodException ex) {\r
+           // not using a recent jdk...\r
+           url_defport = null;\r
+       }\r
+    }\r
+    \r
+    /**\r
+     * Checks that the protocol://host:port part of two URLs are equal.\r
+     * @param u1, the first URL to check\r
+     * @param u2, the second URL to check\r
+     * @return a boolean, true if the protocol://host:port part of the URL\r
+     * are equals, false otherwise\r
+     */\r
+    public static boolean equalsProtocolHostPort(URL u1, URL u2) {\r
+       if ((u1 == null) || (u2 == null)) {\r
+           return false;\r
+       }\r
+       // check that the protocol are the same (as it impacts the \r
+       // default port check\r
+       if (!u1.getProtocol().equalsIgnoreCase(u2.getProtocol())) {\r
+           return false;\r
+       }\r
+       // check that both hostnames are equal\r
+       if (!u1.getHost().equalsIgnoreCase(u2.getHost())) {\r
+           return false;\r
+       }\r
+       int u1p = u1.getPort();\r
+       int u2p = u2.getPort();\r
+       // if port is ok, it's good!\r
+       if (u1p == u2p) {\r
+           return true;\r
+       } else if ((u1p>0) && (u2p>0)) {\r
+           return false;\r
+       }\r
+       // otherwise, the painful comparison of -1 and such\r
+       if (url_defport != null) {\r
+           if (u1p == -1) {\r
+               try {\r
+                   int u1dp;\r
+                   u1dp = ((Integer) url_defport.invoke(u1, \r
+                                                (Object [])null)).intValue();\r
+                   return (u2p == u1dp);\r
+               } catch (InvocationTargetException ex) {\r
+               } catch (IllegalAccessException iex) {\r
+               }\r
+           } else {\r
+               try {\r
+                   int u2dp;\r
+                   u2dp = ((Integer) url_defport.invoke(u2, \r
+                                                (Object [])null)).intValue();\r
+                   return (u1p == u2dp);\r
+               } catch (InvocationTargetException ex) {\r
+               } catch (IllegalAccessException iex) {\r
+               }\r
+           }\r
+       } \r
+       // no JDK 1.4 this is becoming painful...\r
+       if (u1p == -1) {\r
+           String s = u1.getProtocol();\r
+           int u1dp = 0;\r
+           if (s.equalsIgnoreCase("http")) {\r
+               u1dp = 80;\r
+           } else if (s.equalsIgnoreCase("https")) {\r
+               u1dp = 443;\r
+           } // FIXME do others?\r
+           return (u2p == u1dp);\r
+       } else {\r
+           String s = u2.getProtocol();\r
+           int u2dp = 0;\r
+           if (s.equalsIgnoreCase("http")) {\r
+               u2dp = 80;\r
+           } else if (s.equalsIgnoreCase("https")) {\r
+               u2dp = 443;\r
+           } // FIXME do others?\r
+           return (u1p == u2dp);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * normalize an URL, \r
+     * @param u, the URL to normalize\r
+     * @return a new URL, the normalized version of the parameter, or\r
+     * the u URL, if something failed in the process\r
+     */\r
+    public static URL normalize(URL u) {\r
+       String proto = u.getProtocol().toLowerCase();\r
+       String host = u.getHost().toLowerCase();\r
+       int port = u.getPort();\r
+       \r
+       if (port != -1) {\r
+           if (url_defport != null) {\r
+               try {\r
+                   int udp;\r
+                   udp = ((Integer) url_defport.invoke(u, \r
+                                               (Object [])null)).intValue();\r
+                   // we have the default, skip the port part\r
+                   if (udp == port) {\r
+                       port = -1;\r
+                   }\r
+               } catch (InvocationTargetException ex) {\r
+               } catch (IllegalAccessException iex) {\r
+               }       \r
+           } else {\r
+               switch(port) {\r
+               case 21:\r
+                   if (proto.equals("ftp")) {\r
+                       port = -1;\r
+                   }\r
+                   break;\r
+               case 80:\r
+                   if (proto.equals("http")) {\r
+                       port = -1;\r
+                   }\r
+                   break;\r
+               case 443:\r
+                   if (proto.equals("https")) {\r
+                       port = -1;\r
+                   }\r
+                   break;\r
+               }\r
+           }\r
+       }\r
+       try {\r
+           URL _nu;\r
+           if (port == -1) {\r
+               _nu = new URL(proto, host, u.getFile());\r
+           } else {\r
+               _nu = new URL(proto, host, port, u.getFile());\r
+           }\r
+           return _nu;\r
+       } catch (MalformedURLException ex) {\r
+       }\r
+       return u;\r
+    }\r
+}\r