You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by cl...@apache.org on 2026/03/07 15:06:29 UTC
(creadur-rat) 02/04: changed to IODescriptor for input/output configuration streams
This is an automated email from the ASF dual-hosted git repository.
claude pushed a commit to branch feature/restructure
in repository https://gitbox.apache.org/repos/asf/creadur-rat.git
commit 636ec15ee3cabc34ec975f5233bc13187b101d65
Author: Claude Warren <cl...@xenei.com>
AuthorDate: Fri Mar 6 18:02:00 2026 +0000
changed to IODescriptor for input/output configuration streams
---
.../java/org/apache/rat/ReportConfiguration.java | 69 ++++++++++++++++++----
.../org/apache/rat/commandline/StyleSheets.java | 24 ++++----
2 files changed, 70 insertions(+), 23 deletions(-)
diff --git a/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java b/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java
index 9edf694e..82ce4920 100644
--- a/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java
+++ b/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java
@@ -81,6 +81,8 @@ import org.xml.sax.SAXException;
*/
public class ReportConfiguration {
+ /** The IODescriptor for System.out */
+ public static final IODescriptor<OutputStream> SYSTEM_OUT = new IODescriptor<>("System.out", () -> CloseShieldOutputStream.wrap(System.out));
/**
* The styles of processing for various categories of documents.
*/
@@ -130,11 +132,11 @@ public class ReportConfiguration {
/**
* The IOSupplier that provides the output stream to write the report to.
*/
- private IOSupplier<OutputStream> out;
+ private IODescriptor<OutputStream> out;
/**
* The IOSupplier that provides the stylesheet to style the XML output.
*/
- private IOSupplier<InputStream> styleSheet;
+ private IODescriptor<InputStream> styleSheet;
/**
* A list of files to read file names from.
@@ -458,6 +460,15 @@ public class ReportConfiguration {
* the report with.
*/
public IOSupplier<InputStream> getStyleSheet() {
+ return styleSheet.ioSupplier();
+ }
+
+ /**
+ * Gets the IODescriptor of the style sheet.
+ * @return the Supplier of the InputStream that is the XSLT style sheet to style
+ * the report with.
+ */
+ public IODescriptor<InputStream> getStyleSheetDescriptor() {
return styleSheet;
}
@@ -467,7 +478,7 @@ public class ReportConfiguration {
* multiple times.
* @param styleSheet the XSLT style sheet to style the report with.
*/
- public void setStyleSheet(final IOSupplier<InputStream> styleSheet) {
+ public void setStyleSheet(final IODescriptor<InputStream> styleSheet) {
this.styleSheet = styleSheet;
}
@@ -479,7 +490,7 @@ public class ReportConfiguration {
*/
public void setFrom(final Defaults defaults) {
licenseSetFactory.add(defaults.getLicenseSetFactory());
- if (getStyleSheet() == null) {
+ if (getStyleSheetDescriptor() == null) {
setStyleSheet(StyleSheets.PLAIN.getStyleSheet());
}
defaults.getStandardExclusion().forEach(this::addExcludedCollection);
@@ -515,7 +526,7 @@ public class ReportConfiguration {
*/
public void setStyleSheet(final URL styleSheet) {
Objects.requireNonNull(styleSheet, "Stylesheet file must not be null");
- setStyleSheet(styleSheet::openStream);
+ setStyleSheet(new IODescriptor<>(styleSheet.toString(), styleSheet::openStream));
}
/**
@@ -526,7 +537,7 @@ public class ReportConfiguration {
* @param out The OutputStream supplier that provides the output stream to write
* the report to. A null value will use System.out.
*/
- public void setOut(final IOSupplier<OutputStream> out) {
+ public void setOut(final IODescriptor<OutputStream> out) {
this.out = out;
}
@@ -534,7 +545,7 @@ public class ReportConfiguration {
* Sets the OutputStream supplier to use the specified file. The file may be
* opened and closed several times. File is deleted first and then may be
* repeatedly opened in append mode.
- * @see #setOut(IOSupplier)
+ * @see #setOut(IODescriptor)
* @param file The file to create the supplier with.
*/
public void setOut(final File file) {
@@ -550,7 +561,7 @@ public class ReportConfiguration {
if (!parent.mkdirs() && !parent.isDirectory()) {
DefaultLog.getInstance().warn("Unable to create directory: " + file.getParentFile());
}
- setOut(() -> new FileOutputStream(file, true));
+ setOut(new IODescriptor<>(file.toString(), () -> new FileOutputStream(file, true)));
}
/**
@@ -559,7 +570,15 @@ public class ReportConfiguration {
* @return The supplier of the output stream to write the report to.
*/
public IOSupplier<OutputStream> getOutput() {
- return out == null ? () -> CloseShieldOutputStream.wrap(System.out) : out;
+ return getOutputDescriptor().ioSupplier();
+ }
+
+ /**
+ * Returns the output IODescriptor.
+ * @return The IODescriptor of the output stream to write the report to.
+ */
+ public IODescriptor<OutputStream> getOutputDescriptor() {
+ return out == null ? SYSTEM_OUT : out;
}
/**
@@ -870,7 +889,9 @@ public class ReportConfiguration {
.attribute("listLicenses", listLicenses.name())
.attribute("dryRun", Boolean.toString(dryRun))
.attribute("archiveProcessing", getArchiveProcessing().name())
- .attribute("standardProcessing", getStandardProcessing().name());
+ .attribute("standardProcessing", getStandardProcessing().name())
+ .attribute("stylesheet", styleSheet.name())
+ .attribute("output", out.name());
if (StringUtils.isNotEmpty(copyrightMessage)) {
writer.openElement("copyrightMessage").content(copyrightMessage).closeElement();
}
@@ -903,7 +924,7 @@ public class ReportConfiguration {
}
}
- public void deserialize(final IOSupplier<InputStream> inputStreamSupplier) throws IOException {
+ public void deserialize(final IOSupplier<InputStream> inputStreamSupplier, final DocumentName workingDirectory) throws IOException {
DocumentBuilder builder;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
@@ -927,6 +948,18 @@ public class ReportConfiguration {
dryRun = Boolean.parseBoolean(attributes.get("dryRun"));
archiveProcessing = Processing.valueOf(attributes.get("archiveProcessing"));
standardProcessing = Processing.valueOf(attributes.get("standardProcessing"));
+ String styleName = attributes.get("stylesheet");
+ if (styleName != null) {
+ styleSheet = StyleSheets.getStyleSheet(styleName, workingDirectory);
+ }
+ String outputName = attributes.get("output");
+ if (outputName != null) {
+ if (outputName.equals(ReportConfiguration.SYSTEM_OUT.name())) {
+ out = ReportConfiguration.SYSTEM_OUT;
+ } else {
+ out = IODescriptor.output(outputName, workingDirectory);
+ }
+ }
XMLConfigurationReader.nodeListConsumer(document.getElementsByTagName("source"), lNode -> {
Map<String, String> nAttributes = XMLConfigurationReader.attributes(lNode);
@@ -951,11 +984,23 @@ public class ReportConfiguration {
}
}
-
private record DeserializedReportable(DocumentName name) implements IReportable {
@Override
public void run(final RatReport report) throws RatException {
throw new RatException("Attempt to run a deserialized reportable");
}
}
+
+ public record IODescriptor<T>(String name, IOSupplier<T> ioSupplier) {
+ /**
+ * Creates an output IODescriptor for the file name within the working directory.
+ * @param name the name of the file to open.
+ * @param workingDirectory the working directory for the file.
+ * @return the Output IODescriptor.
+ */
+ static IODescriptor<OutputStream> output(final String name, final DocumentName workingDirectory) {
+ DocumentName docName = workingDirectory.resolve(name);
+ return new IODescriptor<OutputStream>(name, () -> new FileOutputStream(docName.asFile()));
+ }
+ };
}
diff --git a/apache-rat-core/src/main/java/org/apache/rat/commandline/StyleSheets.java b/apache-rat-core/src/main/java/org/apache/rat/commandline/StyleSheets.java
index 0a2983f1..f9e47cc2 100644
--- a/apache-rat-core/src/main/java/org/apache/rat/commandline/StyleSheets.java
+++ b/apache-rat-core/src/main/java/org/apache/rat/commandline/StyleSheets.java
@@ -23,8 +23,8 @@ import java.net.URL;
import java.nio.file.Files;
import java.util.Objects;
-import org.apache.commons.io.function.IOSupplier;
import org.apache.rat.ConfigurationException;
+import org.apache.rat.ReportConfiguration;
import org.apache.rat.document.DocumentName;
import static java.lang.String.format;
@@ -73,27 +73,29 @@ public enum StyleSheets {
}
/**
- * Gets the IOSupplier for a style sheet.
- * @return an IOSupplier for the sheet.
+ * Gets the IODescriptor for a style sheet.
+ * @return an IODescriptor for the sheet.
*/
- public IOSupplier<InputStream> getStyleSheet() {
- return Objects.requireNonNull(StyleSheets.class.getClassLoader().getResource(format("org/apache/rat/%s.xsl", name)),
- "missing stylesheet: " + name)::openStream;
+ public ReportConfiguration.IODescriptor<InputStream> getStyleSheet() {
+ URL url = StyleSheets.class.getClassLoader().getResource(format("org/apache/rat/%s.xsl", name));
+ Objects.requireNonNull(url, "missing stylesheet: " + name);
+ return new ReportConfiguration.IODescriptor<>(name, url::openStream);
}
/**
- * Gets the IOSupplier for a style sheet.
+ * Gets the IODescriptor for a style sheet.
* @param name the short name for or the path to a style sheet.
- * @return the IOSupplier for the style sheet.
+ * @param workingDirectory the working directory to resolve the name against.
+ * @return the IODescriptor for the style sheet.
*/
- public static IOSupplier<InputStream> getStyleSheet(final String name, final DocumentName workingDirectory) {
+ public static ReportConfiguration.IODescriptor<InputStream> getStyleSheet(final String name, final DocumentName workingDirectory) {
URL url = StyleSheets.class.getClassLoader().getResource(format("org/apache/rat/%s.xsl", name));
if (url != null) {
- return url::openStream;
+ return new ReportConfiguration.IODescriptor<>(name, url::openStream);
}
DocumentName xslt = workingDirectory.resolve(name);
if (xslt.asFile().exists()) {
- return () -> Files.newInputStream(xslt.asFile().toPath());
+ return new ReportConfiguration.IODescriptor<>(xslt.toString(), () -> Files.newInputStream(xslt.asFile().toPath()));
}
throw new ConfigurationException(format("Stylesheet file '%s' not found", name));
}