diff --git a/build/Graphics/GraphicFile.class b/build/Graphics/GraphicFile.class index b78811e..98d876b 100644 Binary files a/build/Graphics/GraphicFile.class and b/build/Graphics/GraphicFile.class differ diff --git a/build/Graphics/Traitable.class b/build/Graphics/Traitable.class index 218f4bb..fbd41e0 100644 Binary files a/build/Graphics/Traitable.class and b/build/Graphics/Traitable.class differ diff --git a/src/Graphics/GraphicFile.java b/src/Graphics/GraphicFile.java index d41d00a..356202c 100644 --- a/src/Graphics/GraphicFile.java +++ b/src/Graphics/GraphicFile.java @@ -30,6 +30,10 @@ public class GraphicFile extends JPanel { Traitable fileTraited = new Traitable(jsonReader); HashMap allVariables = fileTraited.getVariableMap(); + for (String key : allVariables.keySet()) { + System.out.println("Clé : " + key + " , Valeur : " + allVariables.get(key)); + } + jsonReader.close(); } catch (IOException e) { System.out.println("[!] Fichier " + url.getFile() + " n'existe pas"); diff --git a/src/Graphics/Traitable.java b/src/Graphics/Traitable.java index 76c9aeb..04466f8 100644 --- a/src/Graphics/Traitable.java +++ b/src/Graphics/Traitable.java @@ -38,6 +38,8 @@ public class Traitable { allJson.append(c); } + allJson = this.ajustementVirguleEnd(allJson.toString()); + while (i < allJson.length()) { if (allJson.charAt(i) == '"') { while (allJson.charAt(i) != ',') { @@ -45,7 +47,38 @@ public class Traitable { i++; } - // System.out.println(this.getNomOfRecord(tmp)); + 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); } @@ -53,7 +86,6 @@ public class Traitable { } } catch (StringIndexOutOfBoundsException ignore) { } - } catch (IOException e) { System.out.println("[!] Probleme lors de la lecture du fichier"); } @@ -76,80 +108,50 @@ public class Traitable { return null; } - // TODO: a finir (Bilal) - private Object getValueOfRecord(StringBuilder sb) { - // int i; - // int counter; - // StringBuilder value = new StringBuilder(); - // String type = ""; + /** + * 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]; - // /** - // * Tableau - // */ - // if (sb.indexOf("[") != -1) { - // type = "tableau"; - // } + if (value.charAt(0) == ' ') { + value = value.substring(1); + } - // /** - // * Chaine de characteres - // */ - // for (i = 0, counter = 0; i <= sb.length() - 1; i++) { - // if (sb.charAt(i) == '"') { - // counter++; - // } - // } + 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 }; + } + } - // if (counter < 2 && sb.indexOf(".") != -1) { - // type = "float"; - // } + 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); - // /* - // * Objet - // */ - // if (sb.indexOf("{") != -1) { - // type = "objet"; - // } - - // /* - // * Integer - // */ - // for (i = 0, counter = 0; i <= sb.length() - 1; i++) { - // if (sb.charAt(i) == '"') { - // counter++; - // } - // } - - // if (counter < 2) { - // type = "int"; - // } - - // /** - // * Flottant - // */ - // for (i = 0, counter = 0; i <= sb.length() - 1; i++) { - // if (sb.charAt(i) == '"') { - // counter++; - // } - // } - - // if (counter < 2 && sb.indexOf(".") != -1) { - // type = "float"; - // } - - return null; + return new StringBuilder(nouvelleChaine); } /** - * Ajouter le jeu de cle valeur dans la liste + * Recuperer le jeu de cles valeurs dans GrahicFile * - * @param name Nom de la variable - * @param value Contenue de la variable + * @see Graphics.GraphicFile + * @return Le jeu de cles valeurs */ - private void addToList(String name, Object value) { - System.out.println("[+] => " + name + ": " + value); - this.content.put(name, value); - } - public HashMap getVariableMap() { return this.content; } diff --git a/toFormat.json b/toFormat.json index 25ad443..08cd72f 100644 --- a/toFormat.json +++ b/toFormat.json @@ -1 +1 @@ -{"prenom":"Bilal", "age": 19,} \ No newline at end of file +{"prenom":"Bilal", "age": 19, "nationalite": "Franco-Algerienne", "enVie": true, "poid": 1.7} \ No newline at end of file