Skip to content

Commit

Permalink
licenses.xsd fixes and tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Diederich committed Nov 19, 2023
1 parent f2fe78d commit fd62d4f
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<extendedInfo>true</extendedInfo>
<writeCalcFile>true</writeCalcFile>
<writeExcelFile>true</writeExcelFile>
<useXsd>true</useXsd>
<licenseMerges>
<licenseMerge>The Apache Software License, Version 2.0|Apache License, Version 2.0|Apache Public License
2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static boolean searchTextInExcel(Sheet sheet, String searchText) {
if (cellValue == searchText) {
return true
} else {
log2.log(Level.FINEST, "Cell Value: {}", cellValue)
log2.log(Level.FINEST, "Cell Value: {0}", cellValue)
}
}
}
Expand All @@ -56,7 +56,6 @@ static boolean searchTextInExcel(Sheet sheet, String searchText) {
}

// -------------- Excel ----------------------

excelFile = new File(basedir, 'target/generated-resources/licenses.xlsx')
assert excelFile.exists()
assert excelFile.length() > 100
Expand Down Expand Up @@ -84,4 +83,8 @@ try (OdfSpreadsheetDocument spreadsheet = OdfSpreadsheetDocument.loadDocument(ca
List<OdfTable> tableList = spreadsheet.getTableList()
OdfTable table = tableList.get(0)
assert table.getRowCount() >= 3
}
}

// ----------- Check for XSD file ----------------
licensesXsd = new File(basedir, 'target/generated-resources/licenses.xsd')
assert licensesXsd.exists()
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,15 @@ public abstract class AbstractDownloadLicensesMojo extends AbstractLicensesXmlMo
@Parameter(property = "license.writeVersions", defaultValue = "true")
private boolean writeVersions;

/**
* If {@code true}, a <code>licenses.xsd</code> file will be written in the same directory as the
* XML file, and be referenced in the XML file.
*
* @since 2.4
*/
@Parameter(property = "license.useXsd", defaultValue = "false")
private boolean useXsd;

/**
* Connect timeout in milliseconds passed to the HTTP client when downloading licenses from remote URLs.
*
Expand Down Expand Up @@ -924,7 +933,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
private void writeLicenseSummaries(
List<ProjectLicenseInfo> depProjectLicenses, File outputFile, File excelOutputFile, File calcOutputFile)
throws ParserConfigurationException, TransformerException, IOException {
writeLicenseSummary(depProjectLicenses, outputFile, writeVersions);
writeLicenseSummary(depProjectLicenses, outputFile, writeVersions, useXsd);
if (writeExcelFile) {
ExcelFileWriter.write(depProjectLicenses, excelOutputFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ protected Path[] getAutodetectEolFiles() {
return new Path[] {licensesOutputFile.toPath()};
}

protected void writeLicenseSummary(List<ProjectLicenseInfo> deps, File licensesOutputFile, boolean writeVersions)
protected void writeLicenseSummary(
List<ProjectLicenseInfo> deps, File licensesOutputFile, boolean writeVersions, boolean useXsd)
throws ParserConfigurationException, TransformerException, IOException {
initEncoding();
LicenseSummaryWriter.writeLicenseSummary(
deps, licensesOutputFile, charset, licensesOutputFileEol, writeVersions);
deps, licensesOutputFile, charset, licensesOutputFileEol, writeVersions, useXsd);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public class LicensesXmlInsertVersionsMojo extends AbstractLicensesXmlMojo {
@Parameter(property = "license.skipDownloadLicenses", defaultValue = "false")
private boolean skipDownloadLicenses;

/**
* If {@code true}, a <code>licenses.xsd</code> file will be written in the same directory as the
* XML file, and be referenced in the XML file.
*
* @since 2.4
*/
@Parameter(property = "license.useXsd", defaultValue = "false")
private boolean useXsd;

public void execute() throws MojoExecutionException, MojoFailureException {

if (skipDownloadLicenses) {
Expand Down Expand Up @@ -152,7 +161,7 @@ public ArtifactFilters getArtifactFilters() {
dependencyLicenseInfo.setVersion(dependency.getVersion());
}

writeLicenseSummary(projectLicenseInfos, licensesOutputFile, true);
writeLicenseSummary(projectLicenseInfos, licensesOutputFile, true, useXsd);
} catch (MojoFailureException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
Expand All @@ -51,7 +55,10 @@
import org.codehaus.mojo.license.Eol;
import org.codehaus.mojo.license.extended.ExtendedInfo;
import org.codehaus.mojo.license.extended.InfoFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
Expand All @@ -62,14 +69,44 @@
* @since 1.0
*/
public class LicenseSummaryWriter {

private static final Logger LOG = LoggerFactory.getLogger(LicenseSummaryWriter.class);

static final String LICENSES_XSD_FILE = "licenses.xsd";
private static final String LICENSE_FOLDER = "/org/codehaus/mojo/license/";
static final String LICENSE_PATH = LICENSE_FOLDER + LICENSES_XSD_FILE;

public static void writeLicenseSummary(
List<ProjectLicenseInfo> dependencies, File outputFile, Charset charset, Eol eol, boolean writeVersions)
List<ProjectLicenseInfo> dependencies,
File outputFile,
Charset charset,
Eol eol,
boolean writeVersions,
boolean useXsd)
throws ParserConfigurationException, TransformerException, IOException {
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = fact.newDocumentBuilder();
Document doc = parser.newDocument();

Node root = doc.createElement("licenseSummary");
Element root = doc.createElement("licenseSummary");

if (useXsd) {
root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
root.setAttribute("xsi:noNamespaceSchemaLocation", LICENSES_XSD_FILE);
Path targetXsd = Paths.get(outputFile.toPath().getParent().toFile().getAbsolutePath(), LICENSES_XSD_FILE);
File targetFile = targetXsd.toFile();
if (targetFile.exists()) {
LOG.debug("Target XSD file {} already does exist", targetXsd);
} else {
try (InputStream inputStream = LicenseSummaryWriter.class.getResourceAsStream(LICENSE_PATH)) {
if (inputStream == null) {
throw new NoSuchFileException(LICENSE_PATH);
}
Files.copy(inputStream, targetXsd);
}
}
}

doc.appendChild(root);
Node dependenciesNode = doc.createElement("dependencies");
root.appendChild(dependenciesNode);
Expand Down
Loading

0 comments on commit fd62d4f

Please sign in to comment.