home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / net / URLStreamHandler.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  6.2 KB  |  185 lines

  1. /*
  2.  * @(#)URLStreamHandler.java    1.21 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.File;
  20. import java.io.OutputStream;
  21. import java.util.Hashtable;
  22.  
  23. /**
  24.  * The abstract class <code>URLStreamHandler</code> is the common 
  25.  * superclass for all stream protocol handlers. A stream protocol 
  26.  * handler knows how to make a connection for a particular protocol 
  27.  * type, such as <code>http</code>, <code>ftp</code>, or 
  28.  * <code>gopher</code>. 
  29.  * <p>
  30.  * In most cases, an instance of a <code>URLStreamHandler</code> 
  31.  * subclass is not created directly by an application. Rather, the 
  32.  * first time a protocol name is encountered when constructing a 
  33.  * <code>URL</code>, the appropriate stream protocol handler is 
  34.  * automatically loaded.
  35.  *
  36.  * @author  James Gosling
  37.  * @version 1.21, 03/18/98
  38.  * @see     java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
  39.  * @since   JDK1.0
  40.  */
  41. public abstract class URLStreamHandler {
  42.     /**
  43.      * Opens a connection to the object referenced by the 
  44.      * <code>URL</code> argument. 
  45.      * This method should be overridden by a subclass.
  46.      *
  47.      * @param      u   the URL that this connects to.
  48.      * @return     a <code>URLConnection</code> object for the <code>URL</code>.
  49.      * @exception  IOException  if an I/O error occurs while opening the
  50.      *               connection.
  51.      */
  52.     abstract protected URLConnection openConnection(URL u) throws IOException;
  53.  
  54.     /** 
  55.      * Parses the string representation of a <code>URL</code> into a 
  56.      * <code>URL</code> object. 
  57.      * <p>
  58.      * If there is any inherited context, then it has already been 
  59.      * copied into the <code>URL</code> argument. 
  60.      * <p>
  61.      * The <code>parseURL</code> method of <code>URLStreamHandler</code> 
  62.      * parses the string representation as if it were an 
  63.      * <code>http</code> specification. Most URL protocol families have a 
  64.      * similar parsing. A stream protocol handler for a protocol that has 
  65.      * a different syntax must override this routine. 
  66.      *
  67.      * @param   u       the <code>URL</code> to receive the result of parsing
  68.      *                  the spec.
  69.      * @param   spec    the <code>String</code> representing the URL that
  70.      *                  must be parsed.
  71.      * @param   start   the character index at which to begin parsing. This is
  72.      *                  just past the '<code>:</code>' (if there is one) that
  73.      *                  specifies the determination of the protocol name.
  74.      * @param   limit   the character position to stop parsing at. This is the
  75.      *                  end of the string or the position of the
  76.      *                  "<code>#</code>" character, if present. All information
  77.      *                  after the sharp sign indicates an anchor. 
  78.      */
  79.     protected void parseURL(URL u, String spec, int start, int limit) {
  80.     String protocol = u.getProtocol();
  81.     String host = u.getHost();
  82.     int port = u.getPort();
  83.     String file = u.getFile();
  84.     String ref = u.getRef();
  85.  
  86.     int i;
  87.     if ((start <= limit - 2) && (spec.charAt(start) == '/') &&
  88.         (spec.charAt(start + 1) == '/')) {
  89.         start += 2;
  90.         i = spec.indexOf('/', start);
  91.         if (i < 0) {
  92.         i = limit;
  93.         }
  94.         int prn = spec.indexOf(':', start);
  95.         port = -1;
  96.         if ((prn < i) && (prn >= 0)) {
  97.         try {
  98.             port = Integer.parseInt(spec.substring(prn + 1, i));
  99.         } catch(Exception e) {
  100.             // ignore bogus port numbers
  101.         }
  102.         if (prn > start) {
  103.             host = spec.substring(start, prn);
  104.         }
  105.         } else {
  106.         host = spec.substring(start, i);
  107.         }
  108.         start = i;
  109.         file = null;
  110.     } else if (host == null) {
  111.         host = "";
  112.     }
  113.     if (start < limit) {
  114.         if (spec.charAt(start) == '/') {
  115.         file = spec.substring(start, limit);
  116.         } else if (file != null && file.length() > 0) {
  117.         /* relative to the context file - use either 
  118.          * Unix separators || platform separators
  119.          */
  120.         int ind = Math.max(file.lastIndexOf('/'), 
  121.                    file.lastIndexOf(File.separatorChar));
  122.  
  123.         file = file.substring(0, ind) + "/" + spec.substring(start, limit);
  124.         } else {
  125.         file = "/" + spec.substring(start, limit);
  126.         }
  127.     }
  128.     if ((file == null) || (file.length() == 0)) {
  129.         file = "/"; 
  130.     }
  131.     while ((i = file.indexOf("/./")) >= 0) {
  132.         file = file.substring(0, i) + file.substring(i + 2);
  133.     }
  134.     while ((i = file.indexOf("/../")) >= 0) {
  135.         if ((limit = file.lastIndexOf('/', i - 1)) >= 0) {
  136.         file = file.substring(0, limit) + file.substring(i + 3);
  137.         } else {
  138.         file = file.substring(i + 3);
  139.         }
  140.     }
  141.  
  142.     u.set(protocol, host, port, file, ref);
  143.     }
  144.  
  145.     /**
  146.      * Converts a <code>URL</code> of a specific protocol to a 
  147.      * <code>String</code>. 
  148.      *
  149.      * @param   u   the URL.
  150.      * @return  a string representation of the <code>URL</code> argument.
  151.      */
  152.     protected String toExternalForm(URL u) {
  153.     String result = u.getProtocol() + ":";
  154.     if ((u.getHost() != null) && (u.getHost().length() > 0)) {
  155.         result = result + "//" + u.getHost();
  156.         if (u.getPort() != -1) {
  157.         result += ":" + u.getPort();
  158.         }
  159.     }
  160.     result += u.getFile();
  161.     if (u.getRef() != null) {
  162.         result += "#" + u.getRef();
  163.     }
  164.     return result;
  165.     }
  166.  
  167.     /**
  168.      * Sets the fields of the <code>URL</code> argument to the indicated values.
  169.      * Only classes derived from URLStreamHandler are supposed to be able
  170.      * to call the set method on a URL.
  171.      *
  172.      * @param   u         the URL to modify.
  173.      * @param   protocol  the protocol name.
  174.      * @param   host      the remote host value for the URL.
  175.      * @param   port      the port on the remote machine.
  176.      * @param   file      the file.
  177.      * @param   ref       the reference.
  178.      * @see     java.net.URL#set(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
  179.      */
  180.     protected void setURL(URL u, String protocol, String host, int port,
  181.               String file, String ref) {
  182.         u.set(protocol, host, port, file, ref);
  183.     }
  184. }
  185.