home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / URLStreamHandler.java < prev    next >
Text File  |  1996-05-03  |  5KB  |  156 lines

  1. /*
  2.  * @(#)URLStreamHandler.java    1.14 96/03/06
  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.12, 18 Dec 1995
  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
  45.      * there is any inherited context then it has already been copied
  46.      * into u.  The parameters <code>start</code> and
  47.      * <code>limit</code> refer to the range of characters in spec
  48.      * that should be parsed.  The default method uses parsing rules
  49.      * that match the http spec, which most URL protocol families
  50.      * follow.  If you are writing a protocol handler that has a
  51.      * different syntax, override this routine.
  52.  
  53.      * @param    u the URL to receive the result of parsing the spec
  54.      * @param    spec the URL string to parse
  55.      * @param    start the character position to start parsing at.  This is
  56.      *         just past the ':' (if there is one).
  57.      * @param    limit the character position to stop parsing at.  This is
  58.      *         the end of the string or the position of the "#"
  59.      *         character if present (the "#" reference syntax is
  60.      *         protocol independent).
  61.      */
  62.     protected void parseURL(URL u, String spec, int start, int limit) {
  63.     String protocol = u.getProtocol();
  64.     String host = u.getHost();
  65.     int port = u.getPort();
  66.     String file = u.getFile();
  67.     String ref = u.getRef();
  68.  
  69.     int i;
  70.     if ((start <= limit - 2) &&
  71.         (spec.charAt(start) == '/') &&
  72.         (spec.charAt(start + 1) == '/')) {
  73.         start += 2;
  74.         i = spec.indexOf('/', start);
  75.         if (i < 0) {
  76.         i = limit;
  77.         }
  78.         int prn = spec.indexOf(':', start);
  79.         port = -1;
  80.         if ((prn < i) && (prn >= 0)) {
  81.         try {
  82.             port = Integer.parseInt(spec.substring(prn + 1, i));
  83.         } catch(Exception e) {
  84.             // ignore bogus port numbers
  85.         }
  86.         if (prn > start) {
  87.             host = spec.substring(start, prn);
  88.         }
  89.         } else {
  90.         host = spec.substring(start, i);
  91.         }
  92.         start = i;
  93.         file = null;
  94.     } else if (host == null) {
  95.         host = "";
  96.     }
  97.     if (start < limit) {
  98.         if (spec.charAt(start) == '/') {
  99.         file = spec.substring(start, limit);
  100.         } else {
  101.         file = (file != null ?
  102.               file.substring(0, file.lastIndexOf('/')) : "")
  103.             + "/" + spec.substring(start, limit);
  104.         }
  105.     }
  106.     if ((file == null) || (file.length() == 0)) {
  107.         file = "/";
  108.     }
  109.     while ((i = file.indexOf("/./")) >= 0) {
  110.         file = file.substring(0, i) + file.substring(i + 2);
  111.     }
  112.     while ((i = file.indexOf("/../")) >= 0) {
  113.         if ((limit = file.lastIndexOf('/', i - 1)) >= 0) {
  114.         file = file.substring(0, limit) + file.substring(i + 3);
  115.         } else {
  116.         file = file.substring(i + 3);
  117.         }
  118.     }
  119.  
  120.     u.set(protocol, host, port, file, ref);
  121.     }
  122.  
  123.     /**
  124.      * Reverses the parsing of the URL.  This should probably be overridden if
  125.      * you override parseURL().
  126.      * @param u the URL
  127.      * @return    the textual representation of the fully qualified URL (i.e.
  128.      *        after the context and canonicalization have been applied).
  129.      */
  130.     protected String toExternalForm(URL u) {
  131.     String result = u.getProtocol() + ":";
  132.     if ((u.getHost() != null) && (u.getHost().length() > 0)) {
  133.         result = result + "//" + u.getHost();
  134.         if (u.getPort() != -1) {
  135.         result += ":" + u.getPort();
  136.         }
  137.     }
  138.     result += u.getFile();
  139.     if (u.getRef() != null) {
  140.         result += "#" + u.getRef();
  141.     }
  142.     return result;
  143.     }
  144.  
  145.     /**
  146.      * Calls the (protected) set method out of the URL given.  Only
  147.      * classes derived from URLStreamHandler are supposed to be able
  148.      * to call the set() method on a URL.
  149.      * @see URL#set
  150.      */
  151.     protected void setURL(URL u, String protocol, String host, int port,
  152.               String file, String ref) {
  153.         u.set(protocol, host, port, file, ref);
  154.     }
  155. }
  156.