View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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          props.setProperty("checkstyle.java.version", "value");
48  
49          return loadConfiguration(path, props);
50      }
51  
52      public static Configuration loadConfiguration(String path, Properties props)
53              throws CheckstyleException {
54          return ConfigurationLoader.loadConfiguration(path, new PropertiesExpander(props));
55      }
56  
57      public static Set<Configuration> getModules(Configuration config) {
58          final Set<Configuration> result = new HashSet<>();
59  
60          for (Configuration child : config.getChildren()) {
61              if ("TreeWalker".equals(child.getName())) {
62                  result.addAll(getModules(child));
63              }
64              else {
65                  result.add(child);
66              }
67          }
68  
69          return result;
70      }
71  
72      public static Set<Configuration> getChecks(Configuration config) {
73          final Set<Configuration> result = new HashSet<>();
74  
75          for (Configuration child : config.getChildren()) {
76              if ("TreeWalker".equals(child.getName())) {
77                  result.addAll(getModules(child));
78              }
79          }
80  
81          return result;
82      }
83  
84  }