$
This commit is contained in:
parent
b7d56f4480
commit
b76c93fbfb
4
Makefile
4
Makefile
@ -17,7 +17,7 @@ EXCEPTION = ${SRC}/Exception/*.java
|
||||
STORAGE = ${SRC}/Storage/*.java
|
||||
GRAPHICS = ${SRC}/Graphics/*.java
|
||||
SETTINGS = ${SRC}/Settings/*.java
|
||||
MAIN = ${SRC}/Main.java
|
||||
MAIN = ${SRC}/*.java
|
||||
|
||||
.PHONY: clean docs run
|
||||
|
||||
@ -25,7 +25,7 @@ run:
|
||||
make clean
|
||||
mkdir build/ && mkdir docs/
|
||||
${JAVAC} ${JAVAC_OPTIONS} ${SETTINGS} ${GRAPHICS} ${STORAGE} ${EXCEPTION} ${MAIN}
|
||||
cd build && java fr.sae.JSonInspector.Main dfd && cd ..
|
||||
cd build && java fr.sae.JSonInspector.Main /home/bilal-linux/toFormat.json && cd ..
|
||||
|
||||
clean:
|
||||
rm -rf build && rm -rf docs
|
||||
|
112
src/fr/sae/JSonInspector/DisplayConsole.java
Normal file
112
src/fr/sae/JSonInspector/DisplayConsole.java
Normal file
@ -0,0 +1,112 @@
|
||||
package fr.sae.JSonInspector;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,86 +1,78 @@
|
||||
package fr.sae.JSonInspector;
|
||||
|
||||
import fr.sae.JSonInspector.Exception.JsonSyntaxException;
|
||||
import fr.sae.JSonInspector.Graphics.Frame;
|
||||
import fr.sae.JSonInspector.Storage.Tree;
|
||||
import java.io.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
new Frame();
|
||||
} else {
|
||||
try {
|
||||
URL url = new URL(args[0]);
|
||||
String file = getJsonInOneLine(url);
|
||||
System.out.println(new Tree(file));
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
System.err.println("[!] Chemin du fichier invalide");
|
||||
System.exit(1);
|
||||
|
||||
} catch (JsonSyntaxException j) {
|
||||
System.err.println("[!] Syntaxe du fichier invalide");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getJsonInOneLine(URL f) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
try {
|
||||
BufferedReader buff = new BufferedReader(new InputStreamReader(f.openStream()));
|
||||
String line;
|
||||
while ((line = buff.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
buff.close();
|
||||
} catch (IOException e) {
|
||||
System.err.println("[!] Probleme lors de l'ouverture du fichier");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String cleanFile(String file) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
package fr.sae.JSonInspector;
|
||||
|
||||
import fr.sae.JSonInspector.Graphics.Frame;
|
||||
import java.io.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0) {
|
||||
new Frame();
|
||||
} else {
|
||||
try {
|
||||
URL url = new File(args[0]).toURI().toURL();
|
||||
new DisplayConsole(url);
|
||||
} catch (MalformedURLException e) {
|
||||
System.err.println("[!] Chemin du fichier invalide");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getJsonInOneLine(URL f) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
try {
|
||||
BufferedReader buff = new BufferedReader(new InputStreamReader(f.openStream()));
|
||||
String line;
|
||||
while ((line = buff.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
buff.close();
|
||||
} catch (IOException e) {
|
||||
System.err.println("[!] Probleme lors de l'ouverture du fichier");
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String cleanFile(String file) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user