This commit is contained in:
pro.boooooo
2023-01-15 20:37:28 +01:00
parent 068f0f3b50
commit 3175d93380
16 changed files with 168 additions and 179 deletions

View File

@@ -0,0 +1,100 @@
package fr.sae.JSonInspector.Storage;
import java.util.ArrayList;
public class Node {
private final ArrayList<Value> values = new ArrayList<>();
private final String name;
private final Type type;
public Node(String name, Type type) {
this.type = type;
if (type == Type.ELEMENT) {
this.name = "";
} else {
this.name = name;
}
}
private void findType(String value) {
if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
values.add(new Value<String>(Tree.cleanOpeningExpression(value)));
return;
}
try {
values.add(new Value<Integer>(Integer.parseInt(value)));
} catch (NumberFormatException nfeInt) {
try {
values.add(new Value<Double>(Double.parseDouble(value)));
} catch (NumberFormatException nfeDouble) {
values.add(new Value<Other>(new Other(value)));
}
}
}
public <T> void add(T value) {
if (value != null) {
if (value.getClass().equals(Node.class)) {
values.add(new Value<T>(value));
} else {
findType((String) value);
}
}
}
public Type getType() {
return type;
}
public String getName() {
return name;
}
public Value get(int index) {
return values.get(index);
}
public int getSize() {
return values.size();
}
public boolean isObject() {
return type == Type.OBJECT;
}
public boolean isArray() {
return type == Type.ARRAY;
}
public boolean isElement() {
return type == Type.ELEMENT;
}
public boolean isPair() {
return type == Type.PAIR;
}
public boolean isArrayObjectElement() {
boolean array = type == Type.ARRAY;
boolean object = type == Type.OBJECT;
boolean element = type == Type.ELEMENT;
if (array || object || element) {
return true;
}
return false;
}
@Override
public String toString() {
String string = name + " : ";
for (int i = 0; i < values.size(); i++) {
string += values.get(i) + ", ";
}
return string;
}
}

View File

@@ -0,0 +1,14 @@
package fr.sae.JSonInspector.Storage;
public class Other {
String value;
public Other(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}

View File

@@ -0,0 +1,279 @@
package fr.sae.JSonInspector.Storage;
import fr.sae.JSonInspector.Exception.JsonSyntaxException;
import fr.sae.JSonInspector.Settings.Parameters;
import java.util.ArrayList;
public class Tree {
private Node firstNode;
public Tree(String file) throws JsonSyntaxException {
firstNode = parseElement(file);
}
private Node whichType(String element) throws JsonSyntaxException {
String[] keyValue = splitKeyValue(element);
if (keyValue.length == 2) {
String key = keyValue[0], value = keyValue[1];
if (0 < value.length()) {
if (value.charAt(0) == '[' && value.charAt(value.length() - 1) == ']') {
return parseArray(key, value);
} else if (value.charAt(0) == '{' && value.charAt(value.length() - 1) == '}') {
return parseObject(key, value);
} else {
return parsePair(key, value);
}
} else {
return null;
}
} else if (keyValue[0].equals("")) {
if (keyValue[0].charAt(0) == '{' && keyValue[0].charAt(keyValue[0].length() - 1) == '}') {
return parseElement(keyValue[0]);
} else {
throw new JsonSyntaxException();
}
} else {
throw new JsonSyntaxException();
}
}
private Node parseArray(String name, String rawValues) throws JsonSyntaxException {
Node array = new Node(cleanOpeningExpression(name), Type.ARRAY);
ArrayList<String> elements = splitList(rawValues);
for (String value : elements) {
if (0 < value.length()) {
if (value.charAt(0) == '{') {
if (value.charAt(value.length() - 1) == '}') {
array.add(parseElement(value));
} else {
throw new JsonSyntaxException();
}
} else {
array.add(value);
}
}
}
return array;
}
private Node parseObject(String name, String rawValues) throws JsonSyntaxException {
Node object = new Node(cleanOpeningExpression(name), Type.OBJECT);
ArrayList<String> elements = splitList(rawValues);
for (String value : elements) {
object.add(whichType(value));
}
return object;
}
private Node parsePair(String name, String value) {
Node pair = new Node(cleanOpeningExpression(name), Type.PAIR);
pair.add(value);
return pair;
}
private Node parseElement(String rawValues) throws JsonSyntaxException {
Node element = new Node("", Type.ELEMENT);
ArrayList<String> elements = splitList(rawValues);
for (String value : elements) {
element.add(whichType(value));
}
return element;
}
public static String cleanOpeningExpression(String strToClean) {
StringBuilder cleanedString = new StringBuilder(strToClean);
cleanedString.deleteCharAt(0);
cleanedString.deleteCharAt(cleanedString.length() - 1);
return cleanedString.toString();
}
private ArrayList<String> splitList(String listToSplit) {
char[] chars = cleanOpeningExpression(listToSplit).toCharArray();
int depth = 0;
ArrayList<String> elements = new ArrayList<>();
String buffer = "";
for (char currentChar : chars) {
if (currentChar == ',' && depth == 0) {
elements.add(buffer);
buffer = "";
} else if (currentChar == '{' || currentChar == '[') {
depth += 1;
buffer += currentChar;
} else if (currentChar == '}' || currentChar == ']') {
depth -= 1;
buffer += currentChar;
} else {
buffer += currentChar;
}
}
elements.add(buffer);
return elements;
}
private String[] splitKeyValue(String expressionToSplit) {
boolean inKey = true;
char[] chars = expressionToSplit.toCharArray();
String key = "", value, buffer = "";
for (char currentChar : chars) {
if (inKey) {
if (currentChar == '{' || currentChar == ':') {
key = buffer;
buffer = "";
if (currentChar == '{') {
buffer += currentChar;
}
inKey = false;
} else {
buffer += currentChar;
}
} else {
buffer += currentChar;
}
}
value = buffer;
return new String[] { key, value };
}
public static String printTree(Node node, int depth) {
String line = "", indentation = "";
// créé l'indentation de la bonne taille en fonction de la
// profondeur dans l'arbre
for (int i = 0; i < depth; i++) {
indentation += Parameters.CONSOLE_INDENTATION;
}
if (!node.isElement()) {
line += indentation + "\"" + node.getName() + "\"";
} else {
line += indentation + node.getName();
}
if (node.isObject() || node.isElement()) {
line += printObjectElement(node, depth, indentation);
} else if (node.isPair()) {
line += printPair(node);
} else if (node.isArray()) {
line += printArray(node, depth, indentation);
}
return line;
}
private static String callNextNodes(Node node, int depth) {
String line = "";
for (int i = 0; i < node.getSize(); i++) {
if (node.get(i).isNode()) {
line += "\n" + printTree((Node) node.get(i).getValue(), depth + 1);
}
if (i != node.getSize() - 1) {
line += ",";
}
}
return line;
}
private static String printPair(Node node) {
String line = "";
if (node.getSize() != 0) {
if (node.get(0).isString()) {
line += ": \"" + node.get(0).getValue() + "\"";
} else {
line += ": " + node.get(0).getValue();
}
} else {
line += ": null";
}
return line;
}
private static String printObjectElement(Node node, int depth, String indentation) {
String line = "";
if (node.getType() == Type.ELEMENT) {
line += "{";
} else {
line += ": {";
}
if (node.getSize() == 0) {
line += "}";
} else {
line += callNextNodes(node, depth);
line += "\n" + indentation + "}";
}
return line;
}
private static String printArray(Node node, int depth, String indentation) {
String line = "";
line += ": [";
if (node.getSize() == 0) {
line += "]";
} else {
// Cette boucle parcours les valeurs du tableau
for (int i = 0; i < node.getSize(); i++) {
// si la valeur a l'indice i n'est pas une valeur brute alors
// on appelle de manière récursive la fonction d'affichage de l'arbre
if (node.get(i).isNode()) {
line += "\n" + printTree((Node) node.get(i).getValue(), depth + 1);
} else {
line += "\n" + indentation + Parameters.CONSOLE_INDENTATION;
if (node.get(i).isString()) {
line += "\"" + node.get(i).getValue() + "\"";
} else {
line += node.get(i).getValue();
}
}
// si la valeur n'est pas la dernière alors on lui ajoute une virgule
if (i != node.getSize() - 1) {
line += ",";
}
}
line += "\n" + indentation + "]";
}
return line;
}
@Override
public String toString() {
return printTree(firstNode, 0);
}
public Node getFirstNode() {
return firstNode;
}
}

View File

@@ -0,0 +1,5 @@
package fr.sae.JSonInspector.Storage;
public enum Type {
OBJECT, ELEMENT, ARRAY, PAIR, NULL
}

View File

@@ -0,0 +1,38 @@
package fr.sae.JSonInspector.Storage;
public class Value<T> {
private T value;
public Value(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public boolean isObjectOrArray() {
if (value.getClass().equals(Node.class)) {
Node node = (Node) value;
if (node.getType() == Type.OBJECT || node.getType() == Type.ARRAY) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public boolean isNode() {
return value.getClass().equals(Node.class);
}
public boolean isString() {
return value.getClass().equals(String.class);
}
public boolean isNumber() {
return value.getClass().equals(Integer.class) || value.getClass().equals(Double.class);
}
}