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