修复CheckStyle检查报告中Summary部分文件数目错误问题

Posted by dohkoos on December 30th, 2005 (433 views)

在使用CheckStyle时用了它提供的checkstyle-frames.xsl作为样式文件,
不过checkstyle-frames.xsl生成报告时有些问题,主要是统计数据有点问
题,Summary中报告的文件数目比真实的多,所以对xsl文件的summary模板
作了小小的一些修改,解决了这个问题,且添加了一个统计有错误的文件
个数的功能。

原来的xsl内容:

<xsl:template match="checkstyle" mode="summary">
    <h3>Summary</h3>
    <xsl:variable name="fileCount" select="count(file)"/>
    <xsl:variable name="errorCount" select="count(file/error)"/>
    <table class="log" border="0" cellpadding="5" cellspacing="2" width="100%">
        <tr>
            <th>Files</th>
            <th>Errors</th>
        </tr>
        <tr>
            <xsl:call-template name="alternated-row"/>
            <td><xsl:value-of select="$fileCount"/></td>
            <td><xsl:value-of select="$errorCount"/></td>
        </tr>
    </table>
</xsl:template>

修改后的内容:

<xsl:template match="checkstyle" mode="summary">
    <h3>Summary</h3>
    <xsl:variable name="fileCount">
        <xsl:value-of select="count(file[not(./@name = preceding-sibling::file/@name)])" />
    </xsl:variable>
    <xsl:variable name="fileErrorCount" select="count(file[count(error) > 0])" />
    <xsl:variable name="errorCount" select="count(file/error)" />
    <table class="log" border="0" cellpadding="5" cellspacing="2" width="100%">
        <tr>
            <th>Total Files</th>
            <th>Files With Errors</th>
            <th>Errors</th>
        </tr>
        <tr>
            <xsl:call-template name="alternated-row" />
            <td><xsl:value-of select="$fileCount" /></td>
            <td><xsl:value-of select="$fileErrorCount" /></td>
            <td><xsl:value-of select="$errorCount" /></td>
        </tr>
    </table>
</xsl:template>

following-sibling 按文档顺序选择文档中此后出现的当前节点的所有兄弟节点。
preceding-silbling 按与文档顺序相反的方向选择文档中此前出现的当前节点的所有兄弟节点。
following 除当前节点的所有后代节点外,按顺序选择文档中当前节点之后出现的所有节点,不包括属性节点或名称空间节点。
preceding 按与文档方向顺序相反的方向选择文档中在当前节点之前出现的所有节点。

Related Posts

People who read this, also read...

Ant系列之用Checkstyle检查代码规范的问题

Posted by dohkoos on December 16th, 2005 (1,113 views)

在ant的build.xml文件中添加CheckStyle任务的步骤如下:

1. 将checkstyle-all-4.1.jar拷贝到项目的lib目录;
2. 建立配置文件;
3. 声明checkstyle任务:
<taskdef resource="checkstyletask.properties" classpath="${lib.dir}/checkstyle-all-4.1.jar" />
4. 建立checkstyle任务:
<target name="checkstyle" description="Generates a report of code convention violations.">
    <checkstyle config="${docs.dir}/checkstyle_checks.xml" failOnViolation="false">
        <fileset dir="${src.dir}" includes="**/*.java" />
        <formatter type="xml" toFile="${checkstyle.dir}/checkstyle_report.xml" />
    </checkstyle>
    <xslt basedir="${checkstyle.dir}" destdir="${checkstyle.dir}/html" extension=".html"
            style="${docs.dir}/checkstyle-frames.xsl">
        <param name="output.dir" expression="${checkstyle.dir}/html" />
    </xslt>
</target>
其中output.dir是checkstyle-frames.xsl中的参数:<xsl:param name="output.dir" />

在运行checkstyle任务时可能会出现异常javax.xml.transform.TransformerException: java.lang.RuntimeException: Unrecognized XSLTC extension 'org.apache.xalan.xslt.extensions.Redirect:write',
可以将checkstyle-frames.xsl中的xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect改成xmlns:redirect="http://xml.apache.org/xalan/redirect"。

出现Got an exception - java.lang.RuntimeException: Unable to get class information for XxxException.
异常时可以修改对于Throws的的限制:允许Throws Unchecked Exception以及Throws Subclass Of Another Declared Exception。
<module name="RedundantThrows">
    <property name="allowUnchecked" value="true" />
    <property name="allowSubclasses" value="true" />
</module>

Related Posts

People who read this, also read...