001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.regexp; 021 022import java.io.File; 023import java.util.regex.Pattern; 024 025import com.puppycrawl.tools.checkstyle.StatelessCheck; 026import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; 027import com.puppycrawl.tools.checkstyle.api.FileText; 028 029/** 030 * Implementation of a check that looks that matches across multiple lines in 031 * any file type. 032 */ 033@StatelessCheck 034public class RegexpMultilineCheck extends AbstractFileSetCheck { 035 036 /** The format of the regular expression to match. */ 037 private String format = "$."; 038 /** The message to report for a match. */ 039 private String message; 040 /** The minimum number of matches required per file. */ 041 private int minimum; 042 /** The maximum number of matches required per file. */ 043 private int maximum; 044 /** Whether to ignore case when matching. */ 045 private boolean ignoreCase; 046 047 /** The detector to use. */ 048 private MultilineDetector detector; 049 050 @Override 051 public void beginProcessing(String charset) { 052 final DetectorOptions options = DetectorOptions.newBuilder() 053 .reporter(this) 054 .compileFlags(Pattern.MULTILINE) 055 .format(format) 056 .message(message) 057 .minimum(minimum) 058 .maximum(maximum) 059 .ignoreCase(ignoreCase) 060 .build(); 061 detector = new MultilineDetector(options); 062 } 063 064 @Override 065 protected void processFiltered(File file, FileText fileText) { 066 detector.processLines(fileText); 067 } 068 069 /** 070 * Sets the format of the regular expression to match. 071 * @param format the format of the regular expression to match. 072 */ 073 public void setFormat(String format) { 074 this.format = format; 075 } 076 077 /** 078 * Sets the message to report for a match. 079 * @param message the message to report for a match. 080 */ 081 public void setMessage(String message) { 082 this.message = message; 083 } 084 085 /** 086 * Sets the minimum number of matches required per file. 087 * @param minimum the minimum number of matches required per file. 088 */ 089 public void setMinimum(int minimum) { 090 this.minimum = minimum; 091 } 092 093 /** 094 * Sets the maximum number of matches required per file. 095 * @param maximum the maximum number of matches required per file. 096 */ 097 public void setMaximum(int maximum) { 098 this.maximum = maximum; 099 } 100 101 /** 102 * Sets whether to ignore case when matching. 103 * @param ignoreCase whether to ignore case when matching. 104 */ 105 public void setIgnoreCase(boolean ignoreCase) { 106 this.ignoreCase = ignoreCase; 107 } 108 109}