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