home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / net / urlstre0.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  4.5 KB  |  142 lines

  1. /*
  2.  * @(#)URLStreamHandler.java    1.10 95/08/21
  3.  * 
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for NON-COMMERCIAL purposes and without fee is hereby
  8.  * granted provided that this copyright notice appears in all copies. Please
  9.  * refer to the file "copyright.html" for further important copyright and
  10.  * licensing information.
  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 PURPOSE,
  15.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  16.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  17.  * ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.net;
  21.  
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.util.Hashtable;
  26.  
  27. /**
  28.  * Abstract class for URL stream openers.
  29.  * Subclasses of this class know how to create streams for particular
  30.  * protocol types.
  31.  *
  32.  * @version     1.10, 08/21/95
  33.  * @author     James Gosling
  34.  */
  35. public abstract class URLStreamHandler {
  36.     /**
  37.      * Opens an input stream to the object referenced by the URL.  This method should be
  38.      * overridden by a subclass.
  39.      * @param u the URL that this connects to
  40.      */
  41.     abstract protected URLConnection openConnection(URL u) throws IOException;
  42.  
  43.     /** 
  44.      * This method is called to parse the string spec into URL u.  If there is any inherited
  45.      * context then it has already been copied into u.  The parameters start and limit refer
  46.      * to the range of characters in spec that should be parsed.  The default
  47.      * method uses parsing rules that match the http spec, which most URL
  48.      * protocol families follow.  If you are writing a protocol handler that
  49.      * has a different syntax, then this routine should be overridden.
  50.      * @param    u the URL to receive the result of parsing the spec
  51.      * @param    spec the URL string to parse
  52.      * @param    start the character position to start parsing at.  This is
  53.      *         just past the ':' (if there is one)
  54.      * @param    limit the character position to stop parsing at.  This is
  55.      *         the end of the string or the position of the "#"
  56.      *         character if present (the "#" reference syntax is
  57.      *         protocol independant).
  58.      */
  59.     protected void parseURL(URL u, String spec, int start, int limit) {
  60.     String protocol = u.getProtocol();
  61.     String host = u.getHost();
  62.     int port = u.getPort();
  63.     String file = u.getFile();
  64.     String ref = u.getRef();
  65.  
  66.     int i;
  67.     if ((start <= limit - 2) &&
  68.         (spec.charAt(start) == '/') &&
  69.         (spec.charAt(start + 1) == '/')) {
  70.         start += 2;
  71.         i = spec.indexOf('/', start);
  72.         if (i < 0) {
  73.         i = limit;
  74.         }
  75.         int prn = spec.indexOf(':', start);
  76.         port = -1;
  77.         if ((prn < i) && (prn >= 0)) {
  78.         try {
  79.             port = Integer.parseInt(spec.substring(prn + 1, i));
  80.         } catch(Exception e) {
  81.             // ignore bogus port numbers
  82.         }
  83.         if (prn > start) {
  84.             host = spec.substring(start, prn);
  85.         }
  86.         } else {
  87.         host = spec.substring(start, i);
  88.         }
  89.         start = i;
  90.         file = null;
  91.     } else if (host == null) {
  92.         host = "";
  93.     }
  94.     if (start < limit) {
  95.         if (spec.charAt(start) == '/') {
  96.         file = spec.substring(start, limit);
  97.         } else {
  98.         file = (file != null ?
  99.               file.substring(0, file.lastIndexOf('/')) : "")
  100.             + "/" + spec.substring(start, limit);
  101.         }
  102.     }
  103.     if ((file == null) || (file.length() == 0)) {
  104.         file = "/";
  105.     }
  106.     while ((i = file.indexOf("/./")) >= 0) {
  107.         file = file.substring(0, i) + file.substring(i + 2);
  108.     }
  109.     while ((i = file.indexOf("/../")) >= 0) {
  110.         if ((limit = file.lastIndexOf('/', i - 1)) >= 0) {
  111.         file = file.substring(0, limit) + file.substring(i + 3);
  112.         } else {
  113.         file = file.substring(i + 3);
  114.         }
  115.     }
  116.  
  117.     u.set(protocol, host, port, file, ref);
  118.     }
  119.  
  120.     /**
  121.      * Reverses the parsing of the URL.  This should probably be overridden if
  122.      * you override parseURL().
  123.      * @param u the URL
  124.      * @return    the textual representation of the fully qualified URL (ie.
  125.      *        after the context and canonicalization have been applied).
  126.      */
  127.     protected String toExternalForm(URL u) {
  128.     String result = u.getProtocol() + ":";
  129.     if ((u.getHost() != null) && (u.getHost().length() > 0)) {
  130.         result = result + "//" + u.getHost();
  131.         if (u.getPort() != -1) {
  132.         result += ":" + u.getPort();
  133.         }
  134.     }
  135.     result += u.getFile();
  136.     if (u.getRef() != null) {
  137.         result += "#" + u.getRef();
  138.     }
  139.     return result;
  140.     }
  141. }
  142.