import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class INIReader implements Serializable {
private static final long serialVersionUID = -111111111222222222L;
private final Map map = new HashMap();
public INIReader() {}
public INIReader(InputStream in) throws IOException {
read(in);
}
public void read(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
boolean block = false;
Properties props = null;
map.clear();
for (String buffer; (buffer = reader.readLine()) != null;) {
String trimed = buffer.trim();
if (!isValid(trimed)) continue;
if (block && trimed.startsWith("[") && trimed.endsWith("]")) {
props = null;
block = false;
}
if (!block && trimed.startsWith("[") && trimed.endsWith("]")) {
String key = buffer.substring(1, buffer.length() - 1);
if (key.trim().equals("")) continue;
props = new Properties();
map.put(key, props);
block = true;
continue;
}
if (block) addToProp(props, buffer);
}
}
public void write(OutputStream out) {
PrintWriter writer = new PrintWriter(out);
for (Iterator it = map.keySet().iterator(); it.hasNext();) {
String bkey = (String) it.next();
writer.println("[" + bkey + "]");
Properties props = (Properties) map.get(bkey);
for (Iterator iit = props.keySet().iterator(); iit.hasNext();) {
String key = (String) iit.next();
String value = (String) props.get(key);
writer.println(key + "=" + value);
}
writer.flush();
}
// writer.flush();
writer.close();
}
public boolean existsBlock(String key) {
return map.containsKey(key);
}
public Iterator getKeys() {
return map.keySet().iterator();
}
public Properties getBlock(String key) {
Properties props = (Properties) map.get(key);
return props == null ? new Properties() : props;
// return props;
}
public int size() {
return map.size();
}
public int blockSize(String key) {
Properties props = (Properties) map.get(key);
if (props == null) return 0;
return props.size();
}
public void clearBlock(String key) {
Properties props = (Properties) map.get(key);
if (props == null) return;
props.clear();
}
public void clear() {
map.clear();
}
public void put(String key, Properties props) {
map.put(key, props);
}
public void putAll(INIReader reader) {
for (Iterator it = reader.getKeys(); it.hasNext();) {
String key = (String) it.next();
Properties props = reader.getBlock(key);
map.put(key, props);
}
}
private boolean isValid(String s) {
s = s == null ? "" : s.trim();
if (s.equals("")) return false;
if (s.startsWith("!") || s.startsWith(";") || s.startsWith("#")) return false;
return true;
}
private void addToProp(Properties props, String s) {
if (props == null || s == null || s.trim().equals("")) return;
int index = s.indexOf("=", 0);
if (index < 0) {
props.put(s, "");
} else {
String key = s.substring(0, index);
if (key.trim().equals("")) return;
props.put(key, s.substring(index + 1, s.length()));
}
}
}