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.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   * Constructs a builder that contains other builders.
39   */
40  public abstract class ChildContainerBuilder extends AbstractBuilder {
41  
42      /** The list of builders that will build the enclosed matchers. */
43      protected final List<IHeaderMatcher.Builder> children = new ArrayList<>();
44  
45      /** The resource the builders came from if it was read from a resource */
46      protected String resource;
47  
48      /**
49       * Empty default constructor.
50       */
51      protected ChildContainerBuilder() {
52      }
53  
54      /**
55       * Reads a text file. Each line becomes a text matcher in the resulting list.
56       *
57       * @param resourceName the name of the resource to read.
58       * @return a List of Matchers, one for each non-empty line in the input file.
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       * Adds a builder to the list of builders.
80       * 
81       * @param child the child builder to add.
82       * @return this for chaining.
83       */
84      public AbstractBuilder addEnclosed(IHeaderMatcher.Builder child) {
85          children.add(child);
86          return this;
87      }
88  
89      /**
90       * Adds a collection of builders to the list of child builders.
91       * 
92       * @param children the children to add.
93       * @return this for chaining.
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      * @return the list of child builders for this builder.
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 }