001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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.api.AbstractFileSetCheck; 026import com.puppycrawl.tools.checkstyle.api.FileText; 027 028/** 029 * Implementation of a check that looks that matches across multiple lines in 030 * any file type. 031 */ 032public class RegexpMultilineCheck extends AbstractFileSetCheck { 033 034 /** The format of the regular expression to match. */ 035 private String format = "$."; 036 /** The message to report for a match. */ 037 private String message; 038 /** The minimum number of matches required per file. */ 039 private int minimum; 040 /** The maximum number of matches required per file. */ 041 private int maximum; 042 /** Whether to ignore case when matching. */ 043 private boolean ignoreCase; 044 045 /** The detector to use. */ 046 private MultilineDetector detector; 047 048 @Override 049 public void beginProcessing(String charset) { 050 final DetectorOptions options = DetectorOptions.newBuilder() 051 .reporter(this) 052 .compileFlags(Pattern.MULTILINE) 053 .format(format) 054 .message(message) 055 .minimum(minimum) 056 .maximum(maximum) 057 .ignoreCase(ignoreCase) 058 .build(); 059 detector = new MultilineDetector(options); 060 } 061 062 @Override 063 protected void processFiltered(File file, FileText fileText) { 064 detector.processLines(fileText); 065 } 066 067 /** 068 * Sets the format of the regular expression to match. 069 * @param format the format of the regular expression to match. 070 */ 071 public void setFormat(String format) { 072 this.format = format; 073 } 074 075 /** 076 * Sets the message to report for a match. 077 * @param message the message to report for a match. 078 */ 079 public void setMessage(String message) { 080 this.message = message; 081 } 082 083 /** 084 * Sets the minimum number of matches required per file. 085 * @param minimum the minimum number of matches required per file. 086 */ 087 public void setMinimum(int minimum) { 088 this.minimum = minimum; 089 } 090 091 /** 092 * Sets the maximum number of matches required per file. 093 * @param maximum the maximum number of matches required per file. 094 */ 095 public void setMaximum(int maximum) { 096 this.maximum = maximum; 097 } 098 099 /** 100 * Sets whether to ignore case when matching. 101 * @param ignoreCase whether to ignore case when matching. 102 */ 103 public void setIgnoreCase(boolean ignoreCase) { 104 this.ignoreCase = ignoreCase; 105 } 106 107}