Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions nbbuild/antsrc/org/netbeans/nbbuild/RatRelativize.java
Original file line number Diff line number Diff line change
@@ -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<Element> 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);
}
}
}
12 changes: 2 additions & 10 deletions nbbuild/antsrc/org/netbeans/nbbuild/RatReportTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -235,7 +233,7 @@ private static String writeFiles(Set<String> listFile, String repo) {
private void doPopulateUnapproved(Map<String, ModuleInfo> 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);
Expand All @@ -248,7 +246,7 @@ private void doPopulateUnapproved(Map<String, ModuleInfo> moduleRATInfo, Element
private void doPopulateApproved(Map<String, ModuleInfo> 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);
Expand All @@ -259,12 +257,6 @@ private void doPopulateApproved(Map<String, ModuleInfo> 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);
Expand Down
21 changes: 16 additions & 5 deletions nbbuild/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2192,16 +2192,27 @@ It is possible to use -Ddebug.port=3234 -Ddebug.pause=y to start the system in d
<excludesfile name="build/rat-exclusions-licenseinfo.txt" />
</fileset>

<rat:report xmlns:rat="antlib:org.apache.rat.anttasks" outputStyle="plain-rat" outputfile="nbbuild/build/rat-report.txt">
<resources refid="rat.files" />
</rat:report>
<rat:report xmlns:rat="antlib:org.apache.rat.anttasks" outputStyle="xml" outputfile="nbbuild/build/rat-report.xml">
<rat:report xmlns:rat="antlib:org.apache.rat.anttasks" outputStyle="xml" outputfile="${nb.build.dir}/rat-report.xml">
<resources refid="rat.files" />
</rat:report>

<!-- reduce size of file by removing full path -->
<taskdef name="ratrelativize" classname="org.netbeans.nbbuild.RatRelativize" classpath="${build.ant.classes.dir}"/>
<ratrelativize file="${nb.build.dir}/rat-report.xml" />

<xslt in="${nb.build.dir}/rat-report.xml" out="${nb.build.dir}/rat-report.txt">
<style>
<javaresource name="org/apache/rat/plain-rat.xsl">
<classpath>
<pathelement location="external/apache-rat-0.18.jar"/>
</classpath>
</javaresource>
</style>
</xslt>
<taskdef name="rattotest" classname="org.netbeans.nbbuild.RatReportTask" classpath="${build.ant.classes.dir}"/>
<loadproperties srcFile="cluster.properties" />
<property name="rat-report.haltonfailure" value="false" />
<rattotest source="build/rat-report.xml" report="${nb.build.dir}/rat/" haltonfailure="${rat-report.haltonfailure}" />
<rattotest source="${nb.build.dir}/rat-report.xml" report="${nb.build.dir}/rat/" haltonfailure="${rat-report.haltonfailure}" />

<reportFromLicenseinfo nball=".." report="${nb.build.dir}/rat-appendix.txt" />
</target>
Expand Down
Loading