This repository was archived by the owner on Jul 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJAMLParser.java
158 lines (133 loc) · 3.46 KB
/
JAMLParser.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class JAMLParser {
private List<String> unparsed = new ArrayList<String>();
private List<String> parsed = new ArrayList<String>();
private Map<String, String> DefinitionMap = new HashMap<String, String>();
private File targetFile = null;
private List<String> targetList = null;
public JAMLParser (File target) {
targetFile = target;
//if (!target.getName().endsWith("jaml")) {
// System.err.println("Please use jaml extension");
//}
try {
unwrapFile();
parse();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public JAMLParser(List<String> target) {
targetList = target;
parse();
}
private void unwrapFile() throws FileNotFoundException, IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream(targetFile), StandardCharsets.UTF_8);
try (BufferedReader reader = new BufferedReader(isr)) {
String read = "";
while ((read = reader.readLine()) != null) {
unparsed.add(read);
}
boolean inside = false;
String buf = "";
for (String p : unparsed) {
if (p.startsWith("{")) {
inside = true;
continue;
} else if (p.startsWith("}")) {
inside = false;
parsed.add(buf);
buf = "";
continue;
}
if (inside) {
buf += p;
}
}
targetList = parsed;
parsed = null;
unparsed = null;
} catch (Exception e) {
} finally {
isr.close();
}
}
private void parse () {
for (String p : targetList) {
String name = "";
String beschreibung = "";
char[] arr = p.toCharArray();
boolean beschreibungsStart = false;
int start = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == ':' && !beschreibungsStart) {
name = p.substring(0, i);
continue;
}
if (arr[i] == '"' && !beschreibungsStart) {
beschreibungsStart = true;
start = i;
continue;
} else if (arr[i] == '"' && beschreibungsStart) {
beschreibung = p.substring(start + 1, i);
}
}
DefinitionMap.put(name, beschreibung);
}
}
public Set<String> getKeySet () {
return DefinitionMap.keySet();
}
public Map<String, String> getMap () {
return DefinitionMap;
}
public String getValue (String key) {
return DefinitionMap.get(key);
}
public boolean containsKey (String key) {
return DefinitionMap.containsKey(key);
}
public void setTarget (File target) {
targetFile = target;
reInitialize();
try {
unwrapFile();
parse();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setTarget (List<String> target) {
targetList = target;
reInitialize();
parse();
}
public void reInitialize () {
unparsed = new ArrayList<String>();
parsed = new ArrayList<String>();
DefinitionMap = new HashMap<String, String>();
}
private String return_able = "";
@Override
public String toString() {
return_able += "Current Content:\n";
this.DefinitionMap.forEach((String Key, String Value) -> return_able += "Name: " + Key + ", Value: " + Value + "\n");
return return_able;
}
}