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.analysis.matchers;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Objects;
25
26 import org.apache.rat.analysis.IHeaderMatcher;
27 import org.apache.rat.config.parameters.ComponentType;
28 import org.apache.rat.config.parameters.ConfigComponent;
29
30 /**
31 * A class that implements IHeaderMatcher as a collection of other
32 * IHeaderMatchers.
33 */
34 public abstract class AbstractMatcherContainer extends AbstractHeaderMatcher {
35
36 /** The collection of enclosed headers */
37 @ConfigComponent(desc = "enclosed Matchers", type = ComponentType.PARAMETER, parameterType = IHeaderMatcher.class, required = true)
38 private final Collection<IHeaderMatcher> enclosed;
39
40 /** The resource the headers were read from. May be null */
41 @ConfigComponent(desc = "Resource (URL or file) to read enclosed text matcher definitions from.", type = ComponentType.PARAMETER)
42 private final String resource;
43
44 /**
45 * Constructs the abstract matcher container. If the {@code id} is not set then
46 * a unique random identifier is created. The {@code enclosed} collection is
47 * preserved in a new collection that retains the order of the original
48 * collection.
49 *
50 * @param id The id for the matcher.
51 * @param enclosed the collection of enclosed matchers.
52 * @param resource the name of the resource if this container was read from a file or URL.
53 */
54 public AbstractMatcherContainer(final String id, final Collection<? extends IHeaderMatcher> enclosed, final String resource) {
55 super(id);
56 Objects.requireNonNull(enclosed, "The collection of IHeaderMatcher may not be null");
57 this.enclosed = new ArrayList<>(enclosed);
58 this.resource = resource;
59 }
60
61 /**
62 * Constructs the abstract matcher container with a unique random id. The
63 * {@code enclosed} collection is preserved in a new collection that retains the
64 * order of the original collection.
65 *
66 * @param enclosed the collection of enclosed matchers.
67 * @param resource the name of the resource if this container was read from a file or URL.
68 */
69 public AbstractMatcherContainer(final Collection<? extends IHeaderMatcher> enclosed, final String resource) {
70 this(null, enclosed, resource);
71 }
72
73 @Override
74 public void reset() {
75 enclosed.forEach(IHeaderMatcher::reset);
76 }
77
78 /**
79 * Retrieves the collection of matchers that comprise the children of this matcher.
80 * @return the children of this matcher
81 */
82 public Collection<IHeaderMatcher> getEnclosed() {
83 return Collections.unmodifiableCollection(enclosed);
84 }
85
86 /**
87 * Get the resource that was provided in the constructor.
88 * @return the resource or {@code null} if none was provided in the constructor.
89 */
90 public String getResource() {
91 return resource;
92 }
93 }