home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / net / URLStreamHandler.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.0 KB  |  199 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)URLStreamHandler.java    1.26 98/07/07
  3.  *
  4.  * Copyright 1995-1998 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.26, 07/07/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.      * <p>If for the handler's protocol (such as HTTP or JAR), there
  48.      * exists a public, specialized URLConnection subclass belonging
  49.      * to one of the following packages or one of their subpackages:
  50.      * java.lang, java.io, java.util, java.net, the connection
  51.      * returned will be of that subclass. For example, for HTTP an
  52.      * HttpURLConnection will be returned, and for JAR a
  53.      * JarURLConnection will be returned.
  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.      */
  60.     abstract protected URLConnection openConnection(URL u) throws IOException;
  61.  
  62.     /**
  63.      * Parses the string representation of a <code>URL</code> into a
  64.      * <code>URL</code> object.
  65.      * <p>
  66.      * If there is any inherited context, then it has already been
  67.      * copied into the <code>URL</code> argument.
  68.      * <p>
  69.      * The <code>parseURL</code> method of <code>URLStreamHandler</code>
  70.      * parses the string representation as if it were an
  71.      * <code>http</code> specification. Most URL protocol families have a
  72.      * similar parsing. A stream protocol handler for a protocol that has
  73.      * a different syntax must override this routine.
  74.      *
  75.      * @param   u       the <code>URL</code> to receive the result of parsing
  76.      *                  the spec.
  77.      * @param   spec    the <code>String</code> representing the URL that
  78.      *                  must be parsed.
  79.      * @param   start   the character index at which to begin parsing. This is
  80.      *                  just past the '<code>:</code>' (if there is one) that
  81.      *                  specifies the determination of the protocol name.
  82.      * @param   limit   the character position to stop parsing at. This is the
  83.      *                  end of the string or the position of the
  84.      *                  "<code>#</code>" character, if present. All information
  85.      *                  after the sharp sign indicates an anchor.
  86.      */
  87.     protected void parseURL(URL u, String spec, int start, int limit) {
  88.     String protocol = u.getProtocol();
  89.     String host = u.getHost();
  90.     int port = u.getPort();
  91.     String file = u.getFile();
  92.     String ref = u.getRef();
  93.  
  94.     int i;
  95.     if ((start <= limit - 2) && (spec.charAt(start) == '/') &&
  96.         (spec.charAt(start + 1) == '/')) {
  97.         start += 2;
  98.         i = spec.indexOf('/', start);
  99.         if (i < 0) {
  100.         i = limit;
  101.         }
  102.         int prn = spec.indexOf(':', start);
  103.         port = -1;
  104.         if ((prn < i) && (prn >= 0)) {
  105.         try {
  106.             port = Integer.parseInt(spec.substring(prn + 1, i));
  107.         } catch(Exception e) {
  108.             // ignore bogus port numbers
  109.         }
  110.         if (prn > start) {
  111.             host = spec.substring(start, prn);
  112.         }
  113.         } else {
  114.         host = spec.substring(start, i);
  115.         }
  116.         start = i;
  117.         file = null;
  118.     } else if (host == null) {
  119.         host = "";
  120.     }
  121.     if (start < limit) {
  122.         if (spec.charAt(start) == '/') {
  123.         file = spec.substring(start, limit);
  124.         } else if (file != null && file.length() > 0) {
  125.         /* relative to the context file - use either
  126.          * Unix separators || platform separators
  127.          */
  128.         int ind = Math.max(file.lastIndexOf('/'),
  129.                    file.lastIndexOf(File.separatorChar));
  130.                 // if there is no file separator there is no relative dir
  131.                 if (ind < 0)
  132.                     ind = 0;
  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.     setURL(u, protocol, host, port, file, ref);
  152.     }
  153.  
  154.     /**
  155.      * Converts a <code>URL</code> of a specific protocol to a
  156.      * <code>String</code>.
  157.      *
  158.      * @param   u   the URL.
  159.      * @return  a string representation of the <code>URL</code> argument.
  160.      */
  161.     protected String toExternalForm(URL u) {
  162.     String result = u.getProtocol() + ":";
  163.     if ((u.getHost() != null) && (u.getHost().length() > 0)) {
  164.         result = result + "//" + u.getHost();
  165.         if (u.getPort() != -1) {
  166.         result += ":" + u.getPort();
  167.         }
  168.     }
  169.     result += u.getFile();
  170.     if (u.getRef() != null) {
  171.         result += "#" + u.getRef();
  172.     }
  173.     return result;
  174.     }
  175.  
  176.     /**
  177.      * Sets the fields of the <code>URL</code> argument to the indicated values.
  178.      * Only classes derived from URLStreamHandler are supposed to be able
  179.      * to call the set method on a URL.
  180.      *
  181.      * @param   u         the URL to modify.
  182.      * @param   protocol  the protocol name.
  183.      * @param   host      the remote host value for the URL.
  184.      * @param   port      the port on the remote machine.
  185.      * @param   file      the file.
  186.      * @param   ref       the reference.
  187.      * @see     java.net.URL#set(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
  188.      */
  189.     protected void setURL(URL u, String protocol, String host, int port,
  190.               String file, String ref) {
  191.     if (this != u.handler) {
  192.         throw new SecurityException("handler for url different from " +
  193.                     "this handler");
  194.     }
  195.     // ensure that no one can reset the protocol on a given URL.
  196.         u.set(u.getProtocol(), host, port, file, ref);
  197.     }
  198. }
  199.