Files
SAE32_2022/src/Graphics/Traitable.java

158 lines
4.8 KiB
Java
Raw Normal View History

2023-01-05 05:56:05 +01:00
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);
}
2023-01-05 18:34:55 +01:00
allJson = this.ajustementVirguleEnd(allJson.toString());
2023-01-05 05:56:05 +01:00
while (i < allJson.length()) {
if (allJson.charAt(i) == '"') {
while (allJson.charAt(i) != ',') {
tmp.append(allJson.charAt(i));
i++;
}
2023-01-05 18:34:55 +01:00
Object value = new Object();
String name = this.getNomOfRecord(tmp);
String[] typeOfVariable = this.getValueOfRecord(tmp.toString());
switch (typeOfVariable[0]) {
case "int": {
value = Integer.valueOf(typeOfVariable[1]);
break;
}
case "string": {
value = String.valueOf(typeOfVariable[1]);
break;
}
case "boolean": {
value = Boolean.valueOf(typeOfVariable[1]);
break;
}
case "float": {
value = Double.valueOf(typeOfVariable[1]);
break;
}
default: {
value = null;
break;
}
}
this.content.put(name, value);
2023-01-05 05:56:05 +01:00
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;
}
2023-01-05 18:34:55 +01:00
/**
* Pour recuperer le type et la valeur d'une variable JSON
*
* @param jsonLine La ligne json a evaluer
* @return Un tableau { [0] = type, [1] = valeur }
*/
private String[] getValueOfRecord(String jsonLine) {
String[] parts = jsonLine.split(":");
String value = parts[1];
2023-01-05 05:56:05 +01:00
2023-01-05 18:34:55 +01:00
if (value.charAt(0) == ' ') {
value = value.substring(1);
}
if (value.contains("\"")) {
return new String[] { "string", value };
} else if (value.contains("{")) {
return new String[] { "objet", value };
} else if (value.contains("[")) {
return new String[] { "tableau", value };
} else if (value.contains("true") || value.contains("false")) {
return new String[] { "boolean", value };
} else if (value.contains(".")) {
return new String[] { "float", value };
} else {
return new String[] { "int", value };
}
}
private StringBuilder ajustementVirguleEnd(String str) {
int longueur = str.length();
char avantDernierCaractere = str.charAt(longueur - 2);
String nouvelleChaine = str.substring(0, longueur - 2) + avantDernierCaractere + ","
+ str.substring(longueur - 1);
return new StringBuilder(nouvelleChaine);
2023-01-05 05:56:05 +01:00
}
/**
2023-01-05 18:34:55 +01:00
* Recuperer le jeu de cles valeurs dans GrahicFile
2023-01-05 05:56:05 +01:00
*
2023-01-05 18:34:55 +01:00
* @see Graphics.GraphicFile
* @return Le jeu de cles valeurs
2023-01-05 05:56:05 +01:00
*/
public HashMap<String, Object> getVariableMap() {
return this.content;
}
}