Mise à jour de 'src/fr/sae/JSonInspector/Main.java'

This commit is contained in:
Romain BESSON 2023-01-16 22:30:31 +01:00
parent 44dde661eb
commit 56249a06a3

View File

@ -1,88 +1,53 @@
package fr.sae.JSonInspector; package fr.sae.JSonInspector;
import fr.sae.JSonInspector.Exception.JsonSyntaxException; import fr.sae.JSonInspector.Exception.JsonSyntaxException;
import fr.sae.JSonInspector.Graphics.Frame; import fr.sae.JSonInspector.Graphics.Frame;
import fr.sae.JSonInspector.Storage.Tree; import fr.sae.JSonInspector.Storage.Tree;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
if (args.length == 0) { if (args.length == 0) {
new Frame(); new Frame();
} else { } else {
try { try {
URL url = new File(args[0]).toURI().toURL(); URL url = new File(args[0]).toURI().toURL();
String toSend = getJsonInOneLine(url); String toSend = getJsonInOneLine(url);
Tree receive = new Tree(toSend); Tree receive = new Tree(toSend);
System.out.println(receive); System.out.println(receive);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
System.err.println("[!] Chemin du fichier invalide"); System.err.println("[!] Chemin du fichier invalide");
System.exit(1); System.exit(1);
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
System.err.println("[!] Syntaxe du fichier JSON invalide"); System.err.println("[!] Syntaxe du fichier JSON invalide");
System.exit(1); System.exit(1);
} }
} }
} }
public static String getJsonInOneLine(URL f) { public static String getJsonInOneLine(URL f) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
try { try {
BufferedReader buff = new BufferedReader(new InputStreamReader(f.openStream())); BufferedReader buff = new BufferedReader(new InputStreamReader(f.openStream()));
String line; String line;
while ((line = buff.readLine()) != null) { while ((line = buff.readLine()) != null) {
sb.append(line); line = line.replaceAll("\t", "");
} line = line.replaceAll("\n", "");
line = line.replaceAll(" ", "");
buff.close(); sb.append(line);
} catch (IOException e) { }
System.err.println("[!] Probleme lors de l'ouverture du fichier");
} buff.close();
} catch (IOException e) {
return sb.toString(); System.err.println("[!] Probleme lors de l'ouverture du fichier");
} }
public static String cleanFile(String file) { return sb.toString();
char[] chars = file.toCharArray(); }
boolean inString = false, inValue = false; }
String cleanedFile = "";
for (char currentChar : chars) {
if (!inString && !inValue) {
if (currentChar == '"') {
inString = true;
cleanedFile += currentChar;
} else if (isJsonSyntax(currentChar)) {
cleanedFile += currentChar;
}
} else if (currentChar == '"') {
inString = false;
cleanedFile += currentChar;
} else if (currentChar == ':') {
inValue = true;
cleanedFile += currentChar;
} else {
cleanedFile += currentChar;
}
}
return cleanedFile;
}
private static boolean isJsonSyntax(char character) {
boolean openingArray = character == '[', closingArray = character == ']';
boolean openingObject = character == '{', closingObject = character == '}';
boolean virgule = character == ',', deuxPoints = character == ':';
if (openingArray || closingArray || openingObject || closingObject || virgule || deuxPoints) {
return true;
}
return false;
}
}