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.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.net.URL;
26 import java.nio.charset.StandardCharsets;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.stream.Collectors;
32
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.rat.ConfigurationException;
35 import org.apache.rat.analysis.IHeaderMatcher;
36
37
38
39
40 public abstract class ChildContainerBuilder extends AbstractBuilder {
41
42
43 protected final List<IHeaderMatcher.Builder> children = new ArrayList<>();
44
45
46 protected String resource;
47
48
49
50
51 protected ChildContainerBuilder() {
52 }
53
54
55
56
57
58
59
60 public AbstractBuilder setResource(String resourceName) {
61 URL url = this.getClass().getResource(resourceName);
62 try (final InputStream in = url.openStream();
63 BufferedReader buffer = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));) {
64 String txt;
65 while (null != (txt = buffer.readLine())) {
66 txt = txt.trim();
67 if (StringUtils.isNotBlank(txt)) {
68 children.add(new TextBuilder().setSimpleText(txt));
69 }
70 }
71 this.resource = resourceName;
72 return this;
73 } catch (IOException e) {
74 throw new ConfigurationException("Unable to read matching text file: " + resourceName, e);
75 }
76 }
77
78
79
80
81
82
83
84 public AbstractBuilder addEnclosed(IHeaderMatcher.Builder child) {
85 children.add(child);
86 return this;
87 }
88
89
90
91
92
93
94
95 public AbstractBuilder addEnclosed(Collection<IHeaderMatcher.Builder> children) {
96 this.children.addAll(children);
97 return this;
98 }
99
100 public List<IHeaderMatcher.Builder> getEnclosedBuilders() {
101 return Collections.unmodifiableList(children);
102 }
103
104
105
106 public List<IHeaderMatcher> getEnclosed() {
107 return children.stream().map(IHeaderMatcher.Builder::build).collect(Collectors.toList());
108 }
109
110 @Override
111 public String toString() {
112 StringBuilder sb = new StringBuilder(this.getClass().getSimpleName()).append(":");
113 children.stream().map(Object::toString).forEach(x -> sb.append(System.lineSeparator()).append(x));
114 return sb.toString();
115 }
116
117 }