1 package org.apache.rat.mp.util.ignore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42 }
43
44 public GlobIgnoreMatcher(final Log log, final File scmIgnore) {
45 loadFile(log, scmIgnore);
46 }
47
48
49
50
51
52 public void addRule(final String rule) {
53 if (!exclusionLines.contains(rule)) {
54 exclusionLines.add(rule);
55 }
56 }
57
58
59
60
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
72
73
74
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
97
98
99
100
101
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
129
130 return Optional.empty() ;
131 }
132 }