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.commons.io.IOUtils;
24  import org.apache.maven.plugin.logging.Log;
25  import org.apache.rat.config.SourceCodeManagementSystems;
26  
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileReader;
30  import java.io.IOException;
31  import java.util.ArrayList;
32  import java.util.Arrays;
33  import java.util.List;
34  
35  /**
36   * Helper to parse SCM ignore files to add entries as excludes during RAT runs.
37   * Since we log errors it needs to reside inside of the maven plugin.
38   */
39  public final class ScmIgnoreParser {
40      private ScmIgnoreParser() {
41          // prevent instantiation of utility class
42      }
43  
44      private static List<String> COMMENT_PREFIXES = Arrays.asList("#", "##", "//", "/**", "/*");
45  
46      /**
47       * Parses excludes from the given SCM ignore file.
48       *
49       * @param log       Maven log to show output during RAT runs.
50       * @param scmIgnore if <code>null</code> or invalid an empty list of exclusions is returned.
51       * @return all exclusions (=non-comment lines) from the SCM ignore file.
52       */
53      public static List<String> getExcludesFromFile(final Log log, final File scmIgnore) {
54  
55          final List<String> exclusionLines = new ArrayList<String>();
56  
57          if (scmIgnore != null && scmIgnore.exists() && scmIgnore.isFile()) {
58              log.info("Parsing exclusions from " + scmIgnore);
59              BufferedReader reader = null;
60              try {
61                  reader = new BufferedReader(new FileReader(scmIgnore));
62                  String line;
63                  while ((line = reader.readLine()) != null) {
64                      if (!isComment(line)) {
65                          exclusionLines.add(line);
66                          log.debug("Added " + line);
67                      }
68                  }
69              } catch (final IOException e) {
70                  log.warn("Cannot parse " + scmIgnore + " for exclusions. Will skip this file.");
71                  log.debug("Skip parsing " + scmIgnore + " due to " + e.getMessage());
72              } finally {
73                  IOUtils.closeQuietly(reader);
74              }
75          }
76          return exclusionLines;
77      }
78  
79      /**
80       * Parse ignore files from all known SCMs that have ignore files.
81       *
82       * @param log     Show information via maven logger.
83       * @param baseDir base directory from which to look for SCM ignores.
84       * @return Exclusions from the SCM ignore files.
85       */
86      public static List<String> getExclusionsFromSCM(final Log log, final File baseDir) {
87          List<String> exclusions = new ArrayList<String>();
88          for (SourceCodeManagementSystems scm : SourceCodeManagementSystems.values()) {
89              if (scm.hasIgnoreFile()) {
90                  exclusions.addAll(getExcludesFromFile(log, new File(baseDir, scm.getIgnoreFile())));
91              }
92          }
93          return exclusions;
94  
95      }
96  
97      /**
98       * Determines whether the given line is a comment or not based on scanning
99       * for prefixes
100      * {@see COMMENT_PREFIXES}.
101      *
102      * @param line line to verify.
103      * @return <code>true</code> if the given line is a commented out line.
104      */
105     static boolean isComment(final String line) {
106         if (line == null || line.length() <= 0) {
107             return false;
108         }
109 
110         final String trimLine = line.trim();
111         for (String prefix : COMMENT_PREFIXES) {
112             if (trimLine.startsWith(prefix)) {
113                 return true;
114             }
115         }
116         return false;
117     }
118 }