This commit is contained in:
pro.boooooo
2023-01-15 20:37:28 +01:00
parent 068f0f3b50
commit 3175d93380
16 changed files with 168 additions and 179 deletions

View File

@@ -0,0 +1,9 @@
package fr.sae.JSonInspector.Exception;
public class JsonSyntaxException extends Throwable {
private static final String MESSAGE = "Syntax error in JSON file";
public JsonSyntaxException() {
super(MESSAGE);
}
}

View File

@@ -0,0 +1,40 @@
package fr.sae.JSonInspector.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class ArrayObjectListener implements MouseListener {
private final Line line;
private final Frame frame;
public ArrayObjectListener(Line line, Frame frame) {
this.line = line;
this.frame = frame;
}
@Override
public void mouseClicked(MouseEvent e) {
if (line.isShow()) {
line.retreat();
} else {
line.unfold();
}
frame.repaintFile();
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}

View File

@@ -0,0 +1,149 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Exception.JsonSyntaxException;
import fr.sae.JSonInspector.Main;
import fr.sae.JSonInspector.Settings.Parameters;
import fr.sae.JSonInspector.Storage.Tree;
import javax.swing.*;
import java.awt.*;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
public class Frame extends JFrame {
private static final Dimension DEFAULT_FRAME_SIZE = new Dimension(800, 600);
private static final Dimension MINIMUM_FRAME_SIZE = new Dimension(600, 500);
private final CardLayout cards = new CardLayout();
private boolean showTab = true;
private JPanel secondCard;
private GraphicFile file;
private Tree tree;
public Frame() {
super("Inspecteur JSON");
init();
this.setVisible(true);
}
private void init() {
this.setSize(DEFAULT_FRAME_SIZE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setMinimumSize(MINIMUM_FRAME_SIZE);
this.setLayout(cards);
this.add(firstCard());
// this.add(secondCard());
cards.first(this.getContentPane());
}
private JPanel firstCard() {
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
JTextField textField = new JTextField("file:C:\\Users\\Elève\\Desktop\\temp\\jason.json", 30);
JButton button = new JButton("Valider");
button.addActionListener((event) -> validationAction(textField));
JPanel panel = new JPanel();
panel.setLayout(layout);
gbc.insets = new Insets(10, 10, 10, 10);
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel("URL :");
label.setForeground(Parameters.DEFAULT_TEXT_COLOR);
panel.add(label, gbc);
gbc.gridx = 1;
panel.add(textField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
panel.add(button, gbc);
panel.setBackground(Parameters.IHM_COLOR);
return panel;
}
private JPanel secondCard() {
file = new GraphicFile(this, tree);
return initSecondCard(file);
}
private JPanel secondCard(GraphicFile file) {
this.file = file;
return initSecondCard(file);
}
private JPanel initSecondCard(GraphicFile file) {
JButton unfoldButton = new JButton("Tout déplier");
JButton retreatButton = new JButton("Tout replier");
JButton backButton = new JButton("Retour");
JPanel mainPanel = new JPanel(), southPanel = new JPanel();
JScrollPane scroll = new JScrollPane();
mainPanel.setLayout(new BorderLayout());
southPanel.setBackground(Parameters.IHM_COLOR);
southPanel.add(backButton);
backButton.addActionListener((event) -> backAction(mainPanel));
southPanel.add(unfoldButton);
unfoldButton.addActionListener((event) -> unfoldAction());
southPanel.add(retreatButton);
retreatButton.addActionListener((event) -> retreatAction());
southPanel.add(new JButton("convertir en PHP"));
mainPanel.add(file);
mainPanel.add(southPanel, BorderLayout.SOUTH);
mainPanel.add(scroll);
scroll.setViewportView(file);
return mainPanel;
}
private void unfoldAction() {
file.showAll();
repaintFile();
}
private void retreatAction() {
file.retreatAll();
repaintFile();
}
public void repaintFile() {
file = new GraphicFile(this, file.getLines());
this.remove(secondCard);
secondCard = secondCard(file);
this.add(secondCard);
cards.last(this.getContentPane());
}
private void validationAction(JTextField field) {
try {
URL url = new URL(field.getText());
String file = Main.getJsonInOneLine(url);
if (file.length() <= 2) {
throw new FileNotFoundException();
}
tree = new Tree(file);
secondCard = secondCard();
this.add(secondCard);
cards.last(this.getContentPane());
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(this, "URL invalide", "Error", JOptionPane.ERROR_MESSAGE);
} catch (JsonSyntaxException j) {
JOptionPane.showMessageDialog(this, "Erreur de syntax dans le fichier", "Error", JOptionPane.ERROR_MESSAGE);
} catch (FileNotFoundException f) {
JOptionPane.showMessageDialog(this, "Impossible trouver le fichier", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void backAction(JPanel panel) {
this.remove(panel);
cards.first(this.getContentPane());
}
}

View File

@@ -0,0 +1,275 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Settings.Parameters;
import fr.sae.JSonInspector.Storage.Node;
import fr.sae.JSonInspector.Storage.Tree;
import fr.sae.JSonInspector.Storage.Type;
import fr.sae.JSonInspector.Storage.Value;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class GraphicFile extends JPanel {
private final GridBagConstraints gbc = new GridBagConstraints();
private final JPanel alignementPanel = new JPanel();
private final Frame frame;
private ArrayList<Line> lines;
private Node firstNode;
public GraphicFile(Frame frame, Tree tree) {
super();
firstNode = tree.getFirstNode();
init();
lines = new ArrayList<>();
this.frame = frame;
createFileRecursive(firstNode, 0, false);
// displayAllLines();
displayLines();
}
public GraphicFile(Frame frame, ArrayList<Line> lines) {
super();
init();
this.frame = frame;
this.lines = lines;
// displayAllLines();
displayLines();
}
private void init() {
this.setBackground(Parameters.BACKGROUND_COLOR);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.WEST;
alignementPanel.setLayout(new GridBagLayout());
this.add(alignementPanel);
}
private void createFileRecursive(Node node, int depth, boolean virgule) {
String indentation = "";
for (int i = 0; i < depth; i++) {
indentation += Parameters.IHM_INDENTATION;
}
if (node.isObject() || node.isElement()) {
createObjectElement(node, depth, indentation, virgule);
} else if (node.isArray()) {
createArray(node, depth, indentation, virgule);
} else if (node.isPair()) {
createPair(node, depth, indentation, virgule);
}
}
private void createPair(Node node, int depth, String indentation, boolean virgule) {
Line line = new Line(node, depth);
line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": ");
if (node.getSize() != 0) {
createValue(line, node.get(0));
} else {
line.add("null", Parameters.OTHER_COLOR);
}
if (virgule) {
line.add(",");
}
lines.add(line);
}
private void createObjectElement(Node node, int depth, String indentation, boolean virgule) {
Line line = new Line(node, depth);
line.add(indentation);
if (0 < depth && 0 < node.getSize()) {
line.retreat();
}
if (node.getType() == Type.ELEMENT) {
line.add("{");
} else {
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": {");
}
if (node.getSize() == 0) {
if (virgule) {
line.add(" },");
} else {
line.add(" }");
}
lines.add(line);
} else {
line.addMouseListener(new ArrayObjectListener(line, frame));
lines.add(line);
callNextNodes(node, depth);
Line endLine = new Line(node, indentation + "}", depth);
endLine.setClosingElement();
if (virgule) {
endLine.add(",");
}
lines.add(endLine);
}
}
private void createArray(Node node, int depth, String indentation, boolean virgule) {
Line line = new Line(node, depth);
line.add(indentation);
if (0 < depth && 0 < node.getSize()) {
line.retreat();
}
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": [");
if (node.getSize() == 0) {
if (virgule) {
line.add(" ],");
} else {
line.add(" ]");
}
lines.add(line);
} else {
line.addMouseListener(new ArrayObjectListener(line, frame));
lines.add(line);
for (int i = 0; i < node.getSize(); i++) {
Line valueLine = new Line(new Node("", Type.NULL), depth + 1);
if (node.get(i).isNode()) {
callNextNodes(node, depth);
} else {
String valueString = indentation + Parameters.IHM_INDENTATION;
valueLine.add(valueString, Parameters.STRING_COLOR);
createValue(valueLine, node.get(i));
if (i != node.getSize() - 1) {
valueLine.add(",");
}
}
lines.add(valueLine);
}
Line endLine = new Line(node, indentation + "]", depth);
endLine.setClosingElement();
if (virgule) {
endLine.add(",");
}
lines.add(endLine);
}
}
private void createValue(Line line, Value value) {
if (value.isNumber()) {
line.add("" + value.getValue(), Parameters.NUMBER_COLOR);
} else if (value.isString()) {
line.add("\"" + value.getValue() + "\"", Parameters.STRING_COLOR);
} else {
line.add("" + value.getValue(), Parameters.OTHER_COLOR);
}
}
private void callNextNodes(Node node, int depth) {
boolean virgule;
for (int i = 0; i < node.getSize(); i++) {
// si l'élément afficher est le dernier à son niveau "virgule" est faux
// donc il n'y aura pas de virgule en fin ligne
virgule = i != node.getSize() - 1;
if (node.get(i).isNode()) {
createFileRecursive((Node) node.get(i).getValue(), depth + 1, virgule);
}
}
}
private void displayLines() {
boolean inArrayObject = false, array, object;
Node openedArrayObject = lines.get(0).getNode();
removeAllClosingLabel();
for (int i = 0; i < lines.size(); i++) {
if (!inArrayObject) {
array = lines.get(i).getNode().isArray();
object = lines.get(i).getNode().isObject();
// Vérifie si le noeud est du type ARRAY ou du type OBJECT et s'il doit être
// affiché
if ((array || object) && !lines.get(i).isShow()) {
inArrayObject = true;
openedArrayObject = lines.get(i).getNode();
if (openedArrayObject.isArray()) {
displayOneHidedLine(i, Parameters.ARRAY_CLOSING);
} else {
displayOneHidedLine(i, Parameters.OBJECT_ELEMENT_CLOSING);
}
// Sinon affiche la ligne normalement
} else {
displayOneLine(i);
}
} else if (lines.get(i).getNode().equals(openedArrayObject)) {
inArrayObject = false;
}
}
}
private void displayOneLine(int index) {
gbc.gridy = index;
gbc.gridx = 0;
alignementPanel.add(lines.get(index), gbc);
}
private void displayOneHidedLine(int index, String endOfLine) {
gbc.gridy = index;
gbc.gridx = 0;
lines.get(index).add(endOfLine);
alignementPanel.add(lines.get(index), gbc);
}
public void showAll() {
for (Line line : lines) {
if (!line.isShow()) {
line.removeClosingLabel();
}
line.unfold();
}
}
public void retreatAll() {
for (Line line : lines) {
if (line.getNode().isArrayObjectElement()) {
if (0 < line.getDepth() && 0 < line.getNode().getSize() && !line.isClosingElement()) {
line.retreat();
}
}
}
}
private void removeAllClosingLabel() {
for (Line line : lines) {
line.removeClosingLabel();
}
}
public ArrayList<Line> getLines() {
return lines;
}
}

View File

@@ -0,0 +1,83 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Graphics.MyJLabel;
import fr.sae.JSonInspector.Graphics.MyJPanel;
import fr.sae.JSonInspector.Storage.Node;
import java.awt.*;
public class Line extends MyJPanel {
private boolean show = true;
private final int depth;
private final Node node;
private MyJLabel lastElement;
private boolean closingElement = false;
public Line(Node node, int depth) {
super();
this.node = node;
this.depth = depth;
}
public Line(Node node, String str, int depth) {
super();
this.add(new MyJLabel(str));
this.node = node;
this.depth = depth;
}
public Line(Node node, String str, Color color, int depth) {
super();
this.add(new MyJLabel(str, color));
this.node = node;
this.depth = depth;
}
public void add(String string) {
lastElement = new MyJLabel(string);
this.add(lastElement);
}
public void add(String string, Color color) {
lastElement = new MyJLabel(string, color);
this.add(lastElement);
}
public Node getNode() {
return node;
}
public int getDepth() {
return depth;
}
public boolean isShow() {
return show;
}
public void unfold() {
show = true;
}
public void retreat() {
show = false;
}
public void setClosingElement() {
closingElement = true;
}
public boolean isClosingElement() {
return closingElement;
}
public void removeClosingLabel() {
try {
if (lastElement.getText().equals("...]") || lastElement.getText().equals("...}")) {
this.remove(lastElement);
}
} catch (NullPointerException e) {
// System.out.println("La ligne est vide");
}
}
}

View File

@@ -0,0 +1,20 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Settings.Parameters;
import javax.swing.*;
import java.awt.*;
public class MyJLabel extends JLabel {
public MyJLabel(String text, Color color) {
super(text);
// this.setFont(Parameters.FILE_FONT);
this.setForeground(color);
}
public MyJLabel(String text) {
super(text);
// this.setFont(Parameters.FILE_FONT);
this.setForeground(Parameters.DEFAULT_TEXT_COLOR);
}
}

View File

@@ -0,0 +1,26 @@
package fr.sae.JSonInspector.Graphics;
import fr.sae.JSonInspector.Settings.Parameters;
import javax.swing.*;
import java.awt.*;
public class MyJPanel extends JPanel {
public MyJPanel(Color color) {
super();
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.setBackground(color);
}
public MyJPanel() {
super();
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.setBackground(Parameters.BACKGROUND_COLOR);
}
public MyJPanel(boolean isTransparent) {
super();
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.setOpaque(isTransparent);
}
}

View File

@@ -0,0 +1,86 @@
package fr.sae.JSonInspector;
import fr.sae.JSonInspector.Exception.JsonSyntaxException;
import fr.sae.JSonInspector.Graphics.Frame;
import fr.sae.JSonInspector.Storage.Tree;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
public static void main(String[] args) {
if (args.length == 0) {
new Frame();
} else {
try {
URL url = new URL(args[0]);
String file = getJsonInOneLine(url);
System.out.println(new Tree(file));
} catch (MalformedURLException e) {
System.err.println("Invalid URL");
System.exit(1);
} catch (JsonSyntaxException j) {
System.err.println("Invalid syntax in file");
System.exit(1);
}
}
}
public static String getJsonInOneLine(URL f) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader buff = new BufferedReader(new InputStreamReader(f.openStream()));
String line;
while ((line = buff.readLine()) != null) {
sb.append(line);
}
buff.close();
} catch (IOException e) {
System.err.println("[!] Probleme lors de l'ouverture du fichier");
}
return sb.toString();
}
public static String cleanFile(String file) {
char[] chars = file.toCharArray();
boolean inString = false, inValue = false;
String cleanedFile = "";
for (char currentChar : chars) {
if (!inString && !inValue) {
if (currentChar == '"') {
inString = true;
cleanedFile += currentChar;
} else if (isJsonSyntax(currentChar)) {
cleanedFile += currentChar;
}
} else if (currentChar == '"') {
inString = false;
cleanedFile += currentChar;
} else if (currentChar == ':') {
inValue = true;
cleanedFile += currentChar;
} else {
cleanedFile += currentChar;
}
}
return cleanedFile;
}
private static boolean isJsonSyntax(char character) {
boolean openingArray = character == '[', closingArray = character == ']';
boolean openingObject = character == '{', closingObject = character == '}';
boolean virgule = character == ',', deuxPoints = character == ':';
if (openingArray || closingArray || openingObject || closingObject || virgule || deuxPoints) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,20 @@
package fr.sae.JSonInspector.Settings;
import java.awt.*;
public class Parameters {
public static final Font FILE_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 18);
public static final String IHM_INDENTATION = " ";
public static final String CONSOLE_INDENTATION = " ";
public static final String ARRAY_CLOSING = "...]";
public static final String OBJECT_ELEMENT_CLOSING = "...}";
public static final Color IHM_COLOR = new Color(70, 70, 70);
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 MOUSE_OVER_COLOR = new Color(60, 60, 60);
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

@@ -0,0 +1,100 @@
package fr.sae.JSonInspector.Storage;
import java.util.ArrayList;
public class Node {
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;
}
}
private void findType(String value) {
if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
values.add(new Value<String>(Tree.cleanOpeningExpression(value)));
return;
}
try {
values.add(new Value<Integer>(Integer.parseInt(value)));
} catch (NumberFormatException nfeInt) {
try {
values.add(new Value<Double>(Double.parseDouble(value)));
} catch (NumberFormatException nfeDouble) {
values.add(new Value<Other>(new Other(value)));
}
}
}
public <T> void add(T value) {
if (value != null) {
if (value.getClass().equals(Node.class)) {
values.add(new Value<T>(value));
} else {
findType((String) value);
}
}
}
public Type getType() {
return type;
}
public String getName() {
return name;
}
public Value get(int index) {
return values.get(index);
}
public int getSize() {
return values.size();
}
public boolean isObject() {
return type == Type.OBJECT;
}
public boolean isArray() {
return type == Type.ARRAY;
}
public boolean isElement() {
return type == Type.ELEMENT;
}
public boolean isPair() {
return type == Type.PAIR;
}
public boolean isArrayObjectElement() {
boolean array = type == Type.ARRAY;
boolean object = type == Type.OBJECT;
boolean element = type == Type.ELEMENT;
if (array || object || element) {
return true;
}
return false;
}
@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,14 @@
package fr.sae.JSonInspector.Storage;
public class Other {
String value;
public Other(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}

View File

@@ -0,0 +1,279 @@
package fr.sae.JSonInspector.Storage;
import fr.sae.JSonInspector.Exception.JsonSyntaxException;
import fr.sae.JSonInspector.Settings.Parameters;
import java.util.ArrayList;
public class Tree {
private Node firstNode;
public Tree(String file) throws JsonSyntaxException {
firstNode = parseElement(file);
}
private Node whichType(String element) throws JsonSyntaxException {
String[] keyValue = splitKeyValue(element);
if (keyValue.length == 2) {
String key = keyValue[0], value = keyValue[1];
if (0 < value.length()) {
if (value.charAt(0) == '[' && value.charAt(value.length() - 1) == ']') {
return parseArray(key, value);
} else if (value.charAt(0) == '{' && value.charAt(value.length() - 1) == '}') {
return parseObject(key, value);
} else {
return parsePair(key, value);
}
} else {
return null;
}
} else if (keyValue[0].equals("")) {
if (keyValue[0].charAt(0) == '{' && keyValue[0].charAt(keyValue[0].length() - 1) == '}') {
return parseElement(keyValue[0]);
} else {
throw new JsonSyntaxException();
}
} else {
throw new JsonSyntaxException();
}
}
private Node parseArray(String name, String rawValues) throws JsonSyntaxException {
Node array = new Node(cleanOpeningExpression(name), Type.ARRAY);
ArrayList<String> elements = splitList(rawValues);
for (String value : elements) {
if (0 < value.length()) {
if (value.charAt(0) == '{') {
if (value.charAt(value.length() - 1) == '}') {
array.add(parseElement(value));
} else {
throw new JsonSyntaxException();
}
} else {
array.add(value);
}
}
}
return array;
}
private Node parseObject(String name, String rawValues) throws JsonSyntaxException {
Node object = new Node(cleanOpeningExpression(name), Type.OBJECT);
ArrayList<String> elements = splitList(rawValues);
for (String value : elements) {
object.add(whichType(value));
}
return object;
}
private Node parsePair(String name, String value) {
Node pair = new Node(cleanOpeningExpression(name), Type.PAIR);
pair.add(value);
return pair;
}
private Node parseElement(String rawValues) throws JsonSyntaxException {
Node element = new Node("", Type.ELEMENT);
ArrayList<String> elements = splitList(rawValues);
for (String value : elements) {
element.add(whichType(value));
}
return element;
}
public static String cleanOpeningExpression(String strToClean) {
StringBuilder cleanedString = new StringBuilder(strToClean);
cleanedString.deleteCharAt(0);
cleanedString.deleteCharAt(cleanedString.length() - 1);
return cleanedString.toString();
}
private ArrayList<String> splitList(String listToSplit) {
char[] chars = cleanOpeningExpression(listToSplit).toCharArray();
int depth = 0;
ArrayList<String> elements = new ArrayList<>();
String buffer = "";
for (char currentChar : chars) {
if (currentChar == ',' && depth == 0) {
elements.add(buffer);
buffer = "";
} else if (currentChar == '{' || currentChar == '[') {
depth += 1;
buffer += currentChar;
} else if (currentChar == '}' || currentChar == ']') {
depth -= 1;
buffer += currentChar;
} else {
buffer += currentChar;
}
}
elements.add(buffer);
return elements;
}
private String[] splitKeyValue(String expressionToSplit) {
boolean inKey = true;
char[] chars = expressionToSplit.toCharArray();
String key = "", value, buffer = "";
for (char currentChar : chars) {
if (inKey) {
if (currentChar == '{' || currentChar == ':') {
key = buffer;
buffer = "";
if (currentChar == '{') {
buffer += currentChar;
}
inKey = false;
} else {
buffer += currentChar;
}
} else {
buffer += currentChar;
}
}
value = buffer;
return new String[] { key, value };
}
public static String printTree(Node node, int depth) {
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.CONSOLE_INDENTATION;
}
if (!node.isElement()) {
line += indentation + "\"" + node.getName() + "\"";
} else {
line += indentation + node.getName();
}
if (node.isObject() || node.isElement()) {
line += printObjectElement(node, depth, indentation);
} else if (node.isPair()) {
line += printPair(node);
} else if (node.isArray()) {
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);
}
if (i != node.getSize() - 1) {
line += ",";
}
}
return line;
}
private static String printPair(Node node) {
String line = "";
if (node.getSize() != 0) {
if (node.get(0).isString()) {
line += ": \"" + node.get(0).getValue() + "\"";
} else {
line += ": " + node.get(0).getValue();
}
} else {
line += ": null";
}
return line;
}
private static String printObjectElement(Node node, int depth, String indentation) {
String line = "";
if (node.getType() == Type.ELEMENT) {
line += "{";
} else {
line += ": {";
}
if (node.getSize() == 0) {
line += "}";
} else {
line += callNextNodes(node, depth);
line += "\n" + indentation + "}";
}
return line;
}
private static String printArray(Node node, int depth, String indentation) {
String line = "";
line += ": [";
if (node.getSize() == 0) {
line += "]";
} else {
// Cette boucle parcours les valeurs du tableau
for (int i = 0; i < node.getSize(); i++) {
// si la valeur a l'indice i n'est pas une valeur brute alors
// on appelle de manière récursive la fonction d'affichage de l'arbre
if (node.get(i).isNode()) {
line += "\n" + printTree((Node) node.get(i).getValue(), depth + 1);
} else {
line += "\n" + indentation + Parameters.CONSOLE_INDENTATION;
if (node.get(i).isString()) {
line += "\"" + node.get(i).getValue() + "\"";
} else {
line += node.get(i).getValue();
}
}
// si la valeur n'est pas la dernière alors on lui ajoute une virgule
if (i != node.getSize() - 1) {
line += ",";
}
}
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 fr.sae.JSonInspector.Storage;
public enum Type {
OBJECT, ELEMENT, ARRAY, PAIR, NULL
}

View File

@@ -0,0 +1,38 @@
package fr.sae.JSonInspector.Storage;
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);
}
}