commit 99610d997500c4354cbf69d6f502aa1df70f9a35 Author: pro.boooooo Date: Thu Jan 5 03:22:24 2023 +0100 $ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..11e1d06 --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +# COMMANDES +JAVA = java +JAVAC = javac +JAVADOC = javadoc +OPTIONSDOCS = -d docs +OPTIONS = -d build -Xlint:unchecked -Xlint:deprecation +EXT = .java + +# CHEMINS +SRC = src/ +BUILD = build/ +DOCS = docs/ +CORE = Core +GraphicsPACKAGE = Graphics/ +ConsolePACKAGE = Console/ + +# POUR ALLER PLUS VITE (Core, package:Console, ) +ALL = $(SRC)*$(EXT) $(SRC)$(ConsolePACKAGE)*$(EXT) $(SRC)$(GraphicsPACKAGE)*$(EXT) + +# LE FICHIER JSON +JSON = toFormat.json + +.PHONY: console graphics clean docs + +console: + make clean + mkdir build && mkdir docs + $(JAVAC) $(OPTIONS) $(ALL) + cd build && $(JAVA) $(CORE) $(JSON) && cd .. + +graphics: + make clean + mkdir build && mkdir docs + $(JAVAC) $(OPTIONS) $(ALL) + cd build && $(JAVA) $(CORE) && cd .. + +clean: + rm -rf $(BUILD) && rm -rf $(DOCS) + +docs: + $(JAVADOC) $(OPTIONSDOCS) $(ALL) \ No newline at end of file diff --git a/build/Console/DisplayConsole.class b/build/Console/DisplayConsole.class new file mode 100644 index 0000000..561333c Binary files /dev/null and b/build/Console/DisplayConsole.class differ diff --git a/build/Core.class b/build/Core.class new file mode 100644 index 0000000..aad6669 Binary files /dev/null and b/build/Core.class differ diff --git a/build/Graphics/GraphicFile.class b/build/Graphics/GraphicFile.class new file mode 100644 index 0000000..25828c7 Binary files /dev/null and b/build/Graphics/GraphicFile.class differ diff --git a/build/Graphics/GraphicsCore.class b/build/Graphics/GraphicsCore.class new file mode 100644 index 0000000..4623192 Binary files /dev/null and b/build/Graphics/GraphicsCore.class differ diff --git a/build/Graphics/ListNodes.class b/build/Graphics/ListNodes.class new file mode 100644 index 0000000..5f613ca Binary files /dev/null and b/build/Graphics/ListNodes.class differ diff --git a/build/Graphics/Node.class b/build/Graphics/Node.class new file mode 100644 index 0000000..2a9840a Binary files /dev/null and b/build/Graphics/Node.class differ diff --git a/src/Console/DisplayConsole.java b/src/Console/DisplayConsole.java new file mode 100644 index 0000000..f8873c3 --- /dev/null +++ b/src/Console/DisplayConsole.java @@ -0,0 +1,112 @@ +package Console; + +import java.net.URL; +import java.io.InputStream; +import java.io.IOException; + +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()); + } + } + + public 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; + } + } + + public 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 new file mode 100644 index 0000000..d3c163b --- /dev/null +++ b/src/Core.java @@ -0,0 +1,19 @@ +import java.io.File; +import Console.DisplayConsole; +import Graphics.GraphicsCore; + +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/Graphics/GraphicFile.java b/src/Graphics/GraphicFile.java new file mode 100644 index 0000000..f358e2e --- /dev/null +++ b/src/Graphics/GraphicFile.java @@ -0,0 +1,32 @@ +package Graphics; + +import java.io.InputStream; +import java.io.IOException; +import javax.swing.JPanel; +import java.awt.GridLayout; +import java.net.URL; + +public class GraphicFile extends JPanel { + public GraphicFile(URL url) { + super(); + + try { + System.out.println("[+] Lecture de " + url); + this.setLayout(new GridLayout(100, 1)); + + InputStream jsonReader = url.openStream(); + + // ICI le code + + jsonReader.close(); + } catch (IOException e) { + System.out.println("[!] Fichier " + url.getFile() + " n'existe pas"); + } + } + + public void addIndentation(StringBuilder sb, int indentLevel) { + for (int i = 0; i < indentLevel; i++) { + sb.append("\t"); + } + } +} diff --git a/src/Graphics/GraphicsCore.java b/src/Graphics/GraphicsCore.java new file mode 100644 index 0000000..c9b06b1 --- /dev/null +++ b/src/Graphics/GraphicsCore.java @@ -0,0 +1,140 @@ +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; + +public class GraphicsCore extends JFrame { + private final Dimension DEFAULT_FRAME_SIZE; + private final Dimension MINIMUM_FRAME_SIZE; + private final CardLayout cards; + private JLabel textField; + private URL url; + + public GraphicsCore() { + super("Inspecteur JSON - Romain & Bilal"); + + this.DEFAULT_FRAME_SIZE = new Dimension(800, 600); + this.MINIMUM_FRAME_SIZE = new Dimension(600, 500); + 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); + } + + 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()); + } + + 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; + } + + 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; + } + + 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); + } + } + + private void backAction(JPanel panel) { + this.remove(panel); + cards.first(this.getContentPane()); + } + + public 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/ListNodes.java b/src/Graphics/ListNodes.java new file mode 100644 index 0000000..1d1cb3b --- /dev/null +++ b/src/Graphics/ListNodes.java @@ -0,0 +1,128 @@ +package Graphics; + +import java.util.ArrayList; +import java.util.Objects; + +public class ListNodes { + private Node begin; + + public ListNodes() { + this.begin = null; + } + + public void add(String name, String value) { + try { + if (this.begin == null) { + this.begin = new Node(name, value); + this.begin.addNext(new Node(name, value)); + } else { + Node cpy = this.begin; + + while (cpy.getNext() != null) { + cpy = cpy.getNext(); + } + + cpy.addNext(new Node(name, value)); + + System.out.println( + "[+] Noeud " + + cpy.getName() + " -> " + cpy.getValue().toString() + " ajouté ! " + + "(" + cpy.detectType(cpy.getValue().toString()) + ")"); + } + } catch (NullPointerException ignore) { + } + } + + public void addChild(String name, ArrayList an) { + try { + ArrayList finalList = new ArrayList<>(); + + for (int i = 0; i <= an.size() - 1; i++) { + finalList.add(new Node(null, an.get(i))); + } + + if (this.begin == null) { + this.begin = new Node(name, finalList); + this.begin.addNextChild(new Node(name, finalList)); + } else { + getBegin(name, finalList); + int i = 0; + + for (; i <= an.size() - 1; i++) { + System.out.print(an.get(i) + ", "); + } + + System.out.println("} (taille: " + i + ")"); + } + + } catch (NullPointerException ignore) { + } + } + + private void getBegin(String name, ArrayList finalList) { + Node cpy = this.begin; + + while (cpy.getNext() != null) { + cpy = cpy.getNext(); + } + + cpy.addNextChild(new Node(name, finalList)); + + System.out.print("[+] Noeud " + name + " -> { "); + } + + public void addChild(String name, ArrayList names, ArrayList values) { + try { + ArrayList finalList = new ArrayList<>(); + + for (int i = 0; i <= names.size() - 1; i++) { + finalList.add(new Node(names.get(i), values.get(i))); + } + + if (this.begin == null) { + this.begin = new Node(name, finalList); + this.begin.addNextChild(new Node(name, finalList)); + } else { + getBegin(name, finalList); + int i = 0; + + for (; i <= values.size() - 1; i++) { + System.out.print(names.get(i) + ": " + values.get(i) + ", "); + } + + System.out.println("} (taille: " + i + ")"); + } + + } catch (NullPointerException ignore) { + } + } + + public void displayNodes() { + Node cpy = this.begin; + + while (cpy.getNext() != null) { + cpy = cpy.getNext(); + if (!Objects.equals(cpy.getName().length(), 0)) { + System.out.println(cpy.toString()); + } + } + } + + public boolean isExist(String name) { + Node cpy = this.begin; + + while (cpy.getNext() != null) { + cpy = cpy.getNext(); + + if (Objects.equals(name, cpy.getName())) { + return true; + } + } + + return false; + } + + public boolean isEmpty() { + return this.begin == null || this.begin.getNext() == null; + } +} diff --git a/src/Graphics/Node.java b/src/Graphics/Node.java new file mode 100644 index 0000000..4692f2b --- /dev/null +++ b/src/Graphics/Node.java @@ -0,0 +1,143 @@ +package Graphics; + +import java.util.ArrayList; + +public class Node { + private Node next; + private final String name; + private Object value; + private final ArrayList childs; + + public Node() { + this.name = null; + this.next = null; + this.value = null; + this.childs = new ArrayList<>(); + } + + public Node(String name, ArrayList childs) { + this.name = name; + this.value = null; + this.next = null; + this.childs = childs; + } + + public Node(String name, String value) { + this.name = name; + this.value = value; + this.next = null; + this.childs = null; + } + + public void addNext(Node node) { + try { + switch (this.detectType((String) node.getValue())) { + case "int": { + node.setValue(Integer.valueOf(node.getValue().toString())); + break; + } + + case "boolean": { + node.setValue(Boolean.valueOf(node.getValue().toString())); + break; + } + + case "float": { + node.setValue(Float.valueOf(node.getValue().toString())); + break; + } + + case "char": { + node.setValue(node.getValue().toString().charAt(0)); + break; + } + + default: { + node.setValue(node.getValue().toString()); + break; + } + } + + this.next = node; + } catch (Exception e) { + System.err.println(e); + System.err.println("[!] Probleme lors de l'ajout du noeud : " + this.name + " vers " + node.getName()); + } + } + + public void addNextChild(Node node) { + try { + this.next = node; + } catch (Exception e) { + System.err.println(e); + System.err.println("[!] Probleme lors de l'ajout du noeud : " + this.name + " vers " + node.getName()); + } + } + + public boolean haveChild() { + return this.childs == null; + } + + public String detectType(String str) { + if (str == null) { + return "null"; + } + + if (str.matches("[0-9]+")) { + return "int"; + } else if (str.length() == 1) { + return "char"; + } else if (str.equals("true") || str.equals("false")) { + return "boolean"; + } else if (str.matches("[0-9]+\\.[0-9]+")) { + return "float"; + } else if (str.matches("[a-zA-Z]+")) { + return "string"; + } else if (str.matches("[a-zA-Z0-9]+")) { + return "hexadecimal"; + } else { + return "string"; + } + } + + public String getName() { + return this.name; + } + + public Object getValue() { + return this.value; + } + + public Node getNext() { + return this.next; + } + + public ArrayList getChilds() { + return this.childs; + } + + public void setValue(Object toSet) { + this.value = toSet; + } + + @Override + public String toString() { + if (this.childs == null) { + return "Node: " + this.name + " => " + this.value; + } else { + StringBuilder toReturn = new StringBuilder("Node: " + this.name + " => { "); + for (int i = 0; i <= this.childs.size() - 1; i++) { + if (this.childs.get(i).getName() != null) { + toReturn.append(this.childs.get(i).getName()).append(": ").append(this.childs.get(i).getValue()) + .append(", "); + } else { + toReturn.append(this.childs.get(i).getValue()).append(", "); + } + } + + toReturn.append("} "); + + return toReturn.toString(); + } + } +} diff --git a/toFormat.json b/toFormat.json new file mode 100644 index 0000000..dd859bf --- /dev/null +++ b/toFormat.json @@ -0,0 +1 @@ +{"ecole":"I.U.T Senart-Fontainebleau","eleves":["Romain Besson","Bilal Boudjemline"],"classe":"BUT Informatique","matiere":{"mat1":"SCR", "mat2":"Java"}} \ No newline at end of file