From a4be9f9eaea33ea88677eee2fa2092b7798296d5 Mon Sep 17 00:00:00 2001 From: Eric Barboni Date: Fri, 16 Oct 2026 14:24:04 +0200 Subject: [PATCH] RAT-Report in xml and transform to text and RAT-Report path relativation: Switch from plain string replacing to XML parsing and attribute modification --- .../org/netbeans/nbbuild/RatRelativize.java | 91 +++++++++++++++++++ .../org/netbeans/nbbuild/RatReportTask.java | 12 +-- nbbuild/build.xml | 21 ++++- 3 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 nbbuild/antsrc/org/netbeans/nbbuild/RatRelativize.java diff --git a/nbbuild/antsrc/org/netbeans/nbbuild/RatRelativize.java b/nbbuild/antsrc/org/netbeans/nbbuild/RatRelativize.java new file mode 100644 index 000000000000..3f4c28d727bb --- /dev/null +++ b/nbbuild/antsrc/org/netbeans/nbbuild/RatRelativize.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.nbbuild; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * The apache-rat tasked used to report missing/wrong licensing in the + * repository reports the resources with absolute paths. This task rewrites + * the names to be relative to the root of the repository. + */ +public class RatRelativize extends Task { + + private File file; + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + + @Override + public void execute() throws BuildException { + Path root = file.getParentFile().getParentFile().getParentFile().toPath(); + try { + // Use the DOM parser for covenience. It should be possible to + // handle this in a streaming manor, but it is much easier to load + // the 15MB, process them and dump them back. + Document doc = DocumentBuilderFactory + .newDefaultInstance() + .newDocumentBuilder() + .parse(file); + + // Fetch all resource Elements (don't work directly on the NodeList + // as this kill performance (the nodelist is refreshed on each + // modifying access to the DOM) + NodeList nl = doc.getElementsByTagName("resource"); + List elementsToModify = new ArrayList<>(nl.getLength()); + for(int i = 0; i < nl.getLength(); i++) { + elementsToModify.add((Element) nl.item(i)); + } + + for(Element resourceElement: elementsToModify) { + Path absolutePath = Path.of(resourceElement.getAttribute("name")); + Path relativePath = root.relativize(absolutePath); + resourceElement.setAttribute("name", relativePath.toString()); + } + + TransformerFactory + .newDefaultInstance() + .newTransformer() + .transform(new DOMSource(doc), new StreamResult(file)); + } catch (ParserConfigurationException | RuntimeException | SAXException | IOException | TransformerException ex) { + throw new BuildException(ex); + } + } +} diff --git a/nbbuild/antsrc/org/netbeans/nbbuild/RatReportTask.java b/nbbuild/antsrc/org/netbeans/nbbuild/RatReportTask.java index 7d1bc48a3fa9..c9fef95ee25e 100644 --- a/nbbuild/antsrc/org/netbeans/nbbuild/RatReportTask.java +++ b/nbbuild/antsrc/org/netbeans/nbbuild/RatReportTask.java @@ -25,8 +25,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; @@ -235,7 +233,7 @@ private static String writeFiles(Set listFile, String repo) { private void doPopulateUnapproved(Map moduleRATInfo, Element rootElement, XPath path) throws XPathExpressionException { NodeList evaluate = (NodeList) path.evaluate("descendant::resource[license/@approval=\"false\"]", rootElement, XPathConstants.NODESET); for (int i = 0; i < evaluate.getLength(); i++) { - String resources = relativize(evaluate.item(i).getAttributes().getNamedItem("name").getTextContent()); + String resources = evaluate.item(i).getAttributes().getNamedItem("name").getTextContent(); String moduleName = getModuleName(resources); if (!moduleRATInfo.containsKey(moduleName)) { moduleRATInfo.get(NOT_CLUSTER).addUnapproved(resources); @@ -248,7 +246,7 @@ private void doPopulateUnapproved(Map moduleRATInfo, Element private void doPopulateApproved(Map moduleRATInfo, Element rootElement, XPath path) throws XPathExpressionException { NodeList evaluate = (NodeList) path.evaluate("descendant::resource[license/@approval=\"true\"]", rootElement, XPathConstants.NODESET); for (int i = 0; i < evaluate.getLength(); i++) { - String resources = relativize(evaluate.item(i).getAttributes().getNamedItem("name").getTextContent()); + String resources = evaluate.item(i).getAttributes().getNamedItem("name").getTextContent(); String moduleName = getModuleName(resources); if (!moduleRATInfo.containsKey(moduleName)) { moduleRATInfo.get(NOT_CLUSTER).addApproved(resources); @@ -259,12 +257,6 @@ private void doPopulateApproved(Map moduleRATInfo, Element r } } - private String relativize(String target) { - Path full = Paths.get(target); - Path rootPath = root.toPath(); - return rootPath.relativize(full).toString(); - } - private String getModuleName(String resource) { String moduleName; int firstSeparator = resource.indexOf(File.separator); diff --git a/nbbuild/build.xml b/nbbuild/build.xml index c3b347bdbc20..7fe00ca5b472 100644 --- a/nbbuild/build.xml +++ b/nbbuild/build.xml @@ -2192,16 +2192,27 @@ It is possible to use -Ddebug.port=3234 -Ddebug.pause=y to start the system in d - - - - + + + + + + + + + - +