// Path: tools.pmd.codedefects.core/src/org/nbheaven/sqe/tools/pmd/codedefects/core/settings/impl/PMDSettingsImpl.java
// public final class PMDSettingsImpl implements PMDSettings {
// 
//     private static final File SETTINGS_DIR = new File(Places.getUserDirectory(), "config" + File.separatorChar + "pmd");
//     private static final String SETTINGS_FILE = "pmd.settings";
// 
//     public static PMDSettings globalSettings() {
//         return new PMDSettingsImpl(new File(SETTINGS_DIR, SETTINGS_FILE));
//     }
// 
//     private Map<String, Boolean> ruleMap;
// 
//     File settingsFile;
// 
//     public PMDSettingsImpl(File file) {
//         settingsFile = file;
//         readFile();
//     }
// 
//     @Override
//     public void activateRule(Rule rule) {
//         ruleMap.remove(rule.getName());
//         updateFile();
//     }
// 
//     @Override
//     public void deactivateRule(Rule rule) {
//         ruleMap.put(rule.getName(), Boolean.FALSE);
//         updateFile();
//     }
// 
//     @Override
//     public boolean isRuleActive(Rule rule) {
//         Boolean b = ruleMap.get(rule.getName());
//         return null == b ? true : b.booleanValue();
//     }
// 
//     @Override
//     public RuleSet getActiveRules() {
//         RuleSet activeRuleSet = new RuleSet();
//         RuleSet ruleSet = new RuleSet();
//         RuleSetFactory ruleSetFactory = new RuleSetFactory();
//         ruleSetFactory.setClassLoader(Lookup.getDefault().lookup(ClassLoader.class));
//         try {
//             Iterator<RuleSet> iterator = ruleSetFactory.getRegisteredRuleSets();
//             while (iterator.hasNext()) {
//                 ruleSet = iterator.next();
//                 for (Rule rule : ruleSet.getRules()) {
//                     if (isRuleActive(rule)) {
//                         activeRuleSet.addRule(rule);
//                     }
//                 }
//             }
//         } catch (RuleSetNotFoundException ex) {
//             Exceptions.printStackTrace(ex);
//         }
//         return activeRuleSet;
//     }
// 
//     private void updateFile() {
//         settingsFile.getParentFile().mkdirs();
//         try {
//             BufferedWriter writer = new BufferedWriter(new FileWriter(settingsFile, false));
//             for (Map.Entry<String, Boolean> entry : ruleMap.entrySet()) {
//                 if (!entry.getValue()) {
//                     writer.append(entry.getKey());
//                     writer.newLine();
//                 }
//             }
//             writer.flush();
//             writer.close();
//         } catch (IOException ex) {
//             Exceptions.printStackTrace(ex);
//         }
//     }
// 
//     private void readFile() {
//         ruleMap = new HashMap<String, Boolean>();
//         if (settingsFile.exists()) {
//             try {
//                 BufferedReader fileReader = new BufferedReader(new FileReader(settingsFile));
//                 String line;
//                 while (null != (line = fileReader.readLine())) {
//                     ruleMap.put(line, Boolean.FALSE);
//                 }
//             } catch (IOException ex) {
//                 Exceptions.printStackTrace(ex);
//             }
//         }
//     }
// 
// }


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.logging.Logger;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDException;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import org.nbheaven.sqe.codedefects.core.spi.SQECodedefectScanner;
import org.nbheaven.sqe.tools.pmd.codedefects.core.PMDResult;
import org.nbheaven.sqe.tools.pmd.codedefects.core.settings.PMDSettings;
import org.nbheaven.sqe.tools.pmd.codedefects.core.settings.PMDSettingsProvider;
import org.nbheaven.sqe.tools.pmd.codedefects.core.settings.impl.PMDSettingsImpl;
import org.netbeans.api.java.queries.SourceLevelQuery;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.api.queries.FileEncodingQuery;
import org.openide.filesystems.FileObject;
        return settings;
    }

    private void init() {
        pmd = new PMD();

        ruleContext = new RuleContext();
        report = new Report();
        ruleContext.setReport(report);

        PMDSettingsProvider settingsProvider = getProject().getLookup().lookup(PMDSettingsProvider.class);
        if (settingsProvider != null) {
            settings = settingsProvider.getPMDSettings();
        }
        if (settings == null) {
            settings = PMDSettingsImpl.globalSettings();
        }
        rules = new RuleSets(settings.getActiveRules());
    }

    private LanguageVersion getSourceType(FileObject fo) {
        String sourceLevel = SourceLevelQuery.getSourceLevel(fo);
        LanguageVersion version = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion(sourceLevel);
        assert version != null : "PMD - No LanguageVersion found for sourceLevel=" + sourceLevel;
        return version;
    }

    @Override
    protected void scan() {
        getProgressHandle().progress("Setting up PMD");
// Path: tools.pmd.codedefects.core/src/org/nbheaven/sqe/tools/pmd/codedefects/core/settings/impl/PMDSettingsImpl.java
// public final class PMDSettingsImpl implements PMDSettings {
// 
//     private static final File SETTINGS_DIR = new File(Places.getUserDirectory(), "config" + File.separatorChar + "pmd");
//     private static final String SETTINGS_FILE = "pmd.settings";
// 
//     public static PMDSettings globalSettings() {
//         return new PMDSettingsImpl(new File(SETTINGS_DIR, SETTINGS_FILE));
//     }
// 
//     private Map<String, Boolean> ruleMap;
// 
//     File settingsFile;
// 
//     public PMDSettingsImpl(File file) {
//         settingsFile = file;
//         readFile();
//     }
// 
//     @Override
//     public void activateRule(Rule rule) {
//         ruleMap.remove(rule.getName());
//         updateFile();
//     }
// 
//     @Override
//     public void deactivateRule(Rule rule) {
//         ruleMap.put(rule.getName(), Boolean.FALSE);
//         updateFile();
//     }
// 
//     @Override
//     public boolean isRuleActive(Rule rule) {
//         Boolean b = ruleMap.get(rule.getName());
//         return null == b ? true : b.booleanValue();
//     }
// 
//     @Override
//     public RuleSet getActiveRules() {
//         RuleSet activeRuleSet = new RuleSet();
//         RuleSet ruleSet = new RuleSet();
//         RuleSetFactory ruleSetFactory = new RuleSetFactory();
//         ruleSetFactory.setClassLoader(Lookup.getDefault().lookup(ClassLoader.class));
//         try {
//             Iterator<RuleSet> iterator = ruleSetFactory.getRegisteredRuleSets();
//             while (iterator.hasNext()) {
//                 ruleSet = iterator.next();
//                 for (Rule rule : ruleSet.getRules()) {
//                     if (isRuleActive(rule)) {
//                         activeRuleSet.addRule(rule);
//                     }
//                 }
//             }
//         } catch (RuleSetNotFoundException ex) {
//             Exceptions.printStackTrace(ex);
//         }
//         return activeRuleSet;
//     }
// 
//     private void updateFile() {
//         settingsFile.getParentFile().mkdirs();
//         try {
//             BufferedWriter writer = new BufferedWriter(new FileWriter(settingsFile, false));
//             for (Map.Entry<String, Boolean> entry : ruleMap.entrySet()) {
//                 if (!entry.getValue()) {
//                     writer.append(entry.getKey());
//                     writer.newLine();
//                 }
//             }
//             writer.flush();
//             writer.close();
//         } catch (IOException ex) {
//             Exceptions.printStackTrace(ex);
//         }
//     }
// 
//     private void readFile() {
//         ruleMap = new HashMap<String, Boolean>();
//         if (settingsFile.exists()) {
//             try {
//                 BufferedReader fileReader = new BufferedReader(new FileReader(settingsFile));
//                 String line;
//                 while (null != (line = fileReader.readLine())) {
//                     ruleMap.put(line, Boolean.FALSE);
//                 }
//             } catch (IOException ex) {
//                 Exceptions.printStackTrace(ex);
//             }
//         }
//     }
// 
// }


// Path: tools.pmd.codedefects.core/src/org/nbheaven/sqe/tools/pmd/codedefects/core/internal/PMDScannerJob.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.logging.Logger;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDException;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import org.nbheaven.sqe.codedefects.core.spi.SQECodedefectScanner;
import org.nbheaven.sqe.tools.pmd.codedefects.core.PMDResult;
import org.nbheaven.sqe.tools.pmd.codedefects.core.settings.PMDSettings;
import org.nbheaven.sqe.tools.pmd.codedefects.core.settings.PMDSettingsProvider;
import org.nbheaven.sqe.tools.pmd.codedefects.core.settings.impl.PMDSettingsImpl;
import org.netbeans.api.java.queries.SourceLevelQuery;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.api.queries.FileEncodingQuery;
import org.openide.filesystems.FileObject;

        return settings;
    }

    private void init() {
        pmd = new PMD();

        ruleContext = new RuleContext();
        report = new Report();
        ruleContext.setReport(report);

        PMDSettingsProvider settingsProvider = getProject().getLookup().lookup(PMDSettingsProvider.class);
        if (settingsProvider != null) {
            settings = settingsProvider.getPMDSettings();
        }
        if (settings == null) {
            settings = PMDSettingsImpl.globalSettings();
        }
        rules = new RuleSets(settings.getActiveRules());
    }

    private LanguageVersion getSourceType(FileObject fo) {
        String sourceLevel = SourceLevelQuery.getSourceLevel(fo);
        LanguageVersion version = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion(sourceLevel);
        assert version != null : "PMD - No LanguageVersion found for sourceLevel=" + sourceLevel;
        return version;
    }

    @Override
    protected void scan() {
        getProgressHandle().progress("Setting up PMD");
        init();