/* Copyright (c) 2008-2009, Avian Contributors

   Permission to use, copy, modify, and/or distribute this software
   for any purpose with or without fee is hereby granted, provided
   that the above copyright notice and this permission notice appear
   in all copies.

   There is NO WARRANTY for this software.  See license.txt for
   details. */

package java.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.IOException;

public class Properties extends Hashtable {
  public void load(InputStream in) throws IOException {
    new Parser().parse(in, this);
  }

  public void store(OutputStream out, String comment) throws IOException {
    PrintStream os = new PrintStream(out);
    os.println("# " + comment);
    for (Iterator it = entrySet().iterator();
         it.hasNext();) {
      Map.Entry entry = (Map.Entry)it.next();
      os.print(entry.getKey());
      os.print('=');
      os.println(entry.getValue());
    }
    os.flush();
  }

  public String getProperty(String key) {
    return (String)get(key);
  }

  public String getProperty(String key, String defaultValue) {
    String value = (String) get(key);
    if (value == null) {
      return defaultValue;
    }
    return value;
  }

  public Object setProperty(String key, String value) {
    return put(key, value);
  }
  
  public Enumeration<?> propertyNames() {
    return keys();
  }

  private static class Parser {
    private StringBuilder key = null;
    private StringBuilder value = null;
    private StringBuilder current = null;

    private void append(int c) {
      if (current == null) {
        if (key == null) {
          current = key = new StringBuilder();
        } else {
          current = value = new StringBuilder();
        }
      }

      current.append((char) c);
    }

    private void finishLine(Map<String, Object> map) {
      if (key != null) {
        map.put(key.toString(),
                (value == null ? "" : value.toString().trim()));
      }

      key = value = current = null;
    }

    private void parse(InputStream in, Map map)
      throws IOException
    {
      boolean escaped = false;

      int c;
      while ((c = in.read()) != -1) {
        if (c == '\\') {
          if (escaped) {
            escaped = false;
            append(c);
          } else {
            escaped = true;
          }
        } else {
          switch (c) {
          case '#':
          case '!':
            if (key == null) {
              while ((c = in.read()) != -1 && c != '\n');
            } else {
              append(c);
            }
            break;

          case ' ':
          case '\r':
          case '\t':
            if (escaped || (current != null && value == current)) {
              append(c);
            } else if (key == current) {
              current = null;
            }
            break;

          case ':':
          case '=':
            if (escaped || (current != null && value == current)) {
              append(c);
            } else {
              if (key == null) {
                key = new StringBuilder();
              }
              current = null;
            }
            break;

          case '\n':
            if (escaped) {
              append(c);
            } else {
              finishLine(map);          
            }
            break;
          case 'n':
            if (escaped) {
              append('\n');
            } else {
              append(c);
            }
            break;

          default:
            append(c);
            break;
          }
        
          escaped = false;
        }
      }

      finishLine(map);
    }
  }
}

@@ -476,16 +476,39 @@
 ception;
+%0Aimport java.io.Reader;
 %0A%0Apublic
@@ -613,23 +613,36 @@
 new 
+InputStream
 Parser(
+in
 ).parse(
 in, 
@@ -637,20 +637,120 @@
 ).parse(
-in, 
+this);%0A  %7D%0A  %0A  public void load(Reader reader) throws IOException %7B%0A    new ReaderParser(reader).parse(
 this);%0A 
@@ -1562,16 +1562,25 @@
 private 
+abstract 
 static c
@@ -2211,43 +2211,77 @@
     
-private void parse(InputStream in, 
+abstract int readCharacter() throws IOException;%0A    %0A    void parse(
 Map 
@@ -2371,39 +2371,45 @@
     while ((c = 
-in.
 read
+Character
 ()) != -1) %7B%0A   
@@ -2707,15 +2707,21 @@
 c = 
-in.
 read
+Character
 ()) 
@@ -3871,14 +3871,488 @@
 ;%0A    %7D%0A
-
   %7D%0A
+  %0A  static class InputStreamParser extends Parser %7B%0A    InputStream in;%0A    %0A    %0A    public InputStreamParser(InputStream in) %7B%0A      this.in = in;%0A    %7D%0A%0A    @Override%0A    int readCharacter() throws IOException %7B%0A      return in.read();%0A    %7D%0A  %7D%0A%0A  static class ReaderParser extends Parser %7B%0A    Reader in;%0A    %0A    public ReaderParser(Reader in) %7B%0A      this.in = in;%0A    %7D%0A%0A    @Override%0A    int readCharacter() throws IOException %7B%0A      return in.read();%0A    %7D%0A  %7D%0A%0A
 %7D%0A

add Properties.load(Reader) implementation