From f5a1da67c539e7ab05a30cd78ad99ce7b5f70019 Mon Sep 17 00:00:00 2001 From: besson Date: Thu, 12 Jan 2023 21:57:55 +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/Node.java | 45 +++++++-------- src/JsonInspector/Parameters.java | 10 ++-- src/JsonInspector/Tree.java | 92 +++++++++++++++++++++---------- src/JsonInspector/Type.java | 2 +- src/JsonInspector/Value.java | 49 ++++++++++++++++ 5 files changed, 139 insertions(+), 59 deletions(-) create mode 100644 src/JsonInspector/Value.java diff --git a/src/JsonInspector/Node.java b/src/JsonInspector/Node.java index 2d719a7..8c71554 100644 --- a/src/JsonInspector/Node.java +++ b/src/JsonInspector/Node.java @@ -4,51 +4,46 @@ import java.util.ArrayList; public class Node { - private Type type; - private String name; - private ArrayList values = new ArrayList<>(); - private ArrayList nextNodes = new ArrayList<>(); + private final ArrayList values = new ArrayList<>(); + private final String name; + private final Type type; 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); + if (type == Type.ELEMENT) { + this.name = ""; + } else { + this.name = name; } } - public void addNode(Node next) { - if (type == Type.OBJECT || type == Type.ARRAY) { - nextNodes.add(next); - } + + public void add(T value) { + values.add(new Value(value)); } + 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; + public Value get(int index) { + return values.get(index); } + + public int getSize() { + return values.size(); + } + + @Override public String toString() { String string = name + " : "; diff --git a/src/JsonInspector/Parameters.java b/src/JsonInspector/Parameters.java index 7685657..34ec8a8 100644 --- a/src/JsonInspector/Parameters.java +++ b/src/JsonInspector/Parameters.java @@ -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); } diff --git a/src/JsonInspector/Tree.java b/src/JsonInspector/Tree.java index 8ecf7bd..91f02eb 100644 --- a/src/JsonInspector/Tree.java +++ b/src/JsonInspector/Tree.java @@ -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 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; + indentation += Parameters.CONSOLE_INDENTATION; } if (!node.getName().equals("")) { @@ -92,41 +89,78 @@ public class Tree { if (node.getType() == Type.OBJECT){ - if (node.getName().equals("")) { - line += "{"; - } else { - line += ": {"; - } + line += printObject(node, depth, indentation); + } else if (node.getType() == Type.PAIR){ - if (node.getValues().size() != 0) { - line += ": " + values.get(0); - } else { - line += ": null"; - } + line += printPair(node, depth, indentation); } else if (node.getType() == Type.ARRAY){ - line += ": [\n"; + line += printArray(node, depth, indentation); + } - 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); - } + 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); } } - for (int i = 0; i < nodes.size(); i++) { - line += "\n" + printTree(nodes.get(i), 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"; } - if (node.getType() == Type.OBJECT) { - line += "\n" + indentation + "}"; - } else if (node.getType() == Type.ARRAY) { - line += "\n" + indentation + "]"; + return line; + } + + + 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; } diff --git a/src/JsonInspector/Type.java b/src/JsonInspector/Type.java index c35287f..204cb67 100644 --- a/src/JsonInspector/Type.java +++ b/src/JsonInspector/Type.java @@ -1,5 +1,5 @@ package JsonInspector; public enum Type { - OBJECT, ARRAY, PAIR, NULL + OBJECT, ELEMENT, ARRAY, PAIR, NULL } diff --git a/src/JsonInspector/Value.java b/src/JsonInspector/Value.java new file mode 100644 index 0000000..6ef398d --- /dev/null +++ b/src/JsonInspector/Value.java @@ -0,0 +1,49 @@ +package JsonInspector; + +public class Value { + 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); + } +}