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 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); } allJson = this.ajustementVirguleEnd(allJson.toString()); while (i < allJson.length()) { if (allJson.charAt(i) == '"') { while (allJson.charAt(i) != ',') { tmp.append(allJson.charAt(i)); i++; } 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); 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; } /** * 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]; 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); } /** * Recuperer le jeu de cles valeurs dans GrahicFile * * @see Graphics.GraphicFile * @return Le jeu de cles valeurs */ public HashMap getVariableMap() { return this.content; } }