View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import com.puppycrawl.tools.checkstyle.api.FileText;
8   import com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck;
9   import java.io.File;
10  import java.util.regex.Pattern;
11  
12  /**
13   * Performs multiline regexp match only if a regexp condition passes.
14   * @since 0.5
15   */
16  public final class ConditionalRegexpMultilineCheck extends
17      RegexpMultilineCheck {
18      /**
19       * Condition that has to pass.
20       */
21      private Pattern condition = Pattern.compile(".");
22  
23      @Override
24      public void processFiltered(final File file, final FileText lines) {
25          boolean found = false;
26          for (final String line: lines.toLinesArray()) {
27              if (this.condition.matcher(line).find()) {
28                  found = true;
29                  break;
30              }
31          }
32          if (found) {
33              super.processFiltered(file, lines);
34          }
35      }
36  
37      /**
38       * Condition regexp that has to match before checking the core one.
39       * @param cond Regexp that has to match in file.
40       */
41      public void setCondition(final String cond) {
42          this.condition = Pattern.compile(cond);
43      }
44  }