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 /** 22 * An abstract IHeaderMatcher that does simple matching. Implementations need to implement the {@code doMatch(String)} 23 * method to perform the actual matching. All handling of the {@code State} is managed by this class. By default the 24 * {@code finalizeState()} method will convert {@code State.i} to {@code State.f}. 25 */ 26 public abstract class AbstractSimpleMatcher extends AbstractHeaderMatcher { 27 private State lastState; 28 29 /** 30 * Constructs the AbstractSimpleMatcher with the specified id. 31 * If the id is null or an empty string a unique random id will be generated. 32 * @param id the Id to use. May be null. 33 */ 34 protected AbstractSimpleMatcher(String id) { 35 super(id); 36 this.lastState = State.i; 37 } 38 39 /** 40 * Performs the actual match test. 41 * @param line the line to check. 42 * @return {@code true} if the line matches, {@code false} otherwise. 43 */ 44 abstract protected boolean doMatch(String line); 45 46 @Override 47 public final State matches(String line) { 48 if (lastState == State.t) { 49 return lastState; 50 } 51 if (line != null && doMatch(line)) { 52 lastState = State.t; 53 } 54 return lastState; 55 } 56 57 @Override 58 public void reset() { 59 lastState = State.i; 60 } 61 62 @Override 63 public State finalizeState() { 64 if (lastState == State.i) { 65 lastState = State.f; 66 } 67 return lastState; 68 } 69 70 @Override 71 public final State currentState() { 72 return lastState; 73 } 74 }