View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 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      /**
20       * Condition that has to pass.
21       */
22      private Pattern condition = Pattern.compile(".");
23  
24      @Override
25      public void processFiltered(final File file, final FileText lines) {
26          boolean found = false;
27          for (final String line : lines.toLinesArray()) {
28              if (this.condition.matcher(line).find()) {
29                  found = true;
30                  break;
31              }
32          }
33          if (found) {
34              super.processFiltered(file, lines);
35          }
36      }
37  
38      /**
39       * Condition regexp that has to match before checking the core one.
40       * @param cond Regexp that has to match in file
41       */
42      public void setCondition(final String cond) {
43          this.condition = Pattern.compile(cond);
44      }
45  }