Transférer les fichiers vers 'src/JsonInspector'

This commit is contained in:
Romain BESSON 2023-01-12 09:46:13 +01:00
parent 9ebcf76aa6
commit 338501d52b
5 changed files with 250 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package JsonInspector;
import javax.swing.*;
import java.awt.*;
public class MyJPanel extends JPanel {
public MyJPanel(Color color) {
super();
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setBackground(color);
}
public MyJPanel() {
super();
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//this.setBackground(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
this.setBackground(Parameters.BACKGROUND_COLOR);
}
public MyJPanel(boolean isTransparent) {
super();
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setOpaque(isTransparent);
}
}

View File

@ -0,0 +1,62 @@
package JsonInspector;
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<>();
public Node(String name, Type type) {
this.type = type;
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 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;
}
@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,15 @@
package JsonInspector;
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 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 DEFAULT_TEXT_COLOR = new Color(220, 220, 220);
public static final Color BACKGROUND_COLOR = new Color(45, 45, 45);
}

143
src/JsonInspector/Tree.java Normal file
View File

@ -0,0 +1,143 @@
package JsonInspector;
import java.util.ArrayList;
public class Tree {
private Node firstNode;
public Tree(String file) {
firstNode = buildAST("", file);
}
private Node buildAST(String name, String file) {
char[] chars = file.toCharArray();
int indentLevel = 0;
boolean inString = false;
Node currentNode = new Node("", Type.NULL);
ArrayList<String> nextFiles = new ArrayList<>(), names = new ArrayList<>();
String currentFile = "", currentString = "";
for (char currentChar : chars) {
if (!inString) {
if (currentChar == '{') {
if (indentLevel == 0) {
currentNode = new Node(name, Type.OBJECT);
currentString = "";
} else {
currentFile += currentChar;
}
indentLevel += 1;
} else if (currentChar == '[') {
if (indentLevel == 0) {
currentNode = new Node(name, Type.ARRAY);
currentString = "";
} else {
currentFile += currentChar;
}
indentLevel += 1;
} else if (currentChar == '}' || currentChar == ']') {
if (indentLevel == 2) {
nextFiles.add(currentFile);
names.add(currentString);
currentFile = "";
currentString = "";
} else if (indentLevel == 1) {
break;
} else {
currentFile += currentChar;
}
indentLevel -= 1;
} else if (currentChar == '"') {
inString = true;
} else {
//nextFile += chars[i];
}
} else if (currentChar == '"') {
inString = false;
} else {
currentString += currentChar;
}
}
for (int i = 0; i < nextFiles.size(); i++) {
currentNode.addNode(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;
}
if (!node.getName().equals("")) {
line += indentation + "\"" + node.getName() + "\"";
} else {
line += indentation + node.getName();
}
if (node.getType() == Type.OBJECT){
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";
}
} else if (node.getType() == Type.ARRAY){
line += ": [\n";
for (int i = 0; i < values.size(); 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";
} else {
line += indentation + Parameters.INDENTATION + values.get(i);
}
}
}
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 += "\n" + indentation + "]";
}
return line;
}
@Override
public String toString() {
return printTree(firstNode, 0);
}
public Node getFirstNode() {
return firstNode;
}
}

View File

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