1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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  
33      private ConfigurationUtil() {
34      }
35  
36      public static Configuration loadConfiguration(String path) throws CheckstyleException {
37          final Properties props = new Properties();
38  
39          props.setProperty("checkstyle.basedir", "basedir");
40          props.setProperty("checkstyle.cache.file", "file");
41          props.setProperty("checkstyle.suppressions.file", "file");
42          props.setProperty("checkstyle.suppressions-xpath.file", "file");
43          props.setProperty("checkstyle.header.file", "file");
44          props.setProperty("checkstyle.regexp.header.file", "file");
45          props.setProperty("checkstyle.importcontrol.file", "file");
46          props.setProperty("checkstyle.importcontroltest.file", "file");
47  
48          return loadConfiguration(path, props);
49      }
50  
51      public static Configuration loadConfiguration(String path, Properties props)
52              throws CheckstyleException {
53          return ConfigurationLoader.loadConfiguration(path, new PropertiesExpander(props));
54      }
55  
56      public static Set<Configuration> getModules(Configuration config) {
57          final Set<Configuration> result = new HashSet<>();
58  
59          for (Configuration child : config.getChildren()) {
60              if ("TreeWalker".equals(child.getName())) {
61                  result.addAll(getModules(child));
62              }
63              else {
64                  result.add(child);
65              }
66          }
67  
68          return result;
69      }
70  
71      public static Set<Configuration> getChecks(Configuration config) {
72          final Set<Configuration> result = new HashSet<>();
73  
74          for (Configuration child : config.getChildren()) {
75              if ("TreeWalker".equals(child.getName())) {
76                  result.addAll(getModules(child));
77              }
78          }
79  
80          return result;
81      }
82  
83  }