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

View File

@ -4,12 +4,14 @@ import java.awt.*;
public class Parameters { public class Parameters {
public static final Font FILE_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 18); 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 IHM_COLOR = new Color(70, 70, 70);
public static final Color KEY_COLOR = new Color(200, 40, 100); public static final Color KEY_COLOR = new Color(70, 189, 204);
public static final Color NULL_COLOR = new Color(250, 112, 32); public static final Color OTHER_COLOR = new Color(7, 103, 183);
public static final Color VALUE_COLOR = new Color(113, 20, 252); 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 DEFAULT_TEXT_COLOR = new Color(220, 220, 220);
public static final Color BACKGROUND_COLOR = new Color(45, 45, 45); 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++) { 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; return currentNode;
} }
public static String printTree(Node node, int depth) { public static String printTree(Node node, int depth) {
ArrayList<Node> nodes = node.getNodes();
ArrayList<String> values = node.getValues();
String line = "", indentation = ""; String line = "", indentation = "";
//créé l'indentation de la bonne taille en fonction de la //créé l'indentation de la bonne taille en fonction de la
//profondeur dans l'arbre //profondeur dans l'arbre
for (int i = 0; i < depth; i++) { for (int i = 0; i < depth; i++) {
indentation += Parameters.INDENTATION; indentation += Parameters.CONSOLE_INDENTATION;
} }
if (!node.getName().equals("")) { if (!node.getName().equals("")) {
@ -92,41 +89,78 @@ public class Tree {
if (node.getType() == Type.OBJECT){ if (node.getType() == Type.OBJECT){
if (node.getName().equals("")) { line += printObject(node, depth, indentation);
line += "{";
} else {
line += ": {";
}
} else if (node.getType() == Type.PAIR){ } else if (node.getType() == Type.PAIR){
if (node.getValues().size() != 0) { line += printPair(node, depth, indentation);
line += ": " + values.get(0);
} else {
line += ": null";
}
} else if (node.getType() == Type.ARRAY){ } else if (node.getType() == Type.ARRAY){
line += ": [\n"; line += printArray(node, depth, indentation);
}
for (int i = 0; i < values.size(); i++) { return line;
//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";
} else { private static String callNextNodes(Node node, int depth) {
line += indentation + Parameters.INDENTATION + values.get(i); 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);
} }
} }
for (int i = 0; i < nodes.size(); i++) { return line;
line += "\n" + printTree(nodes.get(i), depth + 1); }
private static String printPair(Node node, int depth, String indentation) {
String line = "";
if (node.getSize() != 0) {
line += ": " + node.get(0);
} else {
line += ": null";
} }
if (node.getType() == Type.OBJECT) { return line;
line += "\n" + indentation + "}"; }
} else if (node.getType() == Type.ARRAY) {
line += "\n" + indentation + "]";
private static String printObject(Node node, int depth, String indentation) {
String line = "";
if (node.getName().equals("")) {
line += "{";
} else {
line += ": {";
} }
line += callNextNodes(node, depth);
line += "\n" + indentation + "}";
return line;
}
private static String printArray(Node node, int depth, String indentation) {
String line = "";
line += ": [\n";
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 != node.getSize()-1) {
line += indentation + Parameters.CONSOLE_INDENTATION + node.get(i).getValue() + ",\n";
} else {
line += indentation + Parameters.CONSOLE_INDENTATION + node.get(i).getValue();
}
}
line += callNextNodes(node, depth);
line += "\n" + indentation + "]";
return line; return line;
} }

View File

@ -1,5 +1,5 @@
package JsonInspector; package JsonInspector;
public enum Type { 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);
}
}