address problematic edge-cases

This commit is contained in:
Djalim Simaila 2022-12-11 15:44:23 +01:00
parent 3b3897abb3
commit e489959886
3 changed files with 82 additions and 13 deletions

View File

@ -28,10 +28,13 @@ public class FedoraParser {
// create a new http client // create a new http client
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
// and create its url // and create its url
HttpRequest request = HttpRequest.newBuilder(URI.create("https://src.fedoraproject.org/rpms/"+packageName+"/raw/main/f/"+packageName+".spec")).build(); String url = "https://src.fedoraproject.org/rpms/"+packageName+"/raw/main/f/"+packageName+".spec";
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
// send its url and return the string given // send its url and return the string given
try { try {
return client.send(request, HttpResponse.BodyHandlers.ofString()).body(); String response = client.send(request, HttpResponse.BodyHandlers.ofString()).body();
if(response.contains("<!DOCTYPE html>")) return "";
return response;
} catch (IOException|InterruptedException e) { } catch (IOException|InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -75,8 +78,8 @@ public class FedoraParser {
public JSONObject parseSpecFile(String spec){ public JSONObject parseSpecFile(String spec){
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
if (spec == "") return json;
// resolve macros // resolve macros
int baseindex = spec.indexOf("%define"); int baseindex = spec.indexOf("%define");
while(baseindex != -1){ while(baseindex != -1){
baseindex += 8; baseindex += 8;
@ -90,27 +93,32 @@ public class FedoraParser {
// parse version // parse version
int index = spec.indexOf("Version:")+8; int index = spec.indexOf("Version:")+8;
String version = spec.substring(index, spec.indexOf("\n",index)).trim(); String version = spec.substring(index, spec.indexOf("\n",index)).trim();
// parse description // parse description
index = spec.indexOf("%description")+13; index = spec.indexOf("%description")+13;
String description = spec.substring(index,spec.indexOf("%",index)); String description = spec.substring(index,spec.indexOf("%",index));
// parse name
index = spec.indexOf("Name:")+5;
String name = spec.substring(index, spec.indexOf("\n",index)).trim();
// parse dependencies // parse dependencies
baseindex = spec.indexOf("\nRequires:"); baseindex = spec.indexOf("\nRequires:");
JSONArray depedencies = new JSONArray(); JSONArray depedencies = new JSONArray();
while(baseindex != -1){ while(baseindex != -1){
baseindex += 10; baseindex += 10;
while(spec.charAt(baseindex) == ' ')baseindex++; while(spec.charAt(baseindex) == ' '|spec.charAt(baseindex) == '\t')baseindex++;
String dep = spec.substring(baseindex,spec.indexOf("\n", baseindex)); String dep = spec.substring(baseindex,spec.indexOf("\n", baseindex));
if(dep.contains(" ")) dep = dep.substring(0, dep.indexOf(" ")); if(dep.contains(" ")) dep = dep.substring(0, dep.indexOf(" "));
depedencies.put(dep); if(!dep.contains("%")){;
depedencies.put(dep);
}
baseindex = spec.indexOf("\nRequires:",baseindex); baseindex = spec.indexOf("\nRequires:",baseindex);
} }
json.put("name", name);
json.put("depedencies", depedencies); json.put("depedencies", depedencies);
json.put("description", description); json.put("description", description);
json.put("version",version); json.put("version",version);
System.out.println(json);
return json; return json;
} }
@ -119,8 +127,29 @@ public class FedoraParser {
List<Package> deps = new ArrayList<>(); List<Package> deps = new ArrayList<>();
// parse the json // parse the json
String spec = getPackageFromAPI(packageName); JSONObject json = parseSpecFile(getPackageFromAPI(packageName));
return new Package("name", "version", "repo", "description", deps); if(json.isEmpty()){
} return new Package(packageName+"(not found)", "N/A", "N/A", "N/A", Collections.emptyList());
}
// get infos except dependencies
name = json.getString("name");
version = json.getString("version");
repo = "rpms/"+packageName;
description = json.getString("description");
// if we're at the maximum depth, return the package without its dependencies
if(depth==0){
return new Package(name, version, repo, description, Collections.emptyList());
}
else {
// iterate for every package in the list
for (Object depPackageNameObj : json.getJSONArray("depedencies")) {
// convert object into String
String depPackageName = (String) depPackageNameObj;
// add package into Package List
deps.add(getPackageTree(depPackageName, depth - 1));
}
return new Package(name, version, repo, description, deps);
}
}
} }

View File

@ -4,7 +4,7 @@ import java.util.List;
public class Package extends SearchedPackage { public class Package extends SearchedPackage {
String version; String version;
List<Package> deps; public List<Package> deps;
public Package(String name, String version, String repo, String description, List<Package> deps) { public Package(String name, String version, String repo, String description, List<Package> deps) {
super(name, repo, description); super(name, repo, description);
@ -12,4 +12,20 @@ public class Package extends SearchedPackage {
this.deps = deps; this.deps = deps;
} }
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public List<Package> getDeps() {
return deps;
}
public void setDeps(List<Package> deps) {
this.deps = deps;
}
} }

View File

@ -11,4 +11,28 @@ public class SearchedPackage {
this.description = desciption; this.description = desciption;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRepo() {
return repo;
}
public void setRepo(String repo) {
this.repo = repo;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} }