With GMetrics we can analyze complexity and size of our Groovy classes and methods. The default HTML reports shows basic information, but if we want to create our own HTML report we best use the XML output and use XSLT to create a HTML report. In a previous blog we learned how to use XSLT to transform the XML output from CodeNarc to a HTML report. In this blog post we see how we can create XML output and how to format it to a HTML report.
First we must get XML output from GMetrics. If we use the ANT task we can specify the report type and we must set it to org.gmetrics.report.XmlReportWriter
.
1 2 3 4 5 6 7 8 9 | // Using AntBuilder. ant.gmetrics(metricSetFile: 'metrics.groovy' ) { report(type: 'org.gmetrics.report.XmlReportWriter' ) { option name: 'gmetrics.xml' option title: 'Sample GMetrics Report' } fileset(dir: 'src/main/groovy' ) { include name: '**/*.groovy' } } |
We get an XML file with the following structure and content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <? xml version = "1.0" ?> < GMetrics url = 'http://www.gmetrics.org' version = 'GMetrics 0.3' > < Report timestamp = 'Jan 11, 2011 12:52:37 PM' /> < Project title = 'Sample GMetrics Report' > < SourceDirectory >src/main/groovy</ SourceDirectory > </ Project > < PackageSummary > < MetricResult name = 'ABC' average = '3.3' maximum = '4.1' minimum = '2.2' total = '6.2' /> < MetricResult name = 'CyclomaticComplexity' average = '1.0' maximum = '1' minimum = '1' total = '2' /> < MetricResult name = 'MethodLineCount' average = '3.0' maximum = '3' minimum = '3' total = '3' /> < MetricResult name = 'ClassLineCount' average = '9.0' maximum = '13' minimum = '5' total = '18' /> </ PackageSummary > < Package path = 'com' > < MetricResult name = 'ABC' average = '3.3' maximum = '4.1' minimum = '2.2' total = '6.2' /> < MetricResult name = 'CyclomaticComplexity' average = '1.0' maximum = '1' minimum = '1' total = '2' /> < MetricResult name = 'MethodLineCount' average = '3.0' maximum = '3' minimum = '3' total = '3' /> < MetricResult name = 'ClassLineCount' average = '9.0' maximum = '13' minimum = '5' total = '18' /> </ Package > < Package path = 'com/mrhaki' > < MetricResult name = 'ABC' average = '3.3' maximum = '4.1' minimum = '2.2' total = '6.2' /> < MetricResult name = 'CyclomaticComplexity' average = '1.0' maximum = '1' minimum = '1' total = '2' /> < MetricResult name = 'MethodLineCount' average = '3.0' maximum = '3' minimum = '3' total = '3' /> < MetricResult name = 'ClassLineCount' average = '9.0' maximum = '13' minimum = '5' total = '18' /> </ Package > < Package path = 'com/mrhaki/app' > < MetricResult name = 'ABC' average = '3.3' maximum = '4.1' minimum = '2.2' total = '6.2' /> < MetricResult name = 'CyclomaticComplexity' average = '1.0' maximum = '1' minimum = '1' total = '2' /> < MetricResult name = 'MethodLineCount' average = '3.0' maximum = '3' minimum = '3' total = '3' /> < MetricResult name = 'ClassLineCount' average = '9.0' maximum = '13' minimum = '5' total = '18' /> < Class name = 'com.mrhaki.app.App' > < MetricResult name = 'ABC' average = '4.1' maximum = '4.1' minimum = '4.1' total = '4.1' /> < MetricResult name = 'CyclomaticComplexity' average = '1.0' maximum = '1' minimum = '1' total = '1' /> < Method name = 'run' > < MetricResult name = 'ABC' average = '4.1' maximum = '4.1' minimum = '4.1' total = '4.1' /> < MetricResult name = 'CyclomaticComplexity' average = '1' maximum = '1' minimum = '1' total = '1' /> </ Method > </ Class > < Class name = 'com.mrhaki.app.Person' > < MetricResult name = 'ClassLineCount' average = '5' maximum = '5' minimum = '5' total = '5' /> </ Class > < Class name = 'com.mrhaki.app.PersonService' > < MetricResult name = 'ABC' average = '2.2' maximum = '2.2' minimum = '2.2' total = '2.2' /> < MetricResult name = 'CyclomaticComplexity' average = '2.0' maximum = '2' minimum = '2' total = '2' /> < MetricResult name = 'MethodLineCount' average = '3.0' maximum = '3' minimum = '3' total = '3' /> < MetricResult name = 'ClassLineCount' average = '13' maximum = '13' minimum = '13' total = '13' /> < Method name = 'findByNickname' > < MetricResult name = 'ABC' average = '2.2' maximum = '2.2' minimum = '2.2' total = '2.2' /> < MetricResult name = 'CyclomaticComplexity' average = '2' maximum = '2' minimum = '2' total = '2' /> < MetricResult name = 'MethodLineCount' average = '3' maximum = '3' minimum = '3' total = '3' /> </ Method > </ Class > </ Package > < Metrics > < Metric name = 'ABC' > < Description > <![CDATA[Measures size and complexity of source code. See http://c2.com/cgi/wiki?AbcMetric]]> </ Description > </ Metric > < Metric name = 'ClassLineCount' > < Description > <![CDATA[Measures the number of lines in each class or interface.]]> </ Description > </ Metric > < Metric name = 'CyclomaticComplexity' > < Description > <![CDATA[Measures the (McCabe) Cyclomatic Complexity of source code. See http://en.wikipedia.org/wiki/Cyclomatic_complexity.]]> </ Description > </ Metric > < Metric name = 'MethodLineCount' > < Description > <![CDATA[Measures the number of lines in each method.]]> </ Description > </ Metric > </ Metrics > </ GMetrics > |
Now we can write a XSLT file with transformation rules to display the information in a HTML report. In the following XSLT file we first create a summary block with general information and averages of the different metrics. Next we create two top 7 tables with a list of classes and methods with the highest cyclomatic complexity number. We also display the information for all classes that are analyzed for the project, and finally we display descriptions of the metrics that are applied for the source files. Through hyperlinking we can simply click through the HTML report.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | <? xml version = "1.0" encoding = "UTF-8" ?> < xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > < xsl:output method = "html" indent = "yes" /> < xsl:decimal-format decimal-separator = "." grouping-separator = "," /> < xsl:key name = "packageSummary" match = "PackageSummary/MetricResult" use = "@name" /> < xsl:template match = "GMetrics" > < html > < head > < title >< xsl:value-of select = "Project/@title" /></ title > < link rel = "stylesheet" href = "default.css" type = "text/css" /> </ head > < body > < a name = "top" ></ a > < div class = "header" > < h1 > < img src = "haki-logo.png" width = "48" height = "48" /> < xsl:value-of select = "Project/@title" /> </ h1 > </ div > < div class = "rule" ></ div > < div class = "summary" > < div class = "first" > < dl > < dt >Generated</ dt > < dd >< xsl:value-of select = "Report/@timestamp" /></ dd > </ dl > < dl > < dt >Tool</ dt > < dd >< a href = "{@url}" >< xsl:value-of select = "@version" /></ a ></ dd > </ dl > < dl > < dt >Sources</ dt > < dl > < xsl:for-each select = "Project/SourceDirectory" > < xsl:value-of select = "." /> </ xsl:for-each > </ dl > </ dl > </ div > < div class = "last" > < xsl:apply-templates select = "PackageSummary" /> </ div > </ div > < div class = "rule" ></ div > < div id = "top-lists" > < div class = "first" > < xsl:apply-templates select = "." mode = "top-classes" /> </ div > < div class = "last" > < xsl:apply-templates select = "." mode = "top-methods" /> </ div > </ div > < div class = "rule" ></ div > < xsl:apply-templates select = "." mode = "full" /> < div class = "rule" ></ div > < xsl:apply-templates select = "Metrics" /> < div class = "rule" ></ div > < p >XSLT created by < a href = "http://www.mrhaki.com" >Hubert A. Klein Ikkink (mrhaki)</ a ></ p > </ body > </ html > </ xsl:template > < xsl:template match = "PackageSummary" > < ul id = "summary_stats" > < li > < strong > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "MetricResult[@name = 'ABC']/@average" /> </ xsl:call-template > </ strong > < span >ABC</ span > </ li > < li > < strong > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "MetricResult[@name = 'CyclomaticComplexity']/@average" /> </ xsl:call-template > </ strong > < span >cyclomatic complexity</ span > </ li > < li > < strong > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "MetricResult[@name = 'MethodLineCount']/@average" /> </ xsl:call-template > </ strong > < span >method lines</ span > </ li > < li > < strong > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "MetricResult[@name = 'ClassLineCount']/@average" /> </ xsl:call-template > </ strong > < span >class lines</ span > </ li > </ ul > </ xsl:template > < xsl:template match = "Metrics" > < h2 >Metrics</ h2 > < table border = "0" width = "100%" cellpadding = "0" cellspacing = "0" > < tr > < th >Name</ th > < th >Description</ th > < th >Average</ th > < th >Total</ th > < th >Maximum</ th > < th >Minimum</ th > </ tr > < xsl:apply-templates select = "Metric" /> </ table > </ xsl:template > < xsl:template match = "Metric" > < xsl:variable name = "metricName" select = "@name" /> < xsl:variable name = "summary" select = "key('packageSummary', $metricName)" /> < tr > < td > < a name = "m-{$metricName}" > </ a > < xsl:value-of select = "$metricName" /> </ td > < td > < xsl:value-of select = "Description" /> </ td > < td class = "number" > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$summary/@average" /> </ xsl:call-template > </ td > < td class = "number" > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$summary/@total" /> </ xsl:call-template > </ td > < td class = "number" > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$summary/@maximum" /> </ xsl:call-template > </ td > < td class = "number" > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$summary/@minimum" /> </ xsl:call-template > </ td > </ tr > </ xsl:template > < xsl:template match = "GMetrics" mode = "top-methods" > < h2 >Top Complexity Methods</ h2 > < table border = "0" cellpadding = "0" cellspacing = "0" > < tr > < th >Name</ th > < th >Complexity</ th > </ tr > < xsl:for-each select = "//Method/MetricResult[@name = 'CyclomaticComplexity']" > < xsl:sort select = "@total" data-type = "number" order = "descending" /> < xsl:if test = "position() <= 7" > < tr > < td > < a href = "#method-{../../@name}-{../@name}" > < xsl:value-of select = "../@name" /> </ a > </ td > < td >< xsl:value-of select = "@total" /></ td > </ tr > </ xsl:if > </ xsl:for-each > </ table > </ xsl:template > < xsl:template match = "GMetrics" mode = "top-classes" > < h2 >Top Complexity Classes</ h2 > < table border = "0" cellpadding = "0" cellspacing = "0" > < tr > < th >Name</ th > < th >Complexity</ th > </ tr > < xsl:for-each select = "//Class/MetricResult[@name = 'CyclomaticComplexity']" > < xsl:sort select = "@total" data-type = "number" order = "descending" /> < xsl:if test = "position() <= 7" > < tr > < td > < a href = "#c-{../@name}" > < xsl:value-of select = "../@name" /> </ a > </ td > < td >< xsl:value-of select = "@total" /></ td > </ tr > </ xsl:if > </ xsl:for-each > </ table > </ xsl:template > < xsl:template match = "GMetrics" mode = "full" > < xsl:for-each select = "Package/Class" > < xsl:sort select = "@name" /> < xsl:apply-templates select = "." mode = "full" /> < p /> < p /> </ xsl:for-each > </ xsl:template > < xsl:template match = "Class" mode = "summary" > < xsl:variable name = "violationCount" select = "count(Violation)" /> < tr > < td >< a href = "#c-{@name}" >< xsl:value-of select = "../@path" />/< xsl:value-of select = "@name" /></ a ></ td > < td >< xsl:value-of select = "$violationCount" /></ td > </ tr > </ xsl:template > < xsl:template match = "Class" mode = "full" > < xsl:variable name = "classLineCount" select = "MetricResult[@name = 'ClassLineCount']" /> < xsl:variable name = "methodLineCount" select = "MetricResult[@name = 'MethodLineCount']" /> < xsl:variable name = "abc" select = "MetricResult[@name = 'ABC']" /> < xsl:variable name = "cyclomaticComplexity" select = "MetricResult[@name = 'CyclomaticComplexity']" /> < a name = "c-{@name}" ></ a > < h2 > Class < xsl:value-of select = "@name" /> </ h2 > < div class = "class_summary" > < table border = "0" cellpadding = "0" cellspacing = "0" width = "100%" > < tbody > < tr > < td > < a href = "#m-ClassLineCount" > < h3 >Class line count</ h3 > </ a > < p > < strong > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$classLineCount/@total" /> < xsl:with-param name = "format" select = "'####0'" /> </ xsl:call-template > </ strong > </ p > </ td > < td > < a href = "#m-MethodLineCount" > < h3 >Methods</ h3 > </ a > < p > < strong > < xsl:value-of select = "count(Method)" /> </ strong > </ p > < dl > < dt >Average LC:</ dt > < dd > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$methodLineCount/@average" /> </ xsl:call-template > </ dd > < dt >Maximum LC:</ dt > < dd > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$methodLineCount/@total" /> </ xsl:call-template > </ dd > </ dl > </ td > < td > < a href = "#m-CyclomaticComplexity" > < h3 >Complexity</ h3 > </ a > < p > < strong > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$cyclomaticComplexity/@average" /> </ xsl:call-template > </ strong > </ p > < dl > < dt >Maximum:</ dt > < dd > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$cyclomaticComplexity/@maximum" /> </ xsl:call-template > </ dd > </ dl > </ td > < td > < a href = "#m-ABC" > < h3 >ABC</ h3 > </ a > < p > < strong > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$abc/@average" /> </ xsl:call-template > </ strong > </ p > < dl > < dt >Maximum:</ dt > < dd > < xsl:call-template name = "display_value" > < xsl:with-param name = "value" select = "$abc/@maximum" /> </ xsl:call-template > </ dd > </ dl > </ td > </ tr > </ tbody > </ table > </ div > < xsl:if test = "count(Method) > 0" > < table border = "0" width = "100%" cellpadding = "0" cellspacing = "0" > < tr > < th >Method</ th > < th >Cyclomatic Complexity</ th > < th >ABC</ th > < th >Lines</ th > </ tr > < xsl:for-each select = "Method" > < xsl:sort select = "MetricResult[@name = 'CyclomaticComplexity']/@total" /> < tr > < td > < a name = "method-{../@name}-{@name}" ></ a > < xsl:value-of select = "@name" /> </ td > < td > < xsl:value-of select = "MetricResult[@name = 'CyclomaticComplexity']/@total" /> </ td > < td > < xsl:value-of select = "MetricResult[@name = 'ABC']/@total" /> </ td > < td > < xsl:value-of select = "MetricResult[@name = 'MethodLineCount']/@total" /> </ td > </ tr > </ xsl:for-each > </ table > </ xsl:if > < a href = "#top" >Back to top</ a > </ xsl:template > < xsl:template name = "display_value" > < xsl:param name = "value" /> < xsl:param name = "format" select = "'####.0'" /> < xsl:choose > < xsl:when test = "string($value) != ''" > < xsl:value-of select = "format-number($value, $format)" /> </ xsl:when > < xsl:otherwise > < xsl:value-of select = "'N/A'" /> </ xsl:otherwise > </ xsl:choose > </ xsl:template > </ xsl:stylesheet > |
The resulting HTML report shows all information nicely formatted and styled. Of course we can customize the XSLT file for our own projects. For example to add extra information in our report. The following image also show the generated HTML report:
The XSLT source file and supporting CSS and images are at Github.