From 338501d52bb2a0ab27763b17b9fb39de7cac3b2b Mon Sep 17 00:00:00 2001 From: besson Date: Thu, 12 Jan 2023 09:46:13 +0100 Subject: [PATCH] =?UTF-8?q?Transf=C3=A9rer=20les=20fichiers=20vers=20'src/?= =?UTF-8?q?JsonInspector'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JsonInspector/MyJPanel.java | 25 ++++++ src/JsonInspector/Node.java | 62 +++++++++++++ src/JsonInspector/Parameters.java | 15 ++++ src/JsonInspector/Tree.java | 143 ++++++++++++++++++++++++++++++ src/JsonInspector/Type.java | 5 ++ 5 files changed, 250 insertions(+) create mode 100644 src/JsonInspector/MyJPanel.java create mode 100644 src/JsonInspector/Node.java create mode 100644 src/JsonInspector/Parameters.java create mode 100644 src/JsonInspector/Tree.java create mode 100644 src/JsonInspector/Type.java diff --git a/src/JsonInspector/MyJPanel.java b/src/JsonInspector/MyJPanel.java new file mode 100644 index 0000000..58edbac --- /dev/null +++ b/src/JsonInspector/MyJPanel.java @@ -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); + } +} diff --git a/src/JsonInspector/Node.java b/src/JsonInspector/Node.java new file mode 100644 index 0000000..2d719a7 --- /dev/null +++ b/src/JsonInspector/Node.java @@ -0,0 +1,62 @@ +package JsonInspector; + +import java.util.ArrayList; + + +public class Node { + private Type type; + private String name; + private ArrayList values = new ArrayList<>(); + private ArrayList 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 getNodes() {return nextNodes;} + + public ArrayList 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; + } +} diff --git a/src/JsonInspector/Parameters.java b/src/JsonInspector/Parameters.java new file mode 100644 index 0000000..7685657 --- /dev/null +++ b/src/JsonInspector/Parameters.java @@ -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); +} diff --git a/src/JsonInspector/Tree.java b/src/JsonInspector/Tree.java new file mode 100644 index 0000000..8ecf7bd --- /dev/null +++ b/src/JsonInspector/Tree.java @@ -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 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 nodes = node.getNodes(); + ArrayList 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; + } +} diff --git a/src/JsonInspector/Type.java b/src/JsonInspector/Type.java new file mode 100644 index 0000000..c35287f --- /dev/null +++ b/src/JsonInspector/Type.java @@ -0,0 +1,5 @@ +package JsonInspector; + +public enum Type { + OBJECT, ARRAY, PAIR, NULL +}