From 3b3897abb3b8ef369e5dc1c87122f38d24867376 Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Sun, 11 Dec 2022 12:53:30 +0100 Subject: [PATCH] finished spec parser --- .../FedoraParser/FedoraParser.java | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/packageviewer/FedoraParser/FedoraParser.java b/src/main/java/fr/packageviewer/FedoraParser/FedoraParser.java index c5d0421..8505f67 100644 --- a/src/main/java/fr/packageviewer/FedoraParser/FedoraParser.java +++ b/src/main/java/fr/packageviewer/FedoraParser/FedoraParser.java @@ -21,6 +21,8 @@ public class FedoraParser { * that didn't had an update for a long time, the branch "main" doesn't * exist, it's "master", so in the future we'll need to address this * case + * + * Handle 404 errors lol * */ // create a new http client @@ -71,24 +73,45 @@ public class FedoraParser { return searchedPackagesList; } - public Map parseSpecFile(String spec){ - Map results = new HashMap<>(); + public JSONObject parseSpecFile(String spec){ + JSONObject json = new JSONObject(); + + // resolve macros + int baseindex = spec.indexOf("%define"); + while(baseindex != -1){ + baseindex += 8; + while(spec.charAt(baseindex) == ' ')baseindex++; + String macroName = spec.substring(baseindex, spec.indexOf(" ", baseindex)); + String macroValue = spec.substring(spec.indexOf(" ", baseindex),spec.indexOf("\n", baseindex)).trim(); + spec = spec.replaceAll("%\\{"+ macroName +"\\}", macroValue); + baseindex = spec.indexOf("%define",baseindex); + } + + // parse version + int index = spec.indexOf("Version:")+8; + String version = spec.substring(index, spec.indexOf("\n",index)).trim(); // parse description - String descriptionStart = spec.substring(spec.indexOf("%description")+13); - String description = descriptionStart.substring(0,descriptionStart.indexOf("%")); + index = spec.indexOf("%description")+13; + String description = spec.substring(index,spec.indexOf("%",index)); // parse dependencies - int baseindex = spec.indexOf("\nRequires:"); + baseindex = spec.indexOf("\nRequires:"); + JSONArray depedencies = new JSONArray(); while(baseindex != -1){ baseindex += 10; while(spec.charAt(baseindex) == ' ')baseindex++; String dep = spec.substring(baseindex,spec.indexOf("\n", baseindex)); if(dep.contains(" ")) dep = dep.substring(0, dep.indexOf(" ")); - System.out.println(dep); + depedencies.put(dep); baseindex = spec.indexOf("\nRequires:",baseindex); } - return results; + + json.put("depedencies", depedencies); + json.put("description", description); + json.put("version",version); + System.out.println(json); + return json; } public Package getPackageTree(String packageName, int depth) {