home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / util / Properties.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  4.6 KB  |  256 lines

  1. package java.util;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.PrintStream;
  7.  
  8. public class Properties extends Hashtable {
  9.    protected Properties defaults;
  10.  
  11.    public Properties() {
  12.       this((Properties)null);
  13.    }
  14.  
  15.    public Properties(Properties defaults) {
  16.       this.defaults = defaults;
  17.    }
  18.  
  19.    public synchronized void load(InputStream in) throws IOException {
  20.       in = Runtime.getRuntime().getLocalizedInputStream(in);
  21.       int ch = in.read();
  22.  
  23.       label136:
  24.       while(true) {
  25.          StringBuffer key;
  26.          switch (ch) {
  27.             case 35:
  28.             case 33:
  29.                while(true) {
  30.                   ch = in.read();
  31.                   if (ch < 0 || ch == 10 || ch == 13) {
  32.                      continue label136;
  33.                   }
  34.                }
  35.             case -1:
  36.                return;
  37.             case 32:
  38.             case 13:
  39.             case 10:
  40.             case 9:
  41.                ch = in.read();
  42.                continue;
  43.             default:
  44.                key = new StringBuffer();
  45.          }
  46.  
  47.          while(ch >= 0 && ch != 61 && ch != 58 && ch != 32 && ch != 9 && ch != 10 && ch != 13) {
  48.             key.append((char)ch);
  49.             ch = in.read();
  50.          }
  51.  
  52.          while(ch == 32 && ch == 9) {
  53.             ch = in.read();
  54.          }
  55.  
  56.          if (ch == 61 || ch == 58) {
  57.             ch = in.read();
  58.          }
  59.  
  60.          while(ch == 32 && ch == 9) {
  61.             ch = in.read();
  62.          }
  63.  
  64.          StringBuffer val = new StringBuffer();
  65.  
  66.          label96:
  67.          while(ch >= 0 && ch != 10 && ch != 13) {
  68.             if (ch == 92) {
  69.                switch (ch = in.read()) {
  70.                   case 117:
  71.                      while((ch = in.read()) == 117) {
  72.                      }
  73.  
  74.                      int d = 0;
  75.  
  76.                      label83:
  77.                      for(int i = 0; i < 4; ch = in.read()) {
  78.                         switch (ch) {
  79.                            case 100:
  80.                            case 99:
  81.                            case 98:
  82.                            case 97:
  83.                            case 102:
  84.                            case 101:
  85.                               d = (d << 4) + 10 + ch - 97;
  86.                               break;
  87.                            case 70:
  88.                            case 69:
  89.                            case 68:
  90.                            case 67:
  91.                            case 66:
  92.                            case 65:
  93.                               d = (d << 4) + 10 + ch - 65;
  94.                               break;
  95.                            case 57:
  96.                            case 56:
  97.                            case 55:
  98.                            case 54:
  99.                            case 53:
  100.                            case 52:
  101.                            case 51:
  102.                            case 50:
  103.                            case 49:
  104.                            case 48:
  105.                               d = (d << 4) + ch - 48;
  106.                               break;
  107.                            default:
  108.                               break label83;
  109.                         }
  110.  
  111.                         ++i;
  112.                      }
  113.  
  114.                      ch = d;
  115.                      break;
  116.                   case 116:
  117.                      ch = 9;
  118.                      break;
  119.                   case 114:
  120.                      ch = 13;
  121.                      break;
  122.                   case 10:
  123.                      while(true) {
  124.                         while((ch = in.read()) == 32) {
  125.                         }
  126.  
  127.                         if (ch != 9) {
  128.                            continue label96;
  129.                         }
  130.                      }
  131.                   case 110:
  132.                      ch = 10;
  133.                }
  134.             }
  135.  
  136.             val.append((char)ch);
  137.             ch = in.read();
  138.          }
  139.  
  140.          ((Hashtable)this).put(key.toString(), val.toString());
  141.       }
  142.    }
  143.  
  144.    public synchronized void save(OutputStream out, String header) {
  145.       OutputStream localOut = Runtime.getRuntime().getLocalizedOutputStream(out);
  146.       PrintStream prnt = new PrintStream(localOut, false);
  147.       boolean localize = localOut != out;
  148.       if (header != null) {
  149.          prnt.write(35);
  150.          prnt.println(header);
  151.       }
  152.  
  153.       prnt.write(35);
  154.       prnt.println(new Date());
  155.       Enumeration e = ((Hashtable)this).keys();
  156.  
  157.       while(e.hasMoreElements()) {
  158.          String key = (String)e.nextElement();
  159.          prnt.print(key);
  160.          prnt.write(61);
  161.          String val = (String)((Hashtable)this).get(key);
  162.          int len = val.length();
  163.          boolean empty = false;
  164.  
  165.          for(int i = 0; i < len; ++i) {
  166.             int ch = val.charAt(i);
  167.             switch (ch) {
  168.                case 92:
  169.                   prnt.write(92);
  170.                   prnt.write(92);
  171.                   break;
  172.                case 13:
  173.                   prnt.write(92);
  174.                   prnt.write(114);
  175.                   break;
  176.                case 10:
  177.                   prnt.write(92);
  178.                   prnt.write(110);
  179.                   break;
  180.                case 9:
  181.                   prnt.write(92);
  182.                   prnt.write(116);
  183.                   break;
  184.                default:
  185.                   if (ch < 32 || ch >= 127 || empty && ch == 32) {
  186.                      if (ch > 255 && localize) {
  187.                         prnt.write(ch);
  188.                      } else {
  189.                         prnt.write(92);
  190.                         prnt.write(117);
  191.                         prnt.write(ch >> 12 & 15);
  192.                         prnt.write(ch >> 8 & 15);
  193.                         prnt.write(ch >> 4 & 15);
  194.                         prnt.write(ch & 15);
  195.                      }
  196.                   } else {
  197.                      prnt.write(ch);
  198.                   }
  199.             }
  200.  
  201.             empty = false;
  202.          }
  203.  
  204.          prnt.write(10);
  205.       }
  206.  
  207.    }
  208.  
  209.    public String getProperty(String key) {
  210.       String val = (String)super.get(key);
  211.       return val == null && this.defaults != null ? this.defaults.getProperty(key) : val;
  212.    }
  213.  
  214.    public String getProperty(String key, String defaultValue) {
  215.       String val = this.getProperty(key);
  216.       return val == null ? defaultValue : val;
  217.    }
  218.  
  219.    public Enumeration propertyNames() {
  220.       Hashtable h = new Hashtable();
  221.       this.enumerate(h);
  222.       return h.keys();
  223.    }
  224.  
  225.    public void list(PrintStream out) {
  226.       out.println("-- listing properties --");
  227.       Hashtable h = new Hashtable();
  228.       this.enumerate(h);
  229.  
  230.       String key;
  231.       String val;
  232.       for(Enumeration e = h.keys(); e.hasMoreElements(); out.println(key + "=" + val)) {
  233.          key = (String)e.nextElement();
  234.          val = (String)h.get(key);
  235.          if (val.length() > 40) {
  236.             val = val.substring(0, 37) + "...";
  237.          }
  238.       }
  239.  
  240.    }
  241.  
  242.    private synchronized void enumerate(Hashtable h) {
  243.       if (this.defaults != null) {
  244.          this.defaults.enumerate(h);
  245.       }
  246.  
  247.       Enumeration e = ((Hashtable)this).keys();
  248.  
  249.       while(e.hasMoreElements()) {
  250.          String key = (String)e.nextElement();
  251.          h.put(key, ((Hashtable)this).get(key));
  252.       }
  253.  
  254.    }
  255. }
  256.