View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd;
5   
6   import java.util.logging.Level;
7   import java.util.logging.Logger;
8   
9   import net.sourceforge.pmd.benchmark.Benchmark;
10  import net.sourceforge.pmd.benchmark.Benchmarker;
11  
12  public final class RulesetsFactoryUtils {
13  
14  	private static final Logger LOG = Logger.getLogger(RulesetsFactoryUtils.class.getName());
15  
16  	private RulesetsFactoryUtils() {}
17  
18      /**
19       * Creates a new rulesets with the given string. The resulting rulesets will contain
20       * all referenced rulesets.
21       * @param rulesets the string with the rulesets to load
22       * @param factory the ruleset factory
23       * @return the rulesets
24       * @throws IllegalArgumentException if rulesets is empty (means, no rules have been found) or if a
25       * ruleset couldn't be found.
26       */
27      public static RuleSets getRuleSets(String rulesets, RuleSetFactory factory) {
28          RuleSets ruleSets = null;
29          try {
30              factory.setWarnDeprecated(true);
31              ruleSets = factory.createRuleSets(rulesets);
32              factory.setWarnDeprecated(false);
33              printRuleNamesInDebug(ruleSets);
34              if (ruleSets.ruleCount() == 0) {
35                  String msg = "No rules found. Maybe you mispelled a rule name? (" + rulesets + ")";
36                  LOG.log(Level.SEVERE, msg);
37                  throw new IllegalArgumentException(msg);
38              }
39          } catch (RuleSetNotFoundException rsnfe) {
40              LOG.log(Level.SEVERE, "Ruleset not found", rsnfe);
41              throw new IllegalArgumentException(rsnfe);
42          }
43          return ruleSets;
44      }
45  
46      /**
47       * See {@link #getRuleSets(String, RuleSetFactory)}. In addition, the loading of the rules
48       * is benchmarked.
49       * @param rulesets the string with the rulesets to load
50       * @param factory the ruleset factory
51       * @return the rulesets
52       * @throws IllegalArgumentException if rulesets is empty (means, no rules have been found) or if a
53       * ruleset couldn't be found.
54       */
55      public static RuleSets getRuleSetsWithBenchmark(String rulesets, RuleSetFactory factory) {
56          long loadRuleStart = System.nanoTime();
57          RuleSets ruleSets = null;
58          try {
59              ruleSets = getRuleSets(rulesets, factory);
60          } finally {
61              long endLoadRules = System.nanoTime();
62              Benchmarker.mark(Benchmark.LoadRules, endLoadRules - loadRuleStart, 0);
63          }
64          return ruleSets;
65      }
66  
67  	public static RuleSetFactory getRulesetFactory(PMDConfiguration configuration) {
68  		RuleSetFactory ruleSetFactory = new RuleSetFactory();
69  		ruleSetFactory.setMinimumPriority(configuration.getMinimumPriority());
70  		return ruleSetFactory;
71  	}
72  
73  	/**
74  	 * If in debug modus, print the names of the rules.
75  	 *
76  	 * @param rulesets     the RuleSets to print
77  	 */
78  	private static void printRuleNamesInDebug(RuleSets rulesets) {
79  		if (LOG.isLoggable(Level.FINER)) {
80  			for (Rule r : rulesets.getAllRules()) {
81  				LOG.finer("Loaded rule " + r.getName());
82  			}
83  		}
84  	}
85  }