From 9fe24c2f9871f5d1e6127efa1ddd3cc9abeee5fb Mon Sep 17 00:00:00 2001 From: besson Date: Sun, 15 Jan 2023 18:26:04 +0100 Subject: [PATCH] =?UTF-8?q?Transf=C3=A9rer=20les=20fichiers=20vers=20'src/?= =?UTF-8?q?JsonInspector'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JsonInspector/Main.java | 86 ++++++++++++++++++++++++++++++++++++ src/JsonInspector/Node.java | 26 ++++++++++- src/JsonInspector/Other.java | 16 +++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 src/JsonInspector/Main.java create mode 100644 src/JsonInspector/Other.java diff --git a/src/JsonInspector/Main.java b/src/JsonInspector/Main.java new file mode 100644 index 0000000..5fcbf82 --- /dev/null +++ b/src/JsonInspector/Main.java @@ -0,0 +1,86 @@ +package JsonInspector; + +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; + +public class Main { + public static void main(String[] args) { + if (args.length == 0) { + new Frame(); + + } else { + try { + URL url = new URL(args[0]); + String file = getJsonInOneLine(url); + System.out.println(new Tree(file)); + + } catch (MalformedURLException e) { + System.err.println("Invalid URL"); + System.exit(1); + + } catch (JsonSyntaxException j) { + System.err.println("Invalid syntax in file"); + System.exit(1); + } + } + } + + public static String getJsonInOneLine(URL f) { + StringBuilder sb = new StringBuilder(); + + try { + BufferedReader buff = new BufferedReader(new InputStreamReader(f.openStream())); + String line; + while ((line = buff.readLine()) != null) { + sb.append(line); + } + + buff.close(); + } catch (IOException e) { + System.err.println("[!] Probleme lors de l'ouverture du fichier"); + } + + return sb.toString(); + } + + + public static String cleanFile(String file) { + char[] chars = file.toCharArray(); + boolean inString = false, inValue = false; + String cleanedFile = ""; + + for (char currentChar : chars) { + if (!inString && !inValue) { + if (currentChar == '"') { + inString = true; + cleanedFile += currentChar; + } else if (isJsonSyntax(currentChar)) { + cleanedFile += currentChar; + } + } else if (currentChar == '"') { + inString = false; + cleanedFile += currentChar; + } else if (currentChar == ':') { + inValue = true; + cleanedFile += currentChar; + } else { + cleanedFile += currentChar; + } + } + + return cleanedFile; + } + + + private static boolean isJsonSyntax(char character) { + boolean openingArray = character == '[', closingArray = character == ']'; + boolean openingObject = character == '{', closingObject = character == '}'; + boolean virgule = character == ',', deuxPoints = character == ':'; + + if (openingArray||closingArray||openingObject||closingObject||virgule||deuxPoints) { + return true; + } + return false; + } +} diff --git a/src/JsonInspector/Node.java b/src/JsonInspector/Node.java index 7f89e64..94099cf 100644 --- a/src/JsonInspector/Node.java +++ b/src/JsonInspector/Node.java @@ -20,8 +20,32 @@ public class Node { } + private void findType(String value) { + if (value.charAt(0) == '"' && value.charAt(value.length()-1) == '"') { + values.add(new Value(Tree.cleanOpeningExpression(value))); + return; + } + + try { + values.add(new Value(Integer.parseInt(value))); + } catch (NumberFormatException nfeInt) { + try { + values.add(new Value(Double.parseDouble(value))); + } catch (NumberFormatException nfeDouble) { + values.add(new Value(new Other(value))); + } + } + } + + public void add(T value) { - values.add(new Value(value)); + if (value != null) { + if (value.getClass().equals(Node.class)) { + values.add(new Value(value)); + } else { + findType((String) value); + } + } } diff --git a/src/JsonInspector/Other.java b/src/JsonInspector/Other.java new file mode 100644 index 0000000..5a73f4b --- /dev/null +++ b/src/JsonInspector/Other.java @@ -0,0 +1,16 @@ +package JsonInspector; + +public class Other { + String value; + + + public Other(String value) { + this.value = value; + } + + + @Override + public String toString() { + return value; + } +}