Skip to content

Commit

Permalink
chore: Add javadoc comments and small fix for Gson
Browse files Browse the repository at this point in the history
Signed-off-by: Taewan Kim <t25.kim@samsung.com>
  • Loading branch information
tiokim committed Jun 2, 2024
1 parent b148997 commit a162cc5
Showing 1 changed file with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,11 @@ public void runScan(LPVSQueue webhookConfig, String path) throws Exception {
public List<LPVSFile> checkLicenses(LPVSQueue webhookConfig) {
List<LPVSFile> detectedFiles = new ArrayList<>();
try {
Gson gson = new Gson();
Reader reader = getReader(webhookConfig);

// convert JSON file to map
Map<String, ArrayList<Object>> map =
gson.fromJson(
new Gson().fromJson(
reader, new TypeToken<Map<String, ArrayList<Object>>>() {}.getType());
if (null == map) {
log.error("Error parsing Json File");
Expand All @@ -166,7 +165,7 @@ public List<LPVSFile> checkLicenses(LPVSQueue webhookConfig) {
file.setFilePath(entry.getKey().toString());

String content = buildContent(entry);
ScanossJsonStructure object = getScanossJsonStructure(gson, content, file);
ScanossJsonStructure object = getScanossJsonStructure(content, file);
Set<LPVSLicense> licenses = buildLicenseSet(object);
file.setLicenses(new HashSet<>(licenses));
if (!file.getLicenses().isEmpty()) detectedFiles.add(file);
Expand All @@ -181,6 +180,12 @@ public List<LPVSFile> checkLicenses(LPVSQueue webhookConfig) {
return detectedFiles;
}

/**
* Builds a set of LPVSLicense objects detected by Scanoss in the given ScanossJsonStructure structure.
*
* @param object The ScanossJsonStructure containing information about the licenses detected by Scanoss.
* @return A set of LPVSLicense objects representing the detected licenses with additional metadata.
*/
private Set<LPVSLicense> buildLicenseSet(ScanossJsonStructure object) {
if (object.licenses == null) {
return new HashSet<>();
Expand All @@ -207,8 +212,15 @@ private Set<LPVSLicense> buildLicenseSet(ScanossJsonStructure object) {
return licenses;
}

private ScanossJsonStructure getScanossJsonStructure(Gson gson, String content, LPVSFile file) {
ScanossJsonStructure object = gson.fromJson(content, ScanossJsonStructure.class);
/**
* Parses the content returned by Scanoss and populates the given LPVSFile entity with the relevant information.
*
* @param content The string content returned by Scanoss.
* @param file The LPVSFile entity to be populated with the parsed information.
* @return The parsed ScanossJsonStructure object containing the extracted information.
*/
private ScanossJsonStructure getScanossJsonStructure(String content, LPVSFile file) {
ScanossJsonStructure object = new Gson().fromJson(content, ScanossJsonStructure.class);
if (object.id != null) file.setSnippetType(object.id);
if (object.matched != null) file.setSnippetMatch(object.matched);
if (object.lines != null) file.setMatchedLines(object.lines);
Expand All @@ -222,6 +234,13 @@ private ScanossJsonStructure getScanossJsonStructure(Gson gson, String content,
return object;
}

/**
* Creates a new BufferedReader object for reading the JSON file containing the scan results.
*
* @param webhookConfig The LPVSQueue representing the GitHub webhook configuration.
* @return A BufferedReader object for reading the JSON file.
* @throws IOException If an error occurs while creating the BufferedReader object.
*/
private static Reader getReader(LPVSQueue webhookConfig) throws IOException {
String fileName = null;
if (webhookConfig.getHeadCommitSHA() == null
Expand All @@ -243,6 +262,12 @@ private static Reader getReader(LPVSQueue webhookConfig) throws IOException {
+ ".json"));
}

/**
* Builds the content string for the given Map.Entry object.
*
* @param entry The Map.Entry object containing the information to be included in the content string.
* @return The constructed content string.
*/
private static String buildContent(Map.Entry<String, ArrayList<Object>> entry) {
String content =
entry.getValue()
Expand All @@ -267,8 +292,7 @@ private static String buildContent(Map.Entry<String, ArrayList<Object>> entry) {
if (content.endsWith("}")) {
content = content.substring(0, content.length() - 1) + "\"}";
}
content = content.replaceAll("}\"}", "\"}}");
return content;
return content.replaceAll("}\"}", "\"}}");
}

/**
Expand Down

0 comments on commit a162cc5

Please sign in to comment.