001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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.PropertyType; 026import com.puppycrawl.tools.checkstyle.StatelessCheck; 027import com.puppycrawl.tools.checkstyle.XdocsPropertyType; 028import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; 029import com.puppycrawl.tools.checkstyle.api.FileText; 030 031/** 032 * <div> 033 * Checks that a specified pattern matches across multiple lines in any file type. 034 * </div> 035 * 036 * <p> 037 * Rationale: This check can be used to when the regular expression can be span multiple lines. 038 * </p> 039 * 040 * @since 5.0 041 */ 042@StatelessCheck 043public class RegexpMultilineCheck extends AbstractFileSetCheck { 044 045 /** Specify the format of the regular expression to match. */ 046 @XdocsPropertyType(PropertyType.PATTERN) 047 private String format = "$."; 048 /** 049 * Specify the message which is used to notify about violations, 050 * if empty then default (hard-coded) message is used. 051 */ 052 private String message; 053 /** Specify the minimum number of matches required in each file. */ 054 private int minimum; 055 /** Specify the maximum number of matches required in each file. */ 056 private int maximum; 057 /** Control whether to ignore case when searching. */ 058 private boolean ignoreCase; 059 /** Control whether to match expressions across multiple lines. */ 060 private boolean matchAcrossLines; 061 062 /** The detector to use. */ 063 private MultilineDetector detector; 064 065 @Override 066 public void beginProcessing(String charset) { 067 final DetectorOptions options = DetectorOptions.newBuilder() 068 .reporter(this) 069 .compileFlags(getRegexCompileFlags()) 070 .format(format) 071 .message(message) 072 .minimum(minimum) 073 .maximum(maximum) 074 .ignoreCase(ignoreCase) 075 .build(); 076 detector = new MultilineDetector(options); 077 } 078 079 @Override 080 protected void processFiltered(File file, FileText fileText) { 081 detector.processLines(fileText); 082 } 083 084 /** 085 * Retrieves the compile-flags for the regular expression being built based 086 * on {@code matchAcrossLines}. 087 * 088 * @return The compile-flags. 089 */ 090 private int getRegexCompileFlags() { 091 final int result; 092 093 if (matchAcrossLines) { 094 result = Pattern.DOTALL; 095 } 096 else { 097 result = Pattern.MULTILINE; 098 } 099 100 return result; 101 } 102 103 /** 104 * Setter to specify the format of the regular expression to match. 105 * 106 * @param format the format of the regular expression to match. 107 * @since 5.0 108 */ 109 public void setFormat(String format) { 110 this.format = format; 111 } 112 113 /** 114 * Setter to specify the message which is used to notify about violations, 115 * if empty then default (hard-coded) message is used. 116 * 117 * @param message the message to report for a match. 118 * @since 5.0 119 */ 120 public void setMessage(String message) { 121 this.message = message; 122 } 123 124 /** 125 * Setter to specify the minimum number of matches required in each file. 126 * 127 * @param minimum the minimum number of matches required in each file. 128 * @since 5.0 129 */ 130 public void setMinimum(int minimum) { 131 this.minimum = minimum; 132 } 133 134 /** 135 * Setter to specify the maximum number of matches required in each file. 136 * 137 * @param maximum the maximum number of matches required in each file. 138 * @since 5.0 139 */ 140 public void setMaximum(int maximum) { 141 this.maximum = maximum; 142 } 143 144 /** 145 * Setter to control whether to ignore case when searching. 146 * 147 * @param ignoreCase whether to ignore case when searching. 148 * @since 5.0 149 */ 150 public void setIgnoreCase(boolean ignoreCase) { 151 this.ignoreCase = ignoreCase; 152 } 153 154 /** 155 * Setter to control whether to match expressions across multiple lines. 156 * 157 * @param matchAcrossLines whether to match expressions across multiple lines. 158 * @since 8.25 159 */ 160 public void setMatchAcrossLines(boolean matchAcrossLines) { 161 this.matchAcrossLines = matchAcrossLines; 162 } 163 164}