Skip to content

Commit 4fff1b6

Browse files
committed
Merge branch 'pr-51'
2 parents 7fd9de5 + 1ae1f29 commit 4fff1b6

File tree

5 files changed

+112
-41
lines changed

5 files changed

+112
-41
lines changed

ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Eclipse Update Site:
1919

2020
### External Contributions
2121

22+
* [#51](https://github.com/pmd/pmd-eclipse-plugin/pull/51): Allow multiple ruleset files - [Phillip Krall](https://github.com/pkrall520)
2223
* [#53](https://github.com/pmd/pmd-eclipse-plugin/pull/53): README: markdown, fix links - [Lars Hvam](https://github.com/larshp)
2324
* [#60](https://github.com/pmd/pmd-eclipse-plugin/pull/60): Update to PMD 6.7.0 - [Jan](https://github.com/jgerken)
2425

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/IProjectProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
package net.sourceforge.pmd.eclipse.runtime.properties;
3838

3939
import java.io.File;
40+
import java.util.List;
4041
import java.util.Set;
4142

4243
import org.eclipse.core.resources.IProject;
@@ -106,7 +107,7 @@ public interface IProjectProperties {
106107
* @return Returns the resolved RuleSet File suitable for loading a rule
107108
* set.
108109
*/
109-
File getResolvedRuleSetFile() throws PropertiesException;
110+
List<File> getResolvedRuleSetFile() throws PropertiesException;
110111

111112
/**
112113
* @return Returns the project Working Set.

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesImpl.java

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import java.io.ByteArrayOutputStream;
4141
import java.io.File;
4242
import java.io.IOException;
43+
import java.util.ArrayList;
4344
import java.util.HashSet;
45+
import java.util.List;
4446
import java.util.Set;
4547

4648
import org.apache.log4j.Logger;
@@ -106,8 +108,8 @@ public ProjectPropertiesImpl(final IProject project, IProjectPropertiesManager p
106108
}
107109

108110
/**
109-
* Determines the included and excluded paths configured for the build path
110-
* of this eclipse project.
111+
* Determines the included and excluded paths configured for the build path of
112+
* this eclipse project.
111113
*/
112114
private void determineBuildPathIncludesExcludes() {
113115
IClasspathEntry source = PMDPlugin.buildSourceClassPathEntryFor(project);
@@ -180,16 +182,19 @@ public RuleSet getProjectRuleSet() throws PropertiesException {
180182
public void setProjectRuleSet(final RuleSet projectRuleSet) throws PropertiesException {
181183
LOG.debug("Set a rule set for project " + this.project.getName());
182184
if (projectRuleSet == null) {
183-
//TODO: NLS
185+
// TODO: NLS
184186
throw new PropertiesException("Setting a project rule set to null");
185187
}
186188

187189
this.needRebuild |= !this.projectRuleSet.getRules().equals(projectRuleSet.getRules());
188190
this.projectRuleSet = projectRuleSet;
189191
if (this.ruleSetStoredInProject) {
190-
File f = getResolvedRuleSetFile();
191-
if (f != null) {
192-
projectRuleFileLastModified = f.lastModified();
192+
for (File f : getResolvedRuleSetFile()) {
193+
if (f != null) {
194+
if (projectRuleFileLastModified < f.lastModified()) {
195+
projectRuleFileLastModified = f.lastModified();
196+
}
197+
}
193198
}
194199
}
195200
}
@@ -211,13 +216,16 @@ public void setRuleSetStoredInProject(final boolean ruleSetStoredInProject) thro
211216
this.ruleSetStoredInProject = ruleSetStoredInProject;
212217
if (this.ruleSetStoredInProject) {
213218
if (!isRuleSetFileExist()) {
214-
//TODO: NLS
215-
throw new PropertiesException("The project ruleset file cannot be found for project "
216-
+ this.project.getName());
219+
// TODO: NLS
220+
throw new PropertiesException(
221+
"The project ruleset file cannot be found for project " + this.project.getName());
217222
}
218-
File f = getResolvedRuleSetFile();
219-
if (f != null) {
220-
projectRuleFileLastModified = f.lastModified();
223+
for (File f : getResolvedRuleSetFile()) {
224+
if (f != null) {
225+
if (projectRuleFileLastModified < f.lastModified()) {
226+
projectRuleFileLastModified = f.lastModified();
227+
}
228+
}
221229
}
222230
}
223231
}
@@ -240,12 +248,15 @@ public void setRuleSetFile(String ruleSetFile) throws PropertiesException {
240248
if (ruleSetStoredInProject) {
241249
if (!isRuleSetFileExist()) {
242250
// TODO: NLS
243-
throw new PropertiesException("The project ruleset file cannot be found for project "
244-
+ project.getName());
251+
throw new PropertiesException(
252+
"The project ruleset file cannot be found for project " + project.getName());
245253
}
246-
File f = getResolvedRuleSetFile();
247-
if (f != null) {
248-
projectRuleFileLastModified = f.lastModified();
254+
for (File f : getResolvedRuleSetFile()) {
255+
if (f != null) {
256+
if (projectRuleFileLastModified < f.lastModified()) {
257+
projectRuleFileLastModified = f.lastModified();
258+
}
259+
}
249260
}
250261
}
251262
}
@@ -277,9 +288,10 @@ public boolean isNeedRebuild() {
277288
LOG.debug(" PMD Enabled = " + pmdEnabled);
278289
LOG.debug(" Project need rebuild = " + needRebuild);
279290
if (ruleSetStoredInProject) {
280-
File f = getResolvedRuleSetFile();
281-
if (f != null) {
282-
needRebuild |= f.lastModified() > projectRuleFileLastModified;
291+
for (File f : getResolvedRuleSetFile()) {
292+
if (f != null) {
293+
needRebuild |= f.lastModified() > projectRuleFileLastModified;
294+
}
283295
}
284296
}
285297
return pmdEnabled && needRebuild;
@@ -297,31 +309,41 @@ public void setNeedRebuild(final boolean needRebuild) {
297309
* @see net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties#isRuleSetFileExist()
298310
*/
299311
public final boolean isRuleSetFileExist() {
300-
return getResolvedRuleSetFile().exists();
312+
boolean ret = true;
313+
for (File f : getResolvedRuleSetFile()) {
314+
if (!f.exists()) {
315+
ret = false;
316+
}
317+
}
318+
return ret;
301319
}
302320

303321
/**
304322
* @see net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties#getResolvedRuleSetFile()
305323
*/
306-
public File getResolvedRuleSetFile() {
324+
public List<File> getResolvedRuleSetFile() {
307325
// Check as project-relative path
308-
IFile file = project.getFile(getRuleSetFile());
309-
File f = getExistingFileOrNull(file);
310-
if (f == null) {
311-
// Check as workspace-relative path
312-
IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();
313-
try {
314-
IFile workspaceFile = workspaceRoot.getFile(new Path(getRuleSetFile()));
315-
f = getExistingFileOrNull(workspaceFile);
316-
} catch (IllegalArgumentException e) {
317-
// Fall back to below
318-
}
326+
List<File> files = new ArrayList<File>();
327+
for (String ruleSetFile : getRuleSetFile().split(",")) {
328+
IFile file = project.getFile(ruleSetFile);
329+
File f = getExistingFileOrNull(file);
319330
if (f == null) {
320-
// Fall back to file system path
321-
f = new File(getRuleSetFile());
331+
// Check as workspace-relative path
332+
IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();
333+
try {
334+
IFile workspaceFile = workspaceRoot.getFile(new Path(ruleSetFile));
335+
f = getExistingFileOrNull(workspaceFile);
336+
} catch (IllegalArgumentException e) {
337+
// Fall back to below
338+
}
339+
if (f == null) {
340+
// Fall back to file system path
341+
f = new File(ruleSetFile);
342+
}
322343
}
344+
files.add(f);
323345
}
324-
return f;
346+
return files;
325347
}
326348

327349
private File getExistingFileOrNull(IFile file) {

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesManagerImpl.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.ArrayList;
4545
import java.util.HashMap;
4646
import java.util.HashSet;
47+
import java.util.Iterator;
4748
import java.util.List;
4849
import java.util.Map;
4950
import java.util.Set;
@@ -171,8 +172,42 @@ private void loadRuleSetFromProject(IProjectProperties projectProperties) throws
171172
LOG.debug("Loading ruleset from project ruleset file: " + projectProperties.getRuleSetFile());
172173
try {
173174
final RuleSetFactory factory = new RuleSetFactory();
174-
final File ruleSetFile = projectProperties.getResolvedRuleSetFile();
175-
projectProperties.setProjectRuleSet(factory.createRuleSets(ruleSetFile.getPath()).getAllRuleSets()[0]);
175+
RuleSet allRuleSets = null;
176+
for (final File ruleSetFile : projectProperties.getResolvedRuleSetFile()) {
177+
RuleSet rs = factory.createRuleSets(ruleSetFile.getPath()).getAllRuleSets()[0];
178+
if (allRuleSets == null) {
179+
/* The first ruleset file */
180+
allRuleSets = rs;
181+
} else {
182+
/*
183+
* Loop through all the rules in an additional ruleset file. If a previous file
184+
* has defined the rule, then remove it. If it is not found, then add the new
185+
* rule.
186+
*/
187+
List<Rule> rules = new ArrayList<Rule>(allRuleSets.getRules());
188+
for (Rule rule2 : rs.getRules()) {
189+
boolean processed = false;
190+
for (Rule rule1 : allRuleSets.getRules()) {
191+
if (rule2.getName().equals(rule1.getName())) {
192+
for (Iterator<Rule> iter = rules.listIterator(); iter.hasNext();) {
193+
Rule r = iter.next();
194+
if (r.getName().equals(rule1.getName())) {
195+
iter.remove();
196+
}
197+
}
198+
rules.add(rule2);
199+
processed = true;
200+
}
201+
if (!processed) {
202+
rules.add(rule2);
203+
}
204+
}
205+
}
206+
allRuleSets.getRules().removeAll(allRuleSets.getRules());
207+
allRuleSets.getRules().addAll(rules);
208+
}
209+
}
210+
projectProperties.setProjectRuleSet(allRuleSets);
176211
projectProperties.setNeedRebuild(false);
177212
} catch (RuleSetNotFoundException e) {
178213
PMDPlugin.getDefault()

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/properties/PMDProjectPropertyPage.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636

3737
package net.sourceforge.pmd.eclipse.ui.properties;
3838

39+
import java.io.File;
3940
import java.util.ArrayList;
4041
import java.util.Collection;
4142
import java.util.Comparator;
43+
import java.util.List;
4244

4345
import org.apache.log4j.Logger;
4446
import org.eclipse.core.resources.IProject;
@@ -399,10 +401,20 @@ private Button buildRuleSetBrowseButton(final Composite parent) {
399401
@Override
400402
public void widgetSelected(SelectionEvent e) {
401403
// TODO EMF's ResourceDialog would be better.
402-
FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
404+
FileDialog fileDialog = new FileDialog(getShell(), SWT.MULTI);
403405
String path = fileDialog.open();
406+
List<String> files = new ArrayList<>();
407+
String[] names = fileDialog.getFileNames();
408+
for (int i = 0, n = names.length; i < n; i++) {
409+
StringBuffer buf = new StringBuffer(fileDialog.getFilterPath());
410+
if (buf.charAt(buf.length() - 1) != File.separatorChar) {
411+
buf.append(File.separatorChar);
412+
}
413+
buf.append(names[i]);
414+
files.add(buf.toString());
415+
}
404416
if (path != null) {
405-
ruleSetFileText.setText(path);
417+
ruleSetFileText.setText(String.join(",", files));
406418
}
407419
}
408420
});

0 commit comments

Comments
 (0)