View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.internal.utils;
21  
22  import java.util.HashSet;
23  import java.util.Properties;
24  import java.util.Set;
25  
26  import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
27  import com.puppycrawl.tools.checkstyle.PropertiesExpander;
28  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
29  import com.puppycrawl.tools.checkstyle.api.Configuration;
30  
31  public final class ConfigurationUtil {
32      private ConfigurationUtil() {
33      }
34  
35      public static Configuration loadConfiguration(String path) throws CheckstyleException {
36          final Properties props = new Properties();
37  
38          props.setProperty("checkstyle.basedir", "basedir");
39          props.setProperty("checkstyle.cache.file", "file");
40          props.setProperty("checkstyle.suppressions.file", "file");
41          props.setProperty("checkstyle.suppressions-xpath.file", "file");
42          props.setProperty("checkstyle.header.file", "file");
43          props.setProperty("checkstyle.regexp.header.file", "file");
44          props.setProperty("checkstyle.importcontrol.file", "file");
45          props.setProperty("checkstyle.importcontroltest.file", "file");
46  
47          return loadConfiguration(path, props);
48      }
49  
50      public static Configuration loadConfiguration(String path, Properties props)
51              throws CheckstyleException {
52          return ConfigurationLoader.loadConfiguration(path, new PropertiesExpander(props));
53      }
54  
55      public static Set<Configuration> getModules(Configuration config) {
56          final Set<Configuration> result = new HashSet<>();
57  
58          for (Configuration child : config.getChildren()) {
59              if ("TreeWalker".equals(child.getName())) {
60                  result.addAll(getModules(child));
61              }
62              else {
63                  result.add(child);
64              }
65          }
66  
67          return result;
68      }
69  
70      public static Set<Configuration> getChecks(Configuration config) {
71          final Set<Configuration> result = new HashSet<>();
72  
73          for (Configuration child : config.getChildren()) {
74              if ("TreeWalker".equals(child.getName())) {
75                  result.addAll(getModules(child));
76              }
77          }
78  
79          return result;
80      }
81  }