Java Test
- Write a program that can parse a Windows .ini format file and store the contents in memory (in some datatype/class of your choice)
- Also provide some convenience api that allows a developer to retrieve information from the parsed representation
e.g. getIniKeys()
getIniVals()
getIniVal(String aKey, aGroupName)
getGroup(String aGroupName
- Provide an interactive way to query different attributes, e.g, myname, main.theIP, Complex_Context.mynameSample Ini File
===============
myname = props[main]
theIP = 192.184.12.1
DNS = 34.2.4.5
DNS = 15.19.27.84# defines a group
[myhome]
IP = 192.184.12.2
IP = 192.168.12.50[complex context]
MyName = halloSome Rules To keep in Mind
==========================
a. Ini File consists of a set of attribute value pairs that are grouped into sections/groups
b. Groups are enclosed within "[]"
c. Strip off any whitespaces encountered between keys or values
d. comments begin with "#"
e. there could be blank lines in the file
f. there could be white space in the group name (which should be preserved)
g. A key could have multiple values (e.g. IP in the above sample has two possible values) which should be preserved
What are we testing
====================
- The programmer's ability to write good clean, readable code that solves the above problem.
- We are NOT looking for efficiency, speed, algorithmic abilities in solving this problem
- Write a program that can parse a Windows .ini format file and store the contents in memory (in some datatype/class of your choice)
- Also provide some convenience api that allows a developer to retrieve information from the parsed representation
e.g. getIniKeys()
getIniVals()
getIniVal(String aKey, aGroupName)
getGroup(String aGroupName
- Provide an interactive way to query different attributes, e.g, myname, main.theIP, Complex_Context.mynameSample Ini File
===============
myname = props[main]
theIP = 192.184.12.1
DNS = 34.2.4.5
DNS = 15.19.27.84# defines a group
[myhome]
IP = 192.184.12.2
IP = 192.168.12.50[complex context]
MyName = halloSome Rules To keep in Mind
==========================
a. Ini File consists of a set of attribute value pairs that are grouped into sections/groups
b. Groups are enclosed within "[]"
c. Strip off any whitespaces encountered between keys or values
d. comments begin with "#"
e. there could be blank lines in the file
f. there could be white space in the group name (which should be preserved)
g. A key could have multiple values (e.g. IP in the above sample has two possible values) which should be preserved
What are we testing
====================
- The programmer's ability to write good clean, readable code that solves the above problem.
- We are NOT looking for efficiency, speed, algorithmic abilities in solving this problem
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class Test2 {
public static void main(String[] args) {
INI ini = new INI("test.ini");
List<String> keys = ini.getIniKeys();
ini.print(keys);
System.out.println();
List<String> values = ini.getIniValues();
ini.print(values);
System.out.println();
ini.print();
System.out.println();
Map<String, List<String>> map = ini.getGroup("main");
ini.print(map);
System.out.println();
List<String> list = ini.getIniValues("ip", "myhome");
ini.print(list);
}
}class INI {
private Map<String, Map<String, List<String>>> inis =
new HashMap<String, Map<String, List<String>>>();
private INI() {
}
public INI(String filename) {
loadFile(filename);
}
/**
* 获得所有的键名
* @return
*/
public List<String> getIniKeys() {
List<String> keys = new ArrayList<String>();
for(Map.Entry<String, Map<String, List<String>>> entry : inis.entrySet()) {
for(Map.Entry<String, List<String>> e : entry.getValue().entrySet()) {
keys.add(e.getKey());
}
}
return keys;
}
/**
* 获得所有值
* @return
*/
public List<String> getIniValues() {
List<String> values = new ArrayList<String>();
for(Map.Entry<String, Map<String, List<String>>> entry : inis.entrySet()) {
for(Map.Entry<String, List<String>> e : entry.getValue().entrySet()) {
for(String s : e.getValue()) {
values.add(s);
}
}
}
return values;
}
/**
* 根据组名、键名查找值
* @param key
* @param group
* @return
*/
public List<String> getIniValues(String key, String group) {
Map<String, List<String>> map = inis.get(group.toLowerCase());
List<String> values = map.get(key.toLowerCase());
return values;
}
/**
* 根据组名查找数据
* @param group
* @return
*/
public Map<String, List<String>> getGroup(String group) {
Map<String, List<String>> map = inis.get(group.toLowerCase());
return map;
}
/**
* 输出某一项数据
* @param list
*/
public void print(List<String> list) {
for(String s : list) {
System.out.println(s);
}
}
/**
* 输出某一组的数据
* @param map
*/
public void print(Map<String, List<String>> map) {
for(Map.Entry<String, List<String>> entry : map.entrySet()) {
for(String s : entry.getValue()) {
System.out.printf("%s = %s%n", entry.getKey(), s);
}
}
}
/**
* 输出整个 ini 数据
*/
public void print() {
for(Map.Entry<String, Map<String, List<String>>> entry : inis.entrySet()) {
System.out.printf("[%s]%n", entry.getKey());
for(Map.Entry<String, List<String>> e : entry.getValue().entrySet()) {
for(String s : e.getValue()) {
System.out.printf("%s = %s%n", e.getKey(), s);
}
}
}
}
/**
* 从文件中加载数据,全部转为小写字母
* @param filename
*/
private void loadFile(String filename) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
String s = "";
String group = "";
while((s = br.readLine()) != null) {
s = s.trim();
if(s.length() < 1 || s.charAt(0) == '#') {
continue;
}
if(s.matches("\\[.*\\]")){
group = s.substring(1, s.length()-1).toLowerCase();
}else{
String key = s.split("=")[0].trim().toLowerCase();
String value = s.split("=")[1].trim().toLowerCase();
Map<String, List<String>> map = null;
if(inis.get(group)==null) {
map = new HashMap<String, List<String>>();
}else{
map = inis.get(group);
}
List<String> list = null;
if(map.get(key)==null) {
list = new ArrayList<String>();
}else{
list = map.get(key);
}
list.add(value);
map.put(key, list);
inis.put(group, map);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}