1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.configuration.builders;
20
21 import java.util.Map;
22
23 import org.apache.rat.ConfigurationException;
24 import org.apache.rat.analysis.IHeaderMatcher;
25 import org.apache.rat.analysis.IHeaders;
26 import org.apache.rat.config.parameters.ComponentType;
27 import org.apache.rat.config.parameters.ConfigComponent;
28 import org.apache.rat.config.parameters.MatcherBuilder;
29
30
31
32
33
34
35
36
37
38
39
40 @MatcherBuilder(MatcherRefBuilder.IHeaderMatcherProxy.class)
41 public class MatcherRefBuilder extends AbstractBuilder {
42
43 private String referenceId;
44
45 private Map<String, IHeaderMatcher> matchers;
46
47
48
49
50
51
52
53 public MatcherRefBuilder setRefId(final String refId) {
54
55 this.referenceId = refId;
56 return this;
57 }
58
59
60
61
62
63
64
65 public MatcherRefBuilder setMatcherMap(final Map<String, IHeaderMatcher> matchers) {
66
67 this.matchers = matchers;
68 return this;
69 }
70
71 @Override
72 public IHeaderMatcher build() {
73 if (matchers == null) {
74 throw new ConfigurationException("'matchers' not set");
75 }
76 IHeaderMatcher result = matchers.get(referenceId);
77 return result != null ? result : new IHeaderMatcherProxy(referenceId, matchers);
78 }
79
80 @Override
81 public String toString() {
82 return "MatcherRefBuilder: " + referenceId;
83 }
84
85
86
87
88
89
90
91
92 @ConfigComponent(type = ComponentType.MATCHER, name = "matcherRef", desc = "A pointer to another Matcher")
93 public static class IHeaderMatcherProxy implements IHeaderMatcher {
94
95
96
97 @ConfigComponent(type = ComponentType.PARAMETER, name = "refId", desc = "Reference to an existing matcher", required = true)
98 private final String proxyId;
99
100 private IHeaderMatcher wrapped;
101
102 @ConfigComponent(type = ComponentType.BUILD_PARAMETER, name = "matcherMap", desc = "Map of matcher names to matcher instances")
103 private Map<String, IHeaderMatcher> matchers;
104
105
106
107
108
109
110
111
112 public IHeaderMatcherProxy(final String proxyId, final Map<String, IHeaderMatcher> matchers) {
113 this.proxyId = proxyId;
114 this.matchers = matchers;
115 }
116
117 private void checkProxy() {
118 if (wrapped == null) {
119 wrapped = matchers.get(proxyId);
120 if (wrapped == null) {
121 throw new IllegalStateException(String.format("%s is not a valid matcher id", proxyId));
122 }
123 matchers = null;
124 }
125 }
126
127 @Override
128 public String getId() {
129 checkProxy();
130 return wrapped.getId();
131 }
132
133 @Override
134 public void reset() {
135 checkProxy();
136 wrapped.reset();
137 }
138
139 @Override
140 public boolean matches(final IHeaders header) {
141 checkProxy();
142 return wrapped.matches(header);
143 }
144
145
146
147
148
149 public String getRefId() {
150
151 return proxyId;
152 }
153 }
154 }