В настоящее время я пытаюсь разобрать дерево зависимостей maven в JSON, но где-то я испортил индексирование и не могу понять где. Он просто не работает с последними 3 элементами в ArrayList. У кого-нибудь есть идеи, где я ошибся? Я занимаюсь этим уже несколько часов.
Вот мой код:
main. java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String baseText =
"[INFO] +- org.antlr:antlr4:jar:4.7.1:compile\n" +
"[INFO] | +- org.antlr:antlr4-runtime:jar:4.7.1:compile\n" +
"[INFO] | +- org.antlr:antlr-runtime:jar:3.5.2:compile\n" +
"[INFO] | \\- com.ibm.icu:icu4j:jar:58.2:compile\n" +
"[INFO] +- commons-io:commons-io:jar:1.3.2:compile\n" +
"[INFO] +- brs:dxprog-lang:jar:3.3-SNAPSHOT:compile\n" +
"[INFO] | +- brs:libutil:jar:2.51:compile\n" +
"[INFO] | | +- commons-collections:commons-collections:jar:3.2.2:compile\n" +
"[INFO] | | +- org.apache.commons:commons-collections4:jar:4.1:compile\n" +
"[INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile\n" +
"[INFO] | | | \\- com.fasterxml.jackson.core:jackson-core:jar:2.9.5:compile";
String[] arrOfStr = baseText.split("\n");
ArrayList<Dependency> depList = new ArrayList<Dependency>();
for (int i = 0; i < arrOfStr.length; i++) {
Dependency dep = new Dependency();
arrOfStr[i] = arrOfStr[i].substring(7);
arrOfStr[i] = arrOfStr[i].replace("+-", "|");
arrOfStr[i] = arrOfStr[i].replace("\\-", "|");
int level = StringUtils.countMatches(arrOfStr[i], "|");
arrOfStr[i] = arrOfStr[i].replace(" | ", "");
arrOfStr[i] = arrOfStr[i].replace("| ", "");
String[] linjeArr = arrOfStr[i].split(":");
dep.setArtifact(linjeArr[0]);
dep.setGroup(linjeArr[1]);
dep.setScope(linjeArr[4]);
dep.setVersion(linjeArr[3]);
dep.setLevel(level);
depList.add(dep);
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(childSorter(depList, 0)));
}
public static ArrayList<Dependency> childSorter(ArrayList<Dependency> depList, int startIndex) {
try {
for (int i = startIndex; i < depList.size()-1; i++) {
if (depList.get(i).getLevel() < depList.get(i + 1).getLevel()) {
depList.get(i).addSubDependency(depList.get(i + 1));
int counter = i + 1;
do {
if (!(i + 1 > depList.size() - 1)) {
if (depList.get(counter).getLevel() == depList.get(i + 1).getLevel()) {
depList.get(i).addSubDependency(depList.get(counter));
depList.remove(counter);
}
} else {
if (depList.get(counter).getLevel() == depList.get(i).getLevel()) {
depList.get(i).addSubDependency(depList.get(counter));
depList.remove(counter);
}
}
counter += 1;
} while (depList.get(i + 1).getLevel() == depList.get(counter).getLevel());
depList.remove(i + 1);
}
}
} catch (IndexOutOfBoundsException e) {
System.out.println(Arrays.toString(e.getStackTrace()));
}
return depList;
}
}
This мой вывод:
[
{
"Group": "antlr4",
"Artifact": "org.antlr",
"Version": "4.7.1",
"Scope": "compile",
"Level": 1,
"SubDependency": [
{
"Group": "antlr4-runtime",
"Artifact": "org.antlr",
"Version": "4.7.1",
"Scope": "compile",
"Level": 2,
"SubDependency": []
},
{
"Group": "antlr4-runtime",
"Artifact": "org.antlr",
"Version": "4.7.1",
"Scope": "compile",
"Level": 2,
"SubDependency": []
},
{
"Group": "icu4j",
"Artifact": "com.ibm.icu",
"Version": "58.2",
"Scope": "compile",
"Level": 2,
"SubDependency": []
}
]
},
{
"Group": "commons-io",
"Artifact": "commons-io",
"Version": "1.3.2",
"Scope": "compile",
"Level": 1,
"SubDependency": []
},
{
"Group": "dxprog-lang",
"Artifact": "brs",
"Version": "3.3-SNAPSHOT",
"Scope": "compile",
"Level": 1,
"SubDependency": [
{
"Group": "libutil",
"Artifact": "brs",
"Version": "2.51",
"Scope": "compile",
"Level": 2,
"SubDependency": []
},
{
"Group": "libutil",
"Artifact": "brs",
"Version": "2.51",
"Scope": "compile",
"Level": 2,
"SubDependency": []
},
{
"Group": "commons-collections4",
"Artifact": "org.apache.commons",
"Version": "4.1",
"Scope": "compile",
"Level": 3,
"SubDependency": []
}
]
},
{
"Group": "jackson-annotations",
"Artifact": "com.fasterxml.jackson.core",
"Version": "2.9.0",
"Scope": "compile",
"Level": 4,
"SubDependency": []
},
{
"Group": "jackson-core",
"Artifact": "com.fasterxml.jackson.core",
"Version": "2.9.5",
"Scope": "compile",
"Level": 4,
"SubDependency": []
}
]
И это ожидаемый вывод:
[
{
"Group": "antlr4",
"Artifact": "org.antlr",
"Version": "4.7.1",
"Scope": "compile",
"Level": 1,
"SubDependency": [
{
"Group": "antlr4-runtime",
"Artifact": "org.antlr",
"Version": "4.7.1",
"Scope": "compile",
"Level": 2,
"SubDependency": []
},
{
"Group": "antlr4-runtime",
"Artifact": "org.antlr",
"Version": "4.7.1",
"Scope": "compile",
"Level": 2,
"SubDependency": []
},
{
"Group": "icu4j",
"Artifact": "com.ibm.icu",
"Version": "58.2",
"Scope": "compile",
"Level": 2,
"SubDependency": []
}
]
},
{
"Group": "commons-io",
"Artifact": "commons-io",
"Version": "1.3.2",
"Scope": "compile",
"Level": 1,
"SubDependency": []
},
{
"Group": "dxprog-lang",
"Artifact": "brs",
"Version": "3.3-SNAPSHOT",
"Scope": "compile",
"Level": 1,
"SubDependency": [
{
"Group": "libutil",
"Artifact": "brs",
"Version": "2.51",
"Scope": "compile",
"Level": 2,
"SubDependency": []
},
{
"Group": "libutil",
"Artifact": "brs",
"Version": "2.51",
"Scope": "compile",
"Level": 2,
"SubDependency": [
{
"Group": "commons-collections4",
"Artifact": "org.apache.commons",
"Version": "4.1",
"Scope": "compile",
"Level": 3,
"SubDependency": [
{
"Group": "jackson-annotations",
"Artifact": "com.fasterxml.jackson.core",
"Version": "2.9.0",
"Scope": "compile",
"Level": 4,
"SubDependency": []
},
{
"Group": "jackson-core",
"Artifact": "com.fasterxml.jackson.core",
"Version": "2.9.5",
"Scope": "compile",
"Level": 4,
"SubDependency": []
}
]
}
]
},
]
}
]
РЕДАКТИРОВАТЬ: Форматирование в ожидаемый вывод