home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / net / URLStreamHandler.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  2.9 KB  |  103 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4.  
  5. public abstract class URLStreamHandler {
  6.    protected abstract URLConnection openConnection(URL var1) throws IOException;
  7.  
  8.    protected void parseURL(URL u, String spec, int start, int limit) {
  9.       String protocol = u.getProtocol();
  10.       String host = u.getHost();
  11.       int port = u.getPort();
  12.       String file = u.getFile();
  13.       String ref = u.getRef();
  14.       if (start <= limit - 2 && spec.charAt(start) == '/' && spec.charAt(start + 1) == '/') {
  15.          start += 2;
  16.          int i = spec.indexOf(47, start);
  17.          if (i < 0) {
  18.             i = limit;
  19.          }
  20.  
  21.          int prn = spec.indexOf(58, start);
  22.          port = -1;
  23.          if (prn < i && prn >= 0) {
  24.             try {
  25.                port = Integer.parseInt(spec.substring(prn + 1, i));
  26.             } catch (Exception var12) {
  27.             }
  28.  
  29.             if (prn > start) {
  30.                host = spec.substring(start, prn);
  31.             }
  32.          } else {
  33.             host = spec.substring(start, i);
  34.          }
  35.  
  36.          start = i;
  37.          file = null;
  38.       } else if (host == null) {
  39.          host = "";
  40.       }
  41.  
  42.       if (start < limit) {
  43.          if (spec.charAt(start) == '/') {
  44.             file = spec.substring(start, limit);
  45.          } else {
  46.             file = (file != null ? file.substring(0, file.lastIndexOf(47)) : "") + "/" + spec.substring(start, limit);
  47.          }
  48.       }
  49.  
  50.       if (file == null || file.length() == 0) {
  51.          file = "/";
  52.       }
  53.  
  54.       int i;
  55.       while((i = file.indexOf("/./")) >= 0) {
  56.          file = file.substring(0, i) + file.substring(i + 2);
  57.       }
  58.  
  59.       while((i = file.indexOf("/../")) >= 0) {
  60.          if ((limit = file.lastIndexOf(47, i - 1)) >= 0) {
  61.             file = file.substring(0, limit) + file.substring(i + 3);
  62.          } else {
  63.             file = file.substring(i + 3);
  64.          }
  65.       }
  66.  
  67.       if (file.endsWith("/.")) {
  68.          file = file.substring(0, file.length() - 1);
  69.       }
  70.  
  71.       if (file.endsWith("/..")) {
  72.          if ((limit = file.lastIndexOf(47, file.length() - 4)) > 0) {
  73.             file = file.substring(0, limit + 1);
  74.          } else {
  75.             file = "/";
  76.          }
  77.       }
  78.  
  79.       u.set(protocol, host, port, file, ref);
  80.    }
  81.  
  82.    protected String toExternalForm(URL u) {
  83.       String result = u.getProtocol() + ":";
  84.       if (u.getHost() != null && u.getHost().length() > 0) {
  85.          result = result + "//" + u.getHost();
  86.          if (u.getPort() != -1) {
  87.             result = result + ":" + u.getPort();
  88.          }
  89.       }
  90.  
  91.       result = result + u.getFile();
  92.       if (u.getRef() != null) {
  93.          result = result + "#" + u.getRef();
  94.       }
  95.  
  96.       return result;
  97.    }
  98.  
  99.    protected void setURL(URL u, String protocol, String host, int port, String file, String ref) {
  100.       u.set(protocol, host, port, file, ref);
  101.    }
  102. }
  103.