-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonConfig.java
More file actions
158 lines (134 loc) · 4.6 KB
/
jsonConfig.java
File metadata and controls
158 lines (134 loc) · 4.6 KB
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
import org.json.simple.*;
import org.json.simple.parser.*;
import java.io.*;
/**
* Configurator of JSON-Files for Automat
*
* @Alexander; Matteo
* @1.0
*/
public class jsonConfig extends Config
{
// Variablen
private int[][] uebergangstabelle;
private int[] endzustaende;
private char[] alphabet;
private int start;
public jsonConfig () {
}
public boolean readFile (String filePath) {
try
{
File file = new File(filePath);
InputStream inputStream = new FileInputStream(file);
String stringFile = readFromInputStream(inputStream);
JSONParser parser = new JSONParser();
try
{
Object obj1 = parser.parse(stringFile);
JSONObject obj = (JSONObject)obj1;
Object temp = obj.get("table");
JSONArray tempU = (JSONArray)temp;
JSONArray quick = (JSONArray)tempU.get(0);
uebergangstabelle = new int[tempU.size()][quick.size()];
for (int i = 0; i < tempU.size(); i++) {
JSONArray tempUe = (JSONArray)tempU.get(i);
for (int j = 0; j < tempUe.size(); j++) {
long tempLong = (long)tempUe.get(j);
uebergangstabelle[i][j] = (int)tempLong;
}
}
temp = obj.get("alphabet");
JSONArray tempA = (JSONArray)temp;
alphabet = new char[tempA.size()];
for (int i = 0; i < tempA.size(); i++) {
String tempString = (String)tempA.get(i);
alphabet[i] = (char)tempString.charAt(0);
}
temp = obj.get("endzustaende");
JSONArray tempE = (JSONArray)temp;
endzustaende = new int[tempE.size()];
for (int i = 0; i < tempE.size(); i++) {
long tempLong = (long)tempE.get(i);
endzustaende[i] = (int)tempLong;
}
temp = obj.get("start");
long tempLong = (long)temp;
start = (int)tempLong;
}
catch (ParseException pe)
{
pe.printStackTrace();
return false;
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
return false;
}
return false;
}
public boolean writeFile (String filePath, int[][] uebergangstabelle, int[] endzustaende, char[] alphabet, int start) {
JSONObject obj = new JSONObject();
obj.put("table", uebergangstabelle);
obj.put("endzustaende", endzustaende);
obj.put("alphabet", alphabet);
obj.put("start", start);
FileWriter file = null;
try {
// Constructs a FileWriter given a file name, using the platform's default charset
file = new FileWriter(filePath);
file.write(obj.toJSONString());
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
file.flush();
file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
return true;
}
private String readFromInputStream(InputStream inputStream)
throws IOException {
StringBuilder resultStringBuilder = new StringBuilder();
try (BufferedReader br
= new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = br.readLine()) != null) {
resultStringBuilder.append(line).append("\n");
}
}
return resultStringBuilder.toString();
}
public void setUebergangstabelle (int[][] uebergangstabelle) {
this.uebergangstabelle = uebergangstabelle;
}
public void setEndzustaende (int[] endzustaende) {
this.endzustaende = endzustaende;
}
public void setAlphabet (char[] alphabet) {
this.alphabet = alphabet;
}
public void setStart (int start) {
this.start = start;
}
public int[][] getUebergangstabelle () {
return uebergangstabelle;
}
public int[] getEndzustaende () {
return endzustaende;
}
public char[] getAlphabet () {
return alphabet;
}
public int getStart () {
return start;
}
}