Skip to content

Commit

Permalink
Working version
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoborges committed Jul 13, 2024
1 parent 4d9cd9d commit 69da127
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 5,456 deletions.
196 changes: 196 additions & 0 deletions rules-catalogue/rules-catalogue.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.microsoft.appcat.rulescatalogue;

import java.util.List;
import javax.xml.transform.stream.StreamSource;

public record Catalogue(String name, String directoryPath, StreamSource xsltTemplate) {

public record Catalogue(String name, List<String> directories, String templateId) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.xml.transform.Transformer;
Expand All @@ -19,64 +18,68 @@ public class Generator {
System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] %5$s %n");
}

static String XSLT_MARKDOWN_TEMPLATE = "rules-catalogue/src/main/resources/markdown.xslt";
static String XSLT_MARKDOWN_TEMPLATE_PATH = "rules-catalogue/src/main/resources/";

static StreamSource XSLT_AZURE_TEMPLATE = new StreamSource(XSLT_MARKDOWN_TEMPLATE_PATH + "azure-template.xslt");
static StreamSource XSLT_GENERIC_TEMPLATE = new StreamSource(XSLT_MARKDOWN_TEMPLATE_PATH + "generic-template.xslt");

static String OUTPUT = "rules-catalogue/target/rules-catalogue.md";

static TransformerFactory factory = TransformerFactory.newInstance();

public static void main(String[] args) {

if (args.length == 1) {
OUTPUT = args[0];
}

var catalogues = List.of(
new Catalogue("Azure", List.of(
"rules/rules-reviewed/azure",
"rules/rules-overridden-azure"), "azure"),
new Catalogue("OpenJDK 11", List.of(
"rules/rules-reviewed/openjdk11/openjdk8"), "generic"),
new Catalogue("OpenJDK 17", List.of(
"rules/rules-reviewed/openjdk17/openjdk11"), "generic"),
new Catalogue("OpenJDK 21", List.of(
"rules/rules-reviewed/openjdk21/openjdk17"), "generic"),
new Catalogue("Cloud Readiness", List.of(
"rules/rules-reviewed/cloud-readiness"), "generic"),
new Catalogue("Linux", List.of(
"rules/rules-reviewed/os/windows"), "generic"));

var markdownFile = new File("rules-catalogue/target/rules-catalogue.md");
new Catalogue("Azure", "rules/rules-reviewed/azure", XSLT_AZURE_TEMPLATE),
new Catalogue("OpenJDK 11", "rules/rules-reviewed/openjdk11/openjdk8", XSLT_GENERIC_TEMPLATE),
new Catalogue("OpenJDK 17", "rules/rules-reviewed/openjdk17/openjdk11", XSLT_GENERIC_TEMPLATE),
new Catalogue("OpenJDK 21", "rules/rules-reviewed/openjdk21/openjdk17", XSLT_GENERIC_TEMPLATE),
new Catalogue("Cloud Readiness", "rules/rules-reviewed/cloud-readiness", XSLT_GENERIC_TEMPLATE),
new Catalogue("Linux", "rules/rules-reviewed/os/windows", XSLT_GENERIC_TEMPLATE),
new Catalogue("Azure (overridden rules)", "rules/rules-overridden-azure", XSLT_AZURE_TEMPLATE));

var markdownFile = new File(OUTPUT);
if (markdownFile.exists()) {
markdownFile.delete();
}

try (FileOutputStream outputFile = new FileOutputStream(markdownFile, true)) {
// 1. Instantiate a TransformerFactory.
var factory = TransformerFactory.newInstance();

// 2. Use the TransformerFactory to create a Transformer for the XSLT.
var xslt = new StreamSource("rules-catalogue/src/main/resources/markdown.xslt");
var transformer = factory.newTransformer(xslt);

// Create a Result object to capture the output.
var output = new StreamResult(outputFile);

boolean headerWritten = false;

// start loop here
for (var entry : catalogues) {
var catalogueName = entry.name();
var directories = entry.directories();

transformer.setParameter("catalogueName", catalogueName);
transformer.setParameter("templateId", entry.templateId());

for (String directoryPath : directories) {
var directory = new File(directoryPath);

processDirectory(transformer, output, headerWritten, directoryPath, directory);
}
// log which catalogue is being parsed
LOGGER.info("Parsing catalogue: " + entry.name());
parseCatalogue(output, entry);
}
System.out.println("Transformation completed successfully.");
LOGGER.info("Transformation completed successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}

private static void processDirectory(Transformer transformer, StreamResult output, boolean headerWritten,
private static void parseCatalogue(StreamResult output,
Catalogue entry) throws Exception {

var transformer = factory.newTransformer(entry.xsltTemplate());
var catalogueName = entry.name();
var directoryPath = entry.directoryPath();

boolean includeHeader = true;
transformer.setParameter("catalogueName", catalogueName);
transformer.setParameter("includeHeader", "true");

var directory = new File(directoryPath);
processDirectory(entry, transformer, output, includeHeader, directoryPath, directory);
}

private static void processDirectory(Catalogue entry, Transformer transformer, StreamResult output,
boolean includeHeader,
String directoryPath, File directory) throws Exception {
if (!directory.exists()) {
LOGGER.warning("Directory does not exist: " + directoryPath);
Expand All @@ -87,15 +90,17 @@ private static void processDirectory(Transformer transformer, StreamResult outpu
for (var file : directory.listFiles()) {
if (isValidRulesetFile(file)) {
LOGGER.info("Processing file: " + file.getAbsolutePath());
transformer.setParameter("filename", file.getName());
var filename = file.getName();
transformer.setParameter("filename", filename);

transformer.transform(new StreamSource(file), output);

if (!headerWritten) {
headerWritten = true;
if (includeHeader) {
includeHeader = false;
transformer.setParameter("includeHeader", "false");
}
} else if (file.isDirectory() && !file.getName().equals("tests")) {
processDirectory(transformer, output, headerWritten, directoryPath, file);
processDirectory(entry, transformer, output, includeHeader, directoryPath, file);
}
}
}
Expand Down

This file was deleted.

66 changes: 66 additions & 0 deletions rules-catalogue/src/main/resources/azure-template.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://windup.jboss.org/schema/jboss-ruleset">
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="*"/>

<xsl:param name="catalogueName" select="'AppCAT'"/>
<xsl:param name="filename" select="'unknown.xml'"/>
<xsl:param name="includeHeader" select="'true'"/>

<!-- Template for the part to be invoked once -->
<xsl:template name="header">
<xsl:text>&#10;</xsl:text>
<xsl:text># </xsl:text>
<xsl:value-of select="$catalogueName"/>
<xsl:text> Rules &#10;</xsl:text>
<xsl:text>| ID | Title | Category | Effort | Azure App Service | Azure Container Apps | Azure Kubernetes Service | Azure Spring Apps | Source file | &#10;</xsl:text>
<xsl:text>| --- | --- | --- | --- | --- | --- | --- | --- | --- | &#10;</xsl:text>
</xsl:template>

<!-- Template for the part to be invoked multiple times -->
<xsl:template name="body">
<xsl:for-each select="//ns:rule">
<xsl:text>| </xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> | </xsl:text>
<xsl:value-of select=".//ns:hint/@title"/>
<xsl:text> | </xsl:text>
<xsl:value-of select=".//ns:hint/@category-id"/>
<xsl:text> | </xsl:text>
<xsl:value-of select=".//ns:hint/@effort"/>
<xsl:text> | </xsl:text>
<xsl:choose>
<xsl:when test="boolean(ancestor::ns:ruleset/ns:metadata/ns:targetTechnology[@id='azure-appservice'])">X</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
<xsl:text> | </xsl:text>
<xsl:choose>
<xsl:when test="boolean(ancestor::ns:ruleset/ns:metadata/ns:targetTechnology[@id='azure-container-apps'])">X</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
<xsl:text> | </xsl:text>
<xsl:choose>
<xsl:when test="boolean(ancestor::ns:ruleset/ns:metadata/ns:targetTechnology[@id='azure-aks'])">X</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
<xsl:text> | </xsl:text>
<xsl:choose>
<xsl:when test="boolean(ancestor::ns:ruleset/ns:metadata/ns:targetTechnology[@id='azure-spring-apps'])">X</xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
<xsl:text> | </xsl:text>
<xsl:value-of select="$filename"/>
<xsl:text> |&#10;</xsl:text>
</xsl:for-each>
</xsl:template>

<!-- Main template to orchestrate the transformations -->
<xsl:template match="/">
<!-- Call the header template once -->
<xsl:if test="$includeHeader = 'true'">
<xsl:call-template name="header"/>
</xsl:if>

<!-- Apply the rule template to each rule element -->
<xsl:call-template name="body" />
</xsl:template>
</xsl:stylesheet>
20 changes: 0 additions & 20 deletions rules-catalogue/src/main/resources/extract.xslt

This file was deleted.

64 changes: 64 additions & 0 deletions rules-catalogue/src/main/resources/generic-template.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://windup.jboss.org/schema/jboss-ruleset">
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="*"/>

<xsl:param name="catalogueName" select="'AppCAT'"/>
<xsl:param name="filename" select="'unknown.xml'"/>
<xsl:param name="includeHeader" select="'true'"/>

<!-- Template for the part to be invoked once -->
<xsl:template name="header">
<xsl:text>&#10;</xsl:text>
<xsl:text># </xsl:text>
<xsl:value-of select="$catalogueName"/>
<xsl:text> Rules &#10;</xsl:text>
<xsl:text>| ID | Title | Category | Effort | Source file | &#10;</xsl:text>
<xsl:text>| --- | --- | --- | --- | --- | &#10;</xsl:text>
</xsl:template>

<!-- Template for the part to be invoked multiple times -->
<xsl:template name="body">
<xsl:for-each select="//ns:rule">
<xsl:text>| </xsl:text>
<xsl:value-of select="@id" />
<xsl:text> | </xsl:text>
<!-- Check for hint or classification -->
<xsl:choose>
<!-- If hint exists -->
<xsl:when test=".//ns:hint">
<xsl:value-of select=".//ns:hint/@title"/>
<xsl:text> | </xsl:text>
<xsl:value-of select=".//ns:hint/@category-id"/>
<xsl:text> | </xsl:text>
<xsl:value-of select=".//ns:hint/@effort"/>
</xsl:when>
<!-- If classification exists -->
<xsl:when test=".//ns:classification">
<xsl:value-of select=".//ns:classification/@title"/>
<xsl:text> | </xsl:text>
<xsl:value-of select=".//ns:classification/@category-id"/>
<xsl:text> | </xsl:text>
<xsl:value-of select=".//ns:classification/@effort"/>
</xsl:when>
<!-- Default case if neither exists -->
<xsl:otherwise>
<xsl:text>Not Available | Not Available | Not Available</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text> | </xsl:text>
<xsl:value-of select="$filename"/>
<xsl:text> | &#10;</xsl:text>
</xsl:for-each>
</xsl:template>

<!-- Main template to orchestrate the transformations -->
<xsl:template match="/">
<!-- Call the header template once -->
<xsl:if test="$includeHeader = 'true'">
<xsl:call-template name="header"/>
</xsl:if>

<!-- Apply the rule template to each rule element -->
<xsl:call-template name="body" />
</xsl:template>
</xsl:stylesheet>
Loading

0 comments on commit 69da127

Please sign in to comment.