View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
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.List;
30  import java.util.stream.Collectors;
31  
32  import org.apache.commons.lang3.StringUtils;
33  import org.apache.rat.ConfigurationException;
34  import org.apache.rat.analysis.IHeaderMatcher;
35  import org.apache.rat.analysis.IHeaderMatcher.Builder;
36  
37  /**
38   * Constructs a builder that contains other builders.
39   */
40  public abstract class ChildContainerBuilder extends AbstractBuilder {
41  
42      /**
43       * The list of builders that will build the enclosed matchers.
44       */
45      protected final List<IHeaderMatcher.Builder> children = new ArrayList<>();
46  
47      /**
48       * Empty default constructor.
49       */
50      protected ChildContainerBuilder() {
51      }
52  
53      /**
54       * Reads a text file. Each line becomes a text matcher in the resulting list.
55       * 
56       * @param resourceName the name of the resource to read.
57       * @return a List of Matchers, one for each non-empty line in the input file.
58       */
59      public AbstractBuilder setResource(String resourceName) {
60            URL url = this.getClass().getResource(resourceName);
61              try (final InputStream in = url.openStream()) {
62                  BufferedReader buffer = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
63                  String txt;
64                  while (null != (txt = buffer.readLine())) {
65                      txt = txt.trim();
66                      if (StringUtils.isNotBlank(txt)) {
67                          children.add(Builder.text().setText(txt));
68                      }
69                  }
70                  return this;
71              } catch (IOException e) {
72                  throw new ConfigurationException("Unable to read matching text file: " + resourceName, e);
73              }
74      }
75      
76      /**
77       * Adds a builder to the list of builders.
78       * @param child the child builder to add.
79       * @return this for chaining.
80       */
81      public AbstractBuilder add(IHeaderMatcher.Builder child) {
82          children.add(child);
83          return this;
84      }
85      
86      /**
87       * Adds a collection of builders to the list of child builders.
88       * @param children the children to add.
89       * @return this for chaining.
90       */
91      public AbstractBuilder add(Collection<IHeaderMatcher.Builder> children) {
92          this.children.addAll(children);
93          return this;
94      }
95  
96      /**
97       * @return the list of child builders for this builder.
98       */
99      public List<IHeaderMatcher> getChildren() {
100         return children.stream().map(IHeaderMatcher.Builder::build).collect(Collectors.toList());
101     }
102     
103     @Override
104     public String toString() {
105         StringBuilder sb = new StringBuilder(this.getClass().getSimpleName()).append( ":");
106         children.stream().map(Object::toString).forEach( x -> sb.append("\n").append(x));
107         return sb.toString();
108     }
109 
110 }