$
This commit is contained in:
@@ -1,117 +1,117 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,24 +1,24 @@
|
||||
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>");
|
||||
}
|
||||
}
|
||||
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>");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,42 +1,42 @@
|
||||
package Graphics;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import javax.swing.JPanel;
|
||||
import java.awt.GridLayout;
|
||||
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 GridLayout(100, 1));
|
||||
|
||||
InputStream jsonReader = url.openStream();
|
||||
|
||||
/**
|
||||
* C'est ici que le hashmap est stocke
|
||||
*/
|
||||
Traitable fileTraited = new Traitable(jsonReader);
|
||||
HashMap<String, Object> allVariables = fileTraited.getVariableMap();
|
||||
|
||||
for (String key : allVariables.keySet()) {
|
||||
System.out.println("Clé : " + key + " , Valeur : " + allVariables.get(key));
|
||||
}
|
||||
|
||||
jsonReader.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("[!] Fichier " + url.getFile() + " n'existe pas");
|
||||
}
|
||||
}
|
||||
}
|
||||
package Graphics;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import javax.swing.JPanel;
|
||||
import java.awt.GridLayout;
|
||||
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 GridLayout(100, 1));
|
||||
|
||||
InputStream jsonReader = url.openStream();
|
||||
|
||||
/**
|
||||
* C'est ici que le hashmap est stocke
|
||||
*/
|
||||
Traitable fileTraited = new Traitable(jsonReader);
|
||||
HashMap<String, Object> allVariables = fileTraited.getVariableMap();
|
||||
|
||||
for (String key : allVariables.keySet()) {
|
||||
System.out.println("Clé : " + key + " , Valeur : " + allVariables.get(key));
|
||||
}
|
||||
|
||||
jsonReader.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println("[!] Fichier " + url.getFile() + " n'existe pas");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,174 +1,174 @@
|
||||
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;
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,158 +1,158 @@
|
||||
package Graphics;
|
||||
|
||||
import java.util.HashMap;
|
||||
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, Object> content;
|
||||
private final InputStream file;
|
||||
|
||||
public Traitable(InputStream file) {
|
||||
this.content = new HashMap<>();
|
||||
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) != ',') {
|
||||
tmp.append(allJson.charAt(i));
|
||||
i++;
|
||||
}
|
||||
|
||||
Object value = new Object();
|
||||
String name = this.getNomOfRecord(tmp);
|
||||
String[] typeOfVariable = this.getValueOfRecord(tmp.toString());
|
||||
|
||||
switch (typeOfVariable[0]) {
|
||||
case "int": {
|
||||
value = Integer.valueOf(typeOfVariable[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
case "string": {
|
||||
value = String.valueOf(typeOfVariable[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
case "boolean": {
|
||||
value = Boolean.valueOf(typeOfVariable[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
case "float": {
|
||||
value = Double.valueOf(typeOfVariable[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
value = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.content.put(name, value);
|
||||
tmp.setLength(0);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
} catch (StringIndexOutOfBoundsException ignore) {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("[!] Probleme lors de la lecture du fichier");
|
||||
}
|
||||
}
|
||||
|
||||
private String getNomOfRecord(StringBuilder sb) {
|
||||
int i = 0;
|
||||
StringBuilder name = new StringBuilder();
|
||||
|
||||
while (i < sb.length()) {
|
||||
if (sb.charAt(i) == ':') {
|
||||
return name.toString().replaceAll("\"", "");
|
||||
}
|
||||
|
||||
name.append(sb.charAt(i));
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pour recuperer le type et la valeur d'une variable JSON
|
||||
*
|
||||
* @param jsonLine La ligne json a evaluer
|
||||
* @return Un tableau { [0] = type, [1] = valeur }
|
||||
*/
|
||||
private String[] getValueOfRecord(String jsonLine) {
|
||||
String[] parts = jsonLine.split(":");
|
||||
String value = parts[1];
|
||||
|
||||
if (value.charAt(0) == ' ') {
|
||||
value = value.substring(1);
|
||||
}
|
||||
|
||||
if (value.contains("\"")) {
|
||||
return new String[] { "string", value };
|
||||
} else if (value.contains("{")) {
|
||||
return new String[] { "objet", value };
|
||||
} else if (value.contains("[")) {
|
||||
return new String[] { "tableau", value };
|
||||
} else if (value.contains("true") || value.contains("false")) {
|
||||
return new String[] { "boolean", value };
|
||||
} else if (value.contains(".")) {
|
||||
return new String[] { "float", value };
|
||||
} else {
|
||||
return new String[] { "int", value };
|
||||
}
|
||||
}
|
||||
|
||||
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, Object> getVariableMap() {
|
||||
return this.content;
|
||||
}
|
||||
package Graphics;
|
||||
|
||||
import java.util.HashMap;
|
||||
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, Object> content;
|
||||
private final InputStream file;
|
||||
|
||||
public Traitable(InputStream file) {
|
||||
this.content = new HashMap<>();
|
||||
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) != ',') {
|
||||
tmp.append(allJson.charAt(i));
|
||||
i++;
|
||||
}
|
||||
|
||||
Object value = new Object();
|
||||
String name = this.getNomOfRecord(tmp);
|
||||
String[] typeOfVariable = this.getValueOfRecord(tmp.toString());
|
||||
|
||||
switch (typeOfVariable[0]) {
|
||||
case "int": {
|
||||
value = Integer.valueOf(typeOfVariable[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
case "string": {
|
||||
value = String.valueOf(typeOfVariable[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
case "boolean": {
|
||||
value = Boolean.valueOf(typeOfVariable[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
case "float": {
|
||||
value = Double.valueOf(typeOfVariable[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
value = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.content.put(name, value);
|
||||
tmp.setLength(0);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
} catch (StringIndexOutOfBoundsException ignore) {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("[!] Probleme lors de la lecture du fichier");
|
||||
}
|
||||
}
|
||||
|
||||
private String getNomOfRecord(StringBuilder sb) {
|
||||
int i = 0;
|
||||
StringBuilder name = new StringBuilder();
|
||||
|
||||
while (i < sb.length()) {
|
||||
if (sb.charAt(i) == ':') {
|
||||
return name.toString().replaceAll("\"", "");
|
||||
}
|
||||
|
||||
name.append(sb.charAt(i));
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pour recuperer le type et la valeur d'une variable JSON
|
||||
*
|
||||
* @param jsonLine La ligne json a evaluer
|
||||
* @return Un tableau { [0] = type, [1] = valeur }
|
||||
*/
|
||||
private String[] getValueOfRecord(String jsonLine) {
|
||||
String[] parts = jsonLine.split(":");
|
||||
String value = parts[1];
|
||||
|
||||
if (value.charAt(0) == ' ') {
|
||||
value = value.substring(1);
|
||||
}
|
||||
|
||||
if (value.contains("\"")) {
|
||||
return new String[] { "string", value };
|
||||
} else if (value.contains("{")) {
|
||||
return new String[] { "objet", value };
|
||||
} else if (value.contains("[")) {
|
||||
return new String[] { "tableau", value };
|
||||
} else if (value.contains("true") || value.contains("false")) {
|
||||
return new String[] { "boolean", value };
|
||||
} else if (value.contains(".")) {
|
||||
return new String[] { "float", value };
|
||||
} else {
|
||||
return new String[] { "int", value };
|
||||
}
|
||||
}
|
||||
|
||||
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, Object> getVariableMap() {
|
||||
return this.content;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user