Transférer les fichiers vers 'src/JsonInspector'

This commit is contained in:
Romain BESSON 2023-01-12 21:57:55 +01:00
parent 6e6a6bceda
commit f5a1da67c5
5 changed files with 139 additions and 59 deletions

View File

@ -4,51 +4,46 @@ import java.util.ArrayList;
public class Node {
private Type type;
private String name;
private ArrayList<String> values = new ArrayList<>();
private ArrayList<Node> nextNodes = new ArrayList<>();
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;
}
public void addValue(String value) {
if (type == Type.PAIR) {
if (values.size() == 0) {
values.add(value);
} else {
values.set(0, value);
}
} else if (type == Type.ARRAY) {
values.add(value);
}
}
public void addNode(Node next) {
if (type == Type.OBJECT || type == Type.ARRAY) {
nextNodes.add(next);
}
public <T> void add(T value) {
values.add(new Value<T>(value));
}
public Type getType() {
return type;
}
public String getName() {
return name;
}
public ArrayList<Node> getNodes() {return nextNodes;}
public ArrayList<String> getValues() {return values;}
public void setType(Type type) {
this.type = type;
public Value get(int index) {
return values.get(index);
}
public int getSize() {
return values.size();
}
@Override
public String toString() {
String string = name + " : ";

View File

@ -4,12 +4,14 @@ import java.awt.*;
public class Parameters {
public static final Font FILE_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 18);
public static final String INDENTATION = " ";
public static final String IHM_INDENTATION = " ";
public static final String CONSOLE_INDENTATION = " ";
public static final Color IHM_COLOR = new Color(70, 70, 70);
public static final Color KEY_COLOR = new Color(200, 40, 100);
public static final Color NULL_COLOR = new Color(250, 112, 32);
public static final Color VALUE_COLOR = new Color(113, 20, 252);
public static final Color KEY_COLOR = new Color(70, 189, 204);
public static final Color OTHER_COLOR = new Color(7, 103, 183);
public static final Color STRING_COLOR = new Color(203, 109, 80);
public static final Color NUMBER_COLOR = new Color(133, 192, 95);
public static final Color DEFAULT_TEXT_COLOR = new Color(220, 220, 220);
public static final Color BACKGROUND_COLOR = new Color(45, 45, 45);
}

View File

@ -66,22 +66,19 @@ public class Tree {
for (int i = 0; i < nextFiles.size(); i++) {
currentNode.addNode(buildAST(names.get(i), nextFiles.get(i)));
currentNode.add(buildAST(names.get(i), nextFiles.get(i)));
}
return currentNode;
}
public static String printTree(Node node, int depth) {
ArrayList<Node> nodes = node.getNodes();
ArrayList<String> values = node.getValues();
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.INDENTATION;
indentation += Parameters.CONSOLE_INDENTATION;
}
if (!node.getName().equals("")) {
@ -92,40 +89,77 @@ public class Tree {
if (node.getType() == Type.OBJECT){
line += printObject(node, depth, indentation);
} else if (node.getType() == Type.PAIR){
line += printPair(node, depth, indentation);
} else if (node.getType() == Type.ARRAY){
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);
}
}
return line;
}
private static String printPair(Node node, int depth, String indentation) {
String line = "";
if (node.getSize() != 0) {
line += ": " + node.get(0);
} else {
line += ": null";
}
return line;
}
private static String printObject(Node node, int depth, String indentation) {
String line = "";
if (node.getName().equals("")) {
line += "{";
} else {
line += ": {";
}
} else if (node.getType() == Type.PAIR){
if (node.getValues().size() != 0) {
line += ": " + values.get(0);
} else {
line += ": null";
line += callNextNodes(node, depth);
line += "\n" + indentation + "}";
return line;
}
} else if (node.getType() == Type.ARRAY){
private static String printArray(Node node, int depth, String indentation) {
String line = "";
line += ": [\n";
for (int i = 0; i < values.size(); i++) {
for (int i = 0; i < node.getSize(); i++) {
//si la valeur a l'indice i est la dernière alors pas de virgule
if (i != values.size()-1) {
line += indentation + Parameters.INDENTATION + values.get(i) + ",\n";
if (i != node.getSize()-1) {
line += indentation + Parameters.CONSOLE_INDENTATION + node.get(i).getValue() + ",\n";
} else {
line += indentation + Parameters.INDENTATION + values.get(i);
}
line += indentation + Parameters.CONSOLE_INDENTATION + node.get(i).getValue();
}
}
for (int i = 0; i < nodes.size(); i++) {
line += "\n" + printTree(nodes.get(i), depth + 1);
}
if (node.getType() == Type.OBJECT) {
line += "\n" + indentation + "}";
} else if (node.getType() == Type.ARRAY) {
line += callNextNodes(node, depth);
line += "\n" + indentation + "]";
}
return line;
}

View File

@ -1,5 +1,5 @@
package JsonInspector;
public enum Type {
OBJECT, ARRAY, PAIR, NULL
OBJECT, ELEMENT, ARRAY, PAIR, NULL
}

View File

@ -0,0 +1,49 @@
package JsonInspector;
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);
}
public boolean isBoolean() {
return value.getClass().equals(Boolean.class);
}
}