ottlinger commented on code in PR #233: URL: https://github.com/apache/creadur-rat/pull/233#discussion_r1564975996
########## src/site/apt/matcher_def.apt.vm: ########## @@ -0,0 +1,364 @@ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~ Licensed to the Apache Software Foundation (ASF) under one or more +~~ contributor license agreements. See the NOTICE file distributed with +~~ this work for additional information regarding copyright ownership. +~~ The ASF licenses this file to You under the Apache License, Version 2.0 +~~ (the "License"); you may not use this file except in compliance with +~~ the License. You may obtain a copy of the License at +~~ +~~ http://www.apache.org/licenses/LICENSE-2.0 +~~ +~~ Unless required by applicable law or agreed to in writing, software +~~ distributed under the License is distributed on an "AS IS" BASIS, +~~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +~~ See the License for the specific language governing permissions and +~~ limitations under the License. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + -------------------------- + How to define new Matchers + -------------------------- + +How to define matchers in Apache Rat + + Matchers in Apache Rat are paired with Builders. A Matcher must implement the "IHeaderMatcher" interface and its associated Builder must implement the IHeaderMatcher.Builder interface. + +* A simple example + +** The Matcher implementation + + For our example we will implement a Matcher that implements the phrase "Quality, speed and cost, pick any two” by looking for the occurrence of all three words anywhere in the header. + In most cases is it simplest to extend the AbstractHeaderMatcher class as this class will handle setting of the unique ID for instances that do not otherwise have a unique id. + ++------------------------------------------+ +public interface IHeaderMatcher extends Component { + /** + * Get the identifier for this matcher. + * <p> + * All matchers must have unique identifiers + * </p> + * + * @return the Identifier for this matcher. + */ + String getId(); + + /** + * Resets this state of this matcher to its initial state in preparation for + * use with another document scan. In most cases this method does not need to + * do anything. + */ + default void reset() { + // does nothing. + } + + /** + * Attempts to match text in the IHeaders instance. + * + * @param headers the representations of the headers to check + * @return {@code true} if the matcher matches the text, {@code false} otherwise. + */ + boolean matches(IHeaders headers); +} ++------------------------------------------+ + ++------------------------------------------+ +public abstract class AbstractHeaderMatcher implements IHeaderMatcher { + + @ConfigComponent(type = Component.Type.Parameter, desc = "The id of the matcher.") + private final String id; + + /** + * Constructs the IHeaderMatcher with an id value. If {@code id} is null then a + * unique random id is created. + * + * @param id the Id to use. + */ + protected AbstractHeaderMatcher(String id) { + this.id = StringUtils.isBlank(id) ? UUID.randomUUID().toString() : id; + } + + @Override + public String getId() { + return id; + } + + @Override + public String toString() { + return getId(); + } + + @Override + public Description getDescription() { + return DescriptionBuilder.build(this); + } +} ++------------------------------------------+ + + So lets start by creating our matcher class and implementing the match method. + ++------------------------------------------+ +package com.example.ratMatcher; + +import org.apache.rat.analysis.IHeaders; +import org.apache.rat.analysis.matchers.AbstractHeaderMatcher; +import org.apache.rat.config.parameters.Component; +import org.apache.rat.config.parameters.ConfigComponent; + +@ConfigComponent(type = Component.Type.Matcher, name = "QSC", desc = "Reports if the 'Quality, speed and cost, pick any two' rule is violated") +public class QSCMatcher extends AbstraactHeaderMatcher { Review Comment: Typo: extends AbstractHeaderMatcher -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@creadur.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org