Skip to content

Commit 38cacdf

Browse files
committed
i still don't like it
1 parent f2cf1c4 commit 38cacdf

File tree

4 files changed

+84
-168
lines changed

4 files changed

+84
-168
lines changed

src/main/java/org/omegaconfig/Tools.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.Closeable;
66
import java.io.IOException;
7+
import java.io.InputStream;
78
import java.lang.reflect.Field;
89
import java.lang.reflect.TypeVariable;
910
import java.util.Arrays;
@@ -66,6 +67,10 @@ public static Class<?> subTypeOf(Field field) {
6667
throw new IllegalArgumentException("Class has more than 2 type arguments");
6768
}
6869

70+
public static String concat(String prefix, String suffix, char key, String... strings) {
71+
return concat(prefix, suffix, key, Arrays.asList(strings));
72+
}
73+
6974
public static String concat(String prefix, String suffix, char key, Collection<String> strings) {
7075
StringBuilder result = new StringBuilder((strings.size() * 8) + 16);
7176
result.append(prefix);
@@ -80,8 +85,12 @@ public static String concat(String prefix, String suffix, char key, Collection<S
8085
return result.toString();
8186
}
8287

83-
public static String concat(String prefix, String suffix, char key, String... strings) {
84-
return concat(prefix, suffix, key, Arrays.asList(strings));
88+
public static byte[] readAllBytes(InputStream in) throws IOException {
89+
try {
90+
return in.readAllBytes();
91+
} finally {
92+
in.close();
93+
}
8594
}
8695

8796
public static void closeQuietly(Closeable in) {

src/main/java/org/omegaconfig/impl/formats/JSONFormat.java

Lines changed: 69 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class JSONFormat implements IFormatCodec {
2626

2727
@Override
2828
public IFormatReader createReader(Path filePath) throws IOException {
29-
return new AnotherFormatReader(filePath);
29+
return new FormatReader(filePath);
3030
}
3131

3232
@Override
@@ -52,7 +52,6 @@ public enum CapturingMode {
5252
PRIMITIVE_VALUE,
5353
STRING_VALUE,
5454
ARRAY,
55-
ARRAY_PRIMITIVE_VALUE,
5655
ARRAY_STRING_VALUE;
5756

5857
public boolean value() {
@@ -64,7 +63,7 @@ public boolean string() {
6463
}
6564
}
6665

67-
public static class AnotherFormatReader implements IFormatReader {
66+
public static class FormatReader implements IFormatReader {
6867
public final LinkedHashMap<String, String> values = new LinkedHashMap<>();
6968

7069
@Override
@@ -87,7 +86,63 @@ public void close() throws IOException {
8786

8887
}
8988

90-
public AnotherFormatReader(Path path) throws IOException {
89+
public static class ArrayValueReader extends ValueReader {
90+
private final List<String> values = new ArrayList<>();
91+
92+
public ArrayValueReader(LinkedHashSet<String> group) {
93+
super(group);
94+
}
95+
96+
public void nextEntry() {
97+
values.add(value.toString());
98+
value = new StringBuilder();
99+
}
100+
101+
@Override
102+
public String getValue() {
103+
return Arrays.toString(values.toArray());
104+
}
105+
}
106+
107+
public static class ValueReader {
108+
protected final LinkedHashSet<String> group;
109+
protected final StringBuilder key = new StringBuilder();
110+
protected StringBuilder value = new StringBuilder();
111+
public ValueReader(LinkedHashSet<String> group) {
112+
this.group = group;
113+
}
114+
115+
public void appendKey(char c) {
116+
key.append(c);
117+
}
118+
119+
public void appendValue(char c) {
120+
value.append(c);
121+
}
122+
123+
public String getKey() {
124+
return Tools.concat("", (group.isEmpty() ? "" : ".") + key, '.', group);
125+
}
126+
127+
public String getValue() {
128+
return value.toString();
129+
}
130+
}
131+
132+
public static void read(Path path) throws IOException {
133+
char[] data = new String(Tools.readAllBytes(new FileInputStream(path.toFile()))).toCharArray();
134+
135+
ValueReader valueReader = null;
136+
boolean escaped;
137+
boolean finished;
138+
139+
for (char c: data) {
140+
// SKIP WHITESPACE PROCESING WHEN IS NOT CAPTURING NON-STRING-VALUES
141+
if (Character.isWhitespace(c)) continue;
142+
}
143+
}
144+
145+
public FormatReader(Path path) throws IOException {
91146
// READ JSON STRING
92147
var in = new FileInputStream(path.toFile());
93148
char[] data = new String(in.readAllBytes()).toCharArray();
@@ -148,7 +203,6 @@ public AnotherFormatReader(Path path) throws IOException {
148203
}
149204

150205
if (capturing == CapturingMode.ARRAY_STRING_VALUE) {
151-
capturing = CapturingMode.ARRAY;
152206
valueArray.add(value.toString());
153207
value = new StringBuilder();
154208
nextChars = new NextChar(new char[] { JSON_CONTINUE, JSON_ARRAY_END }, null);
@@ -217,8 +271,15 @@ public AnotherFormatReader(Path path) throws IOException {
217271
value = new StringBuilder();
218272
}
219273

220-
if (capturing == CapturingMode.ARRAY || capturing == CapturingMode.ARRAY_PRIMITIVE_VALUE) {
221-
nextChars = new NextChar(new char[] { JSON_STRING_LINE }, null);
274+
if (capturing == CapturingMode.ARRAY_STRING_VALUE) {
275+
valueArray.add(value.toString());
276+
value = new StringBuilder();
277+
nextChars = null;
278+
continue;
279+
}
280+
281+
if (capturing == CapturingMode.ARRAY) {
282+
nextChars = null;
222283
continue;
223284
}
224285

@@ -257,168 +318,12 @@ public AnotherFormatReader(Path path) throws IOException {
257318
switch (capturing) {
258319
case KEY -> key.append(c);
259320
case STRING_VALUE, PRIMITIVE_VALUE -> value.append(c);
260-
case ARRAY_STRING_VALUE, ARRAY_PRIMITIVE_VALUE -> value.append(c);
321+
case ARRAY_STRING_VALUE, ARRAY -> value.append(c);
261322
case NONE -> throw new IllegalStateException("Not capturing values");
262323
}
263324

264325
escaped = false;
265326
}
266327
}
267328
}
268-
269-
270-
public static class FormatReader implements IFormatReader {
271-
public final LinkedHashMap<String, String> values = new LinkedHashMap<>();
272-
273-
public FormatReader(Path path) throws IOException {
274-
// READ JSON STRING
275-
var in = new FileInputStream(path.toFile());
276-
char[] data = new String(in.readAllBytes()).toCharArray();
277-
in.close();
278-
279-
char[] expected = new char[] { JSON_OBJECT_START };
280-
char[] permissive = new char[0];
281-
boolean escaped = false;
282-
boolean keyCapture = false;
283-
boolean valueCapture = false;
284-
boolean valueWhiteCapture = false;
285-
boolean finished = false;
286-
final LinkedHashSet<String> group = new LinkedHashSet<>();
287-
StringBuilder key = new StringBuilder();
288-
StringBuilder value =new StringBuilder();
289-
290-
for (char c: data) {
291-
if (Character.isWhitespace(c) && !keyCapture && !valueCapture) continue;
292-
293-
if (finished && group.isEmpty())
294-
throw new IllegalStateException("JSON object finished reading but still contains data");
295-
296-
if (!Tools.contains(c, expected) && !valueCapture && !keyCapture) {
297-
if (Tools.contains(c, permissive)) {
298-
throw new IllegalArgumentException("Expected char(s) " + Arrays.toString(expected) + " but received " + c);
299-
}
300-
throw new IllegalArgumentException("Expected char(s) " + Arrays.toString(expected) + " but received " + c);
301-
}
302-
303-
switch (c) {
304-
case JSON_OBJECT_START -> {
305-
if (!key.isEmpty() && valueCapture) {
306-
group.add(key.toString());
307-
key = new StringBuilder();
308-
}
309-
expected = new char[]{JSON_STRING_LINE, JSON_OBJECT_END}; // key start or object end
310-
continue;
311-
}
312-
313-
case JSON_STRING_LINE -> {
314-
if (escaped) {
315-
escaped = false;
316-
break; // BREAKS THE SWITCH AND JUMP TO STORAGE
317-
}
318-
319-
if (keyCapture) {
320-
keyCapture = false;
321-
valueCapture = false;
322-
expected = new char[]{JSON_ENTRY_SPLIT};
323-
continue;
324-
}
325-
326-
// FIXME: must accept empty values
327-
if (valueCapture && !value.isEmpty()) {
328-
valueCapture = false;
329-
keyCapture = false;
330-
expected = new char[]{JSON_CONTINUE, JSON_OBJECT_END};
331-
values.put(Tools.concat("", key.toString(), '.', group), value.toString());
332-
key = new StringBuilder();
333-
value = new StringBuilder();
334-
continue;
335-
}
336-
337-
if (valueCapture) {
338-
continue; // skip char storage
339-
}
340-
341-
keyCapture = true;
342-
valueCapture = false;
343-
expected = new char[0];
344-
continue;
345-
}
346-
347-
case JSON_ENTRY_SPLIT -> {
348-
if (keyCapture || valueCapture) {
349-
continue; // IF WE ARE ALREADY CAPTURING THEN ITS PART OF THE VALUE
350-
}
351-
valueCapture = false;
352-
keyCapture = false;
353-
expected = new char[]{JSON_STRING_LINE, JSON_OBJECT_START, ' '};
354-
continue;
355-
}
356-
357-
case JSON_CONTINUE -> {
358-
// TODO: here we are not supposted to accept empty values
359-
if (valueCapture && !value.isEmpty()) {
360-
valueCapture = false;
361-
keyCapture = false;
362-
expected = new char[]{JSON_CONTINUE, JSON_OBJECT_END};
363-
values.put(Tools.concat("", key.toString(), '.', group), value.toString());
364-
key = new StringBuilder();
365-
value = new StringBuilder();
366-
continue;
367-
}
368-
expected = new char[]{JSON_STRING_LINE};
369-
permissive = new char[]{JSON_OBJECT_END};
370-
continue;
371-
}
372-
373-
case JSON_ESCAPED -> {
374-
if (valueCapture || keyCapture) {
375-
escaped = true;
376-
continue;
377-
}
378-
379-
throw new IllegalStateException("You cannot escape " + c);
380-
}
381-
382-
case JSON_OBJECT_END -> {
383-
if (group.isEmpty()) {
384-
finished = true;
385-
}
386-
continue;
387-
}
388-
}
389-
390-
if (keyCapture && valueCapture)
391-
throw new IllegalStateException("Cannot capture key and value at the same time");
392-
393-
if (keyCapture) {
394-
key.append(c);
395-
continue;
396-
}
397-
398-
if (valueCapture) {
399-
value.append(c);
400-
}
401-
}
402-
}
403-
404-
@Override
405-
public String read(String fieldName) {
406-
return "";
407-
}
408-
409-
@Override
410-
public void push(String group) {
411-
412-
}
413-
414-
@Override
415-
public void pop() {
416-
417-
}
418-
419-
@Override
420-
public void close() throws IOException {
421-
422-
}
423-
}
424329
}

src/test/java/me/srrrapero720/config/WriteTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package me.srrrapero720.config;
22

3-
import org.omegaconfig.api.formats.IFormatReader;
43
import org.omegaconfig.impl.formats.JSONFormat;
54

65
import java.io.File;
@@ -10,7 +9,7 @@ public class WriteTest {
109

1110

1211
public static void main(String... args) throws IOException {
13-
JSONFormat.AnotherFormatReader reader = (JSONFormat.AnotherFormatReader) new JSONFormat().createReader(new File("test-json.json").toPath());
12+
JSONFormat.FormatReader reader = (JSONFormat.FormatReader) new JSONFormat().createReader(new File("test-json.json").toPath());
1413

1514
reader.values.forEach((k, v) -> {
1615
System.out.println(k + "=" + v);

test-json.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@
1515
"KEY ONE",
1616
"KEY TWO"
1717
],
18+
"eight_numbers": [
19+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
20+
],
1821
"broken_key": "false"
1922
}

0 commit comments

Comments
 (0)