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 java.util.Locale;
22  
23  import org.apache.rat.analysis.IHeaders;
24  
25  /**
26   * Accumulates all letters and numbers contained inside the header and compares
27   * it to the full text of a given license (after reducing it to letters and
28   * numbers as well).
29   * <p>
30   * The text comparison is case insensitive but assumes only characters in the
31   * US-ASCII charset are being matched.
32   * </p>
33   */
34  public class FullTextMatcher extends SimpleTextMatcher {
35      /**
36       * The text that we are searching for.  This text has been pruned and converted to lower case.
37       */
38      private final String prunedText;
39  
40      /**
41       * Constructs the full text matcher with a unique random id and the specified
42       * text to match.
43       *
44       * @param simpleText the text to match
45       */
46      public FullTextMatcher(final String simpleText) {
47          this(null, simpleText);
48      }
49  
50      /**
51       * Constructs the full text matcher for the specified text.
52       *
53       * @param id the id for the matcher
54       * @param simpleText the text to match
55       */
56      public FullTextMatcher(final String id, final String simpleText) {
57          super(id, simpleText);
58          this.prunedText = prune(simpleText).toLowerCase(Locale.ENGLISH);
59      }
60  
61      /**
62       * Removes everything except letter or digit from text.
63       *
64       * @param text The text to remove extra chars from.
65       * @return the pruned text.
66       */
67      public static String prune(final String text) {
68          final int length = text.length();
69          final StringBuilder buffer = new StringBuilder(length);
70          for (int i = 0; i < length; i++) {
71              char at = text.charAt(i);
72              if (Character.isLetterOrDigit(at)) {
73                  buffer.append(at);
74              }
75          }
76          return buffer.toString();
77      }
78  
79      @Override
80      public boolean matches(final IHeaders headers) {
81          if (headers.pruned().length() >= prunedText.length()) { // we have enough data to match
82              return headers.pruned().contains(prunedText);
83          }
84          return false;
85      }
86  }