added JSONC parsing and config print out
parent
aa6d513ca2
commit
7fc42e171d
|
|
@ -1,17 +1,23 @@
|
|||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class config_editor {
|
||||
public static String config_file_path;
|
||||
public static Enum<ConfigFileType> config_file_type;
|
||||
public static ConfigFileType config_file_type;
|
||||
static java.io.File config_file;
|
||||
static Map<String, Object> config_data;
|
||||
|
||||
enum ConfigFileType {
|
||||
JSONC,
|
||||
XML
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws IOException {
|
||||
config_file_path = args[0];
|
||||
verify_config_file();
|
||||
config_file_type();
|
||||
parse_config_file();
|
||||
print_config_data();
|
||||
}
|
||||
|
||||
static void verify_config_file() {
|
||||
|
|
@ -40,4 +46,30 @@ public class config_editor {
|
|||
}
|
||||
}
|
||||
|
||||
static void parse_config_file() throws IOException {
|
||||
switch (config_file_type) {
|
||||
case JSONC:
|
||||
config_data = jsonc_parse.parse_file(config_file_path);
|
||||
break;
|
||||
case XML:
|
||||
// Call methods to parse XML file
|
||||
break;
|
||||
default:
|
||||
System.err.println("No valid config file type detected.");
|
||||
}
|
||||
}
|
||||
|
||||
static void print_config_data() {
|
||||
for (Map.Entry<String, Object> entry : config_data.entrySet()) {
|
||||
System.out.println(entry.getKey());
|
||||
if (entry.getValue() instanceof Map<?, ?>) {
|
||||
Map<?, ?> nestedMap = (Map<?, ?>) entry.getValue();
|
||||
for (Map.Entry<?, ?> nestedEntry : nestedMap.entrySet()) {
|
||||
System.out.println(" " + nestedEntry.getKey() + ": " + nestedEntry.getValue());
|
||||
}
|
||||
} else {
|
||||
System.out.println(" " + entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,318 @@
|
|||
public class jsonc_parse {
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class jsonc_parse {
|
||||
public static Map<String, Object> parse(String json) {
|
||||
if (json == null) {
|
||||
throw new IllegalArgumentException("JSON content must not be null.");
|
||||
}
|
||||
|
||||
Parser parser = new Parser(json);
|
||||
Object value = parser.parseDocument();
|
||||
if (!(value instanceof Map)) {
|
||||
throw parser.error("The top-level JSON value must be an object.");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> result = (Map<String, Object>) value;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Map<String, Object> parse_file(String filePath) throws IOException {
|
||||
byte[] content = Files.readAllBytes(Paths.get(filePath));
|
||||
return parse(new String(content, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static class Parser {
|
||||
private final String input;
|
||||
private int position;
|
||||
|
||||
Parser(String input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
Object parseDocument() {
|
||||
skipTrivia();
|
||||
Object value = parseValue();
|
||||
skipTrivia();
|
||||
if (position != input.length()) {
|
||||
throw error("Unexpected content after the top-level value.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private Object parseValue() {
|
||||
skipTrivia();
|
||||
if (position >= input.length()) {
|
||||
throw error("Expected a value.");
|
||||
}
|
||||
|
||||
char current = input.charAt(position);
|
||||
if (current == '{') {
|
||||
return parseObject();
|
||||
}
|
||||
if (current == '[') {
|
||||
return parseArray();
|
||||
}
|
||||
if (current == '"') {
|
||||
return parseString();
|
||||
}
|
||||
if (current == 't') {
|
||||
return parseLiteral("true", Boolean.TRUE);
|
||||
}
|
||||
if (current == 'f') {
|
||||
return parseLiteral("false", Boolean.FALSE);
|
||||
}
|
||||
if (current == 'n') {
|
||||
return parseLiteral("null", null);
|
||||
}
|
||||
if (current == '-' || isDigit(current)) {
|
||||
return parseNumber();
|
||||
}
|
||||
throw error("Expected a JSON value.");
|
||||
}
|
||||
|
||||
private Map<String, Object> parseObject() {
|
||||
position++;
|
||||
Map<String, Object> object = new LinkedHashMap<String, Object>();
|
||||
skipTrivia();
|
||||
if (consume('}')) {
|
||||
return object;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
skipTrivia();
|
||||
if (position >= input.length() || input.charAt(position) != '"') {
|
||||
throw error("Expected a quoted object key.");
|
||||
}
|
||||
|
||||
String key = parseString();
|
||||
skipTrivia();
|
||||
expect(':');
|
||||
object.put(key, parseValue());
|
||||
skipTrivia();
|
||||
|
||||
if (consume('}')) {
|
||||
return object;
|
||||
}
|
||||
expect(',');
|
||||
skipTrivia();
|
||||
if (consume('}')) {
|
||||
return object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Object> parseArray() {
|
||||
position++;
|
||||
List<Object> array = new ArrayList<Object>();
|
||||
skipTrivia();
|
||||
if (consume(']')) {
|
||||
return array;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
array.add(parseValue());
|
||||
skipTrivia();
|
||||
if (consume(']')) {
|
||||
return array;
|
||||
}
|
||||
expect(',');
|
||||
skipTrivia();
|
||||
if (consume(']')) {
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String parseString() {
|
||||
expect('"');
|
||||
StringBuilder value = new StringBuilder();
|
||||
while (position < input.length()) {
|
||||
char current = input.charAt(position++);
|
||||
if (current == '"') {
|
||||
return value.toString();
|
||||
}
|
||||
if (current < 0x20) {
|
||||
throw error("Unescaped control character in string.");
|
||||
}
|
||||
if (current != '\\') {
|
||||
value.append(current);
|
||||
continue;
|
||||
}
|
||||
if (position >= input.length()) {
|
||||
throw error("Unterminated escape sequence.");
|
||||
}
|
||||
|
||||
char escape = input.charAt(position++);
|
||||
switch (escape) {
|
||||
case '"':
|
||||
case '\\':
|
||||
case '/':
|
||||
value.append(escape);
|
||||
break;
|
||||
case 'b':
|
||||
value.append('\b');
|
||||
break;
|
||||
case 'f':
|
||||
value.append('\f');
|
||||
break;
|
||||
case 'n':
|
||||
value.append('\n');
|
||||
break;
|
||||
case 'r':
|
||||
value.append('\r');
|
||||
break;
|
||||
case 't':
|
||||
value.append('\t');
|
||||
break;
|
||||
case 'u':
|
||||
value.append(parseUnicodeEscape());
|
||||
break;
|
||||
default:
|
||||
throw error("Invalid string escape.");
|
||||
}
|
||||
}
|
||||
throw error("Unterminated string.");
|
||||
}
|
||||
|
||||
private char parseUnicodeEscape() {
|
||||
if (position + 4 > input.length()) {
|
||||
throw error("Incomplete Unicode escape.");
|
||||
}
|
||||
int value = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int digit = Character.digit(input.charAt(position++), 16);
|
||||
if (digit < 0) {
|
||||
throw error("Invalid Unicode escape.");
|
||||
}
|
||||
value = value * 16 + digit;
|
||||
}
|
||||
return (char) value;
|
||||
}
|
||||
|
||||
private Object parseNumber() {
|
||||
int start = position;
|
||||
consume('-');
|
||||
if (consume('0')) {
|
||||
if (position < input.length() && isDigit(input.charAt(position))) {
|
||||
throw error("Leading zero in number.");
|
||||
}
|
||||
} else {
|
||||
consumeDigits();
|
||||
}
|
||||
|
||||
boolean decimal = false;
|
||||
if (consume('.')) {
|
||||
decimal = true;
|
||||
if (!consumeDigits()) {
|
||||
throw error("Expected digits after decimal point.");
|
||||
}
|
||||
}
|
||||
if (position < input.length()
|
||||
&& (input.charAt(position) == 'e' || input.charAt(position) == 'E')) {
|
||||
decimal = true;
|
||||
position++;
|
||||
if (position < input.length()
|
||||
&& (input.charAt(position) == '+' || input.charAt(position) == '-')) {
|
||||
position++;
|
||||
}
|
||||
if (!consumeDigits()) {
|
||||
throw error("Expected digits in exponent.");
|
||||
}
|
||||
}
|
||||
|
||||
String number = input.substring(start, position);
|
||||
try {
|
||||
if (decimal) {
|
||||
return new BigDecimal(number);
|
||||
}
|
||||
BigInteger integer = new BigInteger(number);
|
||||
if (integer.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0
|
||||
&& integer.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0) {
|
||||
return integer.intValue();
|
||||
}
|
||||
if (integer.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) >= 0
|
||||
&& integer.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) {
|
||||
return integer.longValue();
|
||||
}
|
||||
return integer;
|
||||
} catch (NumberFormatException exception) {
|
||||
throw error("Invalid number.");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean consumeDigits() {
|
||||
int start = position;
|
||||
while (position < input.length() && isDigit(input.charAt(position))) {
|
||||
position++;
|
||||
}
|
||||
return position > start;
|
||||
}
|
||||
|
||||
private Object parseLiteral(String literal, Object value) {
|
||||
if (!input.startsWith(literal, position)) {
|
||||
throw error("Invalid JSON literal.");
|
||||
}
|
||||
position += literal.length();
|
||||
return value;
|
||||
}
|
||||
|
||||
private void skipTrivia() {
|
||||
while (position < input.length()) {
|
||||
char current = input.charAt(position);
|
||||
if (Character.isWhitespace(current)) {
|
||||
position++;
|
||||
} else if (current == '/' && position + 1 < input.length()
|
||||
&& input.charAt(position + 1) == '/') {
|
||||
position += 2;
|
||||
while (position < input.length()
|
||||
&& input.charAt(position) != '\n'
|
||||
&& input.charAt(position) != '\r') {
|
||||
position++;
|
||||
}
|
||||
} else if (current == '/' && position + 1 < input.length()
|
||||
&& input.charAt(position + 1) == '*') {
|
||||
position += 2;
|
||||
int end = input.indexOf("*/", position);
|
||||
if (end < 0) {
|
||||
throw error("Unterminated block comment.");
|
||||
}
|
||||
position = end + 2;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void expect(char expected) {
|
||||
if (!consume(expected)) {
|
||||
throw error("Expected '" + expected + "'.");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean consume(char expected) {
|
||||
if (position < input.length() && input.charAt(position) == expected) {
|
||||
position++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isDigit(char value) {
|
||||
return value >= '0' && value <= '9';
|
||||
}
|
||||
|
||||
IllegalArgumentException error(String message) {
|
||||
return new IllegalArgumentException(message + " (at character " + position + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue