diff --git a/build/Console/DisplayConsole.class b/build/Console/DisplayConsole.class deleted file mode 100644 index 4700b3c..0000000 Binary files a/build/Console/DisplayConsole.class and /dev/null differ diff --git a/build/Core.class b/build/Core.class deleted file mode 100644 index 06e49f4..0000000 Binary files a/build/Core.class and /dev/null differ diff --git a/build/Graphics/GraphicFile.class b/build/Graphics/GraphicFile.class deleted file mode 100644 index ef767ed..0000000 Binary files a/build/Graphics/GraphicFile.class and /dev/null differ diff --git a/build/Graphics/GraphicsCore.class b/build/Graphics/GraphicsCore.class deleted file mode 100644 index 28de1b9..0000000 Binary files a/build/Graphics/GraphicsCore.class and /dev/null differ diff --git a/build/Graphics/Traitable.class b/build/Graphics/Traitable.class deleted file mode 100644 index 3fa9325..0000000 Binary files a/build/Graphics/Traitable.class and /dev/null differ diff --git a/build/Graphics/Type/Array.class b/build/Graphics/Type/Array.class deleted file mode 100644 index b877bac..0000000 Binary files a/build/Graphics/Type/Array.class and /dev/null differ diff --git a/build/Graphics/Type/Bool.class b/build/Graphics/Type/Bool.class deleted file mode 100644 index 25036eb..0000000 Binary files a/build/Graphics/Type/Bool.class and /dev/null differ diff --git a/build/Graphics/Type/Chaine.class b/build/Graphics/Type/Chaine.class deleted file mode 100644 index 929530c..0000000 Binary files a/build/Graphics/Type/Chaine.class and /dev/null differ diff --git a/build/Graphics/Type/Entier.class b/build/Graphics/Type/Entier.class deleted file mode 100644 index 2e85271..0000000 Binary files a/build/Graphics/Type/Entier.class and /dev/null differ diff --git a/build/Graphics/Type/Flottant.class b/build/Graphics/Type/Flottant.class deleted file mode 100644 index 7f77b55..0000000 Binary files a/build/Graphics/Type/Flottant.class and /dev/null differ diff --git a/build/Graphics/Type/Type.class b/build/Graphics/Type/Type.class deleted file mode 100644 index 28943c3..0000000 Binary files a/build/Graphics/Type/Type.class and /dev/null differ diff --git a/src/JsonInspector/ArrayObjectListener.java b/src/ArrayObjectListener.java similarity index 95% rename from src/JsonInspector/ArrayObjectListener.java rename to src/ArrayObjectListener.java index 9d9ff12..58b0db8 100644 --- a/src/JsonInspector/ArrayObjectListener.java +++ b/src/ArrayObjectListener.java @@ -1,37 +1,37 @@ -package JsonInspector; - -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) {} -} +package JsonInspector; + +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) {} +} diff --git a/src/Console/DisplayConsole.java b/src/Console/DisplayConsole.java deleted file mode 100644 index d5053cf..0000000 --- a/src/Console/DisplayConsole.java +++ /dev/null @@ -1,117 +0,0 @@ -package Console; - -import java.net.URL; -import java.io.InputStream; -import java.io.IOException; - -/** - * [Bilal] - * Afficher le code JSON dans la console une fois formatter - */ - -public class DisplayConsole { - private URL jsonFile; - - public DisplayConsole(URL jsonFile) { - this.jsonFile = jsonFile; - - if (this.Display() == null) { - System.out.println("[!] Probleme lors du formatage de : " + this.jsonFile.getFile()); - } else { - System.out.println(this.Display()); - } - } - - private String Display() { - try { - InputStream jsonReader = this.jsonFile.openStream(); - StringBuilder containerJsonFormatted = new StringBuilder(); - - int indentLevel = 0; - boolean currentlyInRecord = false; - - int cursor = jsonReader.read(); - - while (cursor != -1) { - char c = (char) cursor; - - switch (c) { - case '{': { - containerJsonFormatted.append(c); - - if (!currentlyInRecord) { - containerJsonFormatted.append("\n"); - indentLevel++; - this.addIndentation(containerJsonFormatted, indentLevel); - } - break; - } - case '[': { - containerJsonFormatted.append(c); - if (!currentlyInRecord) { - containerJsonFormatted.append("\n"); - indentLevel++; - this.addIndentation(containerJsonFormatted, indentLevel); - } - break; - } - case '"': { - currentlyInRecord = !currentlyInRecord; - containerJsonFormatted.append(c); - break; - } - case ':': { - containerJsonFormatted.append(c).append(" "); - break; - } - case ',': { - containerJsonFormatted.append(c); - if (!currentlyInRecord) { - containerJsonFormatted.append("\n"); - this.addIndentation(containerJsonFormatted, indentLevel); - } - break; - } - case ']': { - if (!currentlyInRecord) { - containerJsonFormatted.append("\n"); - indentLevel--; - this.addIndentation(containerJsonFormatted, indentLevel); - } - - containerJsonFormatted.append(c); - break; - } - case '}': { - if (!currentlyInRecord) { - containerJsonFormatted.append("\n"); - indentLevel--; - this.addIndentation(containerJsonFormatted, indentLevel); - } - containerJsonFormatted.append(c); - break; - } - - default: { - containerJsonFormatted.append(c); - break; - } - } - - cursor = jsonReader.read(); - } - - jsonReader.close(); - return containerJsonFormatted.toString(); - } catch (IOException e) { - System.out.println("[!] Fichier " + this.jsonFile.getFile() + " n'existe pas"); - return null; - } - } - - private void addIndentation(StringBuilder sb, int indentLevel) { - for (int i = 0; i < indentLevel; i++) { - sb.append("\t"); - } - } -} diff --git a/src/Core.java b/src/Core.java deleted file mode 100644 index 5056a1a..0000000 --- a/src/Core.java +++ /dev/null @@ -1,24 +0,0 @@ -import java.io.File; -import Console.DisplayConsole; -import Graphics.GraphicsCore; - -/** - * [Bilal et Romain] - * Programme pour afficher un code json dans le teminal ou sur une JFrame (avec - * les options tel que: la coloration syntaxique, repli de tableau etc...) - */ -public class Core { - public static void main(String[] args) { - if (args.length == 1) { - try { - new DisplayConsole(new File(args[0]).toURI().toURL()); - } catch (Exception e) { - System.out.println(e); - } - } else if (args.length == 0) { - new GraphicsCore(); - } else { - System.out.println("[!] Utilisation: ./jsonFormatter "); - } - } -} \ No newline at end of file diff --git a/src/JsonInspector/CoreJSONDescriptor.java b/src/CoreJSONDescriptor.java similarity index 100% rename from src/JsonInspector/CoreJSONDescriptor.java rename to src/CoreJSONDescriptor.java diff --git a/src/JsonInspector/Frame.java b/src/Frame.java similarity index 96% rename from src/JsonInspector/Frame.java rename to src/Frame.java index cf203f0..978e180 100644 --- a/src/JsonInspector/Frame.java +++ b/src/Frame.java @@ -1,155 +1,155 @@ -package JsonInspector; - -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()); - } -} +package JsonInspector; + +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()); + } +} diff --git a/src/JsonInspector/GraphicFile.java b/src/GraphicFile.java similarity index 96% rename from src/JsonInspector/GraphicFile.java rename to src/GraphicFile.java index f6a84f0..2fd87a1 100644 --- a/src/JsonInspector/GraphicFile.java +++ b/src/GraphicFile.java @@ -1,284 +1,284 @@ -package JsonInspector; - -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 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 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 getLines() { - return lines; - } -} +package JsonInspector; + +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 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 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 getLines() { + return lines; + } +} diff --git a/src/Graphics/GraphicFile.java b/src/Graphics/GraphicFile.java deleted file mode 100644 index 3194caa..0000000 --- a/src/Graphics/GraphicFile.java +++ /dev/null @@ -1,89 +0,0 @@ -package Graphics; - -import java.io.InputStream; -import java.io.IOException; -import java.util.HashMap; -import javax.swing.JLabel; -import javax.swing.JPanel; -import Graphics.Type.*; -import java.awt.FlowLayout; -import java.awt.GridLayout; -import java.util.List; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Color; -import java.net.URL; - -/** - * [Romain] - * Pour gerer l'affichage graphique du code JSON - */ - -public class GraphicFile extends JPanel { - /** - * @param url Le chemin vers le fichier JSON - */ - public GraphicFile(URL url) { - super(); - try { - System.out.println("[+] Lecture de " + url); - this.setLayout(new FlowLayout(FlowLayout.LEFT)); - this.setLocation(500, 500); - - InputStream jsonReader = url.openStream(); - Traitable fileTraited = new Traitable(jsonReader); - jsonReader.close(); - - HashMap> allVariables = fileTraited.getVariableMap(); - JPanel core = new JPanel(new GridBagLayout()); - GridBagConstraints settings = new GridBagConstraints(); - settings.gridx = 0; - settings.gridy = 0; - settings.anchor = GridBagConstraints.WEST; - - core.add(new JLabel("{"), settings); - - int rows = 1; - settings.gridy = 1; - settings.gridx = 2; - - for (String key : allVariables.keySet()) { - JPanel fusion = new JPanel(new GridLayout(1, 1)); - - settings.gridy = rows; - - JLabel name = new JLabel("\"" + key + "\": "); - name.setForeground(new Color(163, 90, 0)); - fusion.add(name); - if (allVariables.get(key).getClass().getName() != "Graphics.Type.Array") { - JLabel value = new JLabel(allVariables.get(key).display()); - value.setForeground(allVariables.get(key).getColor()); - fusion.add(value); - } else { - JPanel speForArray = new JPanel(); - speForArray.add(new JLabel("[ ")); - for (int i = 0; i <= allVariables.get(key).listGet().size() - 1; i++) { - JLabel tmp = new JLabel(String.valueOf(allVariables.get(key).listGet().get(i).display())); - tmp.setForeground(allVariables.get(key).listGet().get(i).getColor()); - speForArray.add(tmp); - } - speForArray.add(new JLabel(" ]")); - fusion.add(speForArray); - } - - core.add(fusion, settings); - rows++; - } - - settings.gridx = 0; - settings.gridy = rows; - core.add(new JLabel("}"), settings); - - this.add(core); - - this.setVisible(true); - } catch (IOException e) { - System.out.println("[!] Fichier " + url.getFile() + " n'existe pas"); - } - } -} diff --git a/src/Graphics/GraphicsCore.java b/src/Graphics/GraphicsCore.java deleted file mode 100644 index b3609a1..0000000 --- a/src/Graphics/GraphicsCore.java +++ /dev/null @@ -1,172 +0,0 @@ -package Graphics; - -import java.io.File; -import java.net.MalformedURLException; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JButton; -import javax.swing.JLabel; -import javax.swing.JScrollPane; -import javax.swing.JOptionPane; -import javax.swing.JFileChooser; -import java.net.URL; -import java.awt.Dimension; -import java.awt.CardLayout; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; -import java.awt.BorderLayout; -import javax.swing.filechooser.FileSystemView; -import javax.swing.filechooser.FileNameExtensionFilter; - -/** - * [Romain] - * Faire le pont entre la selection du fichier JSON et l'affichage - * graphique du code JSON - */ - -public class GraphicsCore extends JFrame { - private final Dimension DEFAULT_FRAME_SIZE = new Dimension(800, 600); - private final Dimension MINIMUM_FRAME_SIZE = new Dimension(600, 500); - private final CardLayout cards; - private JLabel textField; - private URL url; - - public GraphicsCore() { - super("Inspecteur JSON - Romain & Bilal"); - - this.url = null; - this.textField = new JLabel("Clique ici pour choisir le chemin du fichier JSON ->"); - - this.cards = new CardLayout(); - - this.init(); - this.add(firstCard()); - cards.last(this.getContentPane()); - this.setVisible(true); - } - - /** - * Initalisation des parametres de la Frame par defaut. - */ - private void init() { - this.setSize(DEFAULT_FRAME_SIZE); - this.setDefaultCloseOperation(EXIT_ON_CLOSE); - this.setMinimumSize(MINIMUM_FRAME_SIZE); - this.setLayout(cards); - this.add(firstCard()); - cards.first(this.getContentPane()); - } - - /** - * Creation de la fenetre ou l'on nous demande de chemin absolut du fichier JSON - * - * @return Le JPanel de la premiere card du CardLayout - */ - private JPanel firstCard() { - GridBagLayout layout = new GridBagLayout(); - GridBagConstraints gbc = new GridBagConstraints(); - this.textField = new JLabel("Clique ici pour choisir le chemin du fichier JSON ->"); - - JButton button = new JButton("Valider"); - button.addActionListener((event) -> validationAction(this.textField.getText())); - - JButton selectedFile = new JButton("..."); - selectedFile.addActionListener((event) -> getPathOf()); - - JPanel panel = new JPanel(); - panel.setLayout(layout); - - gbc.insets = new Insets(10, 10, 10, 10); - gbc.gridx = 0; - gbc.gridy = 0; - panel.add(new JLabel("URL :"), gbc); - - gbc.gridx = 1; - panel.add(this.textField, gbc); - - gbc.gridx = 2; - panel.add(selectedFile, gbc); - - gbc.gridx = 1; - gbc.gridy = 1; - panel.add(button, gbc); - - return panel; - } - - /** - * Creation de la fenetre ou sera afficher le code du fichier JSON - * - * @return Le JPanel contenant le rendu de Traitable - * @see Graphics.Traitable - */ - private JPanel secondCard() { - GraphicFile file = new GraphicFile(this.url); - - JPanel mainPanel = new JPanel(), southPanel = new JPanel(); - JButton backButton = new JButton("Retour"); - backButton.addActionListener((event) -> backAction(mainPanel)); - JScrollPane scroll = new JScrollPane(); - mainPanel.setLayout(new BorderLayout()); - - southPanel.add(backButton); - southPanel.add(new JButton("Tout déplier")); - southPanel.add(new JButton("convertir en PHP")); - - mainPanel.add(file, BorderLayout.CENTER); - mainPanel.add(southPanel, BorderLayout.SOUTH); - mainPanel.add(scroll); - - scroll.setViewportView(file); - return mainPanel; - } - - /** - * Permet de la fenetre ou l'on nous demande de chemin absolut du fichier JSON - * - * @param field Le chemin absolue du fichier JSON - */ - private void validationAction(String field) { - try { - this.url = new File(field).toURI().toURL(); - this.add(secondCard()); - cards.last(this.getContentPane()); - } catch (MalformedURLException e) { - JOptionPane.showMessageDialog(this, "Invalid URL", "Error", JOptionPane.ERROR_MESSAGE); - } - } - - /** - * Retourner dans la selection du fichier JSON - * - * @param panel Le JPanel ou l'on demande a l'utilisateur de choisir le fichier - * JSON - */ - private void backAction(JPanel panel) { - this.remove(panel); - cards.first(this.getContentPane()); - } - - /** - * Selection du fichier JSON - */ - private void getPathOf() { - JFileChooser jc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); - - jc.setDialogTitle("Choissez le fichier json: "); - jc.setFileSelectionMode(JFileChooser.FILES_ONLY); - - FileNameExtensionFilter filter = new FileNameExtensionFilter("Fichier JSON", "json"); - jc.setFileFilter(filter); - - int res = jc.showOpenDialog(this); - - if (res == JFileChooser.APPROVE_OPTION) { - if (jc.getSelectedFile().isFile()) { - this.textField.setText(jc.getSelectedFile().getAbsolutePath()); - this.revalidate(); - } - } - } -} diff --git a/src/Graphics/Traitable.java b/src/Graphics/Traitable.java deleted file mode 100644 index e5a84d7..0000000 --- a/src/Graphics/Traitable.java +++ /dev/null @@ -1,221 +0,0 @@ -package Graphics; - -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.LinkedList; -import Graphics.Type.*; -import java.io.IOException; -import java.io.InputStream; - -/** - * [Bilal] - * Classe qui sert a stocke les valeurs contenue dans le JSON dans une liste. - */ - -public class Traitable { - private final HashMap contentRaw; - private final HashMap> content; - private final InputStream file; - - public Traitable(InputStream file) { - this.contentRaw = new LinkedHashMap<>(); - this.content = new LinkedHashMap<>(); - this.file = file; - this.Run(); - } - - /** - * Lancement automatique une fois que Traitable est instantie - * - * @see Graphics.GraphicFile - */ - private void Run() { - System.out.println("[+] Preparation..."); - - try { - try { - StringBuilder allJson = new StringBuilder(); - StringBuilder tmp = new StringBuilder(); - int i = 0; - - for (int cursor = this.file.read(); cursor != -1; cursor = this.file.read()) { - char c = (char) cursor; - allJson.append(c); - } - - allJson = this.ajustementVirguleEnd(allJson.toString()); - - while (i < allJson.length()) { - if (allJson.charAt(i) == '"') { - while (allJson.charAt(i) != ',') { - if (allJson.charAt(i) == '[') { - while (allJson.charAt(i) != ']') { - tmp.append(allJson.charAt(i)); - i++; - } - } else if (allJson.charAt(i) == '{') { - while (allJson.charAt(i) != '}') { - tmp.append(allJson.charAt(i)); - i++; - } - } else { - tmp.append(allJson.charAt(i)); - i++; - } - } - - String[] varInfo = this.getInfoOfRecord(tmp); - this.saveValue(varInfo[0], varInfo[1]); - tmp.setLength(0); - - i++; - } - - i++; - } - - } catch (StringIndexOutOfBoundsException ignore) { - } - } catch (IOException e) { - System.out.println("[!] Probleme lors de la lecture du fichier"); - } - } - - /** - * Enregistre dans la HashMap la valeur - * - * @param name Le nom de l'entree - * @param value La valeur de l'entree - * - * @see Graphics.Type.Type - * @see java.util.HashMap - */ - private void saveValue(String name, String value) { - String typeOfValue = this.getType(value); - - switch (typeOfValue) { - case "string": { - this.content.put(name, new Chaine(String.valueOf(value))); - break; - } - - case "int": { - this.content.put(name, new Entier(Integer.valueOf(value))); - break; - } - - case "double": { - this.content.put(name, new Flottant(Double.valueOf(value))); - break; - } - - case "boolean": { - this.content.put(name, new Bool(Boolean.valueOf(value))); - break; - } - - case "array": { - this.content.put(name, new Array(value)); - break; - } - } - } - - /** - * Sert a detecter le type d'une valeur - * - * @param value La chaine a evolue pour determiner son type - * @return Le type - */ - private String getType(String value) { - if (value.contains("{")) { - return "object"; - } else if (value.contains("[")) { - return "array"; - } else if (value.contains("true") || value.contains("false")) { - return "boolean"; - } else if (value.contains(".")) { - return "double"; - } else if (value.contains("null")) { - return "null"; - } else if (value.contains("\"")) { - return "string"; - } else { - return "int"; - } - } - - /** - * Recuperer le nom et la valeur divise sous forme de tableau - * - * @param sb La phrase a separer - * @return Un tableau { 0 = nom, 1 = value } - */ - private String[] getInfoOfRecord(StringBuilder sb) { - String[] info = sb.toString().split(":"); - info[0] = this.removeSpeChar(info[0]); - info[1] = this.removeFirstSpaceIfThere(info[1]); - - return info; - } - - /** - * Retourne une valeur JSON qui, si elle contient un espace au debut, le - * supprime - * - * @param str La valeur JSON - * @return La valeur JSON sans l'espace au debut si il y en a un - */ - private String removeFirstSpaceIfThere(String str) { - if (str.length() > 0 && str.charAt(0) == ' ') { - str = str.substring(1); - } - - return str; - } - - /** - * Sert a retirer le charactere guillemet - * - * @param str La phrase a soustraire le symbole guillemet - * @return Retourner une chaine sans le charactere guillemet - */ - private String removeSpeChar(String str) { - StringBuilder tmp = new StringBuilder(); - - for (int i = 0; i <= str.length() - 1; i++) { - if (str.charAt(i) != '"') { - tmp.append(str.charAt(i)); - } - } - - return tmp.toString(); - } - - /** - * Sert a rajouter une virgule a l'avant dernier charactre du fichier (pour ne - * pas skipper la derniere cle valeur) - * - * @param str Le fichier json en une ligne - * @return Le fichier avec la fameuse virgule - */ - private StringBuilder ajustementVirguleEnd(String str) { - int longueur = str.length(); - char avantDernierCaractere = str.charAt(longueur - 2); - String nouvelleChaine = str.substring(0, longueur - 2) + avantDernierCaractere + "," - + str.substring(longueur - 1); - - return new StringBuilder(nouvelleChaine); - } - - /** - * Recuperer le jeu de cles valeurs dans GrahicFile - * - * @see Graphics.GraphicFile - * @return Le jeu de cles valeurs - */ - public HashMap> getVariableMap() { - return this.content; - } -} \ No newline at end of file diff --git a/src/Graphics/Type/Array.java b/src/Graphics/Type/Array.java deleted file mode 100644 index 38ca715..0000000 --- a/src/Graphics/Type/Array.java +++ /dev/null @@ -1,114 +0,0 @@ -package Graphics.Type; - -import java.awt.Color; -import Graphics.Type.*; -import java.util.LinkedList; -import java.util.List; - -/** - * Representation d'un tableau d'object - */ - -public class Array implements Type>> { - private String valueRaw; - private List> value; - private Color color; - - public Array(String valueRaw) { - this.valueRaw = valueRaw.substring(1, valueRaw.length() - 1); - this.value = new LinkedList<>(); - - this.Run(); - } - - public void Run() { - String[] spliced = this.valueRaw.split(","); - List> list = new LinkedList<>(); - - for (int i = 0; i <= spliced.length - 1; i++) { - switch (this.getType(spliced[i])) { - case "string": { - list.add(new Chaine(String.valueOf(spliced[i]))); - break; - } - - case "int": { - list.add(new Entier(Integer.valueOf(spliced[i]))); - break; - } - - case "double": { - list.add(new Flottant(Double.valueOf(spliced[i]))); - break; - } - - case "boolean": { - list.add(new Bool(Boolean.valueOf(spliced[i]))); - break; - } - } - } - - this.value = list; - } - - public String getType(String value) { - if (value.contains("{")) { - return "object"; - } else if (value.contains("[")) { - return "array"; - } else if (value.contains("\"")) { - return "string"; - } else if (value.contains("true") || value.contains("false")) { - return "boolean"; - } else if (value.contains(".")) { - return "double"; - } else if (value.contains("null")) { - return "null"; - } else { - return "int"; - } - } - - public String getValueRaw() { - return this.valueRaw; - } - - @Override - public List> listGet() { - return this.value; - } - - @Override - public Color getColor() { - return this.color; - } - - @Override - public String display() { - StringBuilder str = new StringBuilder(); - - str.append("[ "); - - for (int i = 0; i <= this.value.size() - 1; i++) { - str.append(this.value.get(i).display() + ", "); - } - - str.deleteCharAt(str.length() - 1); - str.deleteCharAt(str.length() - 1); - - str.append(" ]"); - - return str.toString(); - } - - @Override - public String getType() { - return "array"; - } - - @Override - public List> getValue() { - return this.value; - } -} diff --git a/src/Graphics/Type/Bool.java b/src/Graphics/Type/Bool.java deleted file mode 100644 index df5b351..0000000 --- a/src/Graphics/Type/Bool.java +++ /dev/null @@ -1,47 +0,0 @@ -package Graphics.Type; - -import java.awt.Color; -import java.util.List; - -/** - * Representation du type boolean - */ - -public class Bool implements Type { - private Boolean value; - private Color color; - - public Bool(Boolean value) { - this.value = value; - if (value) { - this.color = Color.GREEN; - } else { - this.color = Color.RED; - } - } - - @Override - public List> listGet() { - return null; - } - - @Override - public Color getColor() { - return this.color; - } - - @Override - public String display() { - return String.valueOf(value); - } - - @Override - public String getType() { - return "boolean"; - } - - @Override - public Boolean getValue() { - return this.value; - } -} diff --git a/src/Graphics/Type/Chaine.java b/src/Graphics/Type/Chaine.java deleted file mode 100644 index e798ae9..0000000 --- a/src/Graphics/Type/Chaine.java +++ /dev/null @@ -1,43 +0,0 @@ -package Graphics.Type; - -import java.awt.Color; -import java.util.List; - -/** - * Representation du type string - */ - -public class Chaine implements Type { - private String value; - private Color color; - - public Chaine(String value) { - this.value = value; - this.color = Color.PINK; - } - - @Override - public List> listGet() { - return null; - } - - @Override - public Color getColor() { - return this.color; - } - - @Override - public String display() { - return String.valueOf(value); - } - - @Override - public String getType() { - return "string"; - } - - @Override - public String getValue() { - return this.value; - } -} diff --git a/src/Graphics/Type/Entier.java b/src/Graphics/Type/Entier.java deleted file mode 100644 index a00c6ab..0000000 --- a/src/Graphics/Type/Entier.java +++ /dev/null @@ -1,43 +0,0 @@ -package Graphics.Type; - -import java.awt.Color; -import java.util.List; - -/** - * Representation du type int - */ - -public class Entier implements Type { - public Integer value; - private Color color; - - public Entier(Integer value) { - this.value = value; - this.color = Color.BLUE; - } - - @Override - public List> listGet() { - return null; - } - - @Override - public Color getColor() { - return this.color; - } - - @Override - public String display() { - return String.valueOf(value); - } - - @Override - public String getType() { - return "int"; - } - - @Override - public Integer getValue() { - return this.value; - } -} diff --git a/src/Graphics/Type/Flottant.java b/src/Graphics/Type/Flottant.java deleted file mode 100644 index fc7fe89..0000000 --- a/src/Graphics/Type/Flottant.java +++ /dev/null @@ -1,43 +0,0 @@ -package Graphics.Type; - -import java.awt.Color; -import java.util.List; - -/** - * Representation du type double - */ - -public class Flottant implements Type { - private Double value; - private Color color; - - public Flottant(Double value) { - this.value = value; - this.color = Color.BLUE; - } - - @Override - public List> listGet() { - return null; - } - - @Override - public Color getColor() { - return this.color; - } - - @Override - public String display() { - return String.valueOf(value); - } - - @Override - public String getType() { - return "double"; - } - - @Override - public Double getValue() { - return this.value; - } -} \ No newline at end of file diff --git a/src/Graphics/Type/Type.java b/src/Graphics/Type/Type.java deleted file mode 100644 index f1c853c..0000000 --- a/src/Graphics/Type/Type.java +++ /dev/null @@ -1,36 +0,0 @@ -package Graphics.Type; - -import java.awt.Color; -import java.util.List; - -public interface Type { - /** - * Retourner le type de la variable - * - * @return le type en string - */ - public String getType(); - - /** - * - * @return - */ - public T getValue(); - - /** - * Recuperer la couleur de syntaxe d'un type - */ - public Color getColor(); - - /** - * Afficher la valeur / toutes les valeurs - */ - public String display(); - - /** - * UNIQUEMENT POUR Graphics.Type.Chaine - * - * @return La liste contenant les valeurs du tableau - */ - public List> listGet(); -} diff --git a/src/JsonInspector/JsonSyntaxException.java b/src/JsonSyntaxException.java similarity index 96% rename from src/JsonInspector/JsonSyntaxException.java rename to src/JsonSyntaxException.java index 46c274c..3fd2f7e 100644 --- a/src/JsonInspector/JsonSyntaxException.java +++ b/src/JsonSyntaxException.java @@ -1,9 +1,9 @@ -package JsonInspector; - -public class JsonSyntaxException extends Throwable { - private static final String MESSAGE = "Syntax error in JSON file"; - - public JsonSyntaxException() { - super(MESSAGE); - } -} +package JsonInspector; + +public class JsonSyntaxException extends Throwable { + private static final String MESSAGE = "Syntax error in JSON file"; + + public JsonSyntaxException() { + super(MESSAGE); + } +} diff --git a/src/JsonInspector/Line.java b/src/Line.java similarity index 95% rename from src/JsonInspector/Line.java rename to src/Line.java index 50d6f85..9ed41c1 100644 --- a/src/JsonInspector/Line.java +++ b/src/Line.java @@ -1,92 +1,92 @@ -package JsonInspector; - -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"); - } - } -} +package JsonInspector; + +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"); + } + } +} diff --git a/src/JsonInspector/Main.java b/src/Main.java similarity index 100% rename from src/JsonInspector/Main.java rename to src/Main.java diff --git a/src/JsonInspector/MyJLabel.java b/src/MyJLabel.java similarity index 95% rename from src/JsonInspector/MyJLabel.java rename to src/MyJLabel.java index 9618d3a..84589c8 100644 --- a/src/JsonInspector/MyJLabel.java +++ b/src/MyJLabel.java @@ -1,18 +1,18 @@ -package JsonInspector; - -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); - } -} +package JsonInspector; + +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); + } +} diff --git a/src/JsonInspector/MyJPanel.java b/src/MyJPanel.java similarity index 96% rename from src/JsonInspector/MyJPanel.java rename to src/MyJPanel.java index 05b53ad..e6ae9e1 100644 --- a/src/JsonInspector/MyJPanel.java +++ b/src/MyJPanel.java @@ -1,24 +1,24 @@ -package JsonInspector; - -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); - } -} +package JsonInspector; + +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); + } +} diff --git a/src/JsonInspector/Node.java b/src/Node.java similarity index 95% rename from src/JsonInspector/Node.java rename to src/Node.java index 94099cf..d705da8 100644 --- a/src/JsonInspector/Node.java +++ b/src/Node.java @@ -1,115 +1,115 @@ -package JsonInspector; - -import java.awt.*; -import java.util.ArrayList; - - -public class Node { - private final ArrayList 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(Tree.cleanOpeningExpression(value))); - return; - } - - try { - values.add(new Value(Integer.parseInt(value))); - } catch (NumberFormatException nfeInt) { - try { - values.add(new Value(Double.parseDouble(value))); - } catch (NumberFormatException nfeDouble) { - values.add(new Value(new Other(value))); - } - } - } - - - public void add(T value) { - if (value != null) { - if (value.getClass().equals(Node.class)) { - values.add(new Value(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; - } -} +package JsonInspector; + +import java.awt.*; +import java.util.ArrayList; + + +public class Node { + private final ArrayList 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(Tree.cleanOpeningExpression(value))); + return; + } + + try { + values.add(new Value(Integer.parseInt(value))); + } catch (NumberFormatException nfeInt) { + try { + values.add(new Value(Double.parseDouble(value))); + } catch (NumberFormatException nfeDouble) { + values.add(new Value(new Other(value))); + } + } + } + + + public void add(T value) { + if (value != null) { + if (value.getClass().equals(Node.class)) { + values.add(new Value(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; + } +} diff --git a/src/JsonInspector/Other.java b/src/Other.java similarity index 92% rename from src/JsonInspector/Other.java rename to src/Other.java index 5a73f4b..b7545a1 100644 --- a/src/JsonInspector/Other.java +++ b/src/Other.java @@ -1,16 +1,16 @@ -package JsonInspector; - -public class Other { - String value; - - - public Other(String value) { - this.value = value; - } - - - @Override - public String toString() { - return value; - } -} +package JsonInspector; + +public class Other { + String value; + + + public Other(String value) { + this.value = value; + } + + + @Override + public String toString() { + return value; + } +} diff --git a/src/JsonInspector/Parameters.java b/src/Parameters.java similarity index 97% rename from src/JsonInspector/Parameters.java rename to src/Parameters.java index 0f07e10..e8bab69 100644 --- a/src/JsonInspector/Parameters.java +++ b/src/Parameters.java @@ -1,20 +1,20 @@ -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 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); -} +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 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); +} diff --git a/src/JsonInspector/Tree.java b/src/Tree.java similarity index 96% rename from src/JsonInspector/Tree.java rename to src/Tree.java index 1580093..3f8b528 100644 --- a/src/JsonInspector/Tree.java +++ b/src/Tree.java @@ -1,293 +1,293 @@ -package JsonInspector; - -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 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 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 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 splitList(String listToSplit) { - char[] chars = cleanOpeningExpression(listToSplit).toCharArray(); - int depth = 0; - ArrayList 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; - } -} +package JsonInspector; + +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 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 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 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 splitList(String listToSplit) { + char[] chars = cleanOpeningExpression(listToSplit).toCharArray(); + int depth = 0; + ArrayList 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; + } +} diff --git a/src/JsonInspector/Type.java b/src/Type.java similarity index 94% rename from src/JsonInspector/Type.java rename to src/Type.java index 204cb67..ecd7ada 100644 --- a/src/JsonInspector/Type.java +++ b/src/Type.java @@ -1,5 +1,5 @@ -package JsonInspector; - -public enum Type { - OBJECT, ELEMENT, ARRAY, PAIR, NULL -} +package JsonInspector; + +public enum Type { + OBJECT, ELEMENT, ARRAY, PAIR, NULL +} diff --git a/src/JsonInspector/Value.java b/src/Value.java similarity index 95% rename from src/JsonInspector/Value.java rename to src/Value.java index c244c28..899ec48 100644 --- a/src/JsonInspector/Value.java +++ b/src/Value.java @@ -1,44 +1,44 @@ -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); - } -} +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); + } +} diff --git a/toFormat.json b/toFormat.json index 485ce75..40d1bbf 100644 --- a/toFormat.json +++ b/toFormat.json @@ -1 +1 @@ -{"couple": [true, "Onde"], "age": 19, "nationalite": "Franco-Algerienne", "enVie": true, "poid": 1.7} \ No newline at end of file +{"couple": [true, "Onde", ["F", 3]], "age": 19, "nationalite": "Franco-Algerienne", "enVie": true, "poid": 1.7} \ No newline at end of file