Github user ebarboni commented on a diff in the pull request: https://github.com/apache/incubator-netbeans/pull/70#discussion_r143476986 --- Diff: nbbuild/antsrc/org/netbeans/nbbuild/reporting/RatHtmlReportTask.java --- @@ -0,0 +1,295 @@ +/** + * 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.reporting; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +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; + +/** + * + * @author skygo + */ +public class RatHtmlReportTask extends Task { + + private File sourceFile; + private File targetFile; + private File root; + // not nice but to be last + private static final String OTHERS_AREA = "zothers"; + + // parameters for the task + public void setSource(File sourceFile) { + this.sourceFile = sourceFile; + } + + public void setTarget(File targetFile) { + this.targetFile = targetFile; + } + + @Override + public void execute() throws BuildException { + super.execute(); + root = sourceFile.getParentFile().getParentFile().getParentFile(); + File[] modulesFolder = root.listFiles(); + + // build map to get cluster and module related + Set<String> moduleDB = new HashSet<>(); + Map<String, Set<String>> modulebycluster = new TreeMap<>(); + for (File module : modulesFolder) { + if (module.isDirectory()) { + moduleDB.add(module.getName()); + } + } + Set<String> clusterList = new TreeSet<>(); + for (String key : getProject().getProperties().keySet()) { + if (key.startsWith("nb.cluster.")) { + String simplfiedKey = key.replaceAll("nb.cluster.", ""); + simplfiedKey = simplfiedKey.replaceAll(".dir", ""); + simplfiedKey = simplfiedKey.replaceAll(".depends", ""); + + clusterList.add(simplfiedKey); + modulebycluster.put(simplfiedKey, new HashSet<String>()); + } + } + for (String k : clusterList) { + String property = getProject().getProperty("nb.cluster." + k); + String[] split = property.split(","); + for (String amo : split) { + moduleDB.remove(amo); + modulebycluster.get(k).add(amo); + } + } + modulebycluster.put(OTHERS_AREA, new HashSet<String>()); + for (String k : moduleDB) { + modulebycluster.get(OTHERS_AREA).add(k); + } + // remaining module soted in others + modulebycluster.get(OTHERS_AREA).add("XXX"); + clusterList.add(OTHERS_AREA); + DocumentBuilder dBuilder; + Document doc = null; + Map<String, ModuleInfo> moduleRATInfo = new TreeMap<>(); + + //read XML + try { + dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + doc = dBuilder.parse(sourceFile); // open xml source + XPathFactory xpf = XPathFactory.newInstance(); + + XPath path = xpf.newXPath(); + Element rootElement = doc.getDocumentElement(); + + moduleRATInfo.put("XXX", new ModuleInfo()); + + doPopulateUnapprouved(moduleRATInfo, rootElement, path, modulebycluster); + doPopulateApprouved(moduleRATInfo, rootElement, path, modulebycluster); + + } catch (ParserConfigurationException ex) { + Logger.getLogger(RatHtmlReportTask.class.getName()).log(Level.SEVERE, null, ex); + } catch (SAXException ex) { + Logger.getLogger(RatHtmlReportTask.class.getName()).log(Level.SEVERE, null, ex); + } catch (IOException ex) { + Logger.getLogger(RatHtmlReportTask.class.getName()).log(Level.SEVERE, null, ex); + } catch (XPathExpressionException ex) { + Logger.getLogger(RatHtmlReportTask.class.getName()).log(Level.SEVERE, null, ex); + } + if (doc != null) { + FileWriter fileWriter = null; + BufferedWriter bw = null; + try { + fileWriter = new FileWriter(targetFile); + bw = new BufferedWriter(fileWriter); + bw.write("<!DOCTYPE html>"); + bw.write("<html>"); + bw.write("<head>"); + writeStyle(bw); + bw.write("</head>"); + bw.write("<body>"); + bw.write("<div id=\"content\">"); + writeAreas(bw, clusterList, modulebycluster, moduleRATInfo); + bw.write("</div>"); + bw.write("<nav id=\"main-menu\">"); + writeMenu(bw, clusterList, modulebycluster, moduleRATInfo); + bw.write("</nav>"); + bw.write("</body>"); + bw.write("</html>"); + + } catch (IOException ex) { + Logger.getLogger(RatHtmlReportTask.class.getName()).log(Level.SEVERE, null, ex); + } finally { + try { + bw.close(); + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + } + + private static void writeMenu(BufferedWriter bw, Set<String> clusterDB, Map<String, Set<String>> modulebycluster, Map<String, ModuleInfo> moduleRATInfo) throws IOException { + bw.write("<ul>"); + for (String clusterName : clusterDB) { + int appscanned = 0; + int unappscanned = 0; + for (String mm : modulebycluster.get(clusterName)) { + ModuleInfo get = moduleRATInfo.get(mm); + appscanned += get.getApprouved().size(); + unappscanned += get.getUnapprouved().size(); + } + if ((unappscanned + appscanned) > 0) { + bw.write("<li>"); + } else { + bw.write("<li class=\"notpresent\">"); + } + bw.write(clusterName); + bw.write(" (A:" + appscanned + ", U:" + unappscanned + ")</li>"); + } + bw.write("</ul>"); + } + + private void writeAreas(BufferedWriter bw, Set<String> clusterDB, Map<String, Set<String>> modulebycluster, Map<String, ModuleInfo> unbymodule) throws IOException { + for (String clusterName : clusterDB) { + bw.write("<div class=\"area\" id=\"" + clusterName + "\">"); + bw.write("<h2>Cluster::" + clusterName + "</h2>"); + for (String mm : modulebycluster.get(clusterName)) { + bw.write("<div class=\"module\" id=\"" + mm + "\">"); + bw.write("<h3>Module::" + mm + "</h3>"); + bw.write("<ul>"); + ModuleInfo get = unbymodule.get(mm); + for (String mmu : get.getUnapprouved()) { + bw.write("<li class=\"unapprouved\">"); + bw.write("U:<a href=\"https://github.com/apache/incubator-netbeans/tree/master/" + mmu + "\">" + mmu + "</a>"); --- End diff -- nice addition thanks. will look at it
---