diff --git a/classpath/java/util/Properties.java b/classpath/java/util/Properties.java new file mode 100644 index 0000000000..4233419bfa --- /dev/null +++ b/classpath/java/util/Properties.java @@ -0,0 +1,104 @@ +package java.util; + +import java.io.InputStream; +import java.io.IOException; + +public class Properties extends Hashtable { + public void load(InputStream in) throws IOException { + new Parser().parse(in, this); + } + + 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 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; + + default: + append(c); + break; + } + + escaped = false; + } + } + + finishLine(map); + } + } +} diff --git a/classpath/java/util/PropertyResourceBundle.java b/classpath/java/util/PropertyResourceBundle.java new file mode 100644 index 0000000000..121aed826e --- /dev/null +++ b/classpath/java/util/PropertyResourceBundle.java @@ -0,0 +1,16 @@ +package java.util; + +import java.io.InputStream; +import java.io.IOException; + +public class PropertyResourceBundle extends ResourceBundle { + private final Properties map = new Properties(); + + public PropertyResourceBundle(InputStream in) throws IOException { + map.load(in); + } + + public Object handleGetObject(String key) { + return map.get(key); + } +} diff --git a/classpath/java/util/ResourceBundle.java b/classpath/java/util/ResourceBundle.java index 55a7a979d1..3a1225d941 100644 --- a/classpath/java/util/ResourceBundle.java +++ b/classpath/java/util/ResourceBundle.java @@ -25,7 +25,10 @@ public abstract class ResourceBundle { (replace('.', '/', name) + ".properties"); if (in != null) { try { - return new MapResourceBundle(name, parent, new Parser().parse(in)); + ResourceBundle r = new PropertyResourceBundle(in); + r.name = name; + r.parent = parent; + return r; } finally { in.close(); } @@ -105,117 +108,4 @@ public abstract class ResourceBundle { } protected abstract Object handleGetObject(String key); - - private static class MapResourceBundle extends ResourceBundle { - private final Map map; - - public MapResourceBundle(String name, ResourceBundle parent, - Map map) - { - this.name = name; - this.parent = parent; - this.map = map; - } - - protected Object handleGetObject(String key) { - return map.get(key); - } - } - - 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 map) { - if (key != null) { - map.put(key.toString(), - (value == null ? "" : value.toString().trim())); - } - - key = value = current = null; - } - - private Map parse(InputStream in) - throws IOException - { - Map map = new HashMap(); - 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 || key != current) { - append(c); - } else { - current = null; - } - break; - - case ':': - case '=': - if (escaped || key != current) { - append(c); - } else { - if (key == null) { - key = new StringBuilder(); - } - current = null; - } - break; - - case '\n': - if (escaped) { - append(c); - } else { - finishLine(map); - } - break; - - default: - append(c); - break; - } - - escaped = false; - } - } - - finishLine(map); - - return map; - } - } }