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(final String resourceName) {
61
62 URL url = this.getClass().getResource(resourceName);
63 if (url == null) {
64 throw new ConfigurationException("Unable to read matching text file: " + resourceName);
65 }
66
67 try (InputStream in = url.openStream();
68 BufferedReader buffer = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
69 String txt;
70 while (null != (txt = buffer.readLine())) {
71 txt = txt.trim();
72 if (StringUtils.isNotBlank(txt)) {
73 children.add(new TextBuilder().setSimpleText(txt));
74 }
75 }
76 this.resource = resourceName;
77 return this;
78 } catch (IOException e) {
79 throw new ConfigurationException("Unable to read matching text file: " + resourceName, e);
80 }
81 }
82
83
84
85
86
87
88
89 public AbstractBuilder addEnclosed(final IHeaderMatcher.Builder child) {
90 children.add(child);
91 return this;
92 }
93
94
95
96
97
98
99
100 public AbstractBuilder addEnclosed(final Collection<IHeaderMatcher.Builder> children) {
101
102 this.children.addAll(children);
103 return this;
104 }
105
106 public List<IHeaderMatcher.Builder> getEnclosedBuilders() {
107
108 return Collections.unmodifiableList(children);
109 }
110
111
112
113 public List<IHeaderMatcher> getEnclosed() {
114 return children.stream().map(IHeaderMatcher.Builder::build).collect(Collectors.toList());
115 }
116
117 @Override
118 public String toString() {
119 StringBuilder sb = new StringBuilder(this.getClass().getSimpleName()).append(":");
120 children.stream().map(Object::toString).forEach(x -> sb.append(System.lineSeparator()).append(x));
121 return sb.toString();
122 }
123 }