Search

Dark theme | Light theme

January 15, 2011

Groovy Goodness: Create GMetrics Report with XSLT

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.

// 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:

<?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.

<?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="."/>
                                <br />
                            </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() &lt;= 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() &lt;= 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) &gt; 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.