com.metaeffekt.artifact.analysis.utils.SegmentationUtils Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.metaeffekt.artifact.analysis.utils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SegmentationUtils {
public Map> getLicenseFileMap(File metascanResultJson, boolean segmentAsNumber) throws IOException {
Map> licenseFileMap = new HashMap<>();
JSONArray files = new JSONArray(FileUtils.readFileToString(metascanResultJson, StandardCharsets.UTF_8));
for (int i = 0; i < files.length(); i++) {
JSONObject file = files.optJSONObject(i);
if (file == null) continue;
String fileName = file.optString("file");
JSONObject segments = file.optJSONObject("segments");
if (segments == null) continue;
for (String segment : segments.keySet()) {
JSONArray resolvedLicensesObject = segments.optJSONObject(segment).optJSONArray("resolvedLicenses");
if (resolvedLicensesObject == null) continue;
ArrayList resolvedLicenses = new ArrayList<>();
for (int j = 0; j < resolvedLicensesObject.length(); j++) {
resolvedLicenses.add(resolvedLicensesObject.optString(j));
}
for (String license : resolvedLicenses) {
String asNumber = "/" + fileName + "/" + segment.substring(segment.indexOf("-") + 1);
if (!licenseFileMap.containsKey(license)) {
List fileList = new ArrayList<>();
if (segmentAsNumber) {
fileList.add(asNumber);
} else {
fileList.add("/" + fileName + "/" + segment);
}
licenseFileMap.put(license, fileList);
} else {
if (segmentAsNumber) {
licenseFileMap.get(license).add(asNumber);
} else {
licenseFileMap.get(license).add("/" + fileName + "/" + segment);
}
}
}
}
}
return licenseFileMap;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy