pmd: AntTask support for language

- fix  #1004 targetjdk isn't attribute of PMD task
- introduce a new element, called <language>, that replaces the already existing <version/> element
This commit is contained in:
Romain PELISSE
2013-01-29 12:20:22 +01:00
committed by Andreas Dangel
parent d95d960de3
commit 3daca86045
10 changed files with 323 additions and 74 deletions

View File

@ -26,7 +26,6 @@ import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetFactory;
import net.sourceforge.pmd.RuleSetNotFoundException;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.renderers.AbstractRenderer;
import net.sourceforge.pmd.renderers.Renderer;
@ -110,24 +109,10 @@ public class PMDTask extends Task {
formatters.add(f);
}
public void addConfiguredVersion(Version version) {
LanguageVersion languageVersion = LanguageVersion.findByTerseName(version.getTerseName());
public void addConfiguredSourceLanguage(SourceLanguage version) {
LanguageVersion languageVersion = LanguageVersion.findVersionsForLanguageTerseName(version.getName(), version.getVersion());
if (languageVersion == null) {
StringBuilder buf = new StringBuilder("The <version> element, if used, must be one of ");
boolean first = true;
for (Language language : Language.values()) {
if (language.getVersions().size() > 2) {
for (LanguageVersion v : language.getVersions()) {
if (!first) {
buf.append(", ");
}
buf.append('\'').append(v.getTerseName()).append('\'');
first = false;
}
}
}
buf.append('.');
throw new BuildException(buf.toString());
throw new BuildException("The following language is not supported:" + version + ".");
}
configuration.setDefaultLanguageVersion(languageVersion);
}

View File

@ -0,0 +1,33 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.ant;
/**
* Stores LanguageVersion terse name value.
*/
public class SourceLanguage {
private String name;
private String version;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "<language name=\"" + this.name + "\" version=\"" + this.version + "\" />";
}
}

View File

@ -1,19 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.ant;
/**
* Stores LanguageVersion terse name value.
*/
public class Version {
private String terseName;
public void addText(String text) {
this.terseName = text;
}
public String getTerseName() {
return terseName;
}
}

View File

@ -200,6 +200,23 @@ public enum LanguageVersion {
return versionsAvailable;
}
/**
* A utility method to retrieve the appropriate enum, given the provided parameters
*
* @param languageTerseName The LanguageVersion terse name.
* @param languageVersion The version of the language requested.
* @return A list of versions associated with the terse name.
*/
public static LanguageVersion findVersionsForLanguageTerseName(String languageTerseName, String languageVersion) {
List<LanguageVersion> versionsAvailable = findVersionsForLanguageTerseName(languageTerseName);
for ( LanguageVersion version : versionsAvailable ) {
if ( version.getVersion().equalsIgnoreCase(languageVersion) )
return version;
}
return null;
}
/**
* Return a comma-separated list of LanguageVersion terse names.
* @param languageVersions The language versions.

View File

@ -99,7 +99,7 @@
<p><code>auxclasspath</code> nested element - extra classpath used for Type Resolution rules.</p>
<p><code>version</code> nested element - specify which version of JDK to (1.5, 1.6,...)</p>
<p><code>language</code> nested element - specify which language (Java, Ecmascript, XML,...) and the associated version (1.5, 1.6,...)</p>
<p><code>ruleset</code> nested element - another way to specify rulesets. Here's an example:</p>
<source>
@ -107,7 +107,7 @@
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
<pmd shortFilenames="true">
<version>1.6</version>
<language name="java" versio="1.6"/>
<ruleset>rulesets/java/design.xml</ruleset>
<ruleset>java-basic</ruleset>
<formatter type="html" toFile="pmd_report.html" linkPrefix="http://pmd.sourceforge.net/xref/"/>

View File

@ -40,14 +40,14 @@ public class PMDTaskTest extends BuildFileTest {
public void testNestedRuleset() {
executeTarget("testNestedRuleset");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Deeply nested if");
assertOutputContaining("Fields should be declared at the");
}
@Test
public void testFormatterWithProperties() {
executeTarget("testFormatterWithProperties");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Deeply nested if");
assertOutputContaining("Fields should be declared at the");
assertOutputContaining("link_prefix");
assertOutputContaining("line_prefix");
}
@ -56,35 +56,35 @@ public class PMDTaskTest extends BuildFileTest {
public void testAbstractNames() {
executeTarget("testAbstractNames");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Deeply nested if");
assertOutputContaining("Fields should be declared at the");
}
@Test
public void testAbstractNamesInNestedRuleset() {
executeTarget("testAbstractNamesInNestedRuleset");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Deeply nested if");
assertOutputContaining("Fields should be declared at the");
}
@Test
public void testCommaInRulesetfiles() {
executeTarget("testCommaInRulesetfiles");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Deeply nested if");
assertOutputContaining("Fields should be declared at the");
}
@Test
public void testRelativeRulesets() {
executeTarget("testRelativeRulesets");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Deeply nested if");
assertOutputContaining("Fields should be declared at the");
}
@Test
public void testRelativeRulesetsInRulesetfiles() {
executeTarget("testRelativeRulesetsInRulesetfiles");
assertOutputContaining("Avoid really long methods");
assertOutputContaining("Deeply nested if");
assertOutputContaining("Fields should be declared at");
}
@Test
@ -94,7 +94,7 @@ public class PMDTaskTest extends BuildFileTest {
@Test
public void testInvalidLanguageVersion() {
expectBuildExceptionContaining("testInvalidLanguageVersion", "Fail requested.", "The <version> element, if used, must be one of 'java 1.3', 'java 1.4', 'java 1.5', 'java 1.6', 'java 1.7'.");
expectBuildExceptionContaining("testInvalidLanguageVersion", "Fail requested.", "The following language is not supported:<language name=\"java\" version=\"42\" />.");
}
@Test
@ -103,6 +103,14 @@ public class PMDTaskTest extends BuildFileTest {
assertOutputContaining("Avoid really long methods");
}
@Test
public void testEcmascript() {
executeTarget("testEcmascript");
assertOutputContaining("A 'return', 'break', 'continue', or 'throw' statement should be the last in a block.");
assertOutputContaining("Avoid using global variables");
assertOutputContaining("Use ===/!== to compare with true/false or Numbers");
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(PMDTaskTest.class);
}

View File

@ -1,25 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.ant;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import net.sourceforge.pmd.ant.Version;
import org.junit.Test;
public class VersionTest {
@Test
public void testHappyPath() {
Version version = new Version();
assertNull("default", version.getTerseName());
version.addText("terseName");
assertEquals("set terse name", "terseName", version.getTerseName());
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(VersionTest.class);
}
}

View File

@ -0,0 +1,57 @@
function tablecollapse()
{
/* Variables */
var collapseClass='footcollapse';
var collapsePic='arrow_up.gif';
var expandPic='arrow_down.gif';
var initialCollapse=true;
// loop through all tables
var t=document.getElementsByTagName('table');
var checktest= new RegExp("(^|\\s)" + collapseClass + "(\\s|$)");
for (var i=0;i<t.length;i++)
{
// if the table has not the right class, skip it
if(!checktest.test(t[i].className)){continue;}
// make the footer clickable
t[i].getElementsByTagName('tfoot')[0].onclick=function()
{
// loop through all bodies of this table and show or hide
// them
var tb=this.parentNode.getElementsByTagName('tbody');
for(var i=0;i<tb.length;i++)
{
tb[i].style.display=tb[i].style.display=='none'?'':'none';
}
// change the image accordingly
var li=this.getElementsByTagName('img')[0];
li.src=li.src.indexOf(collapsePic)==-1?collapsePic:expandPic;
}
// if the bodies should be collapsed initially, do so
if(initialCollapse)
{
var tb=t[i].getElementsByTagName('tbody');
for(var j=0;j<tb.length;j++)
{
tb[j].style.display='none';
}
}
// add the image surrounded by a dummy link to allow keyboard
// access to the last cell in the footer
var newa=document.createElement('a');
newa.href='#';
newa.onclick=function(){return false;}
var newimg=document.createElement('img');
newimg.src=initialCollapse?expandPic:collapsePic;
var tf=t[i].getElementsByTagName('tfoot')[0];
var lt=tf.getElementsByTagName('td')[tf.getElementsByTagName('td').length-1];
newa.appendChild(newimg);
lt.insertBefore(newa,lt.firstChild);
}
}
// run tablecollapse when the page loads
window.onload=tablecollapse;

View File

@ -0,0 +1,184 @@
//$Header$
addEvent(window, "load", sortables_init);
var SORT_COLUMN_INDEX;
function sortables_init() {
// Find all tables with class sortable and make them sortable
if (!document.getElementsByTagName) return;
tbls = document.getElementsByTagName("table");
for (ti=0;ti<tbls.length;ti++) {
thisTbl = tbls[ti];
if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {
//initTable(thisTbl.id);
ts_makeSortable(thisTbl);
}
}
}
function ts_makeSortable(table) {
if (table.rows && table.rows.length > 0) {
var firstRow = table.rows[0];
}
if (!firstRow) return;
// We have a first row: assume it's the header, and make its contents clickable links
for (var i=0;i<firstRow.cells.length;i++) {
var cell = firstRow.cells[i];
var txt = ts_getInnerText(cell);
cell.innerHTML = '<a href="#" class="sortheader" onclick="ts_resortTable(this);return false;">'+txt+'<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a>';
}
}
function ts_getInnerText(el) {
if (typeof el == "string") return el;
if (typeof el == "undefined") { return el };
if (el.innerText) return el.innerText; //Not needed but it is faster
var str = "";
var cs = el.childNodes;
var l = cs.length;
for (var i = 0; i < l; i++) {
switch (cs[i].nodeType) {
case 1: //ELEMENT_NODE
str += ts_getInnerText(cs[i]);
break;
case 3: //TEXT_NODE
str += cs[i].nodeValue;
break;
}
}
return str;
}
function ts_resortTable(lnk) {
// get the span
var span;
for (var ci=0;ci<lnk.childNodes.length;ci++) {
if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
}
var spantext = ts_getInnerText(span);
var td = lnk.parentNode;
var column = td.cellIndex;
var table = getParent(td,'TABLE');
// Work out a type for the column
if (table.rows.length <= 1) return;
var itm = ts_getInnerText(table.rows[1].cells[column]);
sortfn = ts_sort_caseinsensitive;
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;
SORT_COLUMN_INDEX = column;
var firstRow = new Array();
var newRows = new Array();
for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }
newRows.sort(sortfn);
if (span.getAttribute("sortdir") == 'down') {
ARROW = '&nbsp;&nbsp;&uarr;';
newRows.reverse();
span.setAttribute('sortdir','up');
} else {
ARROW = '&nbsp;&nbsp;&darr;';
span.setAttribute('sortdir','down');
}
// We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
// don't do sortbottom rows
for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}
// do sortbottom rows only
for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
// Delete any other arrows there may be showing
var allspans = document.getElementsByTagName("span");
for (var ci=0;ci<allspans.length;ci++) {
if (allspans[ci].className == 'sortarrow') {
if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
allspans[ci].innerHTML = '&nbsp;&nbsp;&nbsp;';
}
}
}
span.innerHTML = ARROW;
}
function getParent(el, pTagName) {
if (el == null) return null;
else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase
return el;
else
return getParent(el.parentNode, pTagName);
}
function ts_sort_date(a,b) {
// y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
if (aa.length == 10) {
dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
} else {
yr = aa.substr(6,2);
if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
}
if (bb.length == 10) {
dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
} else {
yr = bb.substr(6,2);
if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
}
if (dt1==dt2) return 0;
if (dt1<dt2) return -1;
return 1;
}
function ts_sort_currency(a,b) {
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
return parseFloat(aa) - parseFloat(bb);
}
function ts_sort_numeric(a,b) {
aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
if (isNaN(aa)) aa = 0;
bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));
if (isNaN(bb)) bb = 0;
return aa-bb;
}
function ts_sort_caseinsensitive(a,b) {
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
if (aa==bb) return 0;
if (aa<bb) return -1;
return 1;
}
function ts_sort_default(a,b) {
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
if (aa==bb) return 0;
if (aa<bb) return -1;
return 1;
}
function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+, NS6 and Mozilla
// By Scott Andrew
{
if (elm.addEventListener){
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent){
var r = elm.attachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be removed");
}
}

View File

@ -13,7 +13,7 @@
<target name="testInvalidLanguageVersion">
<pmd rulesetfiles="${pmd.home}/src/main/resources/rulesets/java/design.xml">
<version>42</version>
<sourceLanguage name="java" version="42"/>
</pmd>
</target>
@ -118,4 +118,13 @@
</pmd>
</target>
<target name="testEcmascript">
<pmd rulesetfiles="${pmd.home}/src/main/resources/rulesets/ecmascript/basic.xml">
<sourceLanguage name="ecmascript" version="3"/>
<formatter type="text" toConsole="true"/>
<fileset dir="${pmd.home}/src/test/resources/ant/ecmascript/">
<include name="*.js"/>
</fileset>
</pmd>
</target>
</project>