Skip to content

Commit

Permalink
Feature: HTML Report: Link artifact coordinates to Maven Central webs…
Browse files Browse the repository at this point in the history
…ite.
  • Loading branch information
smarkwal committed Sep 29, 2024
1 parent cd10705 commit 43a6ee9
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion jarhc-release-tests/src/main/resources/jar-app-toc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,7 @@ org/jarhc/model/ResourceDef.class | size=1135 | crc=4169474302
org/jarhc/pom/Dependency.class | size=3207 | crc=3070612390
org/jarhc/pom/Scope.class | size=1303 | crc=2969203394
org/jarhc/report/html/DefaultStyleProvider.class | size=1255 | crc=3575769662
org/jarhc/report/html/HtmlReportFormat.class | size=6142 | crc=3758829401
org/jarhc/report/html/HtmlReportFormat.class | size=7817 | crc=3648291066
org/jarhc/report/html/StyleProvider.class | size=171 | crc=1011181830
org/jarhc/report/list/ListReportFormat.class | size=2464 | crc=2163679850
org/jarhc/report/Report.class | size=1294 | crc=2508431441
Expand Down
2 changes: 1 addition & 1 deletion jarhc-release-tests/src/main/resources/jar-toc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ org/jarhc/model/ResourceDef.class | size=1135 | crc=4169474302
org/jarhc/pom/Dependency.class | size=3207 | crc=3070612390
org/jarhc/pom/Scope.class | size=1303 | crc=2969203394
org/jarhc/report/html/DefaultStyleProvider.class | size=1255 | crc=3575769662
org/jarhc/report/html/HtmlReportFormat.class | size=6142 | crc=3758829401
org/jarhc/report/html/HtmlReportFormat.class | size=7817 | crc=3648291066
org/jarhc/report/html/StyleProvider.class | size=171 | crc=1011181830
org/jarhc/report/list/ListReportFormat.class | size=2464 | crc=2163679850
org/jarhc/report/Report.class | size=1294 | crc=2508431441
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ List of JAR files found in classpath.

Artifact | Version | Source | Size | Classes | Resources | Checksum (SHA-1) | Coordinates
-------------------------------+-----------+-----------------------------------------------------------------+---------+---------+-----------+------------------------------------------+----------------------------------------------------------------
jarhc | [unknown] | jarhc.jar | 231 KB | 131 | 6 | **************************************** | [unknown]
jarhc | [unknown] | jarhc.jar | 232 KB | 131 | 6 | **************************************** | [unknown]
asm | 9.7 | org.ow2.asm:asm:9.7 | 122 KB | 38 | 0 | 073d7b3086e14beb604ced229c302feff6449723 | org.ow2.asm:asm:9.7
commons-codec | 1.17.1 | commons-codec:commons-codec:1.17.1 | 364 KB | 107 | 128 | 973638b7149d333563584137ebf13a691bb60579 | commons-codec:commons-codec:1.17.1
httpclient | 4.5.14 | org.apache.httpcomponents:httpclient:4.5.14 | 767 KB | 470 | 2 | 1194890e6f56ec29177673f2f12d0b8e627dec98 | org.apache.httpcomponents:httpclient:4.5.14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ List of JAR files found in classpath.

Artifact | Version | Source | Size | Classes | Resources | Checksum (SHA-1) | Coordinates
----------+-----------+---------------+---------+---------+-----------+------------------------------------------+------------
jarhc-app | [unknown] | jarhc-app.jar | 3.63 MB | 2096 | 142 | **************************************** | [unknown]
Classpath | - | - | 3.63 MB | 2096 | 142 | - | -
jarhc-app | [unknown] | jarhc-app.jar | 3.64 MB | 2096 | 142 | **************************************** | [unknown]
Classpath | - | - | 3.64 MB | 2096 | 142 | - | -

Modules
-------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ List of JAR files found in classpath.

Artifact | Version | Source | Size | Classes | Resources | Checksum (SHA-1) | Coordinates
-------------------------------+-----------+-----------------------------------------------------------------+---------+---------+-----------+------------------------------------------+----------------------------------------------------------------
jarhc | [unknown] | jarhc.jar | 231 KB | 131 | 6 | **************************************** | [unknown]
jarhc | [unknown] | jarhc.jar | 232 KB | 131 | 6 | **************************************** | [unknown]
asm | 9.7 | org.ow2.asm:asm:9.7 | 122 KB | 38 | 0 | 073d7b3086e14beb604ced229c302feff6449723 | org.ow2.asm:asm:9.7
commons-codec | 1.17.1 | commons-codec:commons-codec:1.17.1 | 364 KB | 107 | 128 | 973638b7149d333563584137ebf13a691bb60579 | commons-codec:commons-codec:1.17.1
httpclient | 4.5.14 | org.apache.httpcomponents:httpclient:4.5.14 | 767 KB | 470 | 2 | 1194890e6f56ec29177673f2f12d0b8e627dec98 | org.apache.httpcomponents:httpclient:4.5.14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ List of JAR files found in classpath.

Artifact | Version | Source | Size | Classes | Resources | Checksum (SHA-1) | Coordinates
----------+-----------+---------------+---------+---------+-----------+------------------------------------------+------------
jarhc-app | [unknown] | jarhc-app.jar | 3.63 MB | 2096 | 142 | **************************************** | [unknown]
Classpath | - | - | 3.63 MB | 2096 | 142 | - | -
jarhc-app | [unknown] | jarhc-app.jar | 3.64 MB | 2096 | 142 | **************************************** | [unknown]
Classpath | - | - | 3.64 MB | 2096 | 142 | - | -

Modules
-------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ List of JAR files found in classpath.

Artifact | Version | Source | Size | Classes | Resources | Checksum (SHA-1) | Coordinates
-------------------------------+-----------+-----------------------------------------------------------------+---------+---------+-----------+------------------------------------------+----------------------------------------------------------------
jarhc | [unknown] | jarhc.jar | 231 KB | 131 | 6 | **************************************** | [unknown]
jarhc | [unknown] | jarhc.jar | 232 KB | 131 | 6 | **************************************** | [unknown]
asm | 9.7 | org.ow2.asm:asm:9.7 | 122 KB | 38 | 0 | 073d7b3086e14beb604ced229c302feff6449723 | org.ow2.asm:asm:9.7
commons-codec | 1.17.1 | commons-codec:commons-codec:1.17.1 | 364 KB | 107 | 128 | 973638b7149d333563584137ebf13a691bb60579 | commons-codec:commons-codec:1.17.1
httpclient | 4.5.14 | org.apache.httpcomponents:httpclient:4.5.14 | 767 KB | 470 | 2 | 1194890e6f56ec29177673f2f12d0b8e627dec98 | org.apache.httpcomponents:httpclient:4.5.14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ List of JAR files found in classpath.

Artifact | Version | Source | Size | Classes | Resources | Checksum (SHA-1) | Coordinates
----------+-----------+---------------+---------+---------+-----------+------------------------------------------+------------
jarhc-app | [unknown] | jarhc-app.jar | 3.63 MB | 2096 | 142 | **************************************** | [unknown]
Classpath | - | - | 3.63 MB | 2096 | 142 | - | -
jarhc-app | [unknown] | jarhc-app.jar | 3.64 MB | 2096 | 142 | **************************************** | [unknown]
Classpath | - | - | 3.64 MB | 2096 | 142 | - | -

Modules
-------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ List of JAR files found in classpath.

Artifact | Version | Source | Size | Classes | Resources | Checksum (SHA-1) | Coordinates
----------+-----------+-----------+--------+---------+-----------+------------------------------------------+------------
jarhc | [unknown] | jarhc.jar | 231 KB | 131 | 6 | **************************************** | [unknown]
Classpath | - | - | 231 KB | 131 | 6 | - | -
jarhc | [unknown] | jarhc.jar | 232 KB | 131 | 6 | **************************************** | [unknown]
Classpath | - | - | 232 KB | 131 | 6 | - | -

Modules
-------
Expand Down
24 changes: 23 additions & 1 deletion jarhc/src/main/java/org/jarhc/report/html/HtmlReportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
import org.jarhc.report.ReportSection;
import org.jarhc.report.ReportTable;
import org.jarhc.report.writer.ReportWriter;
import org.jarhc.utils.DigestUtils;
import org.jarhc.utils.VersionUtils;
import org.slf4j.LoggerFactory;

public class HtmlReportFormat implements ReportFormat {

private static final Pattern ARTIFACT_PATTERN = Pattern.compile("([a-zA-Z0-9\\-.]+):([a-zA-Z0-9\\-.]+):([a-zA-Z0-9\\-.]+)");
private static final String ARTIFACT_URL = "https://central.sonatype.com/artifact/%s/%s/%s"; // alternative: "https://mvnrepository.com/artifact/%s/%s/%s"

private final StyleProvider styleProvider;

public HtmlReportFormat() {
Expand Down Expand Up @@ -164,12 +168,30 @@ private static void printTableRow(ReportWriter writer, String[] values, boolean
if (header) {
writer.print("<th>%s</th>", escape(value));
} else {
writer.print("<td>%s</td>", escape(value));
writer.print("<td>%s</td>", addArtifactLinks(escape(value)));
}
}
writer.println("</tr>");
}

/**
* Replace artifact coordinates with links to artifact website.
*
* @param value Report row value
* @return Report row value with artifact coordinates replaced by links
*/
private static String addArtifactLinks(String value) {
return ARTIFACT_PATTERN.matcher(value).replaceAll(matchResult -> {
String coordinates = matchResult.group();
String groupId = matchResult.group(1);
String artifactId = matchResult.group(2);
String version = matchResult.group(3);
String url = String.format(ARTIFACT_URL, groupId, artifactId, version);
String target = DigestUtils.sha1Hex(coordinates);
return String.format("<a href=\"%s\" target=\"%s\">%s</a>", url, target, coordinates);
});
}

private static String escape(String text) {
// TODO: implement full HTML escaping
text = text.replace("<", "&lt;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void test_format() throws IOException {
ReportSection section2 = new ReportSection("Section 2", joinLines("Description 2, Line 1", "Description 2, Line 2"));
ReportTable table2 = new ReportTable("Column 1", "Column 2", "Column 3");
table2.addRow("Short", "Medium Medium", "Long Long Long Long Long Long Long");
table2.addRow("Line 1", joinLines("Line 1", "Longer Line 2"), joinLines("Line 1", "Longer Line 2", "Extra Long Line 3"));
table2.addRow("Line 1", joinLines("Line 1", "Longer Line 2"), joinLines("Line 1", "Longer Line 2", "Extra Long Line 3", "commons-io:commons-io:2.8.0"));
section2.add(table2);
report.addSection(section2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ th, td {
<table>
<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>
<tr><td>Short</td><td>Medium Medium</td><td>Long Long Long Long Long Long Long</td></tr>
<tr><td>Line 1</td><td>Line 1<br>Longer Line 2</td><td>Line 1<br>Longer Line 2<br>Extra Long Line 3</td></tr>
<tr><td>Line 1</td><td>Line 1<br>Longer Line 2</td><td>Line 1<br>Longer Line 2<br>Extra Long Line 3<br><a href="https://central.sonatype.com/artifact/commons-io/commons-io/2.8.0" target="c5cea3acec23a2c37e3eb84d1f3830afb6d34f4f">commons-io:commons-io:2.8.0</a></td></tr>
</table>
</section>

Expand Down

0 comments on commit 43a6ee9

Please sign in to comment.