View Javadoc
1   package org.apache.rat.mp.util.ignore;
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  import org.apache.maven.plugin.logging.Log;
23  
24  import java.io.BufferedReader;
25  import java.io.File;
26  import java.io.FileReader;
27  import java.io.IOException;
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collection;
31  import java.util.List;
32  import java.util.Optional;
33  
34  public class GlobIgnoreMatcher implements IgnoreMatcher {
35  
36      final List<String> exclusionLines = new ArrayList<>();
37  
38      private static final List<String> COMMENT_PREFIXES = Arrays.asList("#", "##", "//", "/**", "/*");
39  
40      public GlobIgnoreMatcher() {
41          // No rules to load
42      }
43  
44      public GlobIgnoreMatcher(final Log log, final File scmIgnore) {
45          loadFile(log, scmIgnore);
46      }
47  
48      /**
49       * Add a single rule to the set
50       * @param rule The line that matches some files
51       */
52      public void addRule(final String rule) {
53          if (!exclusionLines.contains(rule)) {
54              exclusionLines.add(rule);
55          }
56      }
57  
58      /**
59       * Add a set of rules to the set
60       * @param rules The line that matches some files
61       */
62      public void addRules(final Collection<String> rules) {
63          for (String rule : rules) {
64              if (!exclusionLines.contains(rule)) {
65                  exclusionLines.add(rule);
66              }
67          }
68      }
69  
70      /**
71       * Parses excludes from the given SCM ignore file.
72       *
73       * @param log       Maven log to show output during RAT runs.
74       * @param scmIgnore if <code>null</code> or invalid an empty list of exclusions is returned.
75       */
76      public void loadFile(final Log log, final File scmIgnore) {
77          if (scmIgnore != null && scmIgnore.exists() && scmIgnore.isFile()) {
78              log.debug("Parsing exclusions from " + scmIgnore);
79  
80              try (BufferedReader reader = new BufferedReader(new FileReader(scmIgnore))) {
81                  String line;
82                  while ((line = reader.readLine()) != null) {
83                      if (!isComment(line)) {
84                          exclusionLines.add(line);
85                          log.debug("Added " + line);
86                      }
87                  }
88              } catch (final IOException e) {
89                  log.warn("Cannot parse " + scmIgnore + " for exclusions. Will skip this file.");
90                  log.debug("Skip parsing " + scmIgnore + " due to " + e.getMessage());
91              }
92          }
93      }
94  
95      /**
96       * Determines whether the given line is a comment or not based on scanning
97       * for prefixes
98       * @see #COMMENT_PREFIXES
99       *
100      * @param line line to verify.
101      * @return <code>true</code> if the given line is a commented out line.
102      */
103     public static boolean isComment(final String line) {
104         if (line == null || line.length() <= 0) {
105             return false;
106         }
107 
108         final String trimLine = line.trim();
109         for (String prefix : COMMENT_PREFIXES) {
110             if (trimLine.startsWith(prefix)) {
111                 return true;
112             }
113         }
114         return false;
115     }
116 
117     public List<String> getExclusionLines() {
118         return exclusionLines;
119     }
120 
121     @Override
122     public boolean isEmpty() {
123         return exclusionLines.isEmpty();
124     }
125 
126     @Override
127     public Optional<Boolean> isIgnoredFile(String filename) {
128         // Not used for Glob Rules; using the DirectoryScanner instead
129         // It CAN be moved here if so desired.
130         return Optional.empty() ;
131     }
132 }