1 package org.apache.rat.mp.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39 public final class ScmIgnoreParser {
40 private ScmIgnoreParser() {
41
42 }
43
44 private static List<String> COMMENT_PREFIXES = Arrays.asList("#", "##", "//", "/**", "/*");
45
46
47
48
49
50
51
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
81
82
83
84
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
99
100
101
102
103
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 }