Transférer les fichiers vers 'src/JsonInspector'

This commit is contained in:
Romain BESSON 2023-01-11 12:11:37 +01:00
parent 4364529fda
commit 64b6400252
5 changed files with 333 additions and 0 deletions

View File

@ -0,0 +1,45 @@
import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.net.URL;
public class CoreJSONDescriptor {
public static void main(String[] args) {
if (args.length == 0) {
new Frame();
} else {
File jsonFile = new File("toFormat.json");
if(jsonFile.exists()) {
//new Descriptor(getCodeJsonInOneLine(jsonFile));
} else {
System.err.println("[!] Probleme lors de l'ouverture du fichier");
System.exit(-1);
}
}
}
public static String getCodeJsonInOneLine(File f) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader buff = new BufferedReader(new FileReader(f));
String line;
while ((line = buff.readLine()) != null) {
line = line.replaceAll("\t", "");
line = line.replaceAll("\n", "");
line = line.replaceAll(" ", "");
sb.append(line);
}
buff.close();
} catch (IOException e) {
System.err.println("[!] Probleme lors de l'ouverture du fichier");
System.exit(-1);
}
return sb.toString();
}
}

View File

@ -0,0 +1,99 @@
import javax.swing.*;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
public class Frame extends JFrame {
private static final Dimension DEFAULT_FRAME_SIZE = new Dimension(800, 600);
private static final Dimension MINIMUM_FRAME_SIZE = new Dimension(600, 500);
private static final String DEFAULT_LINK = "https://gbfs.citibikenyc.com/gbfs/en/station_information.json";
private final CardLayout cards = new CardLayout();
private Node node;
public Frame() {
super("Inspecteur JSON");
init();
this.setVisible(true);
}
public Frame(Node node) {
super("Inspecteur JSON");
this.node = node;
init();
this.add(secondCard());
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());
//this.add(secondCard());
cards.first(this.getContentPane());
}
private JPanel firstCard() {
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
JTextField textField = new JTextField(DEFAULT_LINK, 30);
JButton button = new JButton("Valider");
button.addActionListener((event) -> validationAction(textField));
JPanel panel = new JPanel();
panel.setLayout(layout);
gbc.insets = new Insets(10, 10, 10, 10);
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel("URL :");
label.setForeground(Parameters.DEFAULT_TEXT_COLOR);
panel.add(label, gbc);
gbc.gridx = 1;
panel.add(textField, gbc);
gbc.gridy = 1;
panel.add(button, gbc);
panel.setBackground(Parameters.IHM_COLOR);
return panel;
}
private JPanel secondCard() {
JPanel mainPanel = new JPanel(), southPanel = new JPanel();
JButton backButton = new JButton("Retour");
backButton.addActionListener((event) -> backAction(mainPanel));
GraphicFile file = new GraphicFile(new Tree(""));
JScrollPane scroll = new JScrollPane();
mainPanel.setLayout(new BorderLayout());
southPanel.setBackground(Parameters.IHM_COLOR);
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(JTextField field) {
try {
URL url = new URL(field.getText());
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());
}
}

View File

@ -0,0 +1,142 @@
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.ArrayList;
public class GraphicFile extends JPanel {
private Node firstNode;
private ArrayList<Line> lines = new ArrayList<>();
private int index = 0;
public GraphicFile(Tree tree) {
super();
//firstNode = tree.getFirstNode();
this.setBackground(Parameters.BACKGROUND_COLOR);
firstNode = createTree();
createFileRecursive(firstNode, 0, false);
displayLines();
}
private void createFileRecursive(Node node, int depth, boolean inList) {
String indentation = "";
for (int i = 0; i < depth; i++) {
indentation += Parameters.INDENTATION;
}
if (node.getType() == Type.OBJECT) {
createObject(node, depth, indentation);
} else if (node.getType() == Type.ARRAY) {
createArray(node, depth, indentation);
} else if (node.getType() == Type.PAIR) {
createPair(node, indentation, inList);
}
}
private void createPair(Node node, String indentation, boolean virgule) {
Line line = new Line(node);
line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": ");
line.add("\"" + node.getValues().get(0) + "\"", Parameters.VALUE_COLOR);
if (virgule) {
line.add(",");
}
lines.add(line);
}
private void createObject(Node node, int depth, String indentation) {
boolean virgule;
Line line = new Line(node);
line.add(indentation);
if (node.getName().equals("")) {
line.add("{");
} else {
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": {");
}
lines.add(line);
callNextNodes(node, depth);
lines.add(new Line(node, indentation + "}"));
}
private void createArray(Node node, int depth, String indentation) {
Line line = new Line(node);
line.add(indentation);
line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR);
line.add(": [");
lines.add(line);
callNextNodes(node, depth);
lines.add(new Line(node, indentation + "]"));
}
private void callNextNodes(Node node, int depth) {
boolean virgule;
for (int i = 0; i < node.getNodes().size(); i++) {
// si l'élément afficher est le dernier à son niveau "virgule" est faux
// donc il n'y aura pas de virgule en fin ligne
virgule = !(i == node.getNodes().size() - 1);
createFileRecursive(node.getNodes().get(i), depth + 1, virgule);
}
}
private void displayLines() {
this.setLayout(new GridLayout(30, 1));
MyJPanel tempPanel;
MyJLabel label;
for (int i = 0; i < lines.size(); i++) {
tempPanel = new MyJPanel();
label = new MyJLabel("" + (i + 1));
label.setBackground(Parameters.IHM_COLOR);
tempPanel.add(label);
tempPanel.add(lines.get(i));
this.add(tempPanel);
//System.out.println(lines.get(i).index);
}
System.out.println();
System.out.println(lines.size() + " ligne(s)");
}
private Node createTree() {
Node beginning = new Node("", Type.OBJECT);
Node d = new Node("d", Type.OBJECT);
Node result = new Node("result", Type.ARRAY);
Node inter = new Node("", Type.OBJECT);
Node metadata = new Node("metadata", Type.OBJECT);
Node userID = new Node("UserId", Type.PAIR);
Node roleCode = new Node("RoleCode", Type.PAIR);
Node type = new Node("type", Type.PAIR);
String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35";
beginning.addNode(d);
d.addNode(result);
result.addNode(inter);
inter.addNode(metadata);
inter.addNode(userID);
inter.addNode(roleCode);
metadata.addNode(type);
type.addValue(value1);
userID.addValue(value2);
roleCode.addValue(value3);
System.out.println(Tree.printTree(beginning, 0));
return beginning;
}
}

View File

@ -0,0 +1,31 @@
import java.awt.*;
public class Line extends MyJPanel {
//Le nœud qui est représenté par cette objet Line
private Node node;
public Line(Node node) {
super();
this.node = node;
}
public Line(Node node, String str) {
super();
this.add(new MyJLabel(str));
this.node = node;
}
public Line(Node node, String str, Color color) {
super();
this.add(new MyJLabel(str, color));
this.node = node;
}
public void add(String string) {
this.add(new MyJLabel(string));
}
public void add(String string, Color color) {
this.add(new MyJLabel(string, color));
}
}

View File

@ -0,0 +1,16 @@
import javax.swing.*;
import java.awt.*;
public class MyJLabel extends JLabel {
public MyJLabel(String text, Color color) {
super(text);
this.setFont(Parameters.FILE_FONT);
this.setForeground(color);
}
public MyJLabel(String text) {
super(text);
this.setFont(Parameters.FILE_FONT);
this.setForeground(Parameters.DEFAULT_TEXT_COLOR);
}
}