-
Notifications
You must be signed in to change notification settings - Fork 0
/
INIReader.java
116 lines (99 loc) · 3.31 KB
/
INIReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package chkrr00k.persister;
//"proudly" made by chkrr00k
// LGPL licence
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.AccessDeniedException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.util.Pair;
public class INIReader {
private static Pattern INISECTION;
private static Pattern INIVALUE;
private File file;
private INISettings settings;
private long lastModified;
public INIReader(String fileName) throws AccessDeniedException {
this.file = new File(fileName);
this.settings = null;
this.lastModified = 0;
if(!this.file.canRead()){
throw new AccessDeniedException("Unable to read file");
}
INIReader.INISECTION = Pattern.compile("\\[(.+)\\]");
INIReader.INIVALUE = Pattern.compile("([^;].*) ?= ?(.*)");
}
public INIReader(File file) throws AccessDeniedException {
this.file = file;
this.settings = null;
this.lastModified = 0;
if(!this.file.canRead()){
throw new AccessDeniedException("Unable to read file");
}
INIReader.INISECTION = Pattern.compile("\\[(.+)\\]");
INIReader.INIVALUE = Pattern.compile("([^;].*) ?= ?(.*)");
}
private String read() throws IOException {
StringBuilder result = new StringBuilder();
this.lastModified = this.file.lastModified();
try(BufferedReader br = new BufferedReader(new FileReader(this.file))){
String line;
while((line = br.readLine()) != null){
result.append(line + "\n");
}
}
return result.toString();
}
public INISettings getSettings() throws IOException {
if(this.settings == null || this.lastModified != this.file.lastModified()){
this.settings = this.iniSettingsfy(this.process(this.read()));
}
return this.settings;
}
private INISettings iniSettingsfy(Map<String, List<Pair<String, String>>> input) {
return new INISettings(input);
}
private Map<String, List<Pair<String, String>>> process(String input) {
String current = "";
Map<String, List<Pair<String, String>>> storage = new HashMap<String, List<Pair<String, String>>>();
for(Pair<String, String> pair : this.parseAll(input)){
if(pair.getValue() == null && !storage.containsKey(pair.getKey())){
storage.put(pair.getKey(), new LinkedList<Pair<String, String>>());
current = pair.getKey();
}else if(pair.getValue() != null && !current.equals("")){
storage.get(current).add(pair);
}
}
return storage;
}
private List<Pair<String, String>> parseAll(String input) {
List<Pair<String, String>> result = new LinkedList<Pair<String, String>>();
Pair<String, String> tmp = null;
for(String line : input.split("\n")){
tmp = this.parse(line.trim());
if(tmp != null){
result.add(tmp);
}
}
return result;
}
private Pair<String, String> parse(String input) {
Matcher m = INIReader.INISECTION.matcher(input);
if(m.matches()){
return new Pair<String, String>(m.group(1).trim(), null);
}else{
m = INIReader.INIVALUE.matcher(input);
if(m.matches()){
return new Pair<String, String>(m.group(1).trim(), m.group(2).trim());
}
}
return null;
}
}