This commit is contained in:
Bilou 2023-01-08 05:05:57 +01:00
parent 522b60c86d
commit d8b7a4767f
10 changed files with 368 additions and 68 deletions

View File

@ -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

View File

@ -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<String, Object> allVariables = fileTraited.getVariableMap();
HashMap<String, Type<?>> 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();

View File

@ -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<String, Object> content;
private final HashMap<String, String> contentRaw;
private final HashMap<String, Type<?>> 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<String, Object> getVariableMap() {
public HashMap<String, Type<?>> getVariableMap() {
return this.content;
}
}

View File

@ -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<List<Type<?>>> {
private String valueRaw;
private List<Type<?>> 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<Type<?>> 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<Type<?>> getValue() {
return this.value;
}
}

View File

@ -0,0 +1,28 @@
package Graphics.Type;
/**
* Representation du type boolean
*/
public class Bool implements Type<Boolean> {
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;
}
}

View File

@ -0,0 +1,28 @@
package Graphics.Type;
/**
* Representation du type string
*/
public class Chaine implements Type<String> {
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;
}
}

View File

@ -0,0 +1,28 @@
package Graphics.Type;
/**
* Representation du type int
*/
public class Entier implements Type<Integer> {
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;
}
}

View File

@ -0,0 +1,28 @@
package Graphics.Type;
/**
* Representation du type double
*/
public class Flottant implements Type<Double> {
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;
}
}

View File

@ -0,0 +1,22 @@
package Graphics.Type;
public interface Type<T> {
/**
* 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();
}

View File

@ -1 +1 @@
{"prenom":"Bilal", "age": 19, "nationalite": "Franco-Algerienne", "enVie": true, "poid": 1.7}
{"couple": [true, "Onde"], "age": 19, "nationalite": "Franco-Algerienne", "enVie": true, "poid": 1.7}