From d8b7a4767fe94ac68c0a63dde93998fa70599fbd Mon Sep 17 00:00:00 2001 From: Bilou Date: Sun, 8 Jan 2023 05:05:57 +0100 Subject: [PATCH] $ --- Makefile | 5 +- src/Graphics/GraphicFile.java | 7 +- src/Graphics/Traitable.java | 189 +++++++++++++++++++++----------- src/Graphics/Type/Array.java | 99 +++++++++++++++++ src/Graphics/Type/Bool.java | 28 +++++ src/Graphics/Type/Chaine.java | 28 +++++ src/Graphics/Type/Entier.java | 28 +++++ src/Graphics/Type/Flottant.java | 28 +++++ src/Graphics/Type/Type.java | 22 ++++ toFormat.json | 2 +- 10 files changed, 368 insertions(+), 68 deletions(-) create mode 100644 src/Graphics/Type/Array.java create mode 100644 src/Graphics/Type/Bool.java create mode 100644 src/Graphics/Type/Chaine.java create mode 100644 src/Graphics/Type/Entier.java create mode 100644 src/Graphics/Type/Flottant.java create mode 100644 src/Graphics/Type/Type.java diff --git a/Makefile b/Makefile index aab2f6f..d402fa1 100644 --- a/Makefile +++ b/Makefile @@ -12,10 +12,11 @@ BUILD = build/ DOCS = docs/ CORE = Core GraphicsPACKAGE = Graphics/ +GraphicsPackageType = Graphics/Type/ ConsolePACKAGE = Console/ -# POUR ALLER PLUS VITE (Core, package:Console, ) -ALL = $(SRC)*$(EXT) $(SRC)$(ConsolePACKAGE)*$(EXT) $(SRC)$(GraphicsPACKAGE)*$(EXT) +# POUR ALLER PLUS VITE +ALL = $(SRC)*$(EXT) $(SRC)$(ConsolePACKAGE)*$(EXT) $(SRC)$(GraphicsPACKAGE)*$(EXT) $(SRC)$(GraphicsPackageType)*$(EXT) # LE FICHIER JSON (Pour mon test) JSON = /home/bilal-linux/toFormat.json diff --git a/src/Graphics/GraphicFile.java b/src/Graphics/GraphicFile.java index 37bd273..0c74caf 100644 --- a/src/Graphics/GraphicFile.java +++ b/src/Graphics/GraphicFile.java @@ -4,6 +4,9 @@ import java.io.InputStream; import java.io.IOException; import java.util.HashMap; import javax.swing.JPanel; + +import Graphics.Type.*; + import java.awt.GridLayout; import java.net.URL; @@ -28,10 +31,10 @@ public class GraphicFile extends JPanel { * C'est ici que le hashmap est stocke */ Traitable fileTraited = new Traitable(jsonReader); - HashMap allVariables = fileTraited.getVariableMap(); + HashMap> allVariables = fileTraited.getVariableMap(); for (String key : allVariables.keySet()) { - System.out.println("Clé : " + key + " , Valeur : " + allVariables.get(key)); + System.out.println("\"" + key + "\": " + allVariables.get(key).display()); } jsonReader.close(); diff --git a/src/Graphics/Traitable.java b/src/Graphics/Traitable.java index 887518c..d87a562 100644 --- a/src/Graphics/Traitable.java +++ b/src/Graphics/Traitable.java @@ -1,6 +1,10 @@ package Graphics; import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.LinkedList; +import Graphics.Type.*; import java.io.IOException; import java.io.InputStream; @@ -10,11 +14,13 @@ import java.io.InputStream; */ public class Traitable { - private final HashMap content; + private final HashMap contentRaw; + private final HashMap> content; private final InputStream file; public Traitable(InputStream file) { - this.content = new HashMap<>(); + this.contentRaw = new LinkedHashMap<>(); + this.content = new LinkedHashMap<>(); this.file = file; this.Run(); } @@ -43,47 +49,32 @@ public class Traitable { 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; + if (allJson.charAt(i) == '[') { + while (allJson.charAt(i) != ']') { + tmp.append(allJson.charAt(i)); + i++; + } + } else if (allJson.charAt(i) == '{') { + while (allJson.charAt(i) != '}') { + tmp.append(allJson.charAt(i)); + i++; + } + } else { + tmp.append(allJson.charAt(i)); + i++; } } - this.content.put(name, value); + String[] varInfo = this.getInfoOfRecord(tmp); + this.saveValue(varInfo[0], varInfo[1]); tmp.setLength(0); + + i++; } i++; } + } catch (StringIndexOutOfBoundsException ignore) { } } catch (IOException e) { @@ -91,52 +82,124 @@ public class Traitable { } } - private String getNomOfRecord(StringBuilder sb) { - int i = 0; - StringBuilder name = new StringBuilder(); + /** + * Enregistre dans la HashMap la valeur + * + * @param name Le nom de l'entree + * @param value La valeur de l'entree + * + * @see Graphics.Type.Type + * @see java.util.HashMap + */ + private void saveValue(String name, String value) { + String typeOfValue = this.getType(value); - while (i < sb.length()) { - if (sb.charAt(i) == ':') { - return name.toString().replaceAll("\"", ""); + switch (typeOfValue) { + case "string": { + this.content.put(name, new Chaine(String.valueOf(value))); + break; } - name.append(sb.charAt(i)); + case "int": { + this.content.put(name, new Entier(Integer.valueOf(value))); + break; + } - i++; + case "double": { + this.content.put(name, new Flottant(Double.valueOf(value))); + break; + } + + case "boolean": { + this.content.put(name, new Bool(Boolean.valueOf(value))); + break; + } + + case "array": { + this.content.put(name, new Array(value)); + break; + } } - - return null; } /** - * Pour recuperer le type et la valeur d'une variable JSON + * Sert a detecter le type d'une valeur * - * @param jsonLine La ligne json a evaluer - * @return Un tableau { [0] = type, [1] = valeur } + * @param value La chaine a evolue pour determiner son type + * @return Le type */ - private String[] getValueOfRecord(String jsonLine) { - String[] parts = jsonLine.split(":"); - String value = parts[1]; - - if (value.charAt(0) == ' ') { - value = value.substring(1); - } - + private String getType(String value) { if (value.contains("\"")) { - return new String[] { "string", value }; + return "string"; } else if (value.contains("{")) { - return new String[] { "objet", value }; + return "object"; } else if (value.contains("[")) { - return new String[] { "tableau", value }; + return "array"; } else if (value.contains("true") || value.contains("false")) { - return new String[] { "boolean", value }; + return "boolean"; } else if (value.contains(".")) { - return new String[] { "float", value }; + return "double"; + } else if (value.contains("null")) { + return "null"; } else { - return new String[] { "int", value }; + return "int"; } } + /** + * Recuperer le nom et la valeur divise sous forme de tableau + * + * @param sb La phrase a separer + * @return Un tableau { 0 = nom, 1 = value } + */ + private String[] getInfoOfRecord(StringBuilder sb) { + String[] info = sb.toString().split(":"); + info[0] = this.removeSpeChar(info[0]); + info[1] = this.removeFirstSpaceIfThere(info[1]); + + return info; + } + + /** + * Retourne une valeur JSON qui, si elle contient un espace au debut, le + * supprime + * + * @param str La valeur JSON + * @return La valeur JSON sans l'espace au debut si il y en a un + */ + private String removeFirstSpaceIfThere(String str) { + if (str.length() > 0 && str.charAt(0) == ' ') { + str = str.substring(1); + } + + return str; + } + + /** + * Sert a retirer le charactere guillemet + * + * @param str La phrase a soustraire le symbole guillemet + * @return Retourner une chaine sans le charactere guillemet + */ + private String removeSpeChar(String str) { + StringBuilder tmp = new StringBuilder(); + + for (int i = 0; i <= str.length() - 1; i++) { + if (str.charAt(i) != '"') { + tmp.append(str.charAt(i)); + } + } + + return tmp.toString(); + } + + /** + * Sert a rajouter une virgule a l'avant dernier charactre du fichier (pour ne + * pas skipper la derniere cle valeur) + * + * @param str Le fichier json en une ligne + * @return Le fichier avec la fameuse virgule + */ private StringBuilder ajustementVirguleEnd(String str) { int longueur = str.length(); char avantDernierCaractere = str.charAt(longueur - 2); @@ -152,7 +215,7 @@ public class Traitable { * @see Graphics.GraphicFile * @return Le jeu de cles valeurs */ - public HashMap getVariableMap() { + public HashMap> getVariableMap() { return this.content; } } \ No newline at end of file diff --git a/src/Graphics/Type/Array.java b/src/Graphics/Type/Array.java new file mode 100644 index 0000000..b4a4787 --- /dev/null +++ b/src/Graphics/Type/Array.java @@ -0,0 +1,99 @@ +package Graphics.Type; + +import Graphics.Type.*; +import java.util.LinkedList; +import java.util.List; + +/** + * Representation d'un tableau d'object + */ + +public class Array implements Type>> { + private String valueRaw; + private List> value; + + public Array(String valueRaw) { + this.valueRaw = valueRaw.substring(1, valueRaw.length() - 1); + this.value = new LinkedList<>(); + + this.Run(); + } + + public void Run() { + String[] spliced = this.valueRaw.split(","); + List> list = new LinkedList<>(); + + for (int i = 0; i <= spliced.length - 1; i++) { + switch (this.getType(spliced[i])) { + case "string": { + list.add(new Chaine(String.valueOf(spliced[i]))); + break; + } + + case "int": { + list.add(new Entier(Integer.valueOf(spliced[i]))); + break; + } + + case "double": { + list.add(new Flottant(Double.valueOf(spliced[i]))); + break; + } + + case "boolean": { + list.add(new Bool(Boolean.valueOf(spliced[i]))); + break; + } + } + } + + this.value = list; + } + + public String getType(String value) { + if (value.contains("\"")) { + return "string"; + } else if (value.contains("{")) { + return "object"; + } else if (value.contains("[")) { + return "array"; + } else if (value.contains("true") || value.contains("false")) { + return "boolean"; + } else if (value.contains(".")) { + return "double"; + } else if (value.contains("null")) { + return "null"; + } else { + return "int"; + } + } + + public String getValueRaw() { + return this.valueRaw; + } + + @Override + public String display() { + StringBuilder str = new StringBuilder(); + + str.append("["); + + for (int i = 0; i <= this.value.size() - 1; i++) { + str.append(this.value.get(i).display() + ", "); + } + + str.append("]"); + + return str.toString(); + } + + @Override + public String getType() { + return "array"; + } + + @Override + public List> getValue() { + return this.value; + } +} diff --git a/src/Graphics/Type/Bool.java b/src/Graphics/Type/Bool.java new file mode 100644 index 0000000..75d8498 --- /dev/null +++ b/src/Graphics/Type/Bool.java @@ -0,0 +1,28 @@ +package Graphics.Type; + +/** + * Representation du type boolean + */ + +public class Bool implements Type { + private Boolean value; + + public Bool(Boolean value) { + this.value = value; + } + + @Override + public String display() { + return String.valueOf(value); + } + + @Override + public String getType() { + return "boolean"; + } + + @Override + public Boolean getValue() { + return this.value; + } +} diff --git a/src/Graphics/Type/Chaine.java b/src/Graphics/Type/Chaine.java new file mode 100644 index 0000000..554afe2 --- /dev/null +++ b/src/Graphics/Type/Chaine.java @@ -0,0 +1,28 @@ +package Graphics.Type; + +/** + * Representation du type string + */ + +public class Chaine implements Type { + private String value; + + public Chaine(String value) { + this.value = value; + } + + @Override + public String display() { + return String.valueOf(value); + } + + @Override + public String getType() { + return "string"; + } + + @Override + public String getValue() { + return this.value; + } +} diff --git a/src/Graphics/Type/Entier.java b/src/Graphics/Type/Entier.java new file mode 100644 index 0000000..5d123d9 --- /dev/null +++ b/src/Graphics/Type/Entier.java @@ -0,0 +1,28 @@ +package Graphics.Type; + +/** + * Representation du type int + */ + +public class Entier implements Type { + public Integer value; + + public Entier(Integer value) { + this.value = value; + } + + @Override + public String display() { + return String.valueOf(value); + } + + @Override + public String getType() { + return "int"; + } + + @Override + public Integer getValue() { + return this.value; + } +} diff --git a/src/Graphics/Type/Flottant.java b/src/Graphics/Type/Flottant.java new file mode 100644 index 0000000..a51a318 --- /dev/null +++ b/src/Graphics/Type/Flottant.java @@ -0,0 +1,28 @@ +package Graphics.Type; + +/** + * Representation du type double + */ + +public class Flottant implements Type { + private Double value; + + public Flottant(Double value) { + this.value = value; + } + + @Override + public String display() { + return String.valueOf(value); + } + + @Override + public String getType() { + return "double"; + } + + @Override + public Double getValue() { + return this.value; + } +} \ No newline at end of file diff --git a/src/Graphics/Type/Type.java b/src/Graphics/Type/Type.java new file mode 100644 index 0000000..4ece8aa --- /dev/null +++ b/src/Graphics/Type/Type.java @@ -0,0 +1,22 @@ +package Graphics.Type; + +public interface Type { + /** + * Retourner le type de la variable + * + * @return le type en string + */ + public String getType(); + + /** + * Recuperer la valeur de la variable + * + * @return La valeur du resultat + */ + public T getValue(); + + /** + * Afficher la valeur / toutes les valeurs + */ + public String display(); +} diff --git a/toFormat.json b/toFormat.json index 08cd72f..485ce75 100644 --- a/toFormat.json +++ b/toFormat.json @@ -1 +1 @@ -{"prenom":"Bilal", "age": 19, "nationalite": "Franco-Algerienne", "enVie": true, "poid": 1.7} \ No newline at end of file +{"couple": [true, "Onde"], "age": 19, "nationalite": "Franco-Algerienne", "enVie": true, "poid": 1.7} \ No newline at end of file