This commit is contained in:
pro.boooooo 2023-01-05 05:56:05 +01:00
parent 99610d9975
commit e6e8925fd2
16 changed files with 219 additions and 284 deletions

View File

@ -2,7 +2,7 @@
JAVA = java JAVA = java
JAVAC = javac JAVAC = javac
JAVADOC = javadoc JAVADOC = javadoc
OPTIONSDOCS = -d docs OPTIONSDOCS = -d docs -noqualifier all
OPTIONS = -d build -Xlint:unchecked -Xlint:deprecation OPTIONS = -d build -Xlint:unchecked -Xlint:deprecation
EXT = .java EXT = .java
@ -17,8 +17,8 @@ ConsolePACKAGE = Console/
# POUR ALLER PLUS VITE (Core, package:Console, ) # POUR ALLER PLUS VITE (Core, package:Console, )
ALL = $(SRC)*$(EXT) $(SRC)$(ConsolePACKAGE)*$(EXT) $(SRC)$(GraphicsPACKAGE)*$(EXT) ALL = $(SRC)*$(EXT) $(SRC)$(ConsolePACKAGE)*$(EXT) $(SRC)$(GraphicsPACKAGE)*$(EXT)
# LE FICHIER JSON # LE FICHIER JSON (Pour mon test)
JSON = toFormat.json JSON = /home/bilal-linux/toFormat.json
.PHONY: console graphics clean docs .PHONY: console graphics clean docs

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

@ -4,6 +4,11 @@ import java.net.URL;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
/**
* [Bilal]
* Afficher le code JSON dans la console une fois formatter
*/
public class DisplayConsole { public class DisplayConsole {
private URL jsonFile; private URL jsonFile;

View File

@ -2,11 +2,16 @@ import java.io.File;
import Console.DisplayConsole; import Console.DisplayConsole;
import Graphics.GraphicsCore; 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 class Core {
public static void main(String[] args) { public static void main(String[] args) {
if (args.length == 1) { if (args.length == 1) {
try { try {
new DisplayConsole(new File("../" + args[0]).toURI().toURL()); new DisplayConsole(new File(args[0]).toURI().toURL());
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); System.out.println(e);
} }

View File

@ -2,31 +2,37 @@ package Graphics;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import javax.swing.JPanel; import javax.swing.JPanel;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.net.URL; import java.net.URL;
/**
* [Romain]
* Pour gerer l'affichage graphique du code JSON
*/
public class GraphicFile extends JPanel { public class GraphicFile extends JPanel {
/**
* @param url Le chemin vers le fichier JSON
*/
public GraphicFile(URL url) { public GraphicFile(URL url) {
super(); super();
try { try {
System.out.println("[+] Lecture de " + url); System.out.println("[+] Lecture de " + url);
this.setLayout(new GridLayout(100, 1)); this.setLayout(new GridLayout(100, 1));
InputStream jsonReader = url.openStream(); InputStream jsonReader = url.openStream();
// ICI le code /**
* C'est ici que le hashmap est stocke
*/
Traitable fileTraited = new Traitable(jsonReader);
HashMap<String, Object> allVariables = fileTraited.getVariableMap();
jsonReader.close(); jsonReader.close();
} catch (IOException e) { } catch (IOException e) {
System.out.println("[!] Fichier " + url.getFile() + " n'existe pas"); 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");
}
}
} }

View File

@ -19,6 +19,12 @@ import java.awt.BorderLayout;
import javax.swing.filechooser.FileSystemView; import javax.swing.filechooser.FileSystemView;
import javax.swing.filechooser.FileNameExtensionFilter; 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 { public class GraphicsCore extends JFrame {
private final Dimension DEFAULT_FRAME_SIZE; private final Dimension DEFAULT_FRAME_SIZE;
private final Dimension MINIMUM_FRAME_SIZE; private final Dimension MINIMUM_FRAME_SIZE;
@ -42,6 +48,9 @@ public class GraphicsCore extends JFrame {
this.setVisible(true); this.setVisible(true);
} }
/**
* Initalisation des parametres de la Frame par defaut.
*/
private void init() { private void init() {
this.setSize(DEFAULT_FRAME_SIZE); this.setSize(DEFAULT_FRAME_SIZE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setDefaultCloseOperation(EXIT_ON_CLOSE);
@ -51,6 +60,11 @@ public class GraphicsCore extends JFrame {
cards.first(this.getContentPane()); 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() { private JPanel firstCard() {
GridBagLayout layout = new GridBagLayout(); GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints(); GridBagConstraints gbc = new GridBagConstraints();
@ -83,6 +97,12 @@ public class GraphicsCore extends JFrame {
return panel; 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() { private JPanel secondCard() {
GraphicFile file = new GraphicFile(this.url); GraphicFile file = new GraphicFile(this.url);
@ -104,6 +124,11 @@ public class GraphicsCore extends JFrame {
return mainPanel; 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) { private void validationAction(String field) {
try { try {
this.url = new File(field).toURI().toURL(); this.url = new File(field).toURI().toURL();
@ -114,11 +139,20 @@ public class GraphicsCore extends JFrame {
} }
} }
/**
* 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) { private void backAction(JPanel panel) {
this.remove(panel); this.remove(panel);
cards.first(this.getContentPane()); cards.first(this.getContentPane());
} }
/**
* Selection du fichier JSON
*/
public void getPathOf() { public void getPathOf() {
JFileChooser jc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); JFileChooser jc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());

View File

@ -1,128 +0,0 @@
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<String> an) {
try {
ArrayList<Node> 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<Node> 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<String> names, ArrayList<String> values) {
try {
ArrayList<Node> 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;
}
}

View File

@ -1,143 +0,0 @@
package Graphics;
import java.util.ArrayList;
public class Node {
private Node next;
private final String name;
private Object value;
private final ArrayList<Node> childs;
public Node() {
this.name = null;
this.next = null;
this.value = null;
this.childs = new ArrayList<>();
}
public Node(String name, ArrayList<Node> 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<Node> 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();
}
}
}

156
src/Graphics/Traitable.java Normal file
View File

@ -0,0 +1,156 @@
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);
}
while (i < allJson.length()) {
if (allJson.charAt(i) == '"') {
while (allJson.charAt(i) != ',') {
tmp.append(allJson.charAt(i));
i++;
}
// System.out.println(this.getNomOfRecord(tmp));
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;
}
// TODO: a finir (Bilal)
private Object getValueOfRecord(StringBuilder sb) {
// int i;
// int counter;
// StringBuilder value = new StringBuilder();
// String type = "";
// /**
// * Tableau
// */
// if (sb.indexOf("[") != -1) {
// type = "tableau";
// }
// /**
// * Chaine de characteres
// */
// for (i = 0, counter = 0; i <= sb.length() - 1; i++) {
// if (sb.charAt(i) == '"') {
// counter++;
// }
// }
// if (counter < 2 && sb.indexOf(".") != -1) {
// type = "float";
// }
// /*
// * Objet
// */
// if (sb.indexOf("{") != -1) {
// type = "objet";
// }
// /*
// * Integer
// */
// for (i = 0, counter = 0; i <= sb.length() - 1; i++) {
// if (sb.charAt(i) == '"') {
// counter++;
// }
// }
// if (counter < 2) {
// type = "int";
// }
// /**
// * Flottant
// */
// for (i = 0, counter = 0; i <= sb.length() - 1; i++) {
// if (sb.charAt(i) == '"') {
// counter++;
// }
// }
// if (counter < 2 && sb.indexOf(".") != -1) {
// type = "float";
// }
return null;
}
/**
* Ajouter le jeu de cle valeur dans la liste
*
* @param name Nom de la variable
* @param value Contenue de la variable
*/
private void addToList(String name, Object value) {
System.out.println("[+] => " + name + ": " + value);
this.content.put(name, value);
}
public HashMap<String, Object> getVariableMap() {
return this.content;
}
}

View File

@ -1 +1 @@
{"ecole":"I.U.T Senart-Fontainebleau","eleves":["Romain Besson","Bilal Boudjemline"],"classe":"BUT Informatique","matiere":{"mat1":"SCR", "mat2":"Java"}} {"prenom":"Bilal", "age": 19,}