This commit is contained in:
pro.boooooo 2023-01-05 03:22:24 +01:00
commit 99610d9975
14 changed files with 616 additions and 0 deletions

41
Makefile Normal file
View File

@ -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)

Binary file not shown.

BIN
build/Core.class Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
build/Graphics/Node.class Normal file

Binary file not shown.

View File

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

19
src/Core.java Normal file
View File

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

View File

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

View File

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

128
src/Graphics/ListNodes.java Normal file
View File

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

143
src/Graphics/Node.java Normal file
View File

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

1
toFormat.json Normal file
View File

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