home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / URLStreamHandler.java < prev    next >
Text File  |  1997-10-27  |  7KB  |  197 lines

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