View Javadoc
1   package org.apache.rat.mp.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import org.apache.maven.plugin.logging.Log;
24  import org.apache.rat.config.SourceCodeManagementSystems;
25  import org.apache.rat.mp.util.ignore.GitIgnoreMatcher;
26  import org.apache.rat.mp.util.ignore.GlobIgnoreMatcher;
27  import org.apache.rat.mp.util.ignore.IgnoreMatcher;
28  
29  import java.io.File;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * Helper to parse SCM ignore files to add entries as excludes during RAT runs.
35   * Since we log errors it needs to reside inside of the maven plugin.
36   */
37  public final class ScmIgnoreParser {
38      private ScmIgnoreParser() {
39          // prevent instantiation of utility class
40      }
41  
42      /**
43       * Parse ignore files from all known SCMs that have ignore files.
44       *
45       * @param log     Show information via maven logger.
46       * @param baseDir base directory from which to look for SCM ignores.
47       * @return Exclusions from the SCM ignore files.
48       */
49      public static List<IgnoreMatcher> getExclusionsFromSCM(final Log log, final File baseDir) {
50          List<IgnoreMatcher> ignoreMatchers = new ArrayList<>();
51          for (SourceCodeManagementSystems scm : SourceCodeManagementSystems.values()) {
52              switch (scm) {
53                  case GIT:
54                      GitIgnoreMatcher gitIgnoreMatcher = new GitIgnoreMatcher(log, baseDir);
55                      if (!gitIgnoreMatcher.isEmpty()) {
56                          ignoreMatchers.add(gitIgnoreMatcher);
57                      }
58                      break;
59                  default:
60                      if (scm.hasIgnoreFile()) {
61                          GlobIgnoreMatcher ignoreMatcher = new GlobIgnoreMatcher(log, new File(baseDir, scm.getIgnoreFile()));
62                          if (!ignoreMatcher.isEmpty()) {
63                              ignoreMatchers.add(ignoreMatcher);
64                          }
65                      }
66                      break;
67              }
68          }
69          return ignoreMatchers;
70      }
71  
72  
73  }