home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1995 November / PCWK1195.iso / inne / win95 / sieciowe / hotja32.lzh / hotjava / classsrc / net / www / html / urlstreamhandler.java < prev    next >
Text File  |  1995-08-11  |  6KB  |  180 lines

  1. /*
  2.  * @(#)URLStreamHandler.java    1.12 95/02/07 James Gosling
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. /*- 
  21.  *    abstract class for url stream openers
  22.  */
  23.  
  24. package net.www.html;
  25.  
  26. import java.util.Hashtable;
  27. import java.io.*;
  28.  
  29. /** Subclasses of this class know how to create streams for particular
  30.     protocol types */
  31. public class URLStreamHandler {
  32.     static Hashtable extension_map = new Hashtable();
  33.  
  34.     static {
  35.     URL.classInit();
  36.     setSuffix("", URL.content_unknown);
  37.     setSuffix(".uu", URL.content_octet);
  38.     setSuffix(".saveme", URL.content_octet);
  39.     setSuffix(".dump", URL.content_octet);
  40.     setSuffix(".hqx", URL.content_octet);
  41.     setSuffix(".arc", URL.content_octet);
  42.     setSuffix(".o", URL.content_octet);
  43.     setSuffix(".a", URL.content_octet);
  44.     setSuffix(".bin", URL.content_octet);
  45.     setSuffix(".exe", URL.content_octet);
  46.     /* Temporary only. */
  47.     setSuffix(".z",            URL.content_octet);
  48.     setSuffix(".gz", URL.content_octet);
  49.  
  50.     setSuffix(".oda", URL.content_oda);
  51.     setSuffix(".pdf", URL.content_pdf);
  52.     setSuffix(".eps", URL.content_postscript);
  53.     setSuffix(".ai", URL.content_postscript);
  54.     setSuffix(".ps", URL.content_postscript);
  55.     setSuffix(".rtf", URL.content_richtext);
  56.     setSuffix(".dvi", URL.content_dvi);
  57.     setSuffix(".hdf", URL.content_hdf);
  58.     setSuffix(".latex", URL.content_latex);
  59.     setSuffix(".cdf", URL.content_netcdf);
  60.     setSuffix(".nc", URL.content_netcdf);
  61.     setSuffix(".tex", URL.content_tex);
  62.     setSuffix(".texinfo", URL.content_texinfo);
  63.     setSuffix(".texi", URL.content_texinfo);
  64.     setSuffix(".t", URL.content_troff);
  65.     setSuffix(".tr", URL.content_troff);
  66.     setSuffix(".roff", URL.content_troff);
  67.     setSuffix(".man", URL.content_man);
  68.     setSuffix(".me", URL.content_me);
  69.     setSuffix(".ms", URL.content_ms);
  70.     setSuffix(".src", URL.content_source);
  71.     setSuffix(".wsrc", URL.content_source);
  72.     setSuffix(".zip", URL.content_zip);
  73.     setSuffix(".bcpio", URL.content_bcpio);
  74.     setSuffix(".cpio", URL.content_cpio);
  75.     setSuffix(".gtar", URL.content_gtar);
  76.     setSuffix(".shar", URL.content_shar);
  77.     setSuffix(".sh", URL.content_shar);
  78.     setSuffix(".sv4cpio", URL.content_sv4cpio);
  79.     setSuffix(".sv4crc", URL.content_sv4crc);
  80.     setSuffix(".tar", URL.content_tar);
  81.     setSuffix(".ustar", URL.content_ustar);
  82.     setSuffix(".snd", URL.content_basic);
  83.     setSuffix(".au", URL.content_basic);
  84.     setSuffix(".aifc", URL.content_aiff);
  85.     setSuffix(".aif", URL.content_aiff);
  86.     setSuffix(".aiff", URL.content_aiff);
  87.     setSuffix(".wav", URL.content_wav);
  88.     setSuffix(".gif", URL.content_gif);
  89.     setSuffix(".ief", URL.content_ief);
  90.     setSuffix(".jfif", URL.content_jpeg);
  91.     setSuffix(".jfif-tbnl", URL.content_jpeg);
  92.     setSuffix(".jpe", URL.content_jpeg);
  93.     setSuffix(".jpg", URL.content_jpeg);
  94.     setSuffix(".jpeg", URL.content_jpeg);
  95.     setSuffix(".tif", URL.content_tiff);
  96.     setSuffix(".tiff", URL.content_tiff);
  97.     setSuffix(".ras", URL.content_rast);
  98.     setSuffix(".pnm", URL.content_anymap);
  99.     setSuffix(".pbm", URL.content_bitmap);
  100.     setSuffix(".pgm", URL.content_graymap);
  101.     setSuffix(".ppm", URL.content_pixmap);
  102.     setSuffix(".rgb", URL.content_rgb);
  103.     setSuffix(".xbm", URL.content_xbitmap);
  104.     setSuffix(".xpm", URL.content_xpixmap);
  105.     setSuffix(".xwd", URL.content_xwindowdump);
  106.     setSuffix(".htm", URL.content_html);
  107.     setSuffix(".html", URL.content_html);
  108.     setSuffix(".text", URL.content_plain);
  109.     setSuffix(".c", URL.content_plain);
  110.     setSuffix(".cc", URL.content_plain);
  111.     setSuffix(".c++", URL.content_plain);
  112.     setSuffix(".h", URL.content_plain);
  113.     setSuffix(".pl", URL.content_plain);
  114.     setSuffix(".txt", URL.content_plain);
  115.     setSuffix(".java", URL.content_plain);
  116.     setSuffix(".rtx", URL.content_richtext);
  117.     setSuffix(".tsv", URL.content_values);
  118.     setSuffix(".etx", URL.content_setext);
  119.     setSuffix(".mpg", URL.content_mpeg);
  120.     setSuffix(".mpe", URL.content_mpeg);
  121.     setSuffix(".mpeg", URL.content_mpeg);
  122.     setSuffix(".mov", URL.content_quicktime);
  123.     setSuffix(".qt", URL.content_quicktime);
  124.     setSuffix(".avi", URL.content_msvideo);
  125.     setSuffix(".movie", URL.content_movie);
  126.     setSuffix(".mv", URL.content_movie);
  127.     setSuffix(".mime", URL.content_rfc822);
  128.     }
  129.  
  130.  
  131.     static private void setSuffix(String ext, String ct) {
  132.     extension_map.put(ext, ct);
  133.     }
  134.  
  135.     /**
  136.      * Open an input stream to the object referenced by the URL.  Should be
  137.      * overridden by a subclass.  Failure is indicated by throwing an
  138.      * exception.
  139.      * @return    The opened input stream.  A value of null indicates that while
  140.      * the open was successful, there is no useful data provided by this
  141.      * protocol, it's done for side-effect only (the usual example is the
  142.      * "mailto" protocol).
  143.      */
  144.     abstract public InputStream openStream(URL u);
  145.  
  146.     /**
  147.      * Similar to openStream except that it allows the stream handler
  148.      * to interact with the user to resolve certain problems.  For
  149.      * example, the http handler will prompt for a user name and
  150.      * password to handle authentication failures.  In these cases,
  151.      * openStream would just toss an exception.  If this method is
  152.      * not overridden, it behaves exactly like openStream.
  153.      */
  154.     public InputStream openStreamInteractively(URL u) {
  155.         return openStream(u);
  156.     }
  157.  
  158.     /**
  159.     Try to guess the contents of a file based upon its extension.
  160.     */
  161.     public static String formatFromName(String fname) {
  162.     String ext = "";
  163.     int i = fname.lastIndexOf('#');
  164.  
  165.     // Strip off the #nameref, if any.
  166.     //
  167.     if (i != -1) 
  168.         fname = fname.substring(0, i-1);
  169.     i = fname.lastIndexOf('.');
  170.     i = Math.max(i, fname.lastIndexOf('/'));
  171.     i = Math.max(i, fname.lastIndexOf('?'));
  172.  
  173.     if (i != -1 && fname.charAt(i) == '.') {
  174.         ext = fname.substring(i).toLowerCase();
  175.     }
  176.     String ret = (String) extension_map.get(ext);
  177.     return ret != null ? ret : URL.content_unknown;
  178.     }
  179. }
  180.