| 1 | /** |
| 2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
| 3 | */ |
| 4 | package org.sourceforge.xradar.statics.renderers; |
| 5 | |
| 6 | import java.io.IOException; |
| 7 | import java.io.InputStream; |
| 8 | import java.io.StringWriter; |
| 9 | import java.util.List; |
| 10 | import java.util.Map; |
| 11 | import java.util.logging.Level; |
| 12 | import java.util.logging.Logger; |
| 13 | |
| 14 | import javax.xml.xpath.XPathExpressionException; |
| 15 | import javax.xml.xpath.XPathFactory; |
| 16 | |
| 17 | import org.sourceforge.xradar.DefaultReportValues; |
| 18 | import org.sourceforge.xradar.XRadarException; |
| 19 | import org.sourceforge.xradar.results.XPathQueryAnalyser; |
| 20 | import org.sourceforge.xradar.statics.MergeProduct; |
| 21 | import org.sourceforge.xradar.statics.Param; |
| 22 | import org.sourceforge.xradar.statics.Preambule; |
| 23 | import org.sourceforge.xradar.statics.Statics; |
| 24 | import org.sourceforge.xradar.util.StreamUtils; |
| 25 | import org.sourceforge.xradar.util.XMLUtils; |
| 26 | import org.sourceforge.xradar.util.XRadarUtils; |
| 27 | import org.w3c.dom.Node; |
| 28 | import org.xml.sax.InputSource; |
| 29 | import org.xml.sax.SAXException; |
| 30 | |
| 31 | /** |
| 32 | * @author Romain PELISSE <belaran@gmail.com> |
| 33 | * |
| 34 | */ |
| 35 | public class XslHtmlWebsite extends Statics { |
| 36 | |
| 37 | private Logger logger = Logger.getLogger(XslHtmlWebsite.class.getName()); |
| 38 | |
| 39 | private Map<String, Param> params; |
| 40 | |
| 41 | private static final String MASTER_REPORT_DETAIL = DefaultReportValues.getString("radar.internal.master.report.detail"); |
| 42 | private static final String MASTER_REPORT_XML_PLUGIN = DefaultReportValues.getString("radar.internal.master.report.xml.plugin"); |
| 43 | private static final String GENERATE_STATICS_SVG = DefaultReportValues.getString("radar.internal.generate.statics.svgs"); |
| 44 | private static final String GENERATE_DUPLICATION_GRAPH = DefaultReportValues.getString("radar.internal.graph.duplication"); |
| 45 | private static final String GENERATE_COUPLING_GRAPH = DefaultReportValues.getString("radar.internal.graph.coupling"); |
| 46 | private static final String GENERATE_PACKAGE_GRAPH = DefaultReportValues.getString("radar.internal.graph.package"); |
| 47 | private static final String GENERATE_MODULE_GRAPH = DefaultReportValues.getString("radar.internal.graph.modules"); |
| 48 | private static final String GENERATE_STATICS_OLD_STYLE_SVG = DefaultReportValues.getString("radar.internal.generate.statics.old.style.svgs"); |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Generate final statics website for XRadar. |
| 53 | * @param products |
| 54 | * @param params |
| 55 | * @return |
| 56 | * @throws XRadarException |
| 57 | */ |
| 58 | public String generateHtmlReport(MergeProduct products,Map<String,Param> params) throws XRadarException |
| 59 | { |
| 60 | logger.log(Level.FINE,"Params are:" + params.toString()); |
| 61 | this.params = params; |
| 62 | // Adding generic parameters |
| 63 | Preambule preambule = new Preambule(); |
| 64 | preambule.setParams(params); |
| 65 | if (XRadarUtils.isRunningOnWindowsOS()) |
| 66 | preambule.getParams().put(DOCS_HOME,new Param(DOCS_HOME, SYSTEM_FILE_SEPARATOR + this.getDocsHome())); |
| 67 | else |
| 68 | preambule.getParams().put(DOCS_HOME,new Param(DOCS_HOME,this.getDocsHome())); |
| 69 | // Adding variable parameters |
| 70 | if ( ! params.containsKey("log") ) |
| 71 | addLogLevelDefinitionToPreambule(preambule); |
| 72 | // If we run unit test, we may the according view on the website |
| 73 | preambule.getParams().put(TEST_ENABLED,new Param(TEST_ENABLED,String.valueOf(this.isUnitTestAvailaible()))); |
| 74 | logger.log(Level.FINE,"Test enabled:" + this.isUnitTestAvailaible()); |
| 75 | logger.log(Level.FINE,"Configuration file:" + params.get(XRADAR_CONFIG_FILE)); |
| 76 | logger.log(Level.FINE,"Docs home:" + this.getDocsHome()); |
| 77 | |
| 78 | logger.log(Level.FINEST,"XML input is (MasterReport) \n" + products.getMasterReport()); |
| 79 | logger.log(Level.FINE,"Processing radar normalized with " + MASTER_REPORT_DETAIL); |
| 80 | |
| 81 | merger.setXmlResult(StreamUtils.createOuput(new StringWriter())); |
| 82 | // Generate website |
| 83 | StreamUtils.closeStream(processStep(new InputSource(products.getMasterReport()),MASTER_REPORT_DETAIL,preambule,this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "generating-website.xml")); |
| 84 | // generate_plugin_api |
| 85 | generateGraph(products.getRadarNormalized(),MASTER_REPORT_XML_PLUGIN,"Master XML plugin",preambule,this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "master-xml-plugin.xml"); |
| 86 | // generate_statics_svgs |
| 87 | generateGraph(products.getRadarNormalized(),GENERATE_STATICS_SVG,"Generating statics svg",preambule,this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "statics-svg.xml"); |
| 88 | // duplication graphics |
| 89 | generateGraph(products.getRadarNormalized(),GENERATE_DUPLICATION_GRAPH,"Generating duplication graphics",preambule,this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "duplication-graphics.xml"); |
| 90 | // Coupling graphics generation |
| 91 | generateGraph(products.getRadarNormalized(),GENERATE_COUPLING_GRAPH,"Generating coupling graphics",preambule,this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "coupling-graphics.xml"); |
| 92 | // Package graphics generation |
| 93 | generateGraph(products.getRadarNormalized(),GENERATE_PACKAGE_GRAPH,"Generating package graphics",preambule,this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "package-graphics.xml"); |
| 94 | // Module graphics generation |
| 95 | generateGraph(products.getRadarNormalized(),GENERATE_MODULE_GRAPH,"Generating module graphics",preambule,this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "module-graphics.xml"); |
| 96 | // generate_statics_svgs (old style) |
| 97 | // TODO: Replace the distance graph from 'old style' by the new one ! |
| 98 | generateGraph(products.getMasterReport(),GENERATE_STATICS_OLD_STYLE_SVG,"Generating statics svg (old style)",preambule,this.getDocsHome() + SYSTEM_FILE_SEPARATOR + "statics-svg.xml"); |
| 99 | // Customizing website |
| 100 | return ""; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void copyStaticResourcesToWebSite(String imagesTargetPrefix,String xmlDataPrefix) throws XRadarException { |
| 105 | super.copyStaticResourcesToWebSite(imagesTargetPrefix,xmlDataPrefix); |
| 106 | this.customizeLogo(params.get(XRADAR_CONFIG_FILE).getExpression()); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * FIXME: Find a better way than tweak XPathAnalyser into this... |
| 111 | */ |
| 112 | private void customizeLogo(String radarConfigFile) throws XRadarException { |
| 113 | logger.finer("called with " + radarConfigFile); |
| 114 | XPathQueryAnalyser xpathAnalyser = new XPathQueryAnalyser(); |
| 115 | xpathAnalyser.setInputStream(StreamUtils.readFile(radarConfigFile)); |
| 116 | xpathAnalyser.setXpath(XPathFactory.newInstance().newXPath()); |
| 117 | xpathAnalyser.setBuilder(XMLUtils.getDocumentBuilder()); |
| 118 | logger.finer("analyser built"); |
| 119 | try { |
| 120 | List<Node> nodes = xpathAnalyser.executeXPathQuery(xpathAnalyser.getBuilder().parse(xpathAnalyser.getInputStream()), "//website/logo"); |
| 121 | for ( Node node : nodes ) { |
| 122 | String path = node.getAttributes().getNamedItem("path").getNodeValue(); |
| 123 | if ( path != null && ! "".equals(path) ) { |
| 124 | String target = this.getDocsHome() + SYSTEM_FILE_SEPARATOR + DefaultReportValues.getString("radar.internal.static.resources.target.4") ; |
| 125 | logger.finer("Custom logo find with path " + path); |
| 126 | StreamUtils.copyFileToFile(path, target); |
| 127 | } |
| 128 | } |
| 129 | } catch (XPathExpressionException e) { |
| 130 | throw new XRadarException(e); |
| 131 | } catch (SAXException e) { |
| 132 | throw new XRadarException(e); |
| 133 | } catch (IOException e) { |
| 134 | throw new XRadarException(e); |
| 135 | } |
| 136 | logger.finer("works done"); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * Commodity method to generate svg ( and log data ) |
| 143 | * @param inputXml |
| 144 | * @param stylesheet |
| 145 | * @param logMssg |
| 146 | * @param preambule |
| 147 | */ |
| 148 | private void generateGraph(InputStream inputXml,String stylesheet,String logMssg,Preambule preambule,String outputFilename) throws XRadarException |
| 149 | { |
| 150 | logger.log(Level.INFO,logMssg); |
| 151 | logger.log(Level.FINE,"Processing radar normalized with " + stylesheet); |
| 152 | logger.log(Level.FINEST,"XML input is \n" + inputXml + "\n"); |
| 153 | StreamUtils.closeStream(processStep(new InputSource(inputXml),stylesheet,preambule,outputFilename)); |
| 154 | } |
| 155 | } |