home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / www / MimeTable.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  2.8 KB  |  111 lines

  1. /*
  2.  * @(#)MimeTable.java    1.4 95/08/22 James Gosling
  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 sun.net.www;
  21. import java.io.*;
  22. import java.net.URL;
  23.  
  24. abstract class MimeTable {
  25.     private MimeEntry root, last;
  26.  
  27.     void add(MimeEntry m) {
  28.     if (m != null) {
  29.         if (last == null)
  30.         root = m;
  31.         else
  32.         last.next = m;
  33.         last = m;
  34.     }
  35.     }
  36.  
  37.     MimeEntry find(String type) {
  38.     for (MimeEntry p = root; p != null; p = p.next)
  39.         if (p.matches(type))
  40.         return p;
  41.     return null;
  42.     }
  43.  
  44.     void ParseMailcap(InputStream is) {
  45.     try {
  46.         char b[] = new char[200];
  47.         int i = 0;
  48.         int c;
  49.         int slot = 0;
  50.         int eqpos = -1;
  51.         String MimeType = null;
  52.         String Command = null;
  53.         String TempName = null;
  54.         while ((c = is.read()) >= 0) {
  55.         if (c == ';' || c == '\n') {
  56.             while (i > 0 && b[i - 1] < ' ')
  57.             i--;
  58.             if (slot <= 1) {
  59.             String s;
  60.             if (slot == 0 && i > 0 && b[i - 1] == '*')
  61.                 i--;/* '*' is recognized by the trailing slash */
  62.             if (i == 0)
  63.                 s = "";
  64.             else
  65.                 s = String.copyValueOf(b, 0, i);
  66.             if (slot == 0)
  67.                 MimeType = s;
  68.             else
  69.                 Command = s;
  70.             } else if (i > 0) {
  71.             String key;
  72.             String value;
  73.             if (eqpos >= 0) {
  74.                 key = String.valueOf(b, 0, eqpos);
  75.                 value = String.copyValueOf(b, eqpos + 1, i - eqpos - 1);
  76.             } else {
  77.                 key = String.valueOf(b, 0, i);
  78.                 value = null;
  79.             }
  80.             if (key.equalsIgnoreCase("nametemplate"))
  81.                 TempName = value;
  82.             }
  83.             slot++;
  84.             i = 0;
  85.             if (c == '\n') {
  86.             if (slot >= 2)
  87.                 add(new MimeEntry(MimeType.toLowerCase(), Command, TempName));
  88.             slot = 0;
  89.             }
  90.         } else if (c == '#' && i == 0) {
  91.             while ((c = is.read()) >= 0 && c != '\n');
  92.         } else {
  93.             if (c == '\\') {
  94.             c = is.read();
  95.             if (c == '\n')    /* line  continuation */
  96.                 continue;
  97.             }
  98.             if ((c > ' ' || i > 0) && i < b.length) {
  99.             if (c == '=')
  100.                 eqpos = i;
  101.             b[i++] = (char) c;
  102.             }
  103.         }
  104.         }
  105.     } catch(Exception e) {
  106.     }
  107.     }
  108.  
  109.     abstract String TempTemplate();
  110. }
  111.