1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.analysis.license;
20
21 import java.util.Locale;
22
23 import org.apache.rat.analysis.IHeaderMatcher;
24 import org.apache.rat.analysis.RatHeaderAnalysisException;
25 import org.apache.rat.api.Document;
26 import org.apache.rat.api.MetaData.Datum;
27
28
29
30
31
32
33
34
35
36
37
38 public class FullTextMatchingLicense extends BaseLicense
39 implements IHeaderMatcher {
40
41
42 private static final int DEFAULT_INITIAL_LINE_LENGTH = 20;
43
44 private String fullText;
45
46 private String firstLine;
47
48 private boolean seenFirstLine = false;
49
50 private final StringBuilder buffer = new StringBuilder();
51
52 public FullTextMatchingLicense() {
53 }
54
55 protected FullTextMatchingLicense(Datum licenseFamilyCategory,
56 Datum licenseFamilyName,
57 String notes,
58 String fullText) {
59 super(licenseFamilyCategory, licenseFamilyName, notes);
60 setFullText(fullText);
61 }
62
63 public final void setFullText(String text) {
64 int offset = text.indexOf('\n');
65 if (offset == -1) {
66 offset = Math.min(DEFAULT_INITIAL_LINE_LENGTH, text.length());
67 }
68 firstLine = prune(text.substring(0, offset)).toLowerCase(Locale.ENGLISH);
69 fullText = prune(text).toLowerCase(Locale.ENGLISH);
70 init();
71 }
72
73 public final boolean hasFullText() {
74 return fullText != null;
75 }
76
77 public boolean match(Document subject, String line) throws RatHeaderAnalysisException {
78 final String inputToMatch = prune(line).toLowerCase(Locale.ENGLISH);
79 if (seenFirstLine) {
80 buffer.append(inputToMatch);
81 } else {
82 int offset = inputToMatch.indexOf(firstLine);
83 if (offset >= 0) {
84
85 buffer.append(inputToMatch.substring(offset));
86 seenFirstLine = true;
87
88 } else {
89
90 return false;
91 }
92 }
93
94 if (buffer.length() >= fullText.length()) {
95 if (buffer.toString().contains(fullText)) {
96 reportOnLicense(subject);
97 return true;
98 } else {
99
100 int offset = buffer.substring(1).toString().indexOf(firstLine);
101 if (offset >= 0) {
102 buffer.delete(0,offset);
103 } else {
104 init();
105 }
106 }
107 }
108 return false;
109 }
110
111 public void reset() {
112 init();
113 }
114
115
116 private void init() {
117 buffer.setLength(0);
118 seenFirstLine = false;
119 }
120 }