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.analysis.matchers;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.rat.analysis.IHeaders;
23  import org.apache.rat.config.parameters.ComponentType;
24  import org.apache.rat.config.parameters.ConfigComponent;
25  
26  /**
27   * A simple text matching IHeaderMatcher implementation.
28   */
29  @ConfigComponent(type = ComponentType.MATCHER, name = "text", desc = "Matches the enclosed text")
30  public class SimpleTextMatcher extends AbstractHeaderMatcher {
31      
32      @ConfigComponent(type = ComponentType.PARAMETER, name = "simpleText", desc = "The text to match", required=true)
33      private final String simpleText;
34  
35      /**
36       * Constructs the simple text matcher for the simple string.
37       *
38       * @param simpleText The pattern to match. Will only match a single line from
39       * the input stream.
40       */
41      public SimpleTextMatcher(String simpleText) {
42          this(null, simpleText);
43      }
44  
45      /**
46       * Constructs the simple text matcher for the simple string.
47       *
48       * @param id The id for this matcher.
49       * @param simpleText The pattern to match. Will only match a single line from
50       * the input stream.
51       */
52      public SimpleTextMatcher(String id, String simpleText) {
53          super(id);
54          if (StringUtils.isBlank(simpleText)) {
55              throw new IllegalArgumentException("Simple text may not be null, empty or blank");
56          }
57          this.simpleText = simpleText;
58      }
59  
60      public String getSimpleText() {
61          return this.simpleText;
62      }
63  
64      @Override
65      public boolean matches(IHeaders headers) {
66          return headers.raw().contains(simpleText);
67      }
68  }