This commit is contained in:
pro.boooooo 2023-01-15 20:08:18 +01:00
parent b7cd8d1d37
commit 068f0f3b50
38 changed files with 1113 additions and 2062 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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");
}
}
}

View File

@ -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 <path>");
}
}
}

View File

@ -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<String, Type<?>> 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");
}
}
}

View File

@ -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();
}
}
}
}

View File

@ -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<String, String> contentRaw;
private final HashMap<String, Type<?>> 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<String, Type<?>> getVariableMap() {
return this.content;
}
}

View File

@ -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<List<Type<?>>> {
private String valueRaw;
private List<Type<?>> 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<Type<?>> 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<Type<?>> 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<Type<?>> getValue() {
return this.value;
}
}

View File

@ -1,47 +0,0 @@
package Graphics.Type;
import java.awt.Color;
import java.util.List;
/**
* Representation du type boolean
*/
public class Bool implements Type<Boolean> {
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<Type<?>> 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;
}
}

View File

@ -1,43 +0,0 @@
package Graphics.Type;
import java.awt.Color;
import java.util.List;
/**
* Representation du type string
*/
public class Chaine implements Type<String> {
private String value;
private Color color;
public Chaine(String value) {
this.value = value;
this.color = Color.PINK;
}
@Override
public List<Type<?>> 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;
}
}

View File

@ -1,43 +0,0 @@
package Graphics.Type;
import java.awt.Color;
import java.util.List;
/**
* Representation du type int
*/
public class Entier implements Type<Integer> {
public Integer value;
private Color color;
public Entier(Integer value) {
this.value = value;
this.color = Color.BLUE;
}
@Override
public List<Type<?>> 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;
}
}

View File

@ -1,43 +0,0 @@
package Graphics.Type;
import java.awt.Color;
import java.util.List;
/**
* Representation du type double
*/
public class Flottant implements Type<Double> {
private Double value;
private Color color;
public Flottant(Double value) {
this.value = value;
this.color = Color.BLUE;
}
@Override
public List<Type<?>> 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;
}
}

View File

@ -1,36 +0,0 @@
package Graphics.Type;
import java.awt.Color;
import java.util.List;
public interface Type<T> {
/**
* 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<Type<?>> listGet();
}

View File

@ -1 +1 @@
{"couple": [true, "Onde"], "age": 19, "nationalite": "Franco-Algerienne", "enVie": true, "poid": 1.7}
{"couple": [true, "Onde", ["F", 3]], "age": 19, "nationalite": "Franco-Algerienne", "enVie": true, "poid": 1.7}