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          return getRuleSets(rulesets, null, factory);
29      }
30  
31      /**
32       * Creates a new rulesets with the given string. The resulting rulesets will contain
33       * all referenced rulesets.
34       * @param rulesets the string with the rulesets to load
35       * @param factory the ruleset factory
36       * @param pmdRuleSets RuleSets initialized in PMDConfiguration
37       * @return the rulesets
38       * @throws IllegalArgumentException if rulesets is empty (means, no rules have been found) or if a
39       * ruleset couldn't be found.
40       */
41      public static RuleSets getRuleSets(String rulesets, RuleSets pmdRuleSets, RuleSetFactory factory) {
42          RuleSets ruleSets = null;
43          try {
44              factory.setWarnDeprecated(true);
45              ruleSets = factory.createRuleSets(rulesets, pmdRuleSets);
46              factory.setWarnDeprecated(false);
47              printRuleNamesInDebug(ruleSets);
48              if (ruleSets.ruleCount() == 0) {
49                  String msg = "No rules found. Maybe you mispelled a rule name? (" + rulesets + ")";
50                  LOG.log(Level.SEVERE, msg);
51                  throw new IllegalArgumentException(msg);
52              }
53          } catch (RuleSetNotFoundException rsnfe) {
54              LOG.log(Level.SEVERE, "Ruleset not found", rsnfe);
55              throw new IllegalArgumentException(rsnfe);
56          }
57          return ruleSets;
58      }
59  
60      /**
61       * See {@link #getRuleSets(String, RuleSetFactory)}. In addition, the loading of the rules
62       * is benchmarked.
63       * @param rulesets the string with the rulesets to load
64       * @param factory the ruleset factory
65       * @return the rulesets
66       * @throws IllegalArgumentException if rulesets is empty (means, no rules have been found) or if a
67       * ruleset couldn't be found.
68       */
69      public static RuleSets getRuleSetsWithBenchmark(String rulesets, RuleSetFactory factory) {
70          return getRuleSetsWithBenchmark(rulesets, null, factory);
71      }
72  
73      /**
74       * See {@link #getRuleSets(String, RuleSetFactory)}. In addition, the loading of the rules
75       * is benchmarked.
76       * @param rulesets the string with the rulesets to load
77       * @param pmdRuleSets RuleSets initialized in PMDConfiguration
78       * @param factory the ruleset factory
79       * @return the rulesets
80       * @throws IllegalArgumentException if rulesets is empty (means, no rules have been found) or if a
81       * ruleset couldn't be found.
82       */
83      public static RuleSets getRuleSetsWithBenchmark(String rulesets, RuleSets pmdRuleSets, RuleSetFactory factory) {
84          long loadRuleStart = System.nanoTime();
85          RuleSets ruleSets = null;
86          try {
87              ruleSets = getRuleSets(rulesets, pmdRuleSets, factory);
88          } finally {
89              long endLoadRules = System.nanoTime();
90              Benchmarker.mark(Benchmark.LoadRules, endLoadRules - loadRuleStart, 0);
91          }
92          return ruleSets;
93      }
94  
95  	public static RuleSetFactory getRulesetFactory(PMDConfiguration configuration) {
96  		RuleSetFactory ruleSetFactory = new RuleSetFactory();
97  		ruleSetFactory.setMinimumPriority(configuration.getMinimumPriority());
98  		return ruleSetFactory;
99  	}
100 
101 	/**
102 	 * If in debug modus, print the names of the rules.
103 	 *
104 	 * @param rulesets     the RuleSets to print
105 	 */
106 	private static void printRuleNamesInDebug(RuleSets rulesets) {
107 		if (LOG.isLoggable(Level.FINER)) {
108 			for (Rule r : rulesets.getAllRules()) {
109 				LOG.finer("Loaded rule " + r.getName());
110 			}
111 		}
112 	}
113 }