Added "inclusions" regex.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7201 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Dale Anson
2011-06-28 22:10:25 +00:00
parent 71ce8591e2
commit f5461a958d
4 changed files with 39 additions and 4 deletions
pmd-jedit/PMDPlugin

@ -90,6 +90,7 @@ net.sf.pmd.Example=Example
net.sf.pmd.Exclusions=Exclusions
net.sf.pmd.Export_Output_as_=Export Output as:
net.sf.pmd.Ignore_Literals_&_identifiers_when_detecting_Duplicate_Code=Ignore Literals & identifiers when detecting Duplicate Code
net.sf.pmb.Inclustions=Inclusions
net.sf.pmd.Invalid_Renderer=Invalid Renderer
net.sf.pmd.Minimum_Tile_Size>=Minimum Tile Size:
net.sf.pmd.Minimum_Tile_size_>=Minimum Tile size :

@ -96,7 +96,11 @@
<p>
The CPD is very fast. It can also work on a variety of languages. Currently the fully supported languages are Java, JSP, PHP, C/C++, Fortran, Ruby, and Javascript. Additionally, CPD can work with just about every file mode supported by jEdit, although results may not be perfect as the modes not specifically supported use a generic tokenizer that may not suitable for a particular language.
<p>
A new feature for running CPD in a directory is the ability to exclude files. This makes it much easier to run CPD on a high level directory in a Maven project so the files in the "target" directories can be skipped. Enter a regular expression in the "Exclusions" field in the directory chooser dialog. For Maven projects, enter <code>.*?/target/.*</code> to exclude all the "target" directories. The "Exclusions" regular expression works on the full path of a file, so this regex might not work in all cases (like maybe code for target.com?)
A new feature for running CPD in a directory is the ability to include files as specified by a regular expression. This makes it easy to narrow your search in a project that may have a lot of files with the same name.
<p>
Another new feature for running CPD in a directory is the ability to exclude files. This makes it much easier to run CPD on a high level directory in a Maven project so the files in the "target" directories can be skipped. Enter a regular expression in the "Exclusions" field in the directory chooser dialog. For Maven projects, enter <code>.*?/target/.*</code> to exclude all the "target" directories. The "Exclusions" regular expression works on the full path of a file, so this regex might not work in all cases (like maybe code for target.com?)
<p>
You can set both an inclusion regular expression and an exclusion regular expression. Files are first checked for inclusion then exclusion. This means the Maven example above is still valid.
<h3>
Custom Rules
</h3>

@ -3,6 +3,7 @@ package net.sourceforge.pmd.jedit;
import java.io.File;
import java.util.regex.*;
import javax.swing.filechooser.FileFilter;
import org.gjt.sp.util.Log;
/**
* This is a file filter for CPD. I wanted to use a FileNameExtension filter, but
@ -15,6 +16,7 @@ public class CPDFileFilter extends FileFilter implements Comparable<CPDFileFilte
private String mode;
private String description;
private String[] extensions;
private Pattern inclusionsPattern = null;
private Pattern exclusionsPattern = null;
/**
@ -30,6 +32,12 @@ public class CPDFileFilter extends FileFilter implements Comparable<CPDFileFilte
this.extensions = extensions;
}
public void setInclusions(String regex) {
if (regex != null && regex.length() > 0) {
inclusionsPattern = Pattern.compile(regex);
}
}
public void setExclusions(String regex) {
if (regex != null && regex.length() > 0) {
exclusionsPattern = Pattern.compile(regex);
@ -50,7 +58,15 @@ public class CPDFileFilter extends FileFilter implements Comparable<CPDFileFilte
return true;
}
// check full path against exclusions
// check full path for inclusions
if (inclusionsPattern != null) {
Matcher m = inclusionsPattern.matcher(f.getAbsolutePath());
if (!m.matches()) {
return false;
}
}
// check full path for exclusions
if (exclusionsPattern != null) {
Matcher m = exclusionsPattern.matcher(f.getAbsolutePath());
if (m.matches()) {
@ -58,6 +74,8 @@ public class CPDFileFilter extends FileFilter implements Comparable<CPDFileFilte
}
}
Log.log(Log.DEBUG, this, "CPD checking: " + f.getAbsolutePath());
// check the extension against acceptable extensions
String name = f.getName();
for (String ext : extensions) {

@ -77,6 +77,7 @@ public class PMDJEditPlugin extends EBPlugin {
public static final String JAVA_VERSION_PROPERTY = "pmd.java.version";
public static final String LAST_DIRECTORY = "pmd.cpd.lastDirectory";
public static final String LAST_EXCLUSION_REGEX = "pmd.cpd.lastExclusionRegex";
public static final String LAST_INCLUSION_REGEX = "pmd.cpd.lastInclusionRegex";
public static final String LAST_SELECTED_FILTER = "pmd.cpd.lastSelectedFilter";
public static final String OPTION_RULES_PREFIX = "options.pmd.rules.";
public static final String PRINT_RULE = "pmd.printRule";
@ -89,6 +90,7 @@ public class PMDJEditPlugin extends EBPlugin {
public static final String RENDERER = "pmd.renderer";
private static int lastSelectedFilter = 0;
private static String lastInclusion = "";
private static String lastExclusion = "";
public void start() {
@ -96,6 +98,7 @@ public class PMDJEditPlugin extends EBPlugin {
// Log.log(Log.DEBUG,this,"Instance created.");
errorSource = new DefaultErrorSource(NAME);
lastSelectedFilter = jEdit.getIntegerProperty(LAST_SELECTED_FILTER, 0);
lastInclusion = jEdit.getProperty(LAST_INCLUSION_REGEX, "");
lastExclusion = jEdit.getProperty(LAST_EXCLUSION_REGEX, "");
}
@ -427,6 +430,8 @@ public class PMDJEditPlugin extends EBPlugin {
fileTypeSelector.setSelectedIndex(lastSelectedFilter);
fileTypeSelector.setEditable(false);
JTextField inclusionsRegex = new JTextField();
inclusionsRegex.setText(lastInclusion);
JTextField exclusionsRegex = new JTextField();
exclusionsRegex.setText(lastExclusion);
@ -434,24 +439,31 @@ public class PMDJEditPlugin extends EBPlugin {
pnlAccessory.add(txttilesize, "1, 0, 1, 1, W,, 3");
pnlAccessory.add(chkRecursive, "0, 1, 2, 1, W,, 3");
pnlAccessory.add(fileTypeSelector, "0, 2, 2, 1, W,, 3");
pnlAccessory.add(new JLabel(jEdit.getProperty("net.sf.pmd.Exclusions", "Exclusions")), "0, 3, 1, 1, W,, 3");
pnlAccessory.add(exclusionsRegex, "1, 3, 1, 1, W, w, 3");
pnlAccessory.add(new JLabel(jEdit.getProperty("net.sf.pmd.Inclusions", "Inclusions")), "0, 3, 1, 1, W,, 3");
pnlAccessory.add(inclusionsRegex, "1, 3, 1, 1, W, w, 3");
pnlAccessory.add(new JLabel(jEdit.getProperty("net.sf.pmd.Exclusions", "Exclusions")), "0, 4, 1, 1, W,, 3");
pnlAccessory.add(exclusionsRegex, "1, 4, 1, 1, W, w, 3");
chooser.setAccessory(pnlAccessory);
int returnVal = chooser.showOpenDialog(view);
File selectedFile = null;
String inclusions = null;
String exclusions = null;
CPDFileFilter mode = null;
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
inclusions = inclusionsRegex.getText();
jEdit.setProperty(LAST_INCLUSION_REGEX, inclusions);
lastInclusion = inclusions;
exclusions = exclusionsRegex.getText();
jEdit.setProperty(LAST_EXCLUSION_REGEX, exclusions);
lastExclusion = exclusions;
mode = (CPDFileFilter) fileTypeSelector.getSelectedItem();
lastSelectedFilter = fileTypeSelector.getSelectedIndex();
jEdit.setIntegerProperty(LAST_SELECTED_FILTER, lastSelectedFilter);
mode.setInclusions(inclusions);
mode.setExclusions(exclusions);
if (! selectedFile.isDirectory()) {