2009-12-02 19:08:29 -07:00
|
|
|
/* Copyright (c) 2008-2009, Avian Contributors
|
2008-02-19 11:06:52 -07:00
|
|
|
|
|
|
|
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. */
|
|
|
|
|
2007-09-17 16:16:57 -06:00
|
|
|
package java.util;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
2007-09-26 09:48:21 -06:00
|
|
|
import java.io.OutputStream;
|
2007-09-28 11:38:58 -06:00
|
|
|
import java.io.PrintStream;
|
2007-09-17 16:16:57 -06:00
|
|
|
import java.io.IOException;
|
2010-08-15 14:28:09 +02:00
|
|
|
import java.io.Reader;
|
2007-09-17 16:16:57 -06:00
|
|
|
|
|
|
|
public class Properties extends Hashtable {
|
|
|
|
public void load(InputStream in) throws IOException {
|
2010-08-15 14:28:09 +02:00
|
|
|
new InputStreamParser(in).parse(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void load(Reader reader) throws IOException {
|
|
|
|
new ReaderParser(reader).parse(this);
|
2007-09-17 16:16:57 -06:00
|
|
|
}
|
|
|
|
|
2007-09-26 09:48:21 -06:00
|
|
|
public void store(OutputStream out, String comment) throws IOException {
|
2007-09-28 11:38:58 -06:00
|
|
|
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();
|
2007-09-26 09:48:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getProperty(String key) {
|
|
|
|
return (String)get(key);
|
|
|
|
}
|
|
|
|
|
2008-07-13 18:27:42 -06:00
|
|
|
public String getProperty(String key, String defaultValue) {
|
|
|
|
String value = (String) get(key);
|
|
|
|
if (value == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-09-18 17:51:05 -06:00
|
|
|
public Object setProperty(String key, String value) {
|
|
|
|
return put(key, value);
|
2007-09-26 09:48:21 -06:00
|
|
|
}
|
2010-08-15 03:13:09 +02:00
|
|
|
|
|
|
|
public Enumeration<?> propertyNames() {
|
|
|
|
return keys();
|
|
|
|
}
|
2007-09-26 09:48:21 -06:00
|
|
|
|
2010-08-15 14:28:09 +02:00
|
|
|
private abstract static class Parser {
|
2007-09-17 16:16:57 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-15 14:28:09 +02:00
|
|
|
abstract int readCharacter() throws IOException;
|
|
|
|
|
|
|
|
void parse(Map map)
|
2007-09-17 16:16:57 -06:00
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
boolean escaped = false;
|
|
|
|
|
|
|
|
int c;
|
2010-08-15 14:28:09 +02:00
|
|
|
while ((c = readCharacter()) != -1) {
|
2007-09-17 16:16:57 -06:00
|
|
|
if (c == '\\') {
|
|
|
|
if (escaped) {
|
|
|
|
escaped = false;
|
|
|
|
append(c);
|
|
|
|
} else {
|
|
|
|
escaped = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (c) {
|
|
|
|
case '#':
|
|
|
|
case '!':
|
|
|
|
if (key == null) {
|
2010-08-15 14:28:09 +02:00
|
|
|
while ((c = readCharacter()) != -1 && c != '\n');
|
2007-09-17 16:16:57 -06:00
|
|
|
} 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;
|
2008-01-23 16:39:45 -07:00
|
|
|
case 'n':
|
|
|
|
if (escaped) {
|
|
|
|
append('\n');
|
|
|
|
} else {
|
|
|
|
append(c);
|
|
|
|
}
|
|
|
|
break;
|
2007-09-17 16:16:57 -06:00
|
|
|
|
|
|
|
default:
|
|
|
|
append(c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
escaped = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finishLine(map);
|
|
|
|
}
|
|
|
|
}
|
2010-08-15 14:28:09 +02:00
|
|
|
|
|
|
|
static class InputStreamParser extends Parser {
|
|
|
|
InputStream in;
|
|
|
|
|
|
|
|
|
|
|
|
public InputStreamParser(InputStream in) {
|
|
|
|
this.in = in;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
int readCharacter() throws IOException {
|
|
|
|
return in.read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class ReaderParser extends Parser {
|
|
|
|
Reader in;
|
|
|
|
|
|
|
|
public ReaderParser(Reader in) {
|
|
|
|
this.in = in;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
int readCharacter() throws IOException {
|
|
|
|
return in.read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-17 16:16:57 -06:00
|
|
|
}
|