home *** CD-ROM | disk | FTP | other *** search
/ Print Shop Ensemble 3 / the-print-shop-ensemble-iii.iso / worldnet / disk2 / java.z / MOZ2_01.ZIP / java / net / URL.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-03-08  |  6.3 KB  |  221 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Hashtable;
  6.  
  7. public final class URL {
  8.    private String protocol;
  9.    private String host;
  10.    private int port;
  11.    private String file;
  12.    private String ref;
  13.    URLStreamHandler handler;
  14.    static URLStreamHandlerFactory factory;
  15.    static Hashtable handlers = new Hashtable();
  16.  
  17.    public URL(String protocol, String host, int port, String file) throws MalformedURLException {
  18.       this.port = -1;
  19.       this.protocol = protocol;
  20.       this.host = host;
  21.       this.file = file;
  22.       this.port = port;
  23.       if ((this.handler = getURLStreamHandler(protocol)) == null) {
  24.          throw new MalformedURLException("unknown protocol: " + protocol);
  25.       }
  26.    }
  27.  
  28.    public URL(String protocol, String host, String file) throws MalformedURLException {
  29.       this(protocol, host, -1, file);
  30.    }
  31.  
  32.    public URL(String spec) throws MalformedURLException {
  33.       this((URL)null, spec);
  34.    }
  35.  
  36.    public URL(URL context, String spec) throws MalformedURLException {
  37.       this.port = -1;
  38.       String original = spec;
  39.       int start = 0;
  40.       String newProtocol = null;
  41.  
  42.       try {
  43.          int limit;
  44.          for(limit = spec.length(); limit > 0 && spec.charAt(limit - 1) <= ' '; --limit) {
  45.          }
  46.  
  47.          while(start < limit && spec.charAt(start) <= ' ') {
  48.             ++start;
  49.          }
  50.  
  51.          if (spec.regionMatches(true, start, "url:", 0, 4)) {
  52.             start += 4;
  53.          }
  54.  
  55.          int c;
  56.          for(int i = start; i < limit && (c = spec.charAt(i)) != 47; ++i) {
  57.             if (c == 58) {
  58.                newProtocol = spec.substring(start, i).toLowerCase();
  59.                start = i + 1;
  60.                break;
  61.             }
  62.          }
  63.  
  64.          if (context == null || newProtocol != null && !newProtocol.equals(context.protocol)) {
  65.             this.protocol = newProtocol;
  66.          } else {
  67.             this.protocol = context.protocol;
  68.             this.host = context.host;
  69.             this.port = context.port;
  70.             this.file = context.file;
  71.          }
  72.  
  73.          if (this.protocol == null) {
  74.             throw new MalformedURLException("no protocol: " + original);
  75.          } else if ((this.handler = getURLStreamHandler(this.protocol)) == null) {
  76.             throw new MalformedURLException("unknown protocol: " + this.protocol);
  77.          } else {
  78.             int var12 = spec.indexOf(35, start);
  79.             if (var12 >= 0) {
  80.                this.ref = spec.substring(var12 + 1, limit);
  81.                limit = var12;
  82.             }
  83.  
  84.             this.handler.parseURL(this, spec, start, limit);
  85.          }
  86.       } catch (MalformedURLException e) {
  87.          throw e;
  88.       } catch (Exception e) {
  89.          throw new MalformedURLException(spec + ": " + e);
  90.       }
  91.    }
  92.  
  93.    protected void set(String protocol, String host, int port, String file, String ref) {
  94.       this.protocol = protocol;
  95.       this.host = host;
  96.       this.port = port;
  97.       this.file = file;
  98.       this.ref = ref;
  99.    }
  100.  
  101.    public int getPort() {
  102.       return this.port;
  103.    }
  104.  
  105.    public String getProtocol() {
  106.       return this.protocol;
  107.    }
  108.  
  109.    public String getHost() {
  110.       return this.host;
  111.    }
  112.  
  113.    public String getHostAddress() {
  114.       try {
  115.          return InetAddress.getByName(this.host).getHostAddress();
  116.       } catch (UnknownHostException e) {
  117.          throw new SecurityException("Untested Host Translation: " + e);
  118.       }
  119.    }
  120.  
  121.    public String getFile() {
  122.       return this.file;
  123.    }
  124.  
  125.    public String getRef() {
  126.       return this.ref;
  127.    }
  128.  
  129.    public boolean equals(Object obj) {
  130.       return obj instanceof URL && this.sameFile((URL)obj);
  131.    }
  132.  
  133.    public int hashCode() {
  134.       int inhash = 0;
  135.       if (!this.host.equals("")) {
  136.          try {
  137.             inhash = InetAddress.getByName(this.host).hashCode();
  138.          } catch (UnknownHostException var2) {
  139.          }
  140.       }
  141.  
  142.       return this.protocol.hashCode() ^ inhash ^ this.file.hashCode();
  143.    }
  144.  
  145.    boolean hostsEqual(String h1, String h2) {
  146.       if (h1.equals(h2)) {
  147.          return true;
  148.       } else {
  149.          try {
  150.             InetAddress a1 = InetAddress.getByName(h1);
  151.             InetAddress a2 = InetAddress.getByName(h2);
  152.             return a1.equals(a2);
  153.          } catch (UnknownHostException var5) {
  154.          } catch (SecurityException var6) {
  155.          }
  156.  
  157.          return false;
  158.       }
  159.    }
  160.  
  161.    public boolean sameFile(URL other) {
  162.       return this.protocol.equals(other.protocol) && this.hostsEqual(this.host, other.host) && this.port == other.port && this.file.equals(other.file);
  163.    }
  164.  
  165.    public String toString() {
  166.       return this.toExternalForm();
  167.    }
  168.  
  169.    public String toExternalForm() {
  170.       return this.handler.toExternalForm(this);
  171.    }
  172.  
  173.    public URLConnection openConnection() throws IOException {
  174.       return this.handler.openConnection(this);
  175.    }
  176.  
  177.    public final InputStream openStream() throws IOException {
  178.       return this.openConnection().getInputStream();
  179.    }
  180.  
  181.    public final Object getContent() throws IOException {
  182.       return this.openConnection().getContent();
  183.    }
  184.  
  185.    public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
  186.       if (factory != null) {
  187.          throw new Error("factory already defined");
  188.       } else {
  189.          SecurityManager security = System.getSecurityManager();
  190.          if (security != null) {
  191.             security.checkSetFactory();
  192.          }
  193.  
  194.          factory = fac;
  195.       }
  196.    }
  197.  
  198.    static synchronized URLStreamHandler getURLStreamHandler(String protocol) {
  199.       URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol);
  200.       if (handler == null) {
  201.          if (factory != null) {
  202.             handler = factory.createURLStreamHandler(protocol);
  203.          }
  204.  
  205.          if (handler == null) {
  206.             try {
  207.                String clname = "sun.net.www.protocol." + protocol + ".Handler";
  208.                handler = (URLStreamHandler)Class.forName(clname).newInstance();
  209.             } catch (Exception var3) {
  210.             }
  211.          }
  212.  
  213.          if (handler != null) {
  214.             handlers.put(protocol, handler);
  215.          }
  216.       }
  217.  
  218.       return handler;
  219.    }
  220. }
  221.